Exemplo n.º 1
0
 public static Dictionary<KeyValue, string> GetColumnType(List<string> selectedColumns, string databaseName)
 {
     Dictionary<KeyValue, string> list = new Dictionary<KeyValue, string>();
     foreach (var col in selectedColumns)
     {
         List<Column> columns = DataLayer.Configuration.GetTableColumns(databaseName, col.Split('.')[0]);
         List<PrimaryKey> pkeys = DataLayer.Configuration.GetPrimaryKey(col);
         if (pkeys.Count > 0)
         {
             PrimaryKey pkey = pkeys.FirstOrDefault();
             Column pkeyCol = columns.Where(x => x.ColumnName == pkey.Key).FirstOrDefault();
             columns.Remove(pkeyCol);
             columns.Insert(0, pkeyCol);
         }
         KeyValue colkeyvalue = new KeyValue();
         colkeyvalue.key = col;
         colkeyvalue.value = columns.IndexOf(columns.Single(x => x.ColumnName == col.Split('.')[1])).ToString();
         var type = columns.Where(x => x.ColumnName == col.Split('.')[1]).FirstOrDefault().Type;
         list.Add(colkeyvalue, type);
     }
     return list;
 }
Exemplo n.º 2
0
 public static List<KeyValue> GetAllColumnIndex(string databaseName, List<string> tables)
 {
     List<KeyValue> list = new List<KeyValue>();
     int i = 0;
     foreach (string tableName in tables)
     {
         List<Column> columns = DataLayer.Configuration.GetTableColumns(databaseName, tableName);
         List<PrimaryKey> pkeys = DataLayer.Configuration.GetPrimaryKey(tableName);
         if (pkeys.Count > 0)
         {
             PrimaryKey pkey = pkeys.FirstOrDefault();
             Column pkeyCol = columns.Where(x => x.ColumnName == pkey.Key).FirstOrDefault();
             columns.Remove(pkeyCol);
             columns.Insert(0, pkeyCol);
         }
         foreach (var col in columns)
         {
             KeyValue colkeyvalue = new KeyValue();
             colkeyvalue.key = tableName + "." + col.ColumnName;
             colkeyvalue.value = i.ToString();//columns.IndexOf(columns.Single(x => x.ColumnName == col.ColumnName)).ToString();
             list.Add(colkeyvalue);
             i++;
         }
     }
     return list;
 }
Exemplo n.º 3
0
 public static void UpdateExistingFK(string databaseName, string tableName, KeyValue fkey)
 {
     List<KeyValue> list = new List<KeyValue>();
     MongoServer server = Utilities.CreateMongoConnection();
     if (server.DatabaseExists(databaseName))
     {
         var database = server.GetDatabase(databaseName);
         if (database.CollectionExists(tableName))
         {
             var collection = database.GetCollection(tableName + "fk");
             if (collection.FindAs<KeyValue>(Query.EQ("key", fkey.key)).Count() > 0)
             {
                 var query = Query<KeyValue>.EQ(e => e.key, fkey.key);
                 var kvelem = collection.AsQueryable<KeyValue>().First(x => x.key == fkey.key);
                 kvelem.value += "#" + fkey.value;
                 var update = Update<KeyValue>.Set(x => x.value, kvelem.value);
                 collection.Update(query, update);
             }
             else
             {
                 collection.Save(fkey);
             }
         }
     }
 }
Exemplo n.º 4
0
        public static void InsertInTable(string databaseName, string tableName, KeyValue keyvalue)
        {
            MongoServer server = Utilities.CreateMongoConnection();

            if (server.DatabaseExists(databaseName))
            {
                var database = server.GetDatabase(databaseName);
                if (database.CollectionExists(tableName))
                {
                    var collection = database.GetCollection(tableName);
                    collection.Save(keyvalue);

                }
            }
        }
Exemplo n.º 5
0
        public static void AddTable(List<Database> dbs)
        {
            foreach (Database db in dbs)
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(filePath);
                XmlNodeList dbNodeList = xmlDoc.DocumentElement.SelectNodes("/Databases/DataBase");
                foreach (XmlNode nodeDB in dbNodeList)
                {
                    XmlNode nodeDbName = nodeDB.Attributes.GetNamedItem("dataBaseName");
                    if (nodeDbName != null)
                        if (db.DatabaseName != null && db.DatabaseName == nodeDbName.Value)
                        {
                            var tablesNodeList = xmlDoc.DocumentElement.SelectNodes("/Databases/DataBase/Tables");
                            if (nodeDB.HasChildNodes)
                            {
                                foreach (XmlNode nodetables in tablesNodeList)
                                {
                                    if (nodetables.ParentNode == nodeDB)
                                    {
                                        Table t = db.Tables.FirstOrDefault();
                                        XmlNode TableNode = xmlDoc.CreateNode(XmlNodeType.Element, "Table", null);
                                        XmlAttribute tableNameAttr = xmlDoc.CreateAttribute("tableName");
                                        if (t.TableName != null)
                                            tableNameAttr.Value = t.TableName;
                                        XmlAttribute fileNameAttr = xmlDoc.CreateAttribute("fileName");
                                        if (t.TableName != null)
                                            fileNameAttr.Value = t.TableName.Replace(" ", "") + ".bin";
                                        XmlAttribute rowLengthAttr = xmlDoc.CreateAttribute("rowLength");
                                        rowLengthAttr.Value = 0.ToString();
                                        TableNode.Attributes.Append(tableNameAttr);
                                        TableNode.Attributes.Append(fileNameAttr);
                                        TableNode.Attributes.Append(rowLengthAttr);
                                        XmlNode NodeStructure = xmlDoc.CreateNode(XmlNodeType.Element, "Structure", null);
                                        foreach (var column in t.Columns)
                                        {
                                            XmlNode ColumnNode = xmlDoc.CreateNode(XmlNodeType.Element, "Attribute", null);
                                            XmlAttribute attributeName = xmlDoc.CreateAttribute("attributeName");
                                            if (column.ColumnName != null)
                                                attributeName.Value = column.ColumnName;
                                            XmlAttribute type = xmlDoc.CreateAttribute("type");
                                            if (column.Type != null)
                                                type.Value = column.Type;
                                            XmlAttribute length = xmlDoc.CreateAttribute("length");
                                            length.Value = column.Length.ToString();
                                            XmlAttribute isnull = xmlDoc.CreateAttribute("isnull");
                                            if (!column.IsNull)
                                                isnull.Value = Utils.Utilities.GetIntStringFromBool(column.IsNull);
                                            ColumnNode.Attributes.Append(attributeName);
                                            ColumnNode.Attributes.Append(type);
                                            ColumnNode.Attributes.Append(length);
                                            ColumnNode.Attributes.Append(isnull);
                                            NodeStructure.AppendChild(ColumnNode);
                                        }
                                        TableNode.AppendChild(NodeStructure);

                                        XmlNode primaryKey = xmlDoc.CreateNode(XmlNodeType.Element, "primaryKey", null);
                                        foreach (PrimaryKey pk in t.PrimaryKey)
                                        {
                                            XmlNode pkNode = xmlDoc.CreateNode(XmlNodeType.Element, "pkAttribute", null);
                                            pkNode.InnerText = pk.Key;
                                            primaryKey.AppendChild(pkNode);
                                        }
                                        TableNode.AppendChild(primaryKey);
                                        XmlNode uniqueKey = xmlDoc.CreateNode(XmlNodeType.Element, "uniqueKeys", null);
                                        foreach (UniqueKey uk in t.UniqueKey)
                                        {
                                            XmlNode ukNode = xmlDoc.CreateNode(XmlNodeType.Element, "UniqueAttribute", null);
                                            ukNode.InnerText = uk.Key;
                                            uniqueKey.AppendChild(ukNode);
                                        }
                                        TableNode.AppendChild(uniqueKey);
                                        XmlNode foreignKeys = xmlDoc.CreateNode(XmlNodeType.Element, "foreignKeys", null);
                                        foreach (ForeignKey fk in t.ForeignKey)
                                        {
                                            XmlNode fkKeyNode = xmlDoc.CreateNode(XmlNodeType.Element, "foreignKey", null);
                                            XmlNode fkKeyAttr = xmlDoc.CreateNode(XmlNodeType.Element, "fkAttribute", null);
                                            fkKeyAttr.InnerText = fk.Key;
                                            XmlNode Refnode = xmlDoc.CreateNode(XmlNodeType.Element, "references", null);
                                            XmlNode refTable = xmlDoc.CreateNode(XmlNodeType.Element, "refTable", null);
                                            refTable.InnerText = fk.RefTable;
                                            XmlNode refAttribute = xmlDoc.CreateNode(XmlNodeType.Element, "refAttribute", null);
                                            refAttribute.InnerText = fk.RefAttribute;
                                            Refnode.AppendChild(refTable);
                                            Refnode.AppendChild(refAttribute);
                                            fkKeyNode.AppendChild(fkKeyAttr);
                                            fkKeyNode.AppendChild(Refnode);
                                            foreignKeys.AppendChild(fkKeyNode);
                                        }
                                        TableNode.AppendChild(foreignKeys);
                                        nodetables.AppendChild(TableNode);
                                        xmlDoc.Save(filePath);
                                    }
                                }
                            }
                            else
                            {
                                XmlNode NodeTables = xmlDoc.CreateNode(XmlNodeType.Element, "Tables", null);
                                nodeDB.AppendChild(NodeTables);
                                Table t = db.Tables.FirstOrDefault();
                                XmlNode TableNode = xmlDoc.CreateNode(XmlNodeType.Element, "Table", null);
                                XmlAttribute tableNameAttr = xmlDoc.CreateAttribute("tableName");
                                if (t.TableName != null)
                                    tableNameAttr.Value = t.TableName;
                                XmlAttribute fileNameAttr = xmlDoc.CreateAttribute("fileName");
                                if (t.TableName != null)
                                    fileNameAttr.Value = t.TableName.Replace(" ", "") + ".ns";
                                XmlAttribute rowLengthAttr = xmlDoc.CreateAttribute("rowLength");
                                rowLengthAttr.Value = 0.ToString();
                                TableNode.Attributes.Append(tableNameAttr);
                                TableNode.Attributes.Append(fileNameAttr);
                                TableNode.Attributes.Append(rowLengthAttr);
                                XmlNode NodeStructure = xmlDoc.CreateNode(XmlNodeType.Element, "Structure", null);
                                foreach (var column in t.Columns)
                                {
                                    XmlNode ColumnNode = xmlDoc.CreateNode(XmlNodeType.Element, "Attribute", null);
                                    XmlAttribute attributeName = xmlDoc.CreateAttribute("attributeName");
                                    if (column.ColumnName != null)
                                        attributeName.Value = column.ColumnName;
                                    XmlAttribute type = xmlDoc.CreateAttribute("type");
                                    if (column.Type != null)
                                        type.Value = column.Type;
                                    XmlAttribute length = xmlDoc.CreateAttribute("length");
                                    length.Value = column.Length.ToString();
                                    XmlAttribute isnull = xmlDoc.CreateAttribute("isnull");
                                    if (!column.IsNull)
                                        isnull.Value = Utils.Utilities.GetIntStringFromBool(column.IsNull);
                                    ColumnNode.Attributes.Append(attributeName);
                                    ColumnNode.Attributes.Append(type);
                                    ColumnNode.Attributes.Append(length);
                                    ColumnNode.Attributes.Append(isnull);
                                    NodeStructure.AppendChild(ColumnNode);
                                }
                                TableNode.AppendChild(NodeStructure);

                                XmlNode primaryKey = xmlDoc.CreateNode(XmlNodeType.Element, "primaryKey", null);
                                foreach (PrimaryKey pk in t.PrimaryKey)
                                {
                                    XmlNode pkNode = xmlDoc.CreateNode(XmlNodeType.Element, "pkAttribute", null);
                                    pkNode.InnerText = pk.Key;
                                    primaryKey.AppendChild(pkNode);
                                }
                                TableNode.AppendChild(primaryKey);
                                XmlNode uniqueKey = xmlDoc.CreateNode(XmlNodeType.Element, "uniqueKeys", null);
                                foreach (UniqueKey uk in t.UniqueKey)
                                {
                                    XmlNode ukNode = xmlDoc.CreateNode(XmlNodeType.Element, "UniqueAttribute", null);
                                    ukNode.InnerText = uk.Key;
                                    uniqueKey.AppendChild(ukNode);
                                }
                                TableNode.AppendChild(uniqueKey);
                                XmlNode foreignKeys = xmlDoc.CreateNode(XmlNodeType.Element, "foreignKeys", null);
                                foreach (ForeignKey fk in t.ForeignKey)
                                {
                                    XmlNode fkKeyNode = xmlDoc.CreateNode(XmlNodeType.Element, "foreignKey", null);
                                    XmlNode fkKeyAttr = xmlDoc.CreateNode(XmlNodeType.Element, "fkAttribute", null);
                                    fkKeyAttr.InnerText = fk.Key;
                                    XmlNode Refnode = xmlDoc.CreateNode(XmlNodeType.Element, "references", null);
                                    XmlNode refTable = xmlDoc.CreateNode(XmlNodeType.Element, "refTable", null);
                                    refTable.InnerText = fk.RefTable;
                                    XmlNode refAttribute = xmlDoc.CreateNode(XmlNodeType.Element, "refAttribute", null);
                                    refAttribute.InnerText = fk.RefAttribute;
                                    Refnode.AppendChild(refTable);
                                    Refnode.AppendChild(refAttribute);
                                    fkKeyNode.AppendChild(fkKeyAttr);
                                    fkKeyNode.AppendChild(Refnode);
                                    foreignKeys.AppendChild(fkKeyNode);
                                }
                                TableNode.AppendChild(foreignKeys);
                                NodeTables.AppendChild(TableNode);
                                nodeDB.AppendChild(NodeTables);
                                xmlDoc.Save(filePath);
                            }
                        }
                }
                MongoServer server = Utilities.CreateMongoConnection();
                foreach (Table table in db.Tables)
                {
                    string newname = table.TableName.Replace(" ", "");
                    var database = server.GetDatabase(db.DatabaseName);
                    var collection = database.GetCollection(newname);
                    KeyValue kv = new KeyValue();
                    collection.Save(kv);
                    collection.RemoveAll();
                    if (table.UniqueKey != null)
                    {
                        var collectionuk = database.GetCollection(newname + "uk");
                        KeyValue kvuk = new KeyValue();
                        collectionuk.Save(kvuk);
                        collectionuk.RemoveAll();
                    }
                    if (table.ForeignKey != null)
                    {
                        var collectionfk = database.GetCollection(newname + "fk");
                        KeyValue kvfk = new KeyValue();
                        collectionfk.Save(kvfk);
                        collectionfk.RemoveAll();
                    }
                }
            }
        }
Exemplo n.º 6
0
 public static void CreateDatabase(string dbName)
 {
     XmlDocument xmlDoc = new XmlDocument();
     xmlDoc.Load(filePath);
     XmlNode dbsNode = xmlDoc.SelectSingleNode("/Databases");
     XmlNode newNode = xmlDoc.CreateNode(XmlNodeType.Element, "DataBase", null);
     XmlAttribute newAttribute = xmlDoc.CreateAttribute("dataBaseName");
     newAttribute.Value = dbName;
     newNode.Attributes.Append(newAttribute);
     dbsNode.AppendChild(newNode);
     xmlDoc.Save(filePath);
     MongoServer server = Utilities.CreateMongoConnection();
     string newname = dbName.Replace(" ", "");
     var database = server.GetDatabase(newname);
     var collection = database.GetCollection(newname);
     KeyValue kv = new KeyValue();
     collection.Save(kv);
     database.DropCollection(newname);
     //var collection = database.GetCollection(dbName);
     //KeyValue kv = new KeyValue { key = "22", value = "sss" };
     //collection.Save(kv);
     MessageBox.Show("Database " + dbName + " successfully created!");
 }