コード例 #1
0
ファイル: Database.cs プロジェクト: pongracz-lajos/pseudo-db
        public void AddTable(string tableName)
        {
            Table table = new Table();
            table.Name = tableName;

            Tables.Add(table);
        }
コード例 #2
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;
            }
        }
コード例 #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;
 }
コード例 #4
0
        public FullScanOperation(Table table, IRepository repository, string databaseFile, string tableName)
        {
            this.table = table;
            this.repository = repository;
            this.databaseFile = databaseFile;
            this.tableName = tableName;

            log4net.Config.XmlConfigurator.Configure();
            log = LogManager.GetLogger("ExecutionPlan");
        }
コード例 #5
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;
        }
コード例 #6
0
ファイル: Query.cs プロジェクト: pongracz-lajos/pseudo-db
        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;
        }
コード例 #7
0
ファイル: Query.cs プロジェクト: pongracz-lajos/pseudo-db
        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;
        }
コード例 #8
0
ファイル: Query.cs プロジェクト: pongracz-lajos/pseudo-db
        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;
        }
コード例 #9
0
ファイル: Query.cs プロジェクト: pongracz-lajos/pseudo-db
        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);
        }
コード例 #10
0
        private void SaveTableButton_Click(object sender, System.EventArgs e)
        {
            //TODO: validate inputs

            var tableName = TableNameTextBox.Text.ToString();
            if (string.IsNullOrEmpty(tableName))
            {
                MessageBox.Show(Resources.ResourceManager.GetString("MissingTableName"));
                return;
            }

            var result = database.Tables.Find(t => t.Name.Equals(tableName));
            if(result != null)
            {
                MessageBox.Show(Resources.ResourceManager.GetString("TableNameTaked"));
                return;
            }

            table = new Table();
            table.Name = tableName;

            try
            {
                for (int i = 0; i < TableDataGridView.Rows.Count - 1; i++)
                {

                    Column column = new Column();
                    column.Name = TableDataGridView.Rows[i].Cells[0].Value.ToString();
                    column.Type = DataTypeConverter.ToDataType(TableDataGridView.Rows[i].Cells[1].Value.ToString());
                    column.Size = Int32.Parse(TableDataGridView.Rows[i].Cells[2].Value.ToString());
                    bool isPrimaryKey = (bool) ((DataGridViewCheckBoxCell) TableDataGridView.Rows[i].Cells[3]).FormattedValue;
                    column.Unique = (bool) ((DataGridViewCheckBoxCell) TableDataGridView.Rows[i].Cells[4]).FormattedValue;
                    column.Nullable = (bool) ((DataGridViewCheckBoxCell)TableDataGridView.Rows[i].Cells[5]).FormattedValue;

                    if (isPrimaryKey)
                    {
                        table.PrimaryKey.Add(column.Name);
                    }

                    table.Columns.Add(column);
                }

                // Create indexes for unique values.
                foreach (var column in table.Columns.Where(c => c.Unique).Select(c => c.Name))
                {
                    Index index = new Index();
                    index.Name = string.Format("{0}_Index_{1}", table.Name, table.Indexes.Count + 1);
                    index.IndexMembers.Add(column);
                    index.Unique = true;
                    table.Indexes.Add(index);
                }

                if (database.GetTable(table.Name) != null)
                {
                    Table tableToDelete = database.GetTable(table.Name);
                    dbContext.Query.DeleteTable(database, tableToDelete);
                    database.Tables.Remove(tableToDelete);
                }

                database.Tables.Add(table);
            }
            catch (NullReferenceException exception)
            {
                MessageBox.Show("Complete all cells!\n" + exception.Message);
            }

            DialogResult = DialogResult.OK;
        }