예제 #1
0
        private void CreateSequenceForIdentity(string tableName, string columnName)
        {
            CheckTable(tableName);
            LockTable(tableName);
            string sequenceName = GetSequenceName(tableName, columnName);

            if (!SequenceExists(String.Empty, sequenceName))
            {
                CreateSequenceExpression sequence = new CreateSequenceExpression()
                {
                    Sequence = new SequenceDefinition()
                    {
                        Name = sequenceName
                    }
                };
                Process(sequence);
            }
            string triggerName  = GetIdentityTriggerName(tableName, columnName);
            string quotedColumn = quoter.Quote(columnName);
            string trigger      = String.Format("as begin if (NEW.{0} is NULL) then NEW.{1} = GEN_ID({2}, 1); end", quotedColumn, quotedColumn, quoter.QuoteSequenceName(sequenceName));

            PerformDBOperationExpression    createTrigger = CreateTriggerExpression(tableName, triggerName, true, TriggerEvent.Insert, trigger);
            PerformDBOperationExpression    deleteTrigger = DeleteTriggerExpression(tableName, triggerName);
            FirebirdProcessedExpressionBase fbExpression  = new FirebirdProcessedExpression(createTrigger, typeof(PerformDBOperationExpression), this);

            if (this.FBOptions.UndoEnabled)
            {
                fbExpression.AddUndoExpression(deleteTrigger);
            }
            RegisterExpression(fbExpression);
            Process(createTrigger);
        }
예제 #2
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);
            }
        }
예제 #3
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);
                }
            }
        }