Package org.apache.ddlutils.alteration

Examples of org.apache.ddlutils.alteration.TableChange


      //       This will be easier once named primary keys are supported
        boolean pkColumnAdded = false;

        for (Iterator changeIt = changes.iterator(); changeIt.hasNext();)
        {
            TableChange change = (TableChange)changeIt.next();

            if (change instanceof AddColumnChange)
            {
              AddColumnChange addColumnChange = (AddColumnChange)change;

              // TODO: we cannot add columns to the primary key this way
              //       because we would have to drop the pk first and then
              //       add a new one afterwards which is not supported yet
              if (addColumnChange.getNewColumn().isPrimaryKey())
                {
                    pkColumnAdded = true;  
                }
                else
              {
                processChange(currentModel, desiredModel, addColumnChange);
                  changeIt.remove();
              }
            }
            else if (change instanceof RemoveColumnChange)
            {
              RemoveColumnChange removeColumnChange = (RemoveColumnChange)change;

              // TODO: we cannot drop primary key columns this way
              //       because we would have to drop the pk first and then
              //       add a new one afterwards which is not supported yet
              if (!removeColumnChange.getColumn().isPrimaryKey())
              {
                processChange(currentModel, desiredModel, removeColumnChange);
                  changeIt.remove();
              }
            }
        }
        for (Iterator changeIt = changes.iterator(); changeIt.hasNext();)
        {
            TableChange change = (TableChange)changeIt.next();

            // we can only add a primary key if all columns are present in the table
            // i.e. none was added during this alteration
            if ((change instanceof AddPrimaryKeyChange) && !pkColumnAdded)
            {
View Full Code Here


                                                List     changes) throws IOException
    {
        // First we drop primary keys as necessary
        for (Iterator changeIt = changes.iterator(); changeIt.hasNext();)
        {
            TableChange change = (TableChange)changeIt.next();

            if (change instanceof RemovePrimaryKeyChange)
            {
                processChange(currentModel, desiredModel, (RemovePrimaryKeyChange)change);
                changeIt.remove();
            }
            else if (change instanceof PrimaryKeyChange)
            {
                PrimaryKeyChange       pkChange       = (PrimaryKeyChange)change;
                RemovePrimaryKeyChange removePkChange = new RemovePrimaryKeyChange(pkChange.getChangedTable(),
                                                                                   pkChange.getOldPrimaryKeyColumns());

                processChange(currentModel, desiredModel, removePkChange);
            }
        }

        ArrayList columnChanges = new ArrayList();

        // Next we add/remove columns
        for (Iterator changeIt = changes.iterator(); changeIt.hasNext();)
        {
            TableChange change = (TableChange)changeIt.next();

            if (change instanceof AddColumnChange)
            {
                AddColumnChange addColumnChange = (AddColumnChange)change;

                // Sql Server can only add not insert columns
                if (addColumnChange.isAtEnd())
                {
                    processChange(currentModel, desiredModel, addColumnChange);
                    changeIt.remove();
                }
            }
            else if (change instanceof RemoveColumnChange)
            {
                processChange(currentModel, desiredModel, (RemoveColumnChange)change);
                changeIt.remove();
            }
            else if (change instanceof ColumnAutoIncrementChange)
            {
                // Sql Server has no way of adding or removing an IDENTITY constraint
                // Thus we have to rebuild the table anyway and can ignore all the other
                // column changes
                columnChanges = null;
            }
            else if ((change instanceof ColumnChange) && (columnChanges != null))
            {
                // we gather all changed columns because we can use the ALTER TABLE ALTER COLUMN
                // statement for them
                columnChanges.add(change);
            }
        }
        if (columnChanges != null)
        {
            HashSet processedColumns = new HashSet();

            for (Iterator changeIt = columnChanges.iterator(); changeIt.hasNext();)
            {
                ColumnChange change       = (ColumnChange)changeIt.next();
                Column       sourceColumn = change.getChangedColumn();
                Column       targetColumn = targetTable.findColumn(sourceColumn.getName(), getPlatform().isDelimitedIdentifierModeOn());

                if (!processedColumns.contains(targetColumn))
                {
                    processColumnChange(sourceTable,
                                        targetTable,
                                        sourceColumn,
                                        targetColumn,
                                        (change instanceof ColumnDataTypeChange) || (change instanceof ColumnSizeChange));
                    processedColumns.add(targetColumn);
                }
                changes.remove(change);
                change.apply(currentModel, getPlatform().isDelimitedIdentifierModeOn());
            }
        }
        // Finally we add primary keys
        for (Iterator changeIt = changes.iterator(); changeIt.hasNext();)
        {
            TableChange change = (TableChange)changeIt.next();

            if (change instanceof AddPrimaryKeyChange)
            {
                processChange(currentModel, desiredModel, (AddPrimaryKeyChange)change);
                changeIt.remove();
View Full Code Here

                                                List     changes) throws IOException
    {
        // HsqlDb can only drop columns that are not part of a primary key
        for (Iterator changeIt = changes.iterator(); changeIt.hasNext();)
        {
            TableChange change = (TableChange)changeIt.next();

            if ((change instanceof RemoveColumnChange) &&
                ((RemoveColumnChange)change).getColumn().isPrimaryKey())
            {
                return;
            }
        }

        // in order to utilize the ALTER TABLE ADD COLUMN BEFORE statement
        // we have to apply the add column changes in the correct order
        // thus we first gather all add column changes and then execute them
        // Since we get them in target table column order, we can simply
        // iterate backwards
        ArrayList addColumnChanges = new ArrayList();

        for (Iterator changeIt = changes.iterator(); changeIt.hasNext();)
        {
            TableChange change = (TableChange)changeIt.next();

            if (change instanceof AddColumnChange)
            {
                addColumnChanges.add(change);
                changeIt.remove();
            }
        }
        for (ListIterator changeIt = addColumnChanges.listIterator(addColumnChanges.size()); changeIt.hasPrevious();)
        {
            AddColumnChange addColumnChange = (AddColumnChange)changeIt.previous();

            processChange(currentModel, desiredModel, addColumnChange);
            changeIt.remove();
        }

        for (Iterator changeIt = changes.iterator(); changeIt.hasNext();)
        {
            TableChange change = (TableChange)changeIt.next();

            if (change instanceof RemoveColumnChange)
            {
                RemoveColumnChange removeColumnChange = (RemoveColumnChange)change;
View Full Code Here

                                                Map      parameters,
                                                List     changes) throws IOException
    {
        for (Iterator changeIt = changes.iterator(); changeIt.hasNext();)
        {
            TableChange change = (TableChange)changeIt.next();

            if (change instanceof AddColumnChange)
            {
                AddColumnChange addColumnChange = (AddColumnChange)change;

                // SapDB can only add not insert columns
                if (!addColumnChange.isAtEnd())
                {
                    return;
                }
            }
        }

        // First we drop primary keys as necessary
        for (Iterator changeIt = changes.iterator(); changeIt.hasNext();)
        {
            TableChange change = (TableChange)changeIt.next();

            if (change instanceof RemovePrimaryKeyChange)
            {
                processChange(currentModel, desiredModel, (RemovePrimaryKeyChange)change);
                changeIt.remove();
            }
            else if (change instanceof PrimaryKeyChange)
            {
                PrimaryKeyChange       pkChange       = (PrimaryKeyChange)change;
                RemovePrimaryKeyChange removePkChange = new RemovePrimaryKeyChange(pkChange.getChangedTable(),
                                                                                   pkChange.getOldPrimaryKeyColumns());

                processChange(currentModel, desiredModel, removePkChange);
            }
        }
        // Next we add/change/remove columns
        // SapDB has a ALTER TABLE MODIFY COLUMN but it is limited regarding the type conversions
        // it can perform, so we don't use it here but rather rebuild the table
        for (Iterator changeIt = changes.iterator(); changeIt.hasNext();)
        {
            TableChange change = (TableChange)changeIt.next();

            if (change instanceof AddColumnChange)
            {
                processChange(currentModel, desiredModel, (AddColumnChange)change);
                changeIt.remove();
            }
            else if (change instanceof ColumnDefaultValueChange)
            {
                processChange(currentModel, desiredModel, (ColumnDefaultValueChange)change);
                changeIt.remove();
            }
            else if (change instanceof ColumnRequiredChange)
            {
                processChange(currentModel, desiredModel, (ColumnRequiredChange)change);
                changeIt.remove();
            }
            else if (change instanceof RemoveColumnChange)
            {
                processChange(currentModel, desiredModel, (RemoveColumnChange)change);
                changeIt.remove();
            }
        }
        // Finally we add primary keys
        for (Iterator changeIt = changes.iterator(); changeIt.hasNext();)
        {
            TableChange change = (TableChange)changeIt.next();

            if (change instanceof AddPrimaryKeyChange)
            {
                processChange(currentModel, desiredModel, (AddPrimaryKeyChange)change);
                changeIt.remove();
View Full Code Here

    {
        // McKoi has this nice ALTER CREATE TABLE statement which saves us a lot of work
        // We only have to handle auto-increment changes manually
        for (Iterator it = changes.iterator(); it.hasNext();)
        {
            TableChange change = (TableChange)it.next();

            if (change instanceof ColumnAutoIncrementChange)
            {
                Column column = ((ColumnAutoIncrementChange)change).getColumn();

                // we have to defer removal of the sequences until they are no longer used
                if (!column.isAutoIncrement())
                {
                    ColumnAutoIncrementChange autoIncrChange = (ColumnAutoIncrementChange)change;

                    createAutoIncrementSequence(autoIncrChange.getChangedTable(),
                                                autoIncrChange.getColumn());
                }
            }
            else if (change instanceof AddColumnChange)
            {
                AddColumnChange addColumnChange = (AddColumnChange)change;

                if (addColumnChange.getNewColumn().isAutoIncrement())
                {
                    createAutoIncrementSequence(addColumnChange.getChangedTable(),
                                                addColumnChange.getNewColumn());
                }
            }
        }

        print("ALTER ");
        super.createTable(desiredModel, targetTable, parameters);

        for (Iterator it = changes.iterator(); it.hasNext();)
        {
            TableChange change = (TableChange)it.next();
   
            if (change instanceof ColumnAutoIncrementChange)
            {
                Column column = ((ColumnAutoIncrementChange)change).getColumn();
   
View Full Code Here

                                                List     changes) throws IOException
    {
        // First we drop primary keys as necessary
        for (Iterator changeIt = changes.iterator(); changeIt.hasNext();)
        {
            TableChange change = (TableChange)changeIt.next();

            if (change instanceof RemovePrimaryKeyChange)
            {
                processChange(currentModel, desiredModel, (RemovePrimaryKeyChange)change);
                changeIt.remove();
            }
            else if (change instanceof PrimaryKeyChange)
            {
                PrimaryKeyChange       pkChange       = (PrimaryKeyChange)change;
                RemovePrimaryKeyChange removePkChange = new RemovePrimaryKeyChange(pkChange.getChangedTable(),
                                                                                   pkChange.getOldPrimaryKeyColumns());

                processChange(currentModel, desiredModel, removePkChange);
            }
        }


        HashMap columnChanges = new HashMap();

        // Next we add/remove columns
        for (Iterator changeIt = changes.iterator(); changeIt.hasNext();)
        {
            TableChange change = (TableChange)changeIt.next();

            if (change instanceof AddColumnChange)
            {
                AddColumnChange addColumnChange = (AddColumnChange)change;

                // Sybase can only add not insert columns
                if (addColumnChange.isAtEnd())
                {
                    processChange(currentModel, desiredModel, addColumnChange);
                    changeIt.remove();
                }
            }
            else if (change instanceof RemoveColumnChange)
            {
                processChange(currentModel, desiredModel, (RemoveColumnChange)change);
                changeIt.remove();
            }
            else if (change instanceof ColumnAutoIncrementChange)
            {
                // Sybase has no way of adding or removing an IDENTITY constraint
                // Thus we have to rebuild the table anyway and can ignore all the other
                // column changes
                columnChanges = null;
            }
            else if ((change instanceof ColumnChange) && (columnChanges != null))
            {
                // we gather all changed columns because we can use the ALTER TABLE ALTER COLUMN
                // statement for them
                ColumnChange columnChange     = (ColumnChange)change;
                ArrayList    changesPerColumn = (ArrayList)columnChanges.get(columnChange.getChangedColumn());

                if (changesPerColumn == null)
                {
                    changesPerColumn = new ArrayList();
                    columnChanges.put(columnChange.getChangedColumn(), changesPerColumn);
                }
                changesPerColumn.add(change);
            }
        }
        if (columnChanges != null)
        {
            for (Iterator changesPerColumnIt = columnChanges.entrySet().iterator(); changesPerColumnIt.hasNext();)
            {
                Map.Entry entry            = (Map.Entry)changesPerColumnIt.next();
                Column    sourceColumn     = (Column)entry.getKey();
                ArrayList changesPerColumn = (ArrayList)entry.getValue();

                // Sybase does not like us to use the ALTER TABLE ALTER statement if we don't actually
                // change the datatype or the required constraint but only the default value
                // Thus, if we only have to change the default, we use a different handler
                if ((changesPerColumn.size() == 1) && (changesPerColumn.get(0) instanceof ColumnDefaultValueChange))
                {
                    processChange(currentModel,
                                  desiredModel,
                                  (ColumnDefaultValueChange)changesPerColumn.get(0));
                }
                else
                {
                    Column targetColumn = targetTable.findColumn(sourceColumn.getName(),
                                                                 getPlatform().isDelimitedIdentifierModeOn());

                    processColumnChange(sourceTable, targetTable, sourceColumn, targetColumn);
                }
                for (Iterator changeIt = changesPerColumn.iterator(); changeIt.hasNext();)
                {
                    ((ColumnChange)changeIt.next()).apply(currentModel, getPlatform().isDelimitedIdentifierModeOn());
                }
            }
        }
        // Finally we add primary keys
        for (Iterator changeIt = changes.iterator(); changeIt.hasNext();)
        {
            TableChange change = (TableChange)changeIt.next();

            if (change instanceof AddPrimaryKeyChange)
            {
                processChange(currentModel, desiredModel, (AddPrimaryKeyChange)change);
                changeIt.remove();
View Full Code Here

    {
        // Derby provides a way to alter the size of a column but it is limited
        // (no pk or fk columns, only for VARCHAR columns), so we don't use it
        for (Iterator changeIt = changes.iterator(); changeIt.hasNext();)
        {
            TableChange change = (TableChange)changeIt.next();

            if (change instanceof AddColumnChange)
            {
                AddColumnChange addColumnChange = (AddColumnChange)change;
View Full Code Here

        // thus we first gather all add column changes and then execute them
        ArrayList addColumnChanges = new ArrayList();

        for (Iterator changeIt = changes.iterator(); changeIt.hasNext();)
        {
            TableChange change = (TableChange)changeIt.next();

            if (change instanceof AddColumnChange)
            {
                addColumnChanges.add((AddColumnChange)change);
                changeIt.remove();
            }
        }
        for (Iterator changeIt = addColumnChanges.iterator(); changeIt.hasNext();)
        {
            AddColumnChange addColumnChange = (AddColumnChange)changeIt.next();

            processChange(currentModel, desiredModel, addColumnChange);
            changeIt.remove();
        }

        ListOrderedSet changedColumns = new ListOrderedSet();
       
        // we don't have to care about the order because the comparator will have ensured
        // that a add primary key change comes after all necessary columns are present
        for (Iterator changeIt = changes.iterator(); changeIt.hasNext();)
        {
            TableChange change = (TableChange)changeIt.next();

            if (change instanceof RemoveColumnChange)
            {
                processChange(currentModel, desiredModel, (RemoveColumnChange)change);
                changeIt.remove();
View Full Code Here

        // we first sort the changes for the tables
        // however since the changes might contain source or target tables
        // we use the names rather than the table objects
        for (Iterator changeIt = changes.iterator(); changeIt.hasNext();)
        {
            TableChange change = (TableChange)changeIt.next();
            String      name   = change.getChangedTable().getName();

            if (!caseSensitive)
            {
                name = name.toUpperCase();
            }
View Full Code Here

    {
        // While Oracle has an ALTER TABLE MODIFY statement, it is somewhat limited
        // esp. if there is data in the table, so we don't use it
        for (Iterator changeIt = changes.iterator(); changeIt.hasNext();)
        {
            TableChange change = (TableChange)changeIt.next();

            if (change instanceof AddColumnChange)
            {
                AddColumnChange addColumnChange = (AddColumnChange)change;

                // Oracle can only add not insert columns
                // Also, we cannot add NOT NULL columns unless they have a default value
                if (!addColumnChange.isAtEnd() ||
                    (addColumnChange.getNewColumn().isRequired() && (addColumnChange.getNewColumn().getDefaultValue() == null)))
                {
                    // we need to rebuild the full table
                    return;
                }
            }
        }

        // First we drop primary keys as necessary
        for (Iterator changeIt = changes.iterator(); changeIt.hasNext();)
        {
            TableChange change = (TableChange)changeIt.next();

            if (change instanceof RemovePrimaryKeyChange)
            {
                processChange(currentModel, desiredModel, (RemovePrimaryKeyChange)change);
                changeIt.remove();
            }
            else if (change instanceof PrimaryKeyChange)
            {
                PrimaryKeyChange       pkChange       = (PrimaryKeyChange)change;
                RemovePrimaryKeyChange removePkChange = new RemovePrimaryKeyChange(pkChange.getChangedTable(),
                                                                                   pkChange.getOldPrimaryKeyColumns());

                processChange(currentModel, desiredModel, removePkChange);
            }
        }

        // Next we add/remove columns
        // While Oracle has an ALTER TABLE MODIFY statement, it is somewhat limited
        // esp. if there is data in the table, so we don't use it
        for (Iterator changeIt = changes.iterator(); changeIt.hasNext();)
        {
            TableChange change = (TableChange)changeIt.next();

            if (change instanceof AddColumnChange)
            {
                processChange(currentModel, desiredModel, (AddColumnChange)change);
                changeIt.remove();
            }
            else if (change instanceof RemoveColumnChange)
            {
                processChange(currentModel, desiredModel, (RemoveColumnChange)change);
                changeIt.remove();
            }
        }
        // Finally we add primary keys
        for (Iterator changeIt = changes.iterator(); changeIt.hasNext();)
        {
            TableChange change = (TableChange)changeIt.next();

            if (change instanceof AddPrimaryKeyChange)
            {
                processChange(currentModel, desiredModel, (AddPrimaryKeyChange)change);
                changeIt.remove();
View Full Code Here

TOP

Related Classes of org.apache.ddlutils.alteration.TableChange

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.