示例#1
0
        /// <summary>
        /// Builds commands for the given <see cref="InsertDataOperation" /> by making calls on the given
        /// <see cref="MigrationCommandListBuilder" />, and then terminates the final command.
        /// </summary>
        /// <param name="operation"> The operation. </param>
        /// <param name="model"> The target model which may be <c>null</c> if the operations exist without a model. </param>
        /// <param name="builder"> The command builder to use to build the commands. </param>
        protected override void Generate(
            InsertDataOperation operation,
            IModel model,
            MigrationCommandListBuilder builder)
        {
            Check.NotNull(operation, nameof(operation));
            Check.NotNull(builder, nameof(builder));

            var sqlBuilder = new StringBuilder();

            foreach (var modificationCommand in operation.GenerateModificationCommands(model))
            {
                var overridingSystemValue = modificationCommand.ColumnModifications.Any(m =>
                                                                                        m.Property?.Npgsql().ValueGenerationStrategy == NpgsqlValueGenerationStrategy.IdentityAlwaysColumn);
                ((NpgsqlUpdateSqlGenerator)Dependencies.UpdateSqlGenerator).AppendInsertOperation(
                    sqlBuilder,
                    modificationCommand,
                    0,
                    overridingSystemValue);
            }

            builder.Append(sqlBuilder.ToString());

            builder.EndCommand();
        }
示例#2
0
        protected override void Generate(
            InsertDataOperation operation,
            IModel model,
            MigrationCommandListBuilder builder)
        {
            Check.NotNull(operation, nameof(operation));
            Check.NotNull(builder, nameof(builder));

            var sqlBuilder = new StringBuilder();

            foreach (var modificationCommand in operation.GenerateModificationCommands(model))
            {
                SqlGenerator.AppendInsertOperation(
                    sqlBuilder,
                    modificationCommand,
                    0);
            }

            builder
            .AppendLine("BEGIN")
            .Append(sqlBuilder)
            .Append("END")
            .AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator);

            builder.EndCommand();
        }
        protected override void Generate(
            InsertDataOperation operation,
            IModel model,
            MigrationCommandListBuilder builder)
        {
            Check.NotNull(operation, nameof(operation));
            Check.NotNull(builder, nameof(builder));

            builder.Append("IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [object_id] = OBJECT_ID(N'");

            if (operation.Schema != null)
            {
                builder
                .Append(Dependencies.SqlGenerationHelper.EscapeLiteral(operation.Schema))
                .Append(".");
            }

            builder
            .Append(Dependencies.SqlGenerationHelper.EscapeLiteral(operation.Table))
            .AppendLine("'))");

            using (builder.Indent())
            {
                builder
                .Append("SET IDENTITY_INSERT ")
                .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table, operation.Schema))
                .Append(" ON")
                .AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator);
            }

            base.Generate(operation, model, builder, terminate: false);

            builder
            .AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator)
            .Append("IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [object_id] = OBJECT_ID(N'");

            if (operation.Schema != null)
            {
                builder
                .Append(Dependencies.SqlGenerationHelper.EscapeLiteral(operation.Schema))
                .Append(".");
            }

            builder
            .Append(Dependencies.SqlGenerationHelper.EscapeLiteral(operation.Table))
            .AppendLine("'))");

            using (builder.Indent())
            {
                builder
                .Append("SET IDENTITY_INSERT ")
                .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table, operation.Schema))
                .Append(" OFF")
                .AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator);
            }

            builder.EndCommand();
        }
        protected virtual void Generate(
            [NotNull] InsertDataOperation operation,
            [CanBeNull] IModel model,
            [NotNull] MigrationCommandListBuilder builder,
            bool terminate)
        {
            Check.NotNull(operation, nameof(operation));
            Check.NotNull(builder, nameof(builder));

            if (operation.Values.Length == 0)
            {
                return;
            }

            builder
            .Append("INSERT INTO ")
            .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table, operation.Schema))
            .Append(" (")
            .Append(ColumnList(operation.Columns))
            .AppendLine(")")
            .Append("VALUES ");

            var rowCount   = operation.Values.GetLength(0);
            var valueCount = operation.Values.GetLength(1);

            for (var i = 0; i < rowCount; i++)
            {
                if (i != 0)
                {
                    builder
                    .AppendLine(",")
                    .Append("       ");
                }

                builder.Append("(");
                for (var j = 0; j < valueCount; j++)
                {
                    if (j != 0)
                    {
                        builder.Append(", ");
                    }

                    var value       = operation.Values[i, j];
                    var typeMapping = Dependencies.TypeMapper.GetMappingForValue(value);
                    builder.Append(typeMapping.GenerateSqlLiteral(value));
                }

                builder.Append(")");
            }

            if (terminate)
            {
                builder.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator);
                EndStatement(builder);
            }
        }
示例#5
0
        /// <summary>
        /// Builds commands for the given <see cref="InsertDataOperation" /> by making calls on the given
        /// <see cref="MigrationCommandListBuilder" />, and then terminates the final command.
        /// </summary>
        /// <param name="operation"> The operation. </param>
        /// <param name="model"> The target model which may be <c>null</c> if the operations exist without a model. </param>
        /// <param name="builder"> The command builder to use to build the commands. </param>
        /// <param name="terminate"> Indicates whether or not to terminate the command after generating SQL for the operation. </param>
        protected override void Generate(
            InsertDataOperation operation,
            IModel model,
            MigrationCommandListBuilder builder,
            bool terminate = true)
        {
            Check.NotNull(operation, nameof(operation));
            Check.NotNull(builder, nameof(builder));

            var sqlBuilder = new StringBuilder();


            builder.Append(sqlBuilder.ToString());

            if (terminate)
            {
                builder.EndCommand();
            }
        }
        protected override void Generate(InsertDataOperation operation, IModel?model, MigrationCommandListBuilder builder, bool terminate = true)
        {
            var sqlBuilder = new StringBuilder();

            foreach (var modificationCommand in GenerateModificationCommands(operation, model))
            {
                var overridingSystemValue = modificationCommand.ColumnModifications.Any(m => m.Property?.GetValueGenerationStrategy() == NpgsqlValueGenerationStrategy.IdentityAlwaysColumn);
                var subSqlBuilder         = new StringBuilder();
                ((NpgsqlUpdateSqlGenerator)Dependencies.UpdateSqlGenerator).AppendInsertOperation(subSqlBuilder, modificationCommand, 0, overridingSystemValue);
                subSqlBuilder.Replace(";", " ON CONFLICT DO NOTHING;");
                sqlBuilder.Append(subSqlBuilder);
            }

            builder.Append(sqlBuilder.ToString());

            if (terminate)
            {
                builder.EndCommand();
            }
        }
        public virtual OperationBuilder <InsertDataOperation> InsertData(
            [NotNull] string table,
            [NotNull] string[] columns,
            [NotNull] object[,] values,
            [CanBeNull] string schema = null)
        {
            Check.NotEmpty(table, nameof(table));
            Check.NotNull(columns, nameof(columns));
            Check.NotNull(values, nameof(values));

            var operation = new InsertDataOperation
            {
                Table   = table,
                Schema  = schema,
                Columns = columns,
                Values  = values
            };

            Operations.Add(operation);

            return(new OperationBuilder <InsertDataOperation>(operation));
        }
 protected virtual void Generate(
     [NotNull] InsertDataOperation operation,
     [CanBeNull] IModel model,
     [NotNull] MigrationCommandListBuilder builder)
 => Generate(operation, model, builder, terminate: true);
示例#9
0
 protected override void Generate([NotNull] InsertDataOperation operation, [CanBeNull] IModel model, [NotNull] MigrationCommandListBuilder builder, bool terminate)
 {
     operation.Schema = _taosConnectionStringBuilder.DataBase;
     base.Generate(operation, model, builder, terminate);
 }