Ejemplo n.º 1
0
        public TableDesignForm(DatabaseContext dbContext, Database database, Table table = null)
        {
            InitializeComponent();

            this.dbContext = dbContext;
            this.database = database;
            DialogResult = DialogResult.Cancel;
            TableDataGridView.CellValueChanged += new DataGridViewCellEventHandler(CreateTableDataGridView_CellValueChanged);
            TableDataGridView.DataError += CreateTableDataGridView_DataError;

            if (table != null)
            {
                TableNameTextBox.Text = table.Name;
                TableNameTextBox.Enabled = false;
                TableDataGridView.Rows.Add(table.Columns.Count);

                int index = 0;
                foreach (var column in table.Columns)
                {
                    TableDataGridView.Rows[index].Cells[0].Value = column.Name;
                    TableDataGridView.Rows[index].Cells[1].Value = DataTypeConverter.ToComboType(column.Type);
                    TableDataGridView.Rows[index].Cells[2].Value = column.Size;

                    if (table.PrimaryKey.Contains(column.Name))
                    {
                        TableDataGridView.Rows[index].Cells[3].Value = true;
                    }
                    else
                    {
                        TableDataGridView.Rows[index].Cells[3].Value = false;
                    }

                    if (column.Unique)
                    {
                        TableDataGridView.Rows[index].Cells[4].Value = true;
                    }
                    else
                    {
                        TableDataGridView.Rows[index].Cells[4].Value = false;
                    }

                    if (column.Nullable)
                    {
                        TableDataGridView.Rows[index].Cells[5].Value = true;
                    }
                    else
                    {
                        TableDataGridView.Rows[index].Cells[5].Value = false;
                    }

                    index++;
                }
            }
            else
            {
                relationshipsButton.Enabled = false;
            }
        }
 public SimpleExecutionPlanner(Database database, IRepository repository,
     ICollection<Selection> selections, ICollection<Join> joins, ICollection<Filter> filters)
 {
     this.database = database;
     this.repository = repository;
     this.selections = selections;
     this.joins = joins;
     this.filters = filters;
 }
Ejemplo n.º 3
0
 public InsertForm(DatabaseContext dbContext, Database database, Table tableSchema)
 {
     InitializeComponent();
     this.dbContext = dbContext;
     this.database = database;
     this.tableSchema = tableSchema;
     BuildUpTheForm();
     this.DialogResult = DialogResult.Cancel;
 }
Ejemplo n.º 4
0
        public NewIndexForm(DatabaseContext dbContext, Database database, Table tableSchema)
        {
            InitializeComponent();
            this.dbContext = dbContext;
            this.database = database;
            this.tableSchema = tableSchema;
            ((DataGridViewComboBoxColumn)IndexDataGridView.Columns["Column"]).Items.AddRange(tableSchema.Columns.Select(c => c.Name).ToArray());

            this.DialogResult = DialogResult.Cancel;
        }
Ejemplo n.º 5
0
        private void createButton_Click(object sender, EventArgs e)
        {
            string databaseName = databaseNameTextBox.Text.ToString();

            var result = dbContext.SchemaQuery.AddDatabase(databaseName);
            if (!result)
            {
                this.DialogResult = DialogResult.Cancel;
                MessageBox.Show("This database already exists!");
                this.DialogResult = DialogResult.Cancel;
                return;
            }
            Database = dbContext.SchemaQuery.GetDatabase(databaseName);

            this.DialogResult = DialogResult.OK;
        }
Ejemplo n.º 6
0
        public bool AddDatabase(string databaseName)
        {
            var result = databases.Find(a => a.Name.Equals(databaseName));
            if(result!= null)
            {
                return false;
            }

            Database database = new Database();
            database.Name = databaseName;

            databases.Add(database);

            XmlSerializer.Serialize<Database>(database, Location + string.Format(@"\{0}.xml", database.Name));
            return true;
        }
Ejemplo n.º 7
0
        public RelationshipsForm(DatabaseContext dbContext, Database database, string tableName)
        {
            InitializeComponent();

            this.dbContext = dbContext;
            this.tableName = tableName;
            this.database = database;

            removeButton.Enabled = false;

            tableNames = new List<string>();
            foreach (var table in database.Tables)
            {
                tableNames.Add(table.Name);
            }

            parentComboBox.Items.AddRange(tableNames.ToArray());
            childComboBox.Items.AddRange(tableNames.ToArray());

            relationshipsListBox.Items.AddRange(database.GetAssociationsWhereTableIsParent(tableName).Select(a => a.Name).ToArray());
            relationshipsListBox.Items.AddRange(database.GetAssociationsWhereTableIsChild(tableName).Select(a => a.Name).ToArray());
        }
Ejemplo n.º 8
0
        public ReturnStatus Delete(Database database, Table table, ICollection<Filter> filters)
        {
            string databaseFileName = KeyValue.GetDatabaseFileName(database.Name);
            var planner = new SimpleExecutionPlanner(database, repository, new List<Selection>(), new List<Join>(), filters);
            var rootOperation = planner.GetRootOperation();
            var nonKeyColumnNames = table.Columns.Where(c => !table.PrimaryKey.Contains(c.Name)).Select(c => c.Name).ToList();

            var metadata = rootOperation.GetMetadata();
            var rows = rootOperation.Execute();

            ReturnStatus status = new ReturnStatus();

            var indexFactory = new IndexFactory(databaseFileName, repository);

            var rowsToDelete = new List<string>();
            var rowData = new List<KeyValuePair<string, string>>();
            foreach (var row in rows)
            {
                rowsToDelete.Add(row.Key);
                rowData.Add(row);
            }

            foreach (var row in rowsToDelete)
            {
                // Check foreign key constraints.
                foreach (var association in database.GetAssociationsWhereTableIsParent(table.Name))
                {
                    var indexMeta = table.Indexes.Where(i => i.Name.Equals(association.Name)).Single();
                    var index = indexFactory.GetIndex(indexMeta);

                    var foreignKey = string.Empty;

                    if (index.Exists(row))
                    {
                        status.ReturnCode = ReturnCode.ForeignKeyConstraintFailed;
                        status.Message = string.Format("Foreign key constraint check failed, the key [{0}] is referenced in {1}.", row, association.Child);

                        return status;
                    }
                }
            }

            foreach (var row in rowData)
            {
                // Delete unique indexes.
                foreach (var uniqueIndex in table.Indexes.Where(i => i.Unique))
                {
                    var index = indexFactory.GetIndex(uniqueIndex);

                    var columnValues = new List<string>();

                    foreach (var column in uniqueIndex.IndexMembers)
                    {
                        columnValues.Add(KeyValue.Split(row.Value).ElementAt(KeyValue.Split(metadata.Value).ToList().IndexOf(column)));
                    }

                    var uniqueKey = KeyValue.Concatenate(columnValues);
                    index.Delete(uniqueKey);
                }

                // Delete foreign key indexes.
                foreach (var association in database.GetAssociationsWhereTableIsChild(table.Name))
                {
                    var parentTable = database.GetTable(association.Parent);
                    var indexMeta = parentTable.Indexes.Where(i => i.Name.Equals(association.Name)).Single();
                    var index = indexFactory.GetIndex(indexMeta);

                    var foreignKey = string.Empty;
                    if (table.PrimaryKey.Contains(association.ColumnMappings.Values.First()))
                    {
                        foreignKey = KeyValue.Split(row.Key).ElementAt(table.PrimaryKey.IndexOf(association.ColumnMappings.Values.First()));
                    }
                    else
                    {
                        foreignKey = KeyValue.Split(row.Value).ElementAt(nonKeyColumnNames.IndexOf(association.ColumnMappings.Values.First()));
                    }

                    index.Delete(foreignKey, row.Key);
                }
            }

            foreach (var rowToDelete in rowsToDelete)
            {
                // Delete primary key and data.
                repository.Delete(databaseFileName, table.Name, rowToDelete);
            }

            status.ReturnCode = ReturnCode.Success;
            status.Message = string.Format("({0} row(s) affected)", rowsToDelete.Count);

            return status;
        }
Ejemplo n.º 9
0
        public DataTable Select(Database database, ICollection<Selection> selections, ICollection<Join> joins,
            ICollection<Filter> filters)
        {
            var planner = new SimpleExecutionPlanner(database, repository, selections, joins, filters);
            var rootOperation = planner.GetRootOperation();

            DataTable result = new DataTable();

            var metadata = rootOperation.GetMetadata();
            var queryResult = rootOperation.Execute();

            // Add header.
            List<string> columns = new List<string>();
            columns.AddRange(KeyValue.Split(metadata.Value));
            columns.AddRange(KeyValue.Split(metadata.Key));

            foreach (var item in columns)
            {
                result.Columns.Add(item, typeof(string));
            }

            // Add query result.
            foreach (var pair in queryResult)
            {
                List<string> row = new List<string>();
                row.AddRange(KeyValue.Split(pair.Value));
                row.AddRange(KeyValue.Split(pair.Key));

                result.Rows.Add(row.ToArray());
            }

            return result;
        }
Ejemplo n.º 10
0
        public ReturnStatus Insert(Database database, Table table, ICollection<string> keyMembers, ICollection<string> values)
        {
            string databaseFileName = KeyValue.GetDatabaseFileName(database.Name);
            string key = KeyValue.Concatenate(keyMembers);
            string value = KeyValue.Concatenate(values);

            ReturnStatus status = new ReturnStatus();

            // Check primary key constraints.
            if (repository.Exists(databaseFileName, table.Name, key))
            {
                status.ReturnCode = ReturnCode.PrimaryKeyConstraintFailed;
                status.Message = string.Format("Primary key constraint check failed for primary key: {0}", key);

                return status;
            }

            // Check unique index constraints.
            var uniqueIndexes = new IConcreteIndex[table.Indexes.Where(i => i.Unique).Count()];
            var uniqueIndexKeys = new string[table.Indexes.Where(i => i.Unique).Count()];

            var indexFactory = new IndexFactory(databaseFileName, repository);

            var counter = 0;
            var nonKeyColumnNames = table.Columns.Where(c => !table.PrimaryKey.Contains(c.Name)).Select(c => c.Name).ToList();

            foreach (var uniqueIndex in table.Indexes.Where(i => i.Unique))
            {
                var index = indexFactory.GetIndex(uniqueIndex);
                uniqueIndexes[counter] = index;

                var columnValues = new List<string>();

                foreach (var column in uniqueIndex.IndexMembers)
                {
                    columnValues.Add(values.ElementAt(nonKeyColumnNames.IndexOf(column)));
                }

                var uniqueKey = KeyValue.Concatenate(columnValues);
                uniqueIndexKeys[counter] = uniqueKey;

                if (index.Exists(uniqueKey))
                {
                    status.ReturnCode = ReturnCode.UniqueConstraintFailed;
                    status.Message = string.Format("Unique constraint check failed for key: {0}", uniqueKey);

                    return status;
                }

                counter++;
            }

            // Check foreign key constraints (the key is simple).
            // Check if the key exists in the parent table.
            var foreignKeyIndexes = new IConcreteIndex[database.GetAssociationsWhereTableIsChild(table.Name).Count()];
            var foreignKeyIndexKeys = new string[database.GetAssociationsWhereTableIsChild(table.Name).Count()];
            counter = 0;

            foreach (var association in database.GetAssociationsWhereTableIsChild(table.Name))
            {
                var parentTable = database.GetTable(association.Parent);
                var indexMeta = parentTable.Indexes.Where(i => i.Name.Equals(association.Name)).Single();
                var index = indexFactory.GetIndex(indexMeta);

                var foreignKey = string.Empty;
                if (table.PrimaryKey.Contains(association.ColumnMappings.Values.First()))
                {
                    foreignKey = keyMembers.ElementAt(table.PrimaryKey.IndexOf(association.ColumnMappings.Values.First()));
                }
                else
                {
                    foreignKey = values.ElementAt(nonKeyColumnNames.IndexOf(association.ColumnMappings.Values.First()));
                }

                foreignKeyIndexes[counter] = index;
                foreignKeyIndexKeys[counter] = foreignKey;

                if (!repository.Exists(databaseFileName, parentTable.Name, foreignKey))
                {
                    status.ReturnCode = ReturnCode.ForeignKeyConstraintFailed;
                    status.Message = string.Format("Foreign key constraint check failed, key is missing from the parent table ({0} [{1}]): {2}", parentTable.Name,
                        association.ColumnMappings.Keys.First(), foreignKey);

                    return status;
                }

                counter++;
            }

            // Insert unique indexes.
            for (int k = 0; k < uniqueIndexes.Length; k++)
            {
                uniqueIndexes[k].Put(uniqueIndexKeys[k], key);
            }

            // Insert foreign key indexes.
            for (int k = 0; k < foreignKeyIndexes.Length; k++)
            {
                foreignKeyIndexes[k].Put(foreignKeyIndexKeys[k], key);
            }

            // Insert row if there is no errors.
            repository.Put(databaseFileName, table.Name, key, value);

            status.ReturnCode = ReturnCode.Success;
            status.Message = string.Format("({0} row(s) affected)", 1);

            return status;
        }
Ejemplo n.º 11
0
        public DataTable GetAll(Database database, Table table)
        {
            DataTable result = new DataTable();
            string databaseFileName = KeyValue.GetDatabaseFileName(database.Name);
            var queryResult = repository.GetAll(databaseFileName, table.Name);

            foreach(var item in table.Columns)
            {
                result.Columns.Add(item.Name, typeof(string));
            }

            foreach (var pair in queryResult)
            {
                List<string> row = new List<string>();
                row.AddRange(KeyValue.Split(pair.Key));
                row.AddRange(KeyValue.Split(pair.Value));

                result.Rows.Add(row.ToArray());
            }

            return result;
        }
Ejemplo n.º 12
0
        public void DeleteTable(Database database, Table table)
        {
            string databaseFileName = KeyValue.GetDatabaseFileName(database.Name);

            // Remove all index tables.
            foreach (var indexName in table.Indexes.Select(i => i.Name))
            {
                repository.DeleteTable(databaseFileName, indexName);
            }

            // Remove table.
            repository.DeleteTable(databaseFileName, table.Name);
        }