Example #1
0
        public override void Process(Expressions.RenameTableExpression expression)
        {
            truncator.Truncate(expression);
            FirebirdSchemaProvider schema   = new FirebirdSchemaProvider(this);
            TableDefinition        tableDef = schema.GetTableDefinition(expression.OldName);

            tableDef.Name = expression.NewName;
            CreateTableExpression createNew = new CreateTableExpression()
            {
                TableName  = expression.NewName,
                SchemaName = String.Empty
            };

            //copy column definitions (nb: avoid to copy key names, because in firebird they must be globally unique, so let it rename)
            tableDef.Columns.ToList().ForEach(x => createNew.Columns.Add(new ColumnDefinition()
            {
                Name             = x.Name,
                DefaultValue     = x.DefaultValue,
                IsForeignKey     = x.IsForeignKey,
                IsIdentity       = x.IsIdentity,
                IsIndexed        = x.IsIndexed,
                IsNullable       = x.IsNullable,
                IsPrimaryKey     = x.IsPrimaryKey,
                IsUnique         = x.IsUnique,
                ModificationType = x.ModificationType,
                Precision        = x.Precision,
                Size             = x.Size,
                Type             = x.Type,
                CustomType       = x.CustomType
            }));

            Process(createNew);

            int columnCount = tableDef.Columns.Count;

            string[]             columns = tableDef.Columns.Select(x => x.Name).ToArray();
            InsertDataExpression data    = new InsertDataExpression();

            using (DataSet ds = ReadTableData(String.Empty, expression.OldName))
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    InsertionDataDefinition insert = new InsertionDataDefinition();
                    for (int i = 0; i < columnCount; i++)
                    {
                        insert.Add(new KeyValuePair <string, object>(columns[i], dr.ItemArray[i]));
                    }
                    data.Rows.Add(insert);
                }
            }
            Process(data);

            DeleteTableExpression delTable = new DeleteTableExpression()
            {
                TableName  = expression.OldName,
                SchemaName = String.Empty
            };

            Process(delTable);
        }
Example #2
0
        protected void SetupUndoUpdateData(UpdateDataExpression expression)
        {
            FirebirdSchemaProvider schema = new FirebirdSchemaProvider(Processor);
            FirebirdTableSchema    table  = schema.GetTableSchema(expression.TableName);
            bool hasPrimary = table.HasPrimaryKey;

            CanUndo = true;

            using (IDataReader ds = Processor.ReadTableData(String.Empty, expression.TableName))
            {
                while (ds.Read())
                {
                    if (!hasPrimary)
                    {
                        CanUndo = false;
                        return;
                    }
                    bool match = true;
                    if (!expression.IsAllRows)
                    {
                        foreach (var where in expression.Where)
                        {
                            if (ds[where.Key].ToString() != where.Value.ToString())
                            {
                                match = false;
                                break;
                            }
                        }
                    }
                    if (match)
                    {
                        UpdateDataExpression update = new UpdateDataExpression()
                        {
                            SchemaName = expression.SchemaName,
                            TableName  = expression.TableName,
                            IsAllRows  = false,
                            Set        = new List <KeyValuePair <string, object> >()
                        };
                        foreach (var set in expression.Set)
                        {
                            update.Set.Add(new KeyValuePair <string, object>(set.Key, ds[set.Key]));
                        }
                        foreach (ColumnDefinition colDef in table.Definition.Columns)
                        {
                            if (colDef.IsPrimaryKey)
                            {
                                if (update.Where == null)
                                {
                                    update.Where = new List <KeyValuePair <string, object> >();
                                }
                                update.Where.Add(new KeyValuePair <string, object>(colDef.Name, ds[colDef.Name]));
                            }
                        }
                        UndoExpressions.Add(update);
                    }
                }
            }
        }
Example #3
0
        protected void SetupUndoDeleteColumn(DeleteColumnExpression expression)
        {
            FirebirdSchemaProvider schema = new FirebirdSchemaProvider(Processor);
            FirebirdTableSchema    table  = schema.GetTableSchema(expression.TableName);
            bool hasPrimary = table.HasPrimaryKey;

            CanUndo = true;

            using (IDataReader ds = Processor.ReadTableData(String.Empty, expression.TableName))
            {
                //Create columns
                foreach (string columnName in expression.ColumnNames)
                {
                    CreateColumnExpression createColumn = new CreateColumnExpression()
                    {
                        SchemaName = String.Empty,
                        TableName  = expression.TableName,
                        Column     = table.Definition.Columns.First(x => x.Name.ToUpper() == columnName.ToUpper())
                    };
                    UndoExpressions.Add(createColumn);

                    //NB: No need to recreate indices? Drop should fail if detects existing user created indices
                }

                //Update data
                while (ds.Read())
                {
                    if (!hasPrimary)
                    {
                        CanUndo = false;
                        return;
                    }
                    UpdateDataExpression update = new UpdateDataExpression()
                    {
                        TableName = expression.TableName,
                        IsAllRows = false,
                        Set       = new List <KeyValuePair <string, object> >(),
                        Where     = new List <KeyValuePair <string, object> >()
                    };
                    foreach (string columnName in expression.ColumnNames)
                    {
                        update.Set.Add(new KeyValuePair <string, object>(columnName, ds[columnName]));
                    }
                    table.Definition.Columns.ToList().ForEach(col =>
                    {
                        if (col.IsPrimaryKey)
                        {
                            update.Where.Add(new KeyValuePair <string, object>(col.Name, ds[col.Name]));
                        }
                    });
                    UndoExpressions.Add(update);
                }
            }
        }
Example #4
0
        protected void SetupUndoDeleteIndex(DeleteIndexExpression expression)
        {
            CanUndo = true;
            FirebirdSchemaProvider schema = new FirebirdSchemaProvider(Processor);

            CreateIndexExpression index = new CreateIndexExpression()
            {
                Index = schema.GetIndex(expression.Index.TableName, expression.Index.Name)
            };

            UndoExpressions.Add(index);
        }
Example #5
0
        private void DeleteSequenceForIdentity(string tableName, string columnName)
        {
            CheckTable(tableName);
            LockTable(tableName);

            string sequenceName;

            try{
                sequenceName = GetSequenceName(tableName, columnName);
            }
            catch (ArgumentException)
            {
                return;
            }

            DeleteSequenceExpression deleteSequence = null;

            if (SequenceExists(String.Empty, sequenceName))
            {
                deleteSequence = new DeleteSequenceExpression()
                {
                    SchemaName = String.Empty, SequenceName = sequenceName
                };
            }
            string triggerName = GetIdentityTriggerName(tableName, columnName);
            PerformDBOperationExpression    deleteTrigger = DeleteTriggerExpression(tableName, triggerName);
            FirebirdProcessedExpressionBase fbExpression  = new FirebirdProcessedExpression <PerformDBOperationExpression>(deleteTrigger, this);
            FirebirdSchemaProvider          schema        = new FirebirdSchemaProvider(this);
            List <TriggerInfo> triggers = schema.GetTableSchema(tableName).Triggers;

            foreach (TriggerInfo trigger in triggers)
            {
                if (trigger.Name.ToUpper() == triggerName.ToUpper())
                {
                    PerformDBOperationExpression createTrigger = CreateTriggerExpression(tableName, trigger);
                    if (this.FBOptions.UndoEnabled)
                    {
                        fbExpression.AddUndoExpression(createTrigger);
                    }
                    break;
                }
            }
            RegisterExpression(fbExpression);
            Process(deleteTrigger);

            if (deleteSequence != null)
            {
                Process(deleteSequence);
            }
        }
        protected void SetupUndoAlterColumn(AlterColumnExpression expression)
        {
            CanUndo = true;
            FirebirdSchemaProvider schema = new FirebirdSchemaProvider(Processor);
            FirebirdTableSchema    table  = schema.GetTableSchema(expression.TableName);
            AlterColumnExpression  alter  = new AlterColumnExpression()
            {
                SchemaName = String.Empty,
                TableName  = expression.TableName,
                Column     = table.Definition.Columns.First(x => x.Name == expression.Column.Name)
            };

            UndoExpressions.Add(alter);
        }
Example #7
0
        protected void SetupUndoDeleteColumn(DeleteColumnExpression expression)
        {
            FirebirdSchemaProvider schema = new FirebirdSchemaProvider(Processor);
            FirebirdTableSchema    table  = schema.GetTableSchema(expression.TableName);
            bool hasPrimary = table.HasPrimaryKey;

            CanUndo = false;

            /*
             * using (DataSet ds = Processor.ReadTableData(String.Empty, expression.TableName))
             * {
             *  //Create columns
             *  foreach (string columnName in expression.ColumnNames)
             *  {
             *      CreateColumnExpression createColumn = new CreateColumnExpression()
             *      {
             *          SchemaName = String.Empty,
             *          TableName = expression.TableName,
             *          Column = table.Definition.Columns.First(x => x.Name.ToUpper() == columnName.ToUpper())
             *      };
             *      UndoExpressions.Add(createColumn);
             *
             *      //NB: No need to recreate indices? Drop should fail if detects existing user created indices
             *
             *  }
             *
             *  //Update data
             *  foreach (DataRow dr in ds.Tables[0].Rows)
             *  {
             *      if (!hasPrimary)
             *      {
             *          CanUndo = false;
             *          return;
             *      }
             *      UpdateDataExpression update = new UpdateDataExpression() { TableName = expression.TableName, IsAllRows = false };
             *      foreach (string columnName in expression.ColumnNames)
             *      {
             *          update.Set.Add(new KeyValuePair<string, object>(columnName, dr[columnName]));
             *      }
             *      table.Definition.Columns.ToList().ForEach(col =>
             *      {
             *          if (col.IsPrimaryKey)
             *              update.Where.Add(new KeyValuePair<string, object>(col.Name, dr[col.Name]));
             *      });
             *      UndoExpressions.Add(update);
             *  }
             * }
             */
        }
Example #8
0
        protected void SetupUndoUpdateData(UpdateDataExpression expression)
        {
            FirebirdSchemaProvider schema = new FirebirdSchemaProvider(Processor);
            FirebirdTableSchema    table  = schema.GetTableSchema(expression.TableName);
            bool hasPrimary = table.HasPrimaryKey;

            CanUndo = false;

            /*
             * using (DataSet ds = Processor.ReadTableData(String.Empty, expression.TableName))
             * {
             *  foreach (DataRow dr in ds.Tables[0].Rows)
             *  {
             *      if (!hasPrimary)
             *      {
             *          CanUndo = false;
             *          return;
             *      }
             *      bool match = true;
             *      if (!expression.IsAllRows)
             *      {
             *          foreach (var where in expression.Where)
             *          {
             *              if (dr[where.Key].ToString() != where.Value.ToString())
             *              {
             *                  match = false;
             *                  break;
             *              }
             *          }
             *      }
             *      if (match)
             *      {
             *          UpdateDataExpression update = new UpdateDataExpression() { SchemaName = String.Empty, TableName = expression.TableName, IsAllRows = false };
             *          foreach (var set in expression.Set)
             *          {
             *              update.Set.Add(new KeyValuePair<string, object>(set.Key, dr[set.Key]));
             *          }
             *          foreach (ColumnDefinition colDef in table.Definition.Columns)
             *          {
             *              if (colDef.IsPrimaryKey)
             *                  update.Where.Add(new KeyValuePair<string, object>(colDef.Name, dr[colDef.Name]));
             *          }
             *          UndoExpressions.Add(update);
             *      }
             *  }
             * }
             */
        }
Example #9
0
        protected void SetupUndoDeleteSequence(DeleteSequenceExpression expression)
        {
            CanUndo = true;
            FirebirdSchemaProvider   schema         = new FirebirdSchemaProvider(Processor);
            SequenceInfo             sequence       = schema.GetSequence(expression.SequenceName);
            CreateSequenceExpression createSequence = new CreateSequenceExpression()
            {
                Sequence = new SequenceDefinition()
                {
                    Name      = sequence.Name,
                    StartWith = sequence.CurrentValue
                }
            };

            UndoExpressions.Add(createSequence);
        }
Example #10
0
        protected void SetupUndoDeleteData(DeleteDataExpression expression)
        {
            CanUndo = true;
            FirebirdSchemaProvider schema = new FirebirdSchemaProvider(Processor);
            FirebirdTableSchema    table  = schema.GetTableSchema(expression.TableName);

            using (IDataReader ds = Processor.ReadTableData(String.Empty, expression.TableName))
            {
                foreach (DeletionDataDefinition deletion in expression.Rows)
                {
                    InsertDataExpression insert = new InsertDataExpression()
                    {
                        SchemaName = String.Empty, TableName = expression.TableName
                    };
                    while (ds.Read())
                    {
                        bool match = true;
                        if (!expression.IsAllRows)
                        {
                            foreach (var where in deletion)
                            {
                                if (ds[where.Key].ToString() != where.Value.ToString())
                                {
                                    match = false;
                                    break;
                                }
                            }
                        }
                        if (match)
                        {
                            InsertionDataDefinition insertion = new InsertionDataDefinition();
                            foreach (ColumnDefinition colDef in table.Definition.Columns)
                            {
                                insertion.Add(new KeyValuePair <string, object>(colDef.Name, ds[colDef.Name]));
                            }
                            insert.Rows.Add(insertion);
                        }
                    }
                    UndoExpressions.Add(insert);
                }
            }
        }
Example #11
0
        public override void Process(Expressions.DeleteTableExpression expression)
        {
            truncator.Truncate(expression);
            CheckTable(expression.TableName);
            LockTable(expression.TableName);
            FirebirdSchemaProvider schema = new FirebirdSchemaProvider(this);
            TableDefinition        table  = schema.GetTableDefinition(expression.TableName);

            foreach (ColumnDefinition colDef in table.Columns)
            {
                if (SequenceExists(String.Empty, GetSequenceName(expression.TableName, colDef.Name)))
                {
                    DeleteSequenceForIdentity(expression.TableName, colDef.Name);
                }
            }
            CheckTable(expression.TableName);
            LockTable(expression.TableName);
            RegisterExpression <DeleteTableExpression>(expression);
            InternalProcess(Generator.Generate(expression));
        }
Example #12
0
        public override void Process(Expressions.AlterColumnExpression expression)
        {
            truncator.Truncate(expression);
            CheckColumn(expression.TableName, expression.Column.Name);
            FirebirdSchemaProvider schema = new FirebirdSchemaProvider(this);
            FirebirdTableSchema    table  = schema.GetTableSchema(expression.TableName);
            ColumnDefinition       colDef = table.Definition.Columns.First(x => x.Name == quoter.ToFbObjectName(expression.Column.Name));

            //Change nullable constraint
            if (colDef.IsNullable != expression.Column.IsNullable)
            {
                PerformDBOperationExpression unSet = new PerformDBOperationExpression();
                unSet.Operation = (connection, transaction) =>
                {
                    string sql = (Generator as FirebirdGenerator).GenerateSetNull(colDef);
                    Announcer.Sql(sql);
                    using (var cmd = Factory.CreateCommand(sql, connection, transaction))
                    {
                        cmd.CommandTimeout = Options.Timeout;
                        cmd.ExecuteNonQuery();
                    }
                };
                FirebirdProcessedExpressionBase fbExpression = new FirebirdProcessedExpression <AlterColumnExpression>(expression, this);
                if (this.FBOptions.UndoEnabled)
                {
                    fbExpression.AddUndoExpression(unSet);
                }
                RegisterExpression(fbExpression);
                InternalProcess((Generator as FirebirdGenerator).GenerateSetNull(expression.Column));
            }

            //Change default value
            if (!FirebirdGenerator.DefaultValuesMatch(colDef, expression.Column))
            {
                IMigrationExpression defaultConstraint;
                IMigrationExpression unsetDefaultConstraint;
                if (expression.Column.DefaultValue is ColumnDefinition.UndefinedDefaultValue)
                {
                    defaultConstraint = new DeleteDefaultConstraintExpression()
                    {
                        SchemaName = expression.SchemaName,
                        TableName  = expression.TableName,
                        ColumnName = expression.Column.Name
                    };
                }
                else
                {
                    defaultConstraint = new AlterDefaultConstraintExpression()
                    {
                        ColumnName   = expression.Column.Name,
                        DefaultValue = expression.Column.DefaultValue,
                        TableName    = expression.TableName,
                        SchemaName   = expression.SchemaName
                    };
                }

                if (colDef.DefaultValue is ColumnDefinition.UndefinedDefaultValue)
                {
                    unsetDefaultConstraint = new DeleteDefaultConstraintExpression()
                    {
                        ColumnName = colDef.Name,
                        TableName  = expression.TableName,
                        SchemaName = expression.SchemaName
                    };
                }
                else
                {
                    unsetDefaultConstraint = new AlterDefaultConstraintExpression()
                    {
                        ColumnName   = colDef.Name,
                        DefaultValue = colDef.DefaultValue,
                        TableName    = expression.TableName,
                        SchemaName   = expression.SchemaName
                    };
                }

                FirebirdProcessedExpressionBase fbExpression = new FirebirdProcessedExpression(defaultConstraint, defaultConstraint.GetType(), this);
                if (this.FBOptions.UndoEnabled)
                {
                    fbExpression.AddUndoExpression(unsetDefaultConstraint);
                }
                RegisterExpression(fbExpression);
                if (defaultConstraint is DeleteDefaultConstraintExpression)
                {
                    InternalProcess(Generator.Generate(defaultConstraint as DeleteDefaultConstraintExpression));
                }
                else if (defaultConstraint is AlterDefaultConstraintExpression)
                {
                    InternalProcess(Generator.Generate(defaultConstraint as AlterDefaultConstraintExpression));
                }
                else
                {
                    throw new InvalidOperationException("No expression generated for alter default constraint");
                }
            }

            //Change type
            if (!FirebirdGenerator.ColumnTypesMatch(colDef, expression.Column))
            {
                PerformDBOperationExpression unSet = new PerformDBOperationExpression();
                unSet.Operation = (connection, transaction) =>
                {
                    string sql = (Generator as FirebirdGenerator).GenerateSetType(colDef);
                    Announcer.Sql(sql);
                    using (var cmd = Factory.CreateCommand(sql, connection, transaction))
                    {
                        cmd.CommandTimeout = Options.Timeout;
                        cmd.ExecuteNonQuery();
                    }
                };
                FirebirdProcessedExpressionBase fbExpression = new FirebirdProcessedExpression <AlterColumnExpression>(expression, this);
                if (this.FBOptions.UndoEnabled)
                {
                    fbExpression.AddUndoExpression(unSet);
                }
                RegisterExpression(fbExpression);
                InternalProcess((Generator as FirebirdGenerator).GenerateSetType(expression.Column));
            }

            bool identitySequenceExists;

            try
            {
                identitySequenceExists = SequenceExists(String.Empty, GetSequenceName(expression.TableName, expression.Column.Name));
            }
            catch (ArgumentException)
            {
                identitySequenceExists = false;
            }


            //Adjust identity generators
            if (expression.Column.IsIdentity)
            {
                if (!identitySequenceExists)
                {
                    CreateSequenceForIdentity(expression.TableName, expression.Column.Name);
                }
            }
            else
            {
                if (identitySequenceExists)
                {
                    DeleteSequenceForIdentity(expression.TableName, expression.Column.Name);
                }
            }
        }
        public override void Process(AlterColumnExpression expression)
        {
            Truncator.Truncate(expression);
            CheckColumn(expression.TableName, expression.Column.Name);
            FirebirdSchemaProvider schema = new FirebirdSchemaProvider(this, _quoter);
            FirebirdTableSchema    table  = schema.GetTableSchema(expression.TableName);
            ColumnDefinition       colDef = table.Definition.Columns.FirstOrDefault(x => x.Name == _quoter.ToFbObjectName(expression.Column.Name));

            var generator = (FirebirdGenerator)Generator;

            var tableName = expression.Column.TableName ?? expression.TableName;

            //Change nullable constraint
            if (colDef == null || colDef.IsNullable != expression.Column.IsNullable)
            {
                string nullConstraintCommand;
                if (IsFirebird3)
                {
                    nullConstraintCommand = generator.GenerateSetNull3(tableName, expression.Column);
                }
                else
                {
                    nullConstraintCommand = generator.GenerateSetNullPre3(tableName, expression.Column);
                }

                InternalProcess(nullConstraintCommand);
            }

            //Change default value
            if (colDef == null || !FirebirdGenerator.DefaultValuesMatch(colDef, expression.Column))
            {
                IMigrationExpression defaultConstraint;
                if (expression.Column.DefaultValue is ColumnDefinition.UndefinedDefaultValue)
                {
                    defaultConstraint = new DeleteDefaultConstraintExpression()
                    {
                        SchemaName = expression.SchemaName,
                        TableName  = expression.TableName,
                        ColumnName = expression.Column.Name
                    };
                }
                else
                {
                    defaultConstraint = new AlterDefaultConstraintExpression()
                    {
                        ColumnName   = expression.Column.Name,
                        DefaultValue = expression.Column.DefaultValue,
                        TableName    = expression.TableName,
                        SchemaName   = expression.SchemaName
                    };
                }

                if (defaultConstraint is DeleteDefaultConstraintExpression deleteDefaultConstraintExpression)
                {
                    InternalProcess(Generator.Generate(deleteDefaultConstraintExpression));
                }
                else
                {
                    InternalProcess(Generator.Generate((AlterDefaultConstraintExpression)defaultConstraint));
                }
            }

            //Change type
            if (colDef == null || !FirebirdGenerator.ColumnTypesMatch(colDef, expression.Column))
            {
                InternalProcess(generator.GenerateSetType(tableName, expression.Column));
            }

            bool identitySequenceExists;

            try
            {
                identitySequenceExists = SequenceExists(string.Empty, GetSequenceName(expression.TableName, expression.Column.Name));
            }
            catch (ArgumentException)
            {
                identitySequenceExists = false;
            }


            //Adjust identity generators
            if (expression.Column.IsIdentity)
            {
                if (!identitySequenceExists)
                {
                    CreateSequenceForIdentity(expression.TableName, expression.Column.Name);
                }
            }
            else
            {
                if (identitySequenceExists)
                {
                    DeleteSequenceForIdentity(expression.TableName, expression.Column.Name);
                }
            }
        }
Example #14
0
        protected void SetupUndoDeleteTable(DeleteTableExpression expression)
        {
            CanUndo = true;
            FirebirdSchemaProvider schema = new FirebirdSchemaProvider(Processor);

            //Table and columns
            CreateTableExpression table = new CreateTableExpression()
            {
                TableName  = expression.TableName,
                SchemaName = expression.SchemaName,
            };

            TableDefinition tableDef = schema.GetTableDefinition(expression.TableName);

            foreach (ColumnDefinition colDef in tableDef.Columns)
            {
                table.Columns.Add(colDef);
            }

            UndoExpressions.Add(table);

            //Indices
            foreach (IndexDefinition indexDef in tableDef.Indexes)
            {
                CreateIndexExpression indexExpression = new CreateIndexExpression()
                {
                    Index = indexDef
                };
                UndoExpressions.Add(indexExpression);
            }

            //Foreign keys
            foreach (ForeignKeyDefinition fkDef in tableDef.ForeignKeys)
            {
                CreateForeignKeyExpression fkExpression = new CreateForeignKeyExpression()
                {
                    ForeignKey = fkDef
                };
                UndoExpressions.Add(fkExpression);
            }

            //Data
            InsertDataExpression values = new InsertDataExpression()
            {
                TableName  = expression.TableName,
                SchemaName = expression.SchemaName
            };

            using (IDataReader data = Processor.ReadTableData(String.Empty, expression.TableName))
            {
                int columnCount = data.FieldCount;

                while (data.Read())
                {
                    InsertionDataDefinition insert = new InsertionDataDefinition();
                    for (int i = 0; i < columnCount; i++)
                    {
                        insert.Add(new KeyValuePair <string, object>(data.GetName(i), data.GetValue(i)));
                    }
                    values.Rows.Add(insert);
                }
            }

            UndoExpressions.Add(values);

            //Triggers
            FirebirdTableSchema tableSchema = schema.GetTableSchema(expression.TableName);

            foreach (TriggerInfo trigger in tableSchema.Triggers)
            {
                PerformDBOperationExpression createTrigger = Processor.CreateTriggerExpression(expression.TableName, trigger);
                UndoExpressions.Add(createTrigger);
            }
        }