public virtual void DropIdentityForColumn(string columnName, string tableName, MigrationCommandListBuilder builder)
        {
            var triggerName = CreateTriggerName(columnName, tableName);

            builder.AppendLine("EXECUTE BLOCK");
            builder.AppendLine("AS");
            builder.AppendLine("BEGIN");
            builder.IncrementIndent();
            builder.Append("if (exists(select 1 from rdb$triggers where rdb$trigger_name = '");
            builder.Append(triggerName);
            builder.Append("')) then");
            builder.AppendLine();
            builder.AppendLine("begin");
            builder.IncrementIndent();
            builder.Append("execute statement 'drop trigger ");
            builder.Append(_sqlHelper.DelimitIdentifier(triggerName));
            builder.Append("'");
            builder.Append(_sqlHelper.StatementTerminator);
            builder.AppendLine();
            builder.DecrementIndent();
            builder.AppendLine("end");
            builder.DecrementIndent();
            builder.Append("END");
            builder.EndCommand();
        }
Example #2
0
        public virtual void CreateSequenceTriggerForColumn(string columnName, string tableName, string schemaName, MigrationCommandListBuilder builder)
        {
            var identitySequenceName = CreateSequenceTriggerSequenceName(columnName, tableName, schemaName);

            builder.AppendLine("EXECUTE BLOCK");
            builder.AppendLine("AS");
            builder.AppendLine("BEGIN");
            builder.IncrementIndent();
            builder.Append("if (not exists(select 1 from rdb$generators where rdb$generator_name = '");
            builder.Append(identitySequenceName);
            builder.Append("')) then");
            builder.AppendLine();
            builder.AppendLine("begin");
            builder.IncrementIndent();
            builder.Append("execute statement 'create sequence ");
            builder.Append(identitySequenceName);
            builder.Append("'");
            builder.Append(_sqlGenerationHelper.StatementTerminator);
            builder.AppendLine();
            builder.DecrementIndent();
            builder.AppendLine("end");
            builder.DecrementIndent();
            builder.Append("END");
            builder.AppendLine();
            builder.AppendLine(_sqlGenerationHelper.StatementTerminator);
            builder.EndCommand();

            builder.Append("CREATE TRIGGER ");
            builder.Append(_sqlGenerationHelper.DelimitIdentifier(CreateSequenceTriggerName(columnName, tableName, schemaName)));
            builder.Append(" ACTIVE BEFORE INSERT ON ");
            builder.Append(_sqlGenerationHelper.DelimitIdentifier(tableName, schemaName));
            builder.AppendLine();
            builder.AppendLine("AS");
            builder.AppendLine("BEGIN");
            builder.IncrementIndent();
            builder.Append("if (new.");
            builder.Append(_sqlGenerationHelper.DelimitIdentifier(columnName));
            builder.Append(" is null) then");
            builder.AppendLine();
            builder.AppendLine("begin");
            builder.IncrementIndent();
            builder.Append("new.");
            builder.Append(_sqlGenerationHelper.DelimitIdentifier(columnName));
            builder.Append(" = next value for ");
            builder.Append(identitySequenceName);
            builder.Append(_sqlGenerationHelper.StatementTerminator);
            builder.AppendLine();
            builder.DecrementIndent();
            builder.AppendLine("end");
            builder.DecrementIndent();
            builder.Append("END");
            builder.AppendLine();
            builder.AppendLine(_sqlGenerationHelper.StatementTerminator);
            builder.EndCommand();
        }
    public virtual void DropSequenceTriggerForColumn(string columnName, string tableName, string schemaName, MigrationsSqlGenerationOptions options, MigrationCommandListBuilder builder)
    {
        var triggerName = CreateSequenceTriggerName(columnName, tableName, schemaName);

        if (options.HasFlag(MigrationsSqlGenerationOptions.Script))
        {
            builder.Append("SET TERM ");
            builder.Append(((IFbSqlGenerationHelper)_sqlGenerationHelper).AlternativeStatementTerminator);
            builder.AppendLine(_sqlGenerationHelper.StatementTerminator);
            builder.EndCommand();
        }

        builder.AppendLine("EXECUTE BLOCK");
        builder.AppendLine("AS");
        builder.AppendLine("BEGIN");
        builder.IncrementIndent();
        builder.Append("if (exists(select 1 from rdb$triggers where rdb$trigger_name = '");
        builder.Append(triggerName);
        builder.Append("')) then");
        builder.AppendLine();
        builder.AppendLine("begin");
        builder.IncrementIndent();
        builder.Append("execute statement 'drop trigger ");
        builder.Append(_sqlGenerationHelper.DelimitIdentifier(triggerName));
        builder.Append("'");
        builder.Append(_sqlGenerationHelper.StatementTerminator);
        builder.AppendLine();
        builder.DecrementIndent();
        builder.AppendLine("end");
        builder.DecrementIndent();
        builder.Append("END");
        builder.AppendLine();
        if (options.HasFlag(MigrationsSqlGenerationOptions.Script))
        {
            builder.AppendLine(((IFbSqlGenerationHelper)_sqlGenerationHelper).AlternativeStatementTerminator);
        }
        else
        {
            builder.AppendLine(_sqlGenerationHelper.StatementTerminator);
        }
        builder.EndCommand();

        if (options.HasFlag(MigrationsSqlGenerationOptions.Script))
        {
            builder.Append("SET TERM ");
            builder.Append(_sqlGenerationHelper.StatementTerminator);
            builder.AppendLine(((IFbSqlGenerationHelper)_sqlGenerationHelper).AlternativeStatementTerminator);
            builder.EndCommand();
        }
    }
Example #4
0
        protected virtual void GenerateCreateRange(
            [NotNull] PostgresRange rangeType,
            [NotNull] IModel model,
            [NotNull] MigrationCommandListBuilder builder)
        {
            var schema = rangeType.Schema ?? model.Relational().DefaultSchema;

            // Schemas are normally created (or rather ensured) by the model differ, which scans all tables, sequences
            // and other database objects. However, it isn't aware of ranges, so we always ensure schema on range creation.
            if (schema != null)
            {
                Generate(new EnsureSchemaOperation {
                    Name = schema
                }, model, builder);
            }

            builder
            .Append("CREATE TYPE ")
            .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(rangeType.Name, schema))
            .AppendLine($" AS RANGE (")
            .IncrementIndent();

            var def = new List <string> {
                $"SUBTYPE = {rangeType.Subtype}"
            };

            if (rangeType.CanonicalFunction != null)
            {
                def.Add($"CANONICAL = {rangeType.CanonicalFunction}");
            }
            if (rangeType.SubtypeOpClass != null)
            {
                def.Add($"SUBTYPE_OPCLASS = {rangeType.SubtypeOpClass}");
            }
            if (rangeType.CanonicalFunction != null)
            {
                def.Add($"COLLATION = {rangeType.Collation}");
            }
            if (rangeType.SubtypeDiff != null)
            {
                def.Add($"SUBTYPE_DIFF = {rangeType.SubtypeDiff}");
            }

            for (var i = 0; i < def.Count; i++)
            {
                builder
                .Append(def[i] + (i == def.Count - 1 ? null : ","))
                .AppendLine();
            }

            builder
            .DecrementIndent()
            .AppendLine(");");
        }
Example #5
0
        /// <inheritdoc />
        protected override void EndStatement(MigrationCommandListBuilder builder, bool suppressTransaction = false)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (_closeScopeBeforeEndingStatement)
            {
                _closeScopeBeforeEndingStatement = false;

                builder.DecrementIndent();
                builder.AppendLine("END");
            }

            base.EndStatement(builder, suppressTransaction);
        }
Example #6
0
 public override MigrationCommandListBuilder DecrementIndent()
 {
     _builder.DecrementIndent();
     return(this);
 }