コード例 #1
0
        public void ReverseSetsTableNameAndColumnNameOnGeneratedExpression()
        {
            var expression = new CreateColumnExpression {
                TableName = "Bacon", Column = { Name = "BaconId" }
            };
            var reverse = expression.Reverse() as DeleteColumnExpression;

            reverse.TableName.ShouldBe("Bacon");
            reverse.ColumnName.ShouldBe("BaconId");
        }
コード例 #2
0
        public void WhenDefaultSchemaConventionIsAppliedAndSchemaIsNotSetThenSchemaShouldBeNull()
        {
            var expression = new CreateColumnExpression {
                TableName = "Bacon", Column = { Name = "BaconId", Type = DbType.Int32 }
            };

            expression.ApplyConventions(new MigrationConventions());

            Assert.That(expression.SchemaName, Is.Null);
        }
コード例 #3
0
        public void WhenDefaultSchemaConventionIsChangedAndSchemaIsNotSetThenSetSchema()
        {
            var expression = new CreateColumnExpression {
                TableName = "Bacon", Column = { Name = "BaconId", Type = DbType.Int32 }
            };

            var processed = expression.Apply(ConventionSets.WithSchemaName);

            Assert.That(processed.SchemaName, Is.EqualTo("testdefault"));
        }
コード例 #4
0
        public void WhenDefaultSchemaConventionIsAppliedAndSchemaIsSetThenSchemaShouldNotBeChanged()
        {
            var expression = new CreateColumnExpression {
                SchemaName = "testschema", TableName = "Bacon", Column = { Name = "BaconId", Type = DbType.Int32 }
            };

            expression.ApplyConventions(new MigrationConventions());

            Assert.That(expression.SchemaName, Is.EqualTo("testschema"));
        }
コード例 #5
0
        public void WhenDefaultSchemaConventionIsAppliedAndSchemaIsNotSetThenSchemaShouldBeNull()
        {
            var expression = new CreateColumnExpression {
                TableName = "Bacon", Column = { Name = "BaconId", Type = DbType.Int32 }
            };

            var processed = expression.Apply(ConventionSets.NoSchemaName);

            Assert.That(processed.SchemaName, Is.Null);
        }
コード例 #6
0
        public ICreateColumnOnTableSyntax Column(string columnName)
        {
            var expression = new CreateColumnExpression(_context.CurrentDatabaseProvider, _databaseProviders, _sqlSyntax)
            {
                Column = { Name = columnName }
            };

            _context.Expressions.Add(expression);
            return(new CreateColumnBuilder(_context, _databaseProviders, expression));
        }
コード例 #7
0
        public void CanDumpCreateColumnExpression()
        {
            var expression = new CreateColumnExpression {
                TableName = _tableName, Column = { Name = _columnName, IsIdentity = true, IsPrimaryKey = true, Type = DbType.String }
            };
            string expectedSql = _generator.Generate(expression);

            _fileDumpProcessor.Process(expression);

            Lines.ShouldContain(expectedSql);
        }
コード例 #8
0
        public override string Generate(CreateColumnExpression expression)
        {
            var errors = ValidateAdditionalFeatureCompatibility(expression.Column.AdditionalFeatures);

            if (!string.IsNullOrEmpty(errors))
            {
                return(errors);
            }
            return(string.Format(AddColumn, Quoter.QuoteTableName(expression.TableName, expression.SchemaName),
                                 Column.Generate(expression.Column)));
        }
コード例 #9
0
        protected void SetupUndoDeleteColumn(DeleteColumnExpression expression)
        {
            FirebirdSchemaProvider schema = new FirebirdSchemaProvider(Processor);
            FirebirdTableSchema    table  = schema.GetTableSchema(expression.TableName);
            bool hasPrimary = table.HasPrimaryKey;

            CanUndo = true;

            using (IDataReader ds = Processor.ReadTableData(String.Empty, expression.TableName))
            {
                //Create columns
                foreach (string columnName in expression.ColumnNames)
                {
                    CreateColumnExpression createColumn = new CreateColumnExpression()
                    {
                        SchemaName = String.Empty,
                        TableName  = expression.TableName,
                        Column     = table.Definition.Columns.First(x => x.Name.ToUpper() == columnName.ToUpper())
                    };
                    UndoExpressions.Add(createColumn);

                    //NB: No need to recreate indices? Drop should fail if detects existing user created indices
                }

                //Update data
                while (ds.Read())
                {
                    if (!hasPrimary)
                    {
                        CanUndo = false;
                        return;
                    }
                    UpdateDataExpression update = new UpdateDataExpression()
                    {
                        TableName = expression.TableName,
                        IsAllRows = false,
                        Set       = new List <KeyValuePair <string, object> >(),
                        Where     = new List <KeyValuePair <string, object> >()
                    };
                    foreach (string columnName in expression.ColumnNames)
                    {
                        update.Set.Add(new KeyValuePair <string, object>(columnName, ds[columnName]));
                    }
                    table.Definition.Columns.ToList().ForEach(col =>
                    {
                        if (col.IsPrimaryKey)
                        {
                            update.Where.Add(new KeyValuePair <string, object>(col.Name, ds[col.Name]));
                        }
                    });
                    UndoExpressions.Add(update);
                }
            }
        }
コード例 #10
0
        public override string Generate(CreateColumnExpression expression)
        {
            var alterTableStatement  = base.Generate(expression);
            var descriptionStatement = DescriptionGenerator.GenerateDescriptionStatement(expression);

            if (string.IsNullOrEmpty(descriptionStatement))
            {
                return(alterTableStatement);
            }

            return(ComposeStatements(alterTableStatement, new[] { descriptionStatement }));
        }
コード例 #11
0
        public void CanCreateColumnWithBinaryIntMax()
        {
            var column = new ColumnDefinition {
                Name = GeneratorTestHelper.TestColumnName1, Type = DbType.Binary, Size = int.MaxValue
            };
            var expression = new CreateColumnExpression {
                TableName = GeneratorTestHelper.TestTableName1, Column = column
            };
            var result = Generator.Generate(expression);

            result.ShouldBe("ALTER TABLE `TestTable1` ADD COLUMN `TestColumn1` LONGBLOB NOT NULL");
        }
コード例 #12
0
        public override string Generate(CreateColumnExpression expression)
        {
            var alterTableStatement  = string.Format("ALTER TABLE {0}.{1}", Quoter.QuoteSchemaName(expression.SchemaName), base.Generate(expression));
            var descriptionStatement = DescriptionGenerator.GenerateDescriptionStatement(expression);

            if (string.IsNullOrEmpty(descriptionStatement))
            {
                return(alterTableStatement);
            }

            return(ComposeStatements(alterTableStatement, new[] { descriptionStatement }));
        }
コード例 #13
0
        /// <inheritdoc />
        public ICreateColumnOnTableSyntax Column(string columnName)
        {
            var expression = new CreateColumnExpression {
                Column =
                {
                    Name = columnName
                }
            };

            _context.Expressions.Add(expression);
            return(new CreateColumnExpressionBuilder(expression, _context));
        }
コード例 #14
0
        public void CanUseSystemMethodCurrentDateTimeAsADefaultValueForAColumn()
        {
            var columnDefinition = new ColumnDefinition {
                Name = "NewColumn", Size = 15, Type = null, CustomType = "TIMESTAMP", DefaultValue = SystemMethods.CurrentDateTime
            };
            var expression = new CreateColumnExpression {
                Column = columnDefinition, TableName = "NewTable"
            };

            var result = Generator.Generate(expression);

            result.ShouldBe("ALTER TABLE `NewTable` ADD COLUMN `NewColumn` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP");
        }
コード例 #15
0
        public void WhenDefaultSchemaConventionIsChangedAndSchemaIsNotSetThenSetSchema()
        {
            var expression = new CreateColumnExpression {
                TableName = "Bacon", Column = { Name = "BaconId", Type = DbType.Int32 }
            };
            var migrationConventions = new MigrationConventions {
                GetDefaultSchema = () => "testdefault"
            };

            expression.ApplyConventions(migrationConventions);

            Assert.That(expression.SchemaName, Is.EqualTo("testdefault"));
        }
コード例 #16
0
        public void CanCreateXmlColumn()
        {
            var expression = new CreateColumnExpression
            {
                TableName = "TestTable1",
                Column    = new ColumnDefinition {
                    Name = "TestColumn1", Type = DbType.Xml
                }
            };

            var result = Generator.Generate(expression);

            result.ShouldBe("ALTER TABLE [TestTable1] ADD [TestColumn1] NTEXT NOT NULL");
        }
コード例 #17
0
        public void CanCreateXmlColumn()
        {
            var expression = new CreateColumnExpression();

            expression.TableName = "TestTable1";

            expression.Column      = new ColumnDefinition();
            expression.Column.Name = "TestColumn1";
            expression.Column.Type = DbType.Xml;

            var sql = generator.Generate(expression);

            sql.ShouldBe("ALTER TABLE [TestTable1] ADD [TestColumn1] NTEXT NOT NULL");
        }
コード例 #18
0
        public void CanCreateXmlColumn()
        {
            var expression = new CreateColumnExpression
            {
                TableName = "Table1",
                Column    = new ColumnDefinition {
                    Name = "MyXmlColumn", Type = DbType.Xml
                }
            };

            var result = Generator.Generate(expression);

            result.ShouldNotBeNull();
        }
コード例 #19
0
        public void CanUseSystemMethodCurrentCDateTimeAsADefaultValueForAColumn()
        {
            const string tableName        = "NewTable";
            var          columnDefinition = new ColumnDefinition {
                Name = "NewColumn", Size = 5, Type = DbType.String, DefaultValue = SystemMethods.CurrentDateTime
            };
            var expression = new CreateColumnExpression {
                Column = columnDefinition, TableName = tableName
            };

            var result = Generator.Generate(expression);

            result.ShouldBe("ALTER TABLE \"NewTable\" ADD (\"NewColumn\" NVARCHAR(5) DEFAULT CURRENT_TIMESTAMP);");
        }
コード例 #20
0
        public void CanCreateXmlColumn()
        {
            var expression = new CreateColumnExpression();

            expression.TableName = "Table1";

            expression.Column      = new ColumnDefinition();
            expression.Column.Name = "MyXmlColumn";
            expression.Column.Type = DbType.Xml;

            var sql = _generator.Generate(expression);

            sql.ShouldNotBeNull();
        }
コード例 #21
0
        public override string Generate(CreateColumnExpression expression)
        {
            var createStatement = new StringBuilder();

            createStatement.AppendFormat("ALTER TABLE {0} ADD {1};", Quoter.QuoteTableName(expression.TableName, expression.SchemaName), Column.Generate(expression.Column));
            var descriptionStatement = DescriptionGenerator.GenerateDescriptionStatement(expression);

            if (!string.IsNullOrEmpty(descriptionStatement))
            {
                createStatement.Append(";");
                createStatement.Append(descriptionStatement);
            }
            return(createStatement.ToString());
        }
コード例 #22
0
        public static CreateColumnExpression GetCreateColumnExpressionWithNullableCustomType()
        {
            var expression = new CreateColumnExpression();

            expression.TableName = TestTableName1;

            expression.Column                  = new ColumnDefinition();
            expression.Column.Name             = TestColumnName1;
            expression.Column.IsNullable       = true;
            expression.Column.CustomType       = "MyDomainType";
            expression.Column.ModificationType = ColumnModificationType.Create;

            return(expression);
        }
コード例 #23
0
        public void CanCreateJsonColumnWithDefaultSchema()
        {
            var column = new ColumnDefinition {
                Name = "TestColumn1", DefaultValue = "{}", CustomType = "json"
            };
            var expression = new CreateColumnExpression {
                TableName = "TestTable1", Column = column
            };


            var result = Generator.Generate(expression);

            result.ShouldBe("ALTER TABLE \"public\".\"TestTable1\" ADD \"TestColumn1\" json NOT NULL DEFAULT '{}';");
        }
コード例 #24
0
        public void CanUseSystemMethodCurrentUTCDateTimeAsADefaultValueForAColumn()
        {
            const string tableName        = "NewTable";
            var          columnDefinition = new ColumnDefinition {
                Name = "NewColumn", Size = 5, Type = DbType.String, DefaultValue = SystemMethods.CurrentUTCDateTime
            };
            var expression = new CreateColumnExpression {
                Column = columnDefinition, TableName = tableName
            };

            var result = Generator.Generate(expression);

            result.ShouldBe("ALTER TABLE NewTable ADD COLUMN NewColumn VARGRAPHIC(5) CCSID 1200 NOT NULL DEFAULT (CURRENT_TIMESTAMP - CURRENT_TIMEZONE)");
        }
コード例 #25
0
        public static CreateColumnExpression GetCreateColumnExpressionWithDateTimeOffsetType()
        {
            var expression = new CreateColumnExpression();

            expression.TableName = TestTableName1;

            expression.Column                  = new ColumnDefinition();
            expression.Column.Name             = TestColumnName1;
            expression.Column.IsNullable       = true;
            expression.Column.Type             = DbType.DateTimeOffset;
            expression.Column.ModificationType = ColumnModificationType.Create;

            return(expression);
        }
コード例 #26
0
        public void CanUseSystemMethodCurrentUserAsADefaultValueForAColumn()
        {
            const string tableName        = "NewTable";
            var          columnDefinition = new ColumnDefinition {
                Name = "NewColumn", Size = 18, Type = DbType.AnsiString, DefaultValue = SystemMethods.CurrentUser
            };
            var expression = new CreateColumnExpression {
                Column = columnDefinition, TableName = tableName
            };

            var result = Generator.Generate(expression);

            result.ShouldBe("ALTER TABLE NewTable ADD COLUMN NewColumn VARCHAR(18) NOT NULL DEFAULT USER");
        }
コード例 #27
0
        public void CanUseSystemMethodCurrentDateTimeOffsetAsADefaultValueForAColumn()
        {
            const string tableName        = "NewTable";
            var          columnDefinition = new ColumnDefinition {
                Name = "NewColumn", Size = 5, Type = DbType.String, DefaultValue = SystemMethods.CurrentDateTimeOffset
            };
            var expression = new CreateColumnExpression {
                Column = columnDefinition, TableName = tableName
            };

            var result = Generator.Generate(expression);

            result.ShouldBe("ALTER TABLE \"public\".\"NewTable\" ADD \"NewColumn\" varchar(5) NOT NULL DEFAULT current_timestamp;");
        }
コード例 #28
0
        public void CanCreateXmlColumnWithDefaultSchema()
        {
            var expression = new CreateColumnExpression();

            expression.TableName = "TestTable1";

            expression.Column      = new ColumnDefinition();
            expression.Column.Name = "TestColumn1";
            expression.Column.Type = DbType.Xml;

            var result = Generator.Generate(expression);

            result.ShouldBe("ALTER TABLE [dbo].[TestTable1] ADD [TestColumn1] XML NOT NULL");
        }
コード例 #29
0
        public void GetPrimaryKeyNamePrefixesTableNameWithPKAndUnderscore()
        {
            var expr = new CreateColumnExpression()
            {
                Column =
                {
                    TableName    = "Foo",
                    IsPrimaryKey = true,
                }
            };

            var processed = expr.Apply(ConventionSets.NoSchemaName);

            processed.Column.PrimaryKeyName.ShouldBe("PK_Foo");
        }
コード例 #30
0
        public void CanCreateXmlColumnWithCustomSchema()
        {
            var expression = new CreateColumnExpression();

            expression.TableName = "TestTable1";

            expression.Column      = new ColumnDefinition();
            expression.Column.Name = "TestColumn1";
            expression.Column.Type = DbType.Xml;
            expression.SchemaName  = "TestSchema";

            var sql = generator.Generate(expression);

            sql.ShouldBe("ALTER TABLE [TestSchema].[TestTable1] ADD [TestColumn1] XML NOT NULL");
        }