public AddForeignKeyConstraintChangeSet Build(ChangeSetCommand command)
 {
     return(new AddForeignKeyConstraintChangeSet
     {
         Id = command.Id,
         Author = command.Author,
         PreConditions = new AddForeignKeyConstraintChangeSet.PreConditionsElement
         {
             OnFail = "MARK_RAN",
             Not = new AddForeignKeyConstraintChangeSet.NotElement
             {
                 ForeignKeyConstraintExists = new AddForeignKeyConstraintChangeSet.ForeignKeyConstraintExistsElement
                 {
                     ForeignKeyName = _foreignKey.Name
                 }
             }
         },
         AddForeignKeyConstraint = new AddForeignKeyConstraintChangeSet.AddForeignKeyConstraintElement
         {
             BaseTableName = _foreignKey.TableName,
             BaseColumnNames = _foreignKey.ColumnName,
             ConstraintName = _foreignKey.Name,
             ReferencedTableName = _foreignKey.ReferencedTableName,
             ReferencedColumnNames = _foreignKey.ReferencedColumnName,
             Deferrable = "false",
             InitiallyDeferred = "false",
             OnDelete = "NO ACTION",
             OnUpdate = "NO ACTION"
         }
     });
 }
        private void CreateTable(GenerateCommand command)
        {
            var tables     = _tableRepository.GetAll();
            var tableNames = command.AllTables ? tables.Select(t => t.Name).ToList() : TableHelper.ParseTableNames(command.TableNames);

            foreach (var tableName in tableNames)
            {
                var table   = tables.First(f => f.Name == tableName);
                var columns = _tableRepository.GetColumns(tableName);

                var builder          = new CreateTableChangeSetBuilder(table, columns);
                var changeSetCommand = new ChangeSetCommand(command.Author);
                var changeSet        = builder.Build(changeSetCommand);

                var xml  = changeSet.ToXml();
                var path = Path.Combine(command.DirectoryPath, $"{ChangeSetCommand.Sequence:000}.{tableName.ToLower()}.create.xml");
                FileHelper.Write(path, xml);

                ChangeSetCommand.Next();

                foreach (var column in columns.Where(w => w.AutoIncrement))
                {
                    CreateSequence(command, changeSetCommand, table, column);
                }

                var foreignKeys = _tableRepository.GetForeignKeys(tableName);

                foreach (var foreignKey in foreignKeys)
                {
                    AddForeignKeyConstraint(command, changeSetCommand, table, foreignKey);
                }
            }
        }
Example #3
0
 public CreateSequenceChangeSet Build(ChangeSetCommand command)
 {
     return(new CreateSequenceChangeSet
     {
         Id = command.Id,
         Author = command.Author,
         Dbms = "oracle",
         PreConditions = new CreateSequenceChangeSet.PreConditionsElement
         {
             OnFail = "MARK_RAN",
             SqlCheck = new CreateSequenceChangeSet.SqlCheckElement
             {
                 ExpectedResult = "0",
                 Text = $"SELECT COUNT(*) FROM USER_SEQUENCES U WHERE UPPER(U.SEQUENCE_NAME) = UPPER('SEQ_{_table.Name}')"
             }
         },
         CreateSequence = new CreateSequenceChangeSet.CreateSequenceElement
         {
             Cycle = "true",
             IncrementBy = "1",
             MaxValue = "9999999999999",
             MinValue = "1",
             Ordered = "true",
             SchemaName = "${dbSchemaName}",
             SequenceName = $"SEQ_{_table.Name}",
             StartValue = "1"
         }
     });
 }
        public AddColumnChangeSet Build(ChangeSetCommand command)
        {
            var columnBuilder = new ColumnElementBuilder(_table);

            return(new AddColumnChangeSet
            {
                Id = command.Id,
                Author = command.Author,
                PreConditions = new AddColumnChangeSet.PreConditionsElement
                {
                    OnFail = "MARK_RAN",
                    OnFailMessage = "Campo já existe",
                    Not = new AddColumnChangeSet.NotElement
                    {
                        ColumnExists = new ColumnExistsElement
                        {
                            ColumnName = _column.Name,
                            TableName = _table.Name
                        }
                    }
                },
                AddColumn = new AddColumnChangeSet.AddColumnElement
                {
                    SchemaName = "${dbSchemaName}",
                    TableName = _table.Name,
                    Column = columnBuilder.Build(_column)
                }
            });
        }
Example #5
0
        public CreateTableChangeSet Build(ChangeSetCommand command)
        {
            var columnBuilder = new ColumnElementBuilder(_table);

            var changeSet = new CreateTableChangeSet
            {
                Id            = command.Id,
                Author        = command.Author,
                PreConditions = new CreateTableChangeSet.PreConditionsElement
                {
                    OnFail = "MARK_RAN",
                    Not    = new CreateTableChangeSet.NotElement
                    {
                        TableExists = new CreateTableChangeSet.TableExistsElement
                        {
                            TableName = _table.Name
                        }
                    }
                },
                CreateTable = new CreateTableChangeSet.CreateTableElement
                {
                    TableName  = _table.Name,
                    Remarks    = _table.Name,
                    SchemaName = "${dbSchemaName}",
                    Column     = columnBuilder.Build(_columns)
                }
            };

            return(changeSet);
        }
        public void Generate(GenerateCommand command)
        {
            _validator.ValidateAndThrow(command);

            ChangeSetCommand.Restart();

            CreateTable(command);
        }
Example #7
0
        public void Diff(DiffCommand command)
        {
            _validator.ValidateAndThrow(command);

            ChangeSetCommand.Restart();

            DropColumns(command);
            AddColumns(command);
            AddForeignKeys(command);
        }
        private static void AddForeignKeyConstraint(GenerateCommand command, ChangeSetCommand changeSetCommand, Table table, ForeignKey foreignKey)
        {
            var builder   = new AddForeignKeyConstraintChangeSetBuilder(foreignKey);
            var changeSet = builder.Build(changeSetCommand);

            var xml  = changeSet.ToXml();
            var path = Path.Combine(command.DirectoryPath, $"{ChangeSetCommand.Sequence:000}.{table.Name.ToLower()}.{foreignKey.Name.ToLower()}.foreign-key.xml");

            FileHelper.Write(path, xml);

            ChangeSetCommand.Next();
        }
        private static void CreateSequence(GenerateCommand command, ChangeSetCommand changeSetCommand, Table table, Column column)
        {
            var builder   = new CreateSequenceChangeSetBuilder(table);
            var changeSet = builder.Build(changeSetCommand);

            var xml  = changeSet.ToXml();
            var path = Path.Combine(command.DirectoryPath, $"{ChangeSetCommand.Sequence:000}.{table.Name.ToLower()}.{column.Name.ToLower()}.sequence.xml");

            FileHelper.Write(path, xml);

            ChangeSetCommand.Next();
        }
Example #10
0
        private static void DropColumn(DiffCommand command, Table table, Column column)
        {
            var changeSetCommand = new ChangeSetCommand(command.Author);
            var builder          = new DropColumnChangeSetBuilder(table, column);
            var changeSet        = builder.Build(changeSetCommand);

            var xml  = changeSet.ToXml();
            var path = Path.Combine(command.DirectoryPath, $"{ChangeSetCommand.Sequence:000}.{table.Name.ToLower()}.{column.Name.ToLower()}.drop.xml");

            FileHelper.Write(path, xml);

            ChangeSetCommand.Next();
        }
Example #11
0
        public void Build()
        {
            var table = new Table
            {
                Name = "GA_TIMESHEET"
            };

            var columns = new List <Column>
            {
                new Column
                {
                    Name       = "ID",
                    PrimaryKey = true,
                    ForeignKey = false,
                    Nullable   = false,
                    Type       = ColumnType.Long,
                    Length     = null,
                    Precision  = 19,
                    Scale      = 0
                },
                new Column
                {
                    Name       = "TIMESHEET_ID",
                    PrimaryKey = false,
                    ForeignKey = true,
                    Nullable   = false,
                    Type       = ColumnType.Long,
                    Length     = null,
                    Precision  = 19,
                    Scale      = 0
                }
            };

            var builder = new CreateTableChangeSetBuilder(table, columns);
            var command = new ChangeSetCommand("altair.sossai");

            var changeSet = builder.Build(command);

            Console.WriteLine(changeSet.ToXml());
        }
 public DropColumnChangeSet Build(ChangeSetCommand command)
 {
     return(new DropColumnChangeSet
     {
         Id = command.Id,
         Author = command.Author,
         PreConditions = new DropColumnChangeSet.PreConditionsElement
         {
             OnFail = "MARK_RAN",
             ColumnExists = new ColumnExistsElement
             {
                 ColumnName = _column.Name,
                 TableName = _table.Name
             }
         },
         DropColumn = new DropColumnChangeSet.DropColumnElement
         {
             ColumnName = _column.Name,
             TableName = _table.Name
         }
     });
 }