Ejemplo n.º 1
0
        public override void PerformXMLActions()
        {
            try
            {
                var mongoDB = new MongoDBAcess(DatabaseName);
                if (RemovedKey == "ALL")
                {
                    mongoDB.RemoveAllKVFromCollection(TableName);
                }
                else
                {
                    // Remove record PK from any Index File
                    RemoveFromIndexFiles(mongoDB);

                    // Remove record PK from FK value
                    RemoveFromFKIndexFiles(mongoDB);

                    // Remove record from UQ file
                    RemoveFromUQIndexFiles(mongoDB);

                    // Remove record from main table collection
                    mongoDB.RemoveKVFromCollection(TableName, RemovedKey);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Ejemplo n.º 2
0
        public override void PerformXMLActions()
        {
            try
            {
                MongoDBAcess.RemoveDatabase(DatabaseName);

                var      xmlDocument   = XDocument.Load(Application.StartupPath + "\\SGBDCatalog.xml");
                XElement deletedXMLTag = null;
                var      databasesTags = xmlDocument.Element("Databases").Descendants("Database").ToArray();
                foreach (XElement tag in databasesTags)
                {
                    if (tag.Attribute("databaseName").Value.Equals(DatabaseName))
                    {
                        deletedXMLTag = tag;
                        break;
                    }
                }
                deletedXMLTag.Remove();
                xmlDocument.Save(Application.StartupPath + "\\SGBDCatalog.xml");
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Ejemplo n.º 3
0
        private void DetermineColumnsPositions(MongoDBAcess mongoDB)
        {
            var columnInfoString = DatabaseManager.FetchTableColumns(DBName, TableName);

            foreach (var info in columnInfoString.Split(';')[1].Split('|'))
            {
                ColumnsInfo.Add(new ColumnInfo(info));
            }

            var columnInfo = DatabaseManager.FetchTableColumns(DBName, TableName).Split(';')[1].Split('|');

            for (int idx = 0; idx < columnInfo.Length; idx++)
            {
                ColumnsPositions.Add(new KeyValuePair <string, int>(columnInfo[idx].Split('#')[0], idx));
            }

            var tableContent = mongoDB.GetEntireCollection(TableName);

            foreach (var record in tableContent)
            {
                var keySplit   = record.GetElement("_id").Value.ToString().Split('#');
                var valueSplit = record.GetElement("value").Value.ToString().Split('#');
                RecordsSplit.Add(keySplit.Concat(valueSplit).ToList());
            }
        }
Ejemplo n.º 4
0
        private void CheckUniqueIndexConstraint(string record)
        {
            var columnValues = record.Split('#');
            var mongoDB      = new MongoDBAcess(DatabaseName);
            var indexFiles   = TableUtils.GetIndexFiles(DatabaseName, TableName);

            foreach (var file in indexFiles)
            {
                if (file.IsUnique)
                {
                    var createKeyOfUqIndex = "";
                    var pk = "";
                    for (int idx = 0; idx < ColumnsInfo.Count; idx++)
                    {
                        if (file.IndexColumns.Exists(elem => elem == ColumnsInfo[idx].ColumnName))
                        {
                            createKeyOfUqIndex += columnValues[idx] + '#';
                        }

                        if (ColumnsInfo[idx].PK)
                        {
                            pk = columnValues[idx] + "#";
                        }
                    }

                    createKeyOfUqIndex = createKeyOfUqIndex.Remove(createKeyOfUqIndex.Length - 1);
                    pk = pk.Remove(pk.Length - 1);

                    MongoDB.InsertKVIntoCollection(file.IndexFileName, createKeyOfUqIndex, pk);
                }
            }
        }
Ejemplo n.º 5
0
        private void RemoveFromUQIndexFiles(MongoDBAcess mongoDB)
        {
            var uniqueKeyFiles = GetUniqueKeyInformation();

            foreach (var uniqueKey in uniqueKeyFiles)
            {
                mongoDB.RemoveValueFromCollection(uniqueKey.Item2, RemovedKey);
            }
        }
Ejemplo n.º 6
0
        private void RemoveFromFKIndexFiles(MongoDBAcess mongoDB)
        {
            ForeignKeyFiles.Clear();
            ForeignKeyFiles = TableUtils.GetOwnForeignKeyFiles(DatabaseName, TableName);

            foreach (var foreignKey in ForeignKeyFiles)
            {
                mongoDB.RemoveValueFromCollection(foreignKey, RemovedKey);
            }
        }
Ejemplo n.º 7
0
        private void IndexRecordsUnique(MongoDBAcess mongoDB)
        {
            List <KeyValuePair <string, string> > indexedContent = new List <KeyValuePair <string, string> >();
            List <string> indexKeys = new List <string>();

            for (int idx = 0; idx < RecordsSplit.Count(); idx++)
            {
                var indexKey = "";
                foreach (var indexColumn in columns)
                {
                    var columnValue = RecordsSplit[idx][ColumnsPositions.Find(elem => elem.Key == indexColumn).Value];
                    indexKey += columnValue + '#';
                }
                indexKey = indexKey.Remove(indexKey.Length - 1);

                var recordPK = "";
                foreach (var column in ColumnsInfo)
                {
                    if (column.PK)
                    {
                        recordPK += RecordsSplit[idx][ColumnsPositions.Find(elem => elem.Key == column.ColumnName).Value] + '#';
                    }
                }
                recordPK = recordPK.Remove(recordPK.Length - 1);


                if (indexKeys.Exists(elem => elem == indexKey))
                {
                    throw new Exception("Could not create unique Index for table: " + TableName + " due to duplicate values in columns: " + columnsString);
                }
                else
                {
                    indexKeys.Add(indexKey);
                    indexedContent.Add(new KeyValuePair <string, string>(indexKey, recordPK));
                }
            }

            try
            {
                foreach (var indexKeyValue in indexedContent)
                {
                    mongoDB.InsertKVIntoCollection(indexName, indexKeyValue.Key, indexKeyValue.Value);
                }
            }
            catch (Exception)
            {
                throw new Exception("Could not index content for table: " + TableName);
            }
        }
Ejemplo n.º 8
0
        public override void PerformXMLActions()
        {
            try
            {
                // Create corresponding MongoDB collection for the index
                var mongoDB = new MongoDBAcess(DBName);

                if (mongoDB.CollectionExists(indexName))
                {
                    throw new Exception("An index for column combination: " + columnsString + " already exists!");
                }

                // If the table already has records => organize the contents in the new index file
                if (mongoDB.CollectionHasDocuments(TableName))
                {
                    DetermineColumnsPositions(mongoDB);
                    if (indexType == true)
                    {
                        IndexRecordsUnique(mongoDB);
                    }
                    else
                    {
                        IndexRecordsNonUnique(mongoDB);
                    }
                }

                // Add the Index to the XML structure
                var        xmlDocument     = XDocument.Load(Application.StartupPath + "\\SGBDCatalog.xml");
                XElement[] databasesNodes  = xmlDocument.Element("Databases").Descendants("Database").ToArray();
                XElement   givenDB         = Array.Find(databasesNodes, elem => elem.Attribute("databaseName").Value.Equals(DBName));
                XElement[] databasesTables = givenDB.Descendants("Table").ToArray();
                XElement   givenTable      = Array.Find(databasesTables, elem => elem.Attribute("tableName").Value.Equals(TableName));
                XElement   indexFilesNode  = givenTable.Descendants("IndexFiles").ToArray()[0];
                XElement   indexNode       = new XElement("IndexFile", new XAttribute("isUnique", indexType), new XAttribute("indexName", indexName));

                foreach (var col in columns)
                {
                    indexNode.Add(new XElement("IndexAttribute", col));
                }

                indexFilesNode.Add(indexNode);
                xmlDocument.Save(Application.StartupPath + "\\SGBDCatalog.xml");
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public SelectWithJoinQuery(string _databaseName, string _attributes)
        {
            DatabaseName = _databaseName;
            Attributes   = _attributes;
            MongoDB      = new MongoDBAcess(DatabaseName);

            SelectedTablesStructure = new List <Tuple <string, List <string> > >();
            SelectJoinInfo          = new List <SelectJoinInfo>();

            SelectConfigList      = new List <SelectRowInfo>();
            OutputParamsAliasList = new List <KeyValuePair <string, List <Tuple <string, string> > > >();
            WhereConditionsList   = new List <KeyValuePair <string, List <Tuple <string, string> > > >();
            AggregateFunctionList = new List <KeyValuePair <string, List <Tuple <string, string> > > >();
            GroupByList           = new List <KeyValuePair <string, List <string> > >();
            HavingParamsList      = new List <KeyValuePair <string, List <Tuple <string, string> > > >();
        }
Ejemplo n.º 10
0
        private void RemoveFromIndexFiles(MongoDBAcess mongoDB)
        {
            var indexFiles = TableUtils.GetIndexFiles(DatabaseName, TableName);

            foreach (var index in indexFiles)
            {
                if (index.IsUnique)
                {
                    // Entire KV pair needs to be removed from the file
                    mongoDB.RemoveByValueFromCollection(index.IndexFileName, RemovedKey);
                }
                else
                {
                    // Only the current key needs to be removed from the Key-Value
                    mongoDB.RemoveValueFromCollection(index.IndexFileName, RemovedKey);
                }
            }
        }
Ejemplo n.º 11
0
        public override string ValidateQuery()
        {
            if (TableUtils.IsTableReferenced(DatabaseName, TableName))
            {
                var MongoDB = new MongoDBAcess(DatabaseName);
                ForeignKeyFiles = TableUtils.GetForeignKeyFiles(DatabaseName, TableName);
                foreach (var foreignKey in ForeignKeyFiles)
                {
                    if (MongoDB.CollectionContainsKey(foreignKey, RemovedKey))
                    {
                        // PK of the record is used as a FK in another table => error
                        var tableName = foreignKey.Split('_');
                        return(Responses.DELETE_RECORD_USED_AS_FK + tableName[1]);
                    }
                }
            }

            return(Responses.DELETE_RECORD_SUCCESS);
        }
Ejemplo n.º 12
0
        public override void ParseAttributes()
        {
            var tableAttributes = QueryAttributes.Split('#');

            DatabaseName     = tableAttributes[0];
            TableName        = tableAttributes[1];
            Columns          = new List <TableColumn>();
            ReferencedTables = new List <string>();

            // Columns definition
            for (int idx = 2; idx < tableAttributes.Length - 1; idx++)
            {
                var columnAttributes = tableAttributes[idx].Split('|');
                var columnName       = columnAttributes[0];
                var columnPK         = bool.Parse(columnAttributes[1]);
                var columnType       = columnAttributes[2];
                int.TryParse(columnAttributes[3], out var columnLength);
                var columnUnique    = bool.Parse(columnAttributes[4]);
                var columnAllowNull = bool.Parse(columnAttributes[5]);
                Columns.Add(new TableColumn(columnName, columnPK, columnType, columnLength, columnUnique, columnAllowNull));
            }

            // FK references definition
            if (tableAttributes[tableAttributes.Length - 1] != "")
            {
                var refTables = tableAttributes[tableAttributes.Length - 1].Split('|');
                foreach (string tableName in refTables)
                {
                    if (tableName != "")
                    {
                        ReferencedTables.Add(tableName);
                    }
                }
            }

            MongoDB = new MongoDBAcess(DatabaseName);
        }
Ejemplo n.º 13
0
        public override void PerformXMLActions()
        {
            var      xmlDocument       = XDocument.Load(Application.StartupPath + "\\SGBDCatalog.xml");
            XElement givenDatabaseNode = null;

            XElement[] databasesNodes = xmlDocument.Element("Databases").Descendants("Database").ToArray();
            for (int i = 0; i < databasesNodes.Length; i++)
            {
                if (databasesNodes[i].Attribute("databaseName").Value.Equals(DatabaseName))
                {
                    givenDatabaseNode = databasesNodes[i];
                    break;
                }
            }

            XElement newTableNode        = new XElement("Table");
            XElement structureNode       = new XElement("Structure");
            XElement primaryKeyNode      = new XElement("PrimaryKey");
            XElement indexFilesAttribute = new XElement("IndexFiles");
            XElement foreignKeysNode     = new XElement("ForeignKeys");
            XElement uniqueKeysNode      = new XElement("UniqueKeys");;

            newTableNode.Add(structureNode);
            newTableNode.Add(primaryKeyNode);
            if (AreThereForeignKeys())
            {
                newTableNode.Add(foreignKeysNode);
            }
            if (AreThereUniqueKeys())
            {
                newTableNode.Add(uniqueKeysNode);
            }
            newTableNode.Add(indexFilesAttribute);

            int rowLength = 0;

            foreach (TableColumn tableColumn in Columns)
            {
                XElement columnNode = new XElement("Column",
                                                   new XAttribute("allowsNulls", tableColumn.AllowsNulls),
                                                   new XAttribute("type", tableColumn.Type),
                                                   new XAttribute("columnName", tableColumn.Name));

                if (tableColumn.Length != 0)
                {
                    columnNode.SetAttributeValue("length", tableColumn.Length);
                }

                structureNode.Add(columnNode);
                rowLength += tableColumn.Length;

                if (tableColumn.IsPrimaryKey)
                {
                    XElement pkColumnNode = new XElement("PrimaryKeyColumn", tableColumn.Name);
                    primaryKeyNode.Add(pkColumnNode);
                }

                if (tableColumn.IsUnique)
                {
                    XElement uniqueColumnNode  = new XElement("UniqueKeyColumn", tableColumn.Name);
                    var      uniqueKeyFilename = "UK_" + TableName + '_' + tableColumn.Name;
                    uniqueColumnNode.Add(new XAttribute("fileName", uniqueKeyFilename));
                    uniqueKeysNode.Add(uniqueColumnNode);

                    // Create MongoDB collection for unique key
                    MongoDB.CreateCollection(uniqueKeyFilename);
                }
            }

            // Create the Foreign Key tags
            if (AreThereForeignKeys())
            {
                foreach (string reference in ReferencedTables)
                {
                    // Create MongoDB Collection for the FK Index file
                    MongoDB.CreateCollection("FK_" + TableName + "_" + reference);

                    // Create the XML nodes for the Reference
                    XElement foreignKeyNode = new XElement("ForeignKey");
                    foreignKeysNode.Add(foreignKeyNode);
                    foreignKeyNode.Add(new XAttribute("fileName", "FK_" + TableName + "_" + reference));

                    foreignKeyNode.Add(new XElement("ReferencedTable", reference));

                    foreach (TableColumn foreignKey in GetPrimaryKeysOfGivenTable(reference, givenDatabaseNode))
                    {
                        XElement referencedColumn = new XElement("ReferencedColumn", foreignKey.Name);
                        foreignKeyNode.Add(referencedColumn);

                        // Also add the PK field to the table structure as a column
                        structureNode.Add(new XElement("Column",
                                                       new XAttribute("allowsNulls", foreignKey.AllowsNulls),
                                                       new XAttribute("length", foreignKey.Length),
                                                       new XAttribute("type", foreignKey.Type),
                                                       new XAttribute("columnName", foreignKey.Name)));
                    }
                }
            }

            newTableNode.SetAttributeValue("rowLength", rowLength);
            newTableNode.SetAttributeValue("fileName", TableName);
            newTableNode.SetAttributeValue("tableName", TableName);
            givenDatabaseNode.Add(newTableNode);
            xmlDocument.Save(Application.StartupPath + "\\SGBDCatalog.xml");

            // Create the correponding MongoDB Collection for the table
            try
            {
                var mongoDB = new MongoDBAcess(DatabaseName);
                mongoDB.CreateCollection(TableName);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Ejemplo n.º 14
0
 public SelectQuery(string _databaseName, string _attributes) : base(Commands.SELECT_RECORDS)
 {
     DatabaseName = _databaseName;
     Attributes   = _attributes;
     MongoDB      = new MongoDBAcess(DatabaseName);
 }
Ejemplo n.º 15
0
        public override void ParseAttributes()
        {
            var recordsArray = RecordsString.Split('|');

            // Create Key-Value pairs that will be added to the MongoDB
            foreach (var newRecord in recordsArray)
            {
                var keyValuePair = newRecord.Split('*');

                // Special handling for tables with one column, where key = value bc I said so
                if (keyValuePair.Length == 1)
                {
                    Records.Add(new KeyValuePair <string, string>(keyValuePair[0], keyValuePair[0]));
                }
                else
                {
                    Records.Add(new KeyValuePair <string, string>(keyValuePair[0], keyValuePair[1]));
                }
            }

            // Get the information about the table columns
            var columnInfoString = DatabaseManager.FetchTableColumns(DatabaseName, TableName);

            foreach (var columnInfo in columnInfoString.Split(';')[1].Split('|'))
            {
                ColumnsInfo.Add(new ColumnInfo(columnInfo));
            }

            // Get a list of Primary Key names + the positions within the table structure of the PK column
            for (int idx = 0; idx < ColumnsInfo.Count; idx++)
            {
                if (ColumnsInfo[idx].PK)
                {
                    PrimaryKeyPositions.Add(new KeyValuePair <string, int>(ColumnsInfo[idx].ColumnName, idx));
                }
            }

            // Get a list of Foreign Key names + the position within the table struture of the FK column
            for (int idx = 0; idx < ColumnsInfo.Count; idx++)
            {
                if (ColumnsInfo[idx].FK)
                {
                    ForeignKeyPositions.Add(new KeyValuePair <string, int>(ColumnsInfo[idx].ColumnName, idx));
                }
            }

            for (int idx = 0; idx < ColumnsInfo.Count; idx++)
            {
                if (ColumnsInfo[idx].Unique)
                {
                    UniqueKeyPositions.Add(new KeyValuePair <string, int>(ColumnsInfo[idx].ColumnName, idx));
                }
            }

            ForeignKeyData = GetReferencesInformation();

            UniqueKeyData = GetUniqueKeyInformation();

            // Initialize MongoDB Access class
            MongoDB = new MongoDBAcess(DatabaseName);
        }
Ejemplo n.º 16
0
 public DropTableQuery(string _queryAttributesDB, string _queryAttributesTB) : base(Commands.DROP_TABLE)
 {
     DBName    = _queryAttributesDB;
     TableName = _queryAttributesTB;
     MongoDB   = new MongoDBAcess(DBName);
 }