Пример #1
0
        //从数据字典中获取表名tabName的表信息
        public OneTable MyDeserialize(string tabName)
        {
            if (tabName == null)
            {
                Console.WriteLine("表名为空啊!");
                return(null);
            }
            IFormatter formatter = new BinaryFormatter();
            Stream     stream1   = new FileStream(Program.nowDirtionary + @"\DataDirtionary.txt", FileMode.Open,
                                                  FileAccess.Read, FileShare.Read);
            List <OneTable> list = new List <OneTable>();

            try
            {
                OneTable obj = (OneTable)formatter.Deserialize(stream1);
                while (obj != null)
                {
                    if (obj.name == tabName)
                    {
                        stream1.Close();
                        return(obj);
                    }
                    obj = (OneTable)formatter.Deserialize(stream1);
                }
            }
            catch
            {
            }
            finally
            {
                stream1.Close();
            }
            return(null);
        }
Пример #2
0
        public List <OneTable> ReadOneTablesFromDD()
        {
            List <OneTable> result    = new List <OneTable>();
            IFormatter      formatter = new BinaryFormatter();
            Stream          stream1   = new FileStream(Program.nowDirtionary + @"\DataDirtionary.txt", FileMode.Open,
                                                       FileAccess.Read, FileShare.Read);

            try
            {
                OneTable obj = (OneTable)formatter.Deserialize(stream1);
                while (obj != null)
                {
                    result.Add(obj);
                    obj = (OneTable)formatter.Deserialize(stream1);
                }
            }
            catch {
                stream1.Close();
                return(result);
            }
            finally
            {
                stream1.Close();
            }
            return(result);
        }
Пример #3
0
        //从一个DD -> list
        public List <string> OneTableToList(OneTable one)
        {
            List <string> newLineName = new List <string>();

            foreach (OneLine item in one.lines)
            {
                newLineName.Add(item.Name);
            }
            return(newLineName);
        }
Пример #4
0
        //返回表的笛卡尔积计算  并且计算最大表的DD
        public List <List <string> > GetMaxTable()
        {
            if (tabNameList.Count >= 2)
            {
                List <List <string> > DiKaEr = RXS(GetTableFromFile(tabNameList[0]), GetTableFromFile(tabNameList[1]));
                int index = 0;
                maxTable = new Insert().MyDeserialize(tabNameList[0]);
                //加上表名前缀
                for (index = 0; index < maxTable.lines.Count; index++)
                {
                    maxTable.lines[index].Name = tabNameList[0] + "." + maxTable.lines[index].Name;
                }
                OneTable otherTable = new Insert().MyDeserialize(tabNameList[1]);
                foreach (OneLine item in otherTable.lines)
                {
                    maxTable.lines.Add(item);
                }
                //加上表名前缀
                for (; index < maxTable.lines.Count; index++)
                {
                    maxTable.lines[index].Name = tabNameList[1] + "." + maxTable.lines[index].Name;
                }

                for (int i = 2; i < tabNameList.Count; i++)
                {
                    DiKaEr     = RXS(DiKaEr, GetTableFromFile(tabNameList[i]));
                    otherTable = new Insert().MyDeserialize(tabNameList[i]);
                    foreach (OneLine item in otherTable.lines)
                    {
                        maxTable.lines.Add(item);
                    }
                    //加上表名前缀
                    for (; index < maxTable.lines.Count; index++)
                    {
                        maxTable.lines[index].Name = tabNameList[i] + "." + maxTable.lines[index].Name;
                    }
                }
                return(DiKaEr);
            }
            else if (tabNameList.Count == 1)
            {
                maxTable = new Insert().MyDeserialize(tabNameList[0]);
                //加上表名前缀
                for (int index = 0; index < maxTable.lines.Count; index++)
                {
                    maxTable.lines[index].Name = tabNameList[0] + "." + maxTable.lines[index].Name;
                }
                return(GetTableFromFile(tabNameList[0]));
            }
            else
            {
                return(new List <List <string> >());
            }
        }
Пример #5
0
        public List <int> getIndexFromOneTable(List <string> PList, OneTable oneTable)
        {
            List <int> IList = new List <int>();

            foreach (string item in PList)
            {
                for (int i = 0; i < oneTable.lines.Count; i++)
                {
                    if (item == oneTable.lines[i].Name)
                    {
                        IList.Add(i);
                        break;
                    }
                }
            }
            return(IList);
        }
Пример #6
0
        public void delete(List <string> Clist)
        {
            switch (Clist[1])
            {
            case "table":
            {
                if (Clist[2].IndexOf(";") != -1)
                {
                    tabName = Clist[2].Remove(Clist[2].IndexOf(";"));
                }
                else if (Clist[1].IndexOf(";") == -1 && Clist[3].Trim() == ";")
                {
                    tabName = Clist[2];
                }
                else
                {
                    Console.WriteLine("语法错误!");
                    return;
                }

                //   删除表文件

                if (File.Exists(Program.nowDirtionary + @"\" + tabName + ".txt"))
                {
                    File.Delete(Program.nowDirtionary + @"\" + tabName + ".txt");
                }
                else
                {
                    Console.WriteLine("没有找到该表,确定一下表名或数据库名称!");
                    return;
                }
                new Create().createIndex("ssn_index", "employee", "ssn");

                // 删除DD中的该表信息
                // 1 将其他信息放入list中
                IFormatter formatter = new BinaryFormatter();
                Stream     stream1   = new FileStream(Program.nowDirtionary + @"\DataDirtionary.txt", FileMode.Open,
                                                      FileAccess.Read, FileShare.Read);
                List <OneTable> list = new List <OneTable>();
                try
                {
                    OneTable obj = (OneTable)formatter.Deserialize(stream1);
                    while (obj != null)
                    {
                        if (obj.name != tabName)
                        {
                            list.Add(obj);
                        }
                        obj = (OneTable)formatter.Deserialize(stream1);
                    }
                }
                catch
                {
                }
                finally
                {
                    stream1.Close();
                }
                //2 list中信息进行序列化入表文件

                Stream stream = new FileStream(Program.nowDirtionary + @"\DataDirtionary.txt", FileMode.Truncate,
                                               FileAccess.Write);
                try
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        formatter.Serialize(stream, list[i]);
                    }
                }
                catch
                {
                }
                finally
                {
                    stream.Close();
                }
                Console.WriteLine("表文件: {0} drop 成功!", tabName);
            }
            break;

            //删除数据库本身 drop database zhang
            case "database":
            {
                if (Clist[2].IndexOf(";") != -1)
                {
                    DataName = Clist[2].Remove(Clist[2].IndexOf(";"));
                }
                else if (Clist[1].IndexOf(";") == -1 && Clist[3].Trim() == ";")
                {
                    DataName = Clist[2];
                }
                else
                {
                    Console.WriteLine("语法错误!");
                    return;
                }
                try
                {
                    Directory.SetCurrentDirectory(new Create().Path);
                    if (Directory.Exists(DataName))
                    {
                        Directory.Delete(new Create().Path + @"\" + DataName, true);
                        Console.WriteLine("已经删除{0}database", DataName);
                        return;
                    }
                    throw new Exception();
                }
                catch (Exception e)
                {
                    Console.WriteLine("没有找到 {0} database!", DataName);
                }
            }
            break;

            case "index":
            {
                string indexName = "";
                if (Clist[2].IndexOf(";") != -1)
                {
                    indexName = Clist[2].Remove(Clist[2].IndexOf(";"));
                }
                else if (Clist[1].IndexOf(";") == -1 && Clist[3].Trim() == ";")
                {
                    indexName = Clist[2];
                }
                else
                {
                    Console.WriteLine("语法错误!");
                    return;
                }
                if (File.Exists(Program.nowDirtionary + @"\" + indexName + ".txt"))
                {
                    File.Delete(Program.nowDirtionary + @"\" + indexName + ".txt");
                }
                else
                {
                    Console.WriteLine("没有建立该索引或者已经删除!");
                    return;
                }
            }

            break;

            default:
                Console.WriteLine("{0}语法错误!", Clist[1]);
                return;
            }
        }
Пример #7
0
        //仅仅只含有values   insert into employee values('张三','230101198009081234','1980-09-08', '哈尔滨道里区十二道街', '男', 3125, '23010119751201312X', 'd1')
        public void onlyValuesInsert(List <string> Clist)
        {
            OneTable table = MyDeserialize(tabName);
            //根据数据字典确定主键的索引值 -- 查重
            List <int> keyIndex = new List <int>();

            for (int i = 0; i < table.lines.Count; i++)
            {
                if (table.lines[i].isKey == true)
                {
                    keyIndex.Add(i);
                }
            }

            int    num = table.lines.Count;
            string str = null;

            for (int i = 0; i < Clist.Count; i++)
            {
                str += Clist[i] + " ";
            }

            string insertStr = str.Substring(str.IndexOf("(") + 1, str.IndexOf(")") - str.IndexOf("(") - 1);
            //'张三','230101198009081234','1980-09-08',+'哈尔滨道里区十二道街',+'男',+3125,+'23010119751201312x',+'d1'

            //根据 , 进行串分割
            List <string> InsertList = Program.commandSplit(insertStr, ",");
            //存储主键
            List <string> KeyList = new List <string>();

            //存储需要写入文件的信息
            List <string> SaveList = new List <string>();

            //判断传入参数是否与数据字典中的参数个数相同
            if (num == InsertList.Count)
            {
                //逐个保存属性 至 SaveList列表中
                for (int i = 0; i < num; i++)
                {
                    switch (table.lines[i].type)
                    {
                    case Type.Bool:
                    {
                        if (InsertList[i] == "true" || InsertList[i] == "false")
                        {
                            SaveList.Add(InsertList[i]);
                            if (keyIndex.Contains(i))
                            {
                                KeyList.Add(InsertList[i]);
                            }
                            break;
                        }
                        else
                        {
                            Console.WriteLine("第{0}个属性值 bool 类型 输入参数错误!", i + 1);
                            return;
                        }
                    }
                    break;

                    case Type.Int:
                    {
                        try
                        {
                            int a = Convert.ToInt32(InsertList[i]);
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("第{0}个属性值 int 类型 输入参数错误!", i + 1);
                            return;
                        }
                        SaveList.Add(InsertList[i]);
                        if (keyIndex.Contains(i))
                        {
                            KeyList.Add(InsertList[i]);
                        }
                    }
                    break;

                    case Type.Float:
                    {
                        try
                        {
                            double a = Convert.ToDouble(InsertList[i]);
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("第{0}个属性值 float 类型 输入参数错误!", i + 1);
                            return;
                        }
                        SaveList.Add(InsertList[i]);
                        if (keyIndex.Contains(i))
                        {
                            KeyList.Add(InsertList[i]);
                        }
                    }
                    break;

                    case Type.Double:
                    {
                        try
                        {
                            double a = Convert.ToDouble(InsertList[i]);
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("第{0}个属性值 double 类型 输入参数错误!", i + 1);
                            return;
                        }
                        SaveList.Add(InsertList[i]);
                        if (keyIndex.Contains(i))
                        {
                            KeyList.Add(InsertList[i]);
                        }
                    }
                    break;

                    case Type.Char:
                    {
                        try
                        {
                            InsertList[i] = InsertList[i].Trim();
                            string mystr = InsertList[i].Substring(InsertList[i].IndexOf("'") + 1, InsertList[i].LastIndexOf("'") - InsertList[i].IndexOf("'") - 1);
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("第{0}个属性值 char 类型 输入参数错误!", i + 1);
                            return;
                        }
                        SaveList.Add(InsertList[i]);
                        if (keyIndex.Contains(i))
                        {
                            KeyList.Add(InsertList[i]);
                        }
                    }
                    break;

                    default:
                    {
                        Console.WriteLine("类型错误!");
                        return;
                    }
                    break;
                    }
                }

                //判断 是否存在该记录
                if (TableExist(keyIndex, KeyList, tabName))
                {
                    return;
                }

                //序列化 存入表文件中
                IFormatter formatter  = new BinaryFormatter();
                FileStream fileStream = new FileStream(Program.nowDirtionary + @"\" + tabName + ".txt", FileMode.Append, FileAccess.Write, FileShare.Write);
                formatter.Serialize(fileStream, SaveList);
                fileStream.Close();

                Console.WriteLine("insert成功!");
                return;
            }

            else
            {
                Console.WriteLine("参数个数错误!");
                return;
            }
        }