Пример #1
0
        private void CreateTableColumnsWithComments(
            CreateTableOperation operation,
            IModel model,
            MigrationCommandListBuilder builder)
        {
            for (var i = 0; i < operation.Columns.Count; i++)
            {
                var column = operation.Columns[i];

                if (i > 0)
                {
                    builder.AppendLine();
                }

                if (!string.IsNullOrEmpty(column.Comment))
                {
                    builder.AppendLines(Dependencies.SqlGenerationHelper.GenerateComment(column.Comment));
                }

                ColumnDefinition(column, model, builder);

                if (i != operation.Columns.Count - 1)
                {
                    builder.AppendLine(",");
                }
            }
        }
Пример #2
0
        /// <summary>
        ///     Builds commands for the given <see cref="CreateTableOperation" /> by making calls on the given
        ///     <see cref="MigrationCommandListBuilder" />.
        /// </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(
            CreateTableOperation operation,
            IModel model,
            MigrationCommandListBuilder builder,
            bool terminate = true)
        {
            Check.NotNull(operation, nameof(operation));
            Check.NotNull(builder, nameof(builder));

            // Lifts a primary key definition into the typename.
            // This handles the quirks of creating integer primary keys using autoincrement, not default rowid behavior.
            if (operation.PrimaryKey?.Columns.Length == 1)
            {
                var columnOp = operation.Columns.FirstOrDefault(o => o.Name == operation.PrimaryKey.Columns[0]);
                if (columnOp != null)
                {
                    columnOp.AddAnnotation(SqliteAnnotationNames.InlinePrimaryKey, true);
                    if (!string.IsNullOrEmpty(operation.PrimaryKey.Name))
                    {
                        columnOp.AddAnnotation(SqliteAnnotationNames.InlinePrimaryKeyName, operation.PrimaryKey.Name);
                    }

                    operation.PrimaryKey = null;
                }
            }

            if (string.IsNullOrEmpty(operation.Comment))
            {
                base.Generate(operation, model, builder, terminate);
            }
            else
            {
                builder
                .Append("CREATE TABLE ")
                .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Name, operation.Schema))
                .AppendLine(" (");

                using (builder.Indent())
                {
                    builder
                    .AppendLines(Dependencies.SqlGenerationHelper.GenerateComment(operation.Comment))
                    .AppendLine();
                    CreateTableColumns(operation, model, builder);
                    CreateTableConstraints(operation, model, builder);
                    builder.AppendLine();
                }

                builder.Append(")");

                if (terminate)
                {
                    builder.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator);
                    EndStatement(builder);
                }
            }
        }