public virtual void Generate( [NotNull] CreateTableOperation operation, [CanBeNull] IModel model, [NotNull] SqlBatchBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); builder .Append("CREATE TABLE ") .Append(_sql.DelimitIdentifier(operation.Name, operation.Schema)) .AppendLine(" ("); using (builder.Indent()) { for (var i = 0; i < operation.Columns.Count; i++) { var column = operation.Columns[i]; ColumnDefinition(column, model, builder); if (i != operation.Columns.Count - 1) { builder.AppendLine(","); } } if (operation.PrimaryKey != null) { builder.AppendLine(","); PrimaryKeyConstraint(operation.PrimaryKey, model, builder); } foreach (var uniqueConstraint in operation.UniqueConstraints) { builder.AppendLine(","); UniqueConstraint(uniqueConstraint, model, builder); } foreach (var foreignKey in operation.ForeignKeys) { builder.AppendLine(","); ForeignKeyConstraint(foreignKey, model, builder); } builder.AppendLine(); } builder.Append(")"); }
public void SqlBatchBuilder_correctly_groups_multiple_statements_into_one_batch() { var batchBuilder = new SqlBatchBuilder(); batchBuilder.AppendLine("Statement1"); batchBuilder.AppendLine("Statement2"); batchBuilder.AppendLine("Statement3"); batchBuilder.EndBatch(); Assert.Equal(1, batchBuilder.SqlBatches.Count); Assert.Equal( @"Statement1 Statement2 Statement3 ", batchBuilder.SqlBatches[0].Sql); }
protected override void EnsureSchema(string schema, SqlBatchBuilder batchBuilder) { Check.NotNull(batchBuilder, "batchBuilder"); if (string.IsNullOrEmpty(schema) || schema.Equals("dbo", StringComparison.OrdinalIgnoreCase) || GeneratedSchemas.Contains(schema)) { return; } batchBuilder .Append("IF schema_id(") .Append(GenerateLiteral(schema)) .AppendLine(") IS NULL"); using (batchBuilder.Indent()) { CreateSchema(schema, batchBuilder); } batchBuilder.AppendLine(); GeneratedSchemas.Add(schema); }
public override void Generate(RenameSequenceOperation operation, IModel model, SqlBatchBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); var separate = false; var name = operation.Name; if (operation.NewName != null) { var qualifiedName = new StringBuilder(); if (operation.Schema != null) { qualifiedName .Append(operation.Schema) .Append("."); } qualifiedName.Append(operation.Name); Rename(qualifiedName.ToString(), operation.NewName, builder); separate = true; name = operation.NewName; } if (operation.NewSchema != null) { if (separate) { builder.AppendLine(_sql.BatchCommandSeparator); } Transfer(operation.NewSchema, operation.Schema, name, builder); } }
public void SqlBatchBuilder_correctly_splits_statements_to_multiple_batches_when_transaction_is_suppressed() { var batchBuilder = new SqlBatchBuilder(); batchBuilder.AppendLine("Statement1"); batchBuilder.AppendLine("Statement2"); batchBuilder.AppendLine("Statement3", suppressTransaction: true); batchBuilder.AppendLine("Statement4"); batchBuilder.AppendLine("Statement5"); batchBuilder.AppendLine("Statement6", suppressTransaction: true); batchBuilder.AppendLine("Statement7", suppressTransaction: true); batchBuilder.EndBatch(); Assert.Equal(2, batchBuilder.SqlBatches.Count); Assert.False(batchBuilder.SqlBatches[0].SuppressTransaction); Assert.Equal( @"Statement1 Statement2 ", batchBuilder.SqlBatches[0].Sql); Assert.True(batchBuilder.SqlBatches[1].SuppressTransaction); Assert.Equal( @"Statement3 Statement4 Statement5 Statement6 Statement7 ", batchBuilder.SqlBatches[1].Sql); }
protected virtual void GenerateTableConstraints([NotNull] CreateTableOperation createTableOperation, [NotNull] SqlBatchBuilder batchBuilder) { Check.NotNull(createTableOperation, "createTableOperation"); Check.NotNull(batchBuilder, "batchBuilder"); var addPrimaryKeyOperation = createTableOperation.PrimaryKey; if (addPrimaryKeyOperation != null) { batchBuilder.AppendLine(","); GeneratePrimaryKey(addPrimaryKeyOperation, batchBuilder); } foreach (var addUniqueConstraintOperation in createTableOperation.UniqueConstraints) { batchBuilder.AppendLine(","); GenerateUniqueConstraint(addUniqueConstraintOperation, batchBuilder); } }
public void SqlBatchBuilder_correctly_produces_multiple_batches() { var batchBuilder = new SqlBatchBuilder(); batchBuilder.AppendLine("Statement1"); batchBuilder.EndBatch(); batchBuilder.AppendLine("Statement2"); batchBuilder.AppendLine("Statement3"); batchBuilder.EndBatch(); batchBuilder.AppendLine("Statement4"); batchBuilder.AppendLine("Statement5"); batchBuilder.AppendLine("Statement6"); batchBuilder.EndBatch(); Assert.Equal(3, batchBuilder.SqlBatches.Count); Assert.Equal( @"Statement1 ", batchBuilder.SqlBatches[0].Sql); Assert.Equal( @"Statement2 Statement3 ", batchBuilder.SqlBatches[1].Sql); Assert.Equal( @"Statement4 Statement5 Statement6 ", batchBuilder.SqlBatches[2].Sql); }
public void SqlBatchBuilder_ignores_empty_batches() { var batchBuilder = new SqlBatchBuilder(); batchBuilder.AppendLine("Statement1"); batchBuilder.EndBatch(); batchBuilder.EndBatch(); batchBuilder.EndBatch(); batchBuilder.AppendLine("Statement2"); batchBuilder.AppendLine("Statement3"); batchBuilder.EndBatch(); batchBuilder.EndBatch(); Assert.Equal(2, batchBuilder.SqlBatches.Count); Assert.Equal( @"Statement1 ", batchBuilder.SqlBatches[0].Sql); Assert.Equal( @"Statement2 Statement3 ", batchBuilder.SqlBatches[1].Sql); }
public virtual IReadOnlyList <SqlBatch> Generate( IReadOnlyList <MigrationOperation> operations, IModel model = null) { Check.NotNull(operations, nameof(operations)); var builder = new SqlBatchBuilder(); foreach (var operation in operations) { Generate(operation, model, builder); builder.AppendLine(Sql.BatchCommandSeparator); } builder.EndBatch(); return(builder.SqlBatches); }
protected override void Generate( [NotNull] AlterColumnOperation operation, [CanBeNull] IModel model, [NotNull] SqlBatchBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); DropDefaultConstraint(operation.Schema, operation.Table, operation.Name, builder); builder .Append("ALTER TABLE ") .Append(Sql.DelimitIdentifier(operation.Table, operation.Schema)) .Append(" ALTER COLUMN "); ColumnDefinition( operation.Schema, operation.Table, operation.Name, operation.ClrType, operation.ColumnType, operation.IsNullable, /*defaultValue:*/ null, /*defaultValueSql:*/ null, operation.ComputedColumnSql, operation, model, builder); if (operation.DefaultValue != null || operation.DefaultValueSql != null) { builder .AppendLine(";") .Append("ALTER TABLE ") .Append(Sql.DelimitIdentifier(operation.Table, operation.Schema)) .Append(" ADD"); DefaultValue(operation.DefaultValue, operation.DefaultValueSql, builder); builder .Append(" FOR ") .Append(Sql.DelimitIdentifier(operation.Name)); } }
public virtual IEnumerable <SqlBatch> Generate([NotNull] IEnumerable <MigrationOperation> migrationOperations) { Check.NotNull(migrationOperations, "migrationOperations"); var batchBuilder = new SqlBatchBuilder(); var migrationOperationsList = migrationOperations.ToList(); for (var i = 0; i < migrationOperationsList.Count; i++) { if (i > 0) { batchBuilder.AppendLine(StatementSeparator); } migrationOperationsList[i].GenerateSql(this, batchBuilder); } batchBuilder.EndBatch(); return(batchBuilder.SqlBatches); }
protected virtual void GenerateColumns( [NotNull] CreateTableOperation createTableOperation, [NotNull] SqlBatchBuilder batchBuilder) { Check.NotNull(createTableOperation, "createTableOperation"); Check.NotNull(batchBuilder, "batchBuilder"); var columns = createTableOperation.Columns; if (columns.Count == 0) { return; } GenerateColumn(createTableOperation.TableName, columns[0], batchBuilder); for (var i = 1; i < columns.Count; i++) { batchBuilder.AppendLine(","); GenerateColumn(createTableOperation.TableName, columns[i], batchBuilder); } }
public virtual void Generate([NotNull] CreateTableOperation createTableOperation, [NotNull] SqlBatchBuilder batchBuilder) { Check.NotNull(createTableOperation, "createTableOperation"); Check.NotNull(batchBuilder, "batchBuilder"); EnsureSchema(createTableOperation.TableName.Schema, batchBuilder); batchBuilder .Append("CREATE TABLE ") .Append(DelimitIdentifier(createTableOperation.TableName)) .AppendLine(" ("); using (batchBuilder.Indent()) { GenerateColumns(createTableOperation, batchBuilder); GenerateTableConstraints(createTableOperation, batchBuilder); } batchBuilder .AppendLine() .Append(")"); }