/// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public override string GetBeginIfExistsScript(string migrationId)
        {
            Check.NotEmpty(migrationId, nameof(migrationId));

            return(new StringBuilder()
                   .Append("IF EXISTS(SELECT * FROM ")
                   .Append(SqlGenerationHelper.DelimitIdentifier(TableName, TableSchema))
                   .Append(" WHERE ")
                   .Append(SqlGenerationHelper.DelimitIdentifier(MigrationIdColumnName))
                   .Append(" = N'")
                   .Append(SqlGenerationHelper.EscapeLiteral(migrationId))
                   .AppendLine("')")
                   .Append("BEGIN")
                   .ToString());
        }
Exemplo n.º 2
0
        public override string GetBeginIfNotExistsScript(string migrationId)
        {
            ThrowIf.Argument.IsNull(migrationId, "migrationId");

            return(new StringBuilder()
                   .Append("IF NOT EXISTS(SELECT * FROM ")
                   .Append(SqlGenerationHelper.DelimitIdentifier(TableName, TableSchema))
                   .Append(" WHERE ")
                   .Append(SqlGenerationHelper.DelimitIdentifier(MigrationIdColumnName))
                   .Append(" = '")
                   .Append(SqlGenerationHelper.EscapeLiteral(migrationId))
                   .AppendLine("')")
                   .Append("BEGIN")
                   .ToString());
        }
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public override string GetCreateIfNotExistsScript()
        {
            var builder = new IndentedStringBuilder();

            builder
            .Append("IF NOT EXISTS (SELECT * FROM (SHOW TABLES) WHERE Name = '")
            .Append(SqlGenerationHelper.EscapeLiteral(TableName))
            .Append("') THEN ");
            using (builder.Indent())
            {
                builder.AppendLines(GetCreateScript());
            }
            builder.AppendLine(";");

            return(builder.ToString());
        }
        protected virtual void DropDefaultConstraint(
            [CanBeNull] string schema,
            [NotNull] string tableName,
            [NotNull] string columnName,
            [NotNull] MigrationCommandListBuilder builder)
        {
            Check.NotEmpty(tableName, nameof(tableName));
            Check.NotEmpty(columnName, nameof(columnName));
            Check.NotNull(builder, nameof(builder));

            var variable = "@var" + _variableCounter++;

            builder
            .Append("DECLARE ")
            .Append(variable)
            .AppendLine(" sysname;")
            .Append("SELECT ")
            .Append(variable)
            .AppendLine(" = [d].[name]")
            .AppendLine("FROM [sys].[default_constraints] [d]")
            .AppendLine("INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]")
            .Append("WHERE ([d].[parent_object_id] = OBJECT_ID(N'");

            if (schema != null)
            {
                builder
                .Append(SqlGenerationHelper.EscapeLiteral(schema))
                .Append(".");
            }

            builder
            .Append(SqlGenerationHelper.EscapeLiteral(tableName))
            .Append("') AND [c].[name] = N'")
            .Append(SqlGenerationHelper.EscapeLiteral(columnName))
            .AppendLine("');")
            .Append("IF ")
            .Append(variable)
            .Append(" IS NOT NULL EXEC(N'ALTER TABLE ")
            .Append(SqlGenerationHelper.DelimitIdentifier(tableName, schema))
            .Append(" DROP CONSTRAINT [' + ")
            .Append(variable)
            .Append(" + ']")
            .Append(SqlGenerationHelper.StatementTerminator)
            .Append("')")
            .AppendLine(SqlGenerationHelper.StatementTerminator);
        }
Exemplo n.º 5
0
        public virtual string GetInsertScript([NotNull] HistoryRow row)
        {
            Check.NotNull(row, nameof(row));

            return(new StringBuilder().Append("INSERT INTO ")
                   .Append(SqlGenerationHelper.DelimitIdentifier(TableName, TableSchema))
                   .Append(" (")
                   .Append(SqlGenerationHelper.DelimitIdentifier(MigrationIdColumnName))
                   .Append(", ")
                   .Append(SqlGenerationHelper.DelimitIdentifier(ProductVersionColumnName))
                   .AppendLine(")")
                   .Append("VALUES ('")
                   .Append(SqlGenerationHelper.EscapeLiteral(row.MigrationId))
                   .Append("', '")
                   .Append(SqlGenerationHelper.EscapeLiteral(row.ProductVersion))
                   .AppendLine("');")
                   .ToString());
        }
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public override string GetBeginIfExistsScript(string migrationId)
        {
            Check.NotEmpty(migrationId, nameof(migrationId));

            return(new StringBuilder()
                   .AppendLine("DECLARE")
                   .AppendLine("    v_Count INTEGER;")
                   .AppendLine("BEGIN")
                   .Append("SELECT COUNT(*) INTO v_Count FROM ")
                   .Append(SqlGenerationHelper.DelimitIdentifier(TableName, TableSchema))
                   .Append(" WHERE ")
                   .Append(SqlGenerationHelper.DelimitIdentifier(MigrationIdColumnName))
                   .Append(" = N'")
                   .Append(SqlGenerationHelper.EscapeLiteral(migrationId))
                   .AppendLine("'")
                   .AppendLine("IF v_Count = 1 THEN")
                   .ToString());
        }