Java Code Examples for org.apache.ddlutils.model.Table#findColumn()
The following examples show how to use
org.apache.ddlutils.model.Table#findColumn() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MySqlPlatform.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
/** * Processes the change of the primary key of a table. * * @param currentModel The current database schema * @param params The parameters used in the creation of new tables. Note that for existing * tables, the parameters won't be applied * @param change The change object */ public void processChange(Database currentModel, CreationParameters params, PrimaryKeyChange change) throws IOException { Table changedTable = findChangedTable(currentModel, change); String[] newPKColumnNames = change.getNewPrimaryKeyColumns(); Column[] newPKColumns = new Column[newPKColumnNames.length]; for (int colIdx = 0; colIdx < newPKColumnNames.length; colIdx++) { newPKColumns[colIdx] = changedTable.findColumn(newPKColumnNames[colIdx], isDelimitedIdentifierModeOn()); } ((MySqlBuilder)getSqlBuilder()).dropPrimaryKey(changedTable); getSqlBuilder().createPrimaryKey(changedTable, newPKColumns); change.apply(currentModel, isDelimitedIdentifierModeOn()); }
Example 2
Source File: InterbasePlatform.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
/** * Processes the addition of a column to a table. * * @param currentModel The current database schema * @param params The parameters used in the creation of new tables. Note that for existing * tables, the parameters won't be applied * @param change The change object */ public void processChange(Database currentModel, CreationParameters params, AddColumnChange change) throws IOException { Table changedTable = findChangedTable(currentModel, change); Column prevColumn = null; if (change.getPreviousColumn() != null) { prevColumn = changedTable.findColumn(change.getPreviousColumn(), isDelimitedIdentifierModeOn()); } ((InterbaseBuilder)getSqlBuilder()).insertColumn(currentModel, changedTable, change.getNewColumn(), prevColumn); change.apply(currentModel, isDelimitedIdentifierModeOn()); }
Example 3
Source File: Db2Platform.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
/** * Processes the change of the primary key of a table. * * @param currentModel The current database schema * @param params The parameters used in the creation of new tables. Note that for existing * tables, the parameters won't be applied * @param change The change object */ public void processChange(Database currentModel, CreationParameters params, PrimaryKeyChange change) throws IOException { Table changedTable = findChangedTable(currentModel, change); String[] newPKColumnNames = change.getNewPrimaryKeyColumns(); Column[] newPKColumns = new Column[newPKColumnNames.length]; for (int colIdx = 0; colIdx < newPKColumnNames.length; colIdx++) { newPKColumns[colIdx] = changedTable.findColumn(newPKColumnNames[colIdx], isDelimitedIdentifierModeOn()); } ((Db2Builder)getSqlBuilder()).dropPrimaryKey(changedTable); getSqlBuilder().createPrimaryKey(changedTable, newPKColumns); change.apply(currentModel, isDelimitedIdentifierModeOn()); }
Example 4
Source File: SapDbPlatform.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
/** * Processes the change of the column of a table. * * @param currentModel The current database schema * @param params The parameters used in the creation of new tables. Note that for existing * tables, the parameters won't be applied * @param change The change object */ public void processChange(Database currentModel, CreationParameters params, ColumnDefinitionChange change) throws IOException { Table changedTable = findChangedTable(currentModel, change); Column changedColumn = changedTable.findColumn(change.getChangedColumn(), isDelimitedIdentifierModeOn()); if (!StringUtilsExt.equals(changedColumn.getDefaultValue(), change.getNewColumn().getDefaultValue())) { ((SapDbBuilder)getSqlBuilder()).changeColumnDefaultValue(changedTable, changedColumn, change.getNewColumn().getDefaultValue()); } if (changedColumn.isRequired() != change.getNewColumn().isRequired()) { ((SapDbBuilder)getSqlBuilder()).changeColumnRequiredStatus(changedTable, changedColumn, change.getNewColumn().isRequired()); } change.apply(currentModel, isDelimitedIdentifierModeOn()); }
Example 5
Source File: SapDbPlatform.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
/** * Processes the change of the primary key of a table. * * @param currentModel The current database schema * @param params The parameters used in the creation of new tables. Note that for existing * tables, the parameters won't be applied * @param change The change object */ public void processChange(Database currentModel, CreationParameters params, PrimaryKeyChange change) throws IOException { Table changedTable = findChangedTable(currentModel, change); String[] newPKColumnNames = change.getNewPrimaryKeyColumns(); Column[] newPKColumns = new Column[newPKColumnNames.length]; for (int colIdx = 0; colIdx < newPKColumnNames.length; colIdx++) { newPKColumns[colIdx] = changedTable.findColumn(newPKColumnNames[colIdx], isDelimitedIdentifierModeOn()); } ((SapDbBuilder)getSqlBuilder()).dropPrimaryKey(changedTable); getSqlBuilder().createPrimaryKey(changedTable, newPKColumns); change.apply(currentModel, isDelimitedIdentifierModeOn()); }
Example 6
Source File: MSSqlPlatform.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
/** * Processes the change of the primary key of a table. * * @param currentModel The current database schema * @param params The parameters used in the creation of new tables. Note that for existing * tables, the parameters won't be applied * @param change The change object */ public void processChange(Database currentModel, CreationParameters params, PrimaryKeyChange change) throws IOException { Table changedTable = findChangedTable(currentModel, change); String[] newPKColumnNames = change.getNewPrimaryKeyColumns(); Column[] newPKColumns = new Column[newPKColumnNames.length]; for (int colIdx = 0; colIdx < newPKColumnNames.length; colIdx++) { newPKColumns[colIdx] = changedTable.findColumn(newPKColumnNames[colIdx], isDelimitedIdentifierModeOn()); } ((MSSqlBuilder)getSqlBuilder()).dropPrimaryKey(changedTable); getSqlBuilder().createPrimaryKey(changedTable, newPKColumns); change.apply(currentModel, isDelimitedIdentifierModeOn()); }
Example 7
Source File: AddColumnChange.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} */ public void apply(Database model, boolean caseSensitive) { Table table = findChangedTable(model, caseSensitive); Column newColumn = new CloneHelper().clone(_newColumn, true); if (_previousColumnName != null) { Column prevColumn = table.findColumn(_previousColumnName, caseSensitive); int idx = table.getColumnIndex(prevColumn) + 1; table.addColumn(idx, newColumn); } else if (_nextColumnName != null) { table.addColumn(0, newColumn); } else { table.addColumn(newColumn); } }
Example 8
Source File: MySqlPlatform.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ protected TableDefinitionChangesPredicate getTableDefinitionChangesPredicate() { return new DefaultTableDefinitionChangesPredicate() { protected boolean isSupported(Table intermediateTable, TableChange change) { if (change instanceof AddColumnChange) { AddColumnChange addColumnChange = (AddColumnChange)change; return !addColumnChange.getNewColumn().isAutoIncrement() && (!addColumnChange.getNewColumn().isRequired() || (addColumnChange.getNewColumn().getDefaultValue() != null)); } else if (change instanceof ColumnDefinitionChange) { ColumnDefinitionChange colDefChange = (ColumnDefinitionChange)change; Column sourceColumn = intermediateTable.findColumn(colDefChange.getChangedColumn(), isDelimitedIdentifierModeOn()); return !ColumnDefinitionChange.isTypeChanged(getPlatformInfo(), sourceColumn, colDefChange.getNewColumn()) && !ColumnDefinitionChange.isSizeChanged(getPlatformInfo(), sourceColumn, colDefChange.getNewColumn()); } else { return (change instanceof RemoveColumnChange) || (change instanceof AddPrimaryKeyChange) || (change instanceof PrimaryKeyChange) || (change instanceof RemovePrimaryKeyChange); } } }; }
Example 9
Source File: Db2Platform.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Processes a change representing the addition of a column. * * @param currentModel The current database schema * @param params The parameters used in the creation of new tables. Note that for existing * tables, the parameters won't be applied * @param change The change object */ public void processChange(Database currentModel, CreationParameters params, RemoveColumnChange change) throws IOException { Table changedTable = findChangedTable(currentModel, change); Column removedColumn = changedTable.findColumn(change.getChangedColumn(), isDelimitedIdentifierModeOn()); ((Db2Builder)getSqlBuilder()).dropColumn(changedTable, removedColumn); change.apply(currentModel, isDelimitedIdentifierModeOn()); }
Example 10
Source File: PlatformImplBase.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Helper method esp. for the {@link ModelBasedResultSetIterator} class that retrieves * the value for a column from the given result set. If a table was specified, * and it contains the column, then the jdbc type defined for the column is used for extracting * the value, otherwise the object directly retrieved from the result set is returned.<br/> * The method is defined here rather than in the {@link ModelBasedResultSetIterator} class * so that concrete platforms can modify its behavior. * * @param resultSet The result set * @param columnName The name of the column * @param table The table * @return The value */ protected Object getObjectFromResultSet(ResultSet resultSet, String columnName, Table table) throws SQLException { Column column = (table == null ? null : table.findColumn(columnName, isDelimitedIdentifierModeOn())); Object value = null; if (column != null) { int originalJdbcType = column.getTypeCode(); int targetJdbcType = getPlatformInfo().getTargetJdbcType(originalJdbcType); int jdbcType = originalJdbcType; // in general we're trying to retrieve the value using the original type // but sometimes we also need the target type: if ((originalJdbcType == Types.BLOB) && (targetJdbcType != Types.BLOB)) { // we should not use the Blob interface if the database doesn't map to this type jdbcType = targetJdbcType; } if ((originalJdbcType == Types.CLOB) && (targetJdbcType != Types.CLOB)) { // we should not use the Clob interface if the database doesn't map to this type jdbcType = targetJdbcType; } value = extractColumnValue(resultSet, columnName, 0, jdbcType); } else { value = resultSet.getObject(columnName); } return resultSet.wasNull() ? null : value; }
Example 11
Source File: FirebirdPlatform.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Processes the removal of a column from a table. * * @param currentModel The current database schema * @param params The parameters used in the creation of new tables. Note that for existing * tables, the parameters won't be applied * @param change The change object */ public void processChange(Database currentModel, CreationParameters params, RemoveColumnChange change) throws IOException { Table changedTable = findChangedTable(currentModel, change); Column droppedColumn = changedTable.findColumn(change.getChangedColumn(), isDelimitedIdentifierModeOn()); ((FirebirdBuilder)getSqlBuilder()).dropColumn(changedTable, droppedColumn); change.apply(currentModel, isDelimitedIdentifierModeOn()); }
Example 12
Source File: MSSqlBuilder.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ protected void copyData(Table sourceTable, Table targetTable) throws IOException { // Sql Server per default does not allow us to insert values explicitly into // identity columns. However, we can change this behavior // We need to this only if // - there is a column in both tables that is auto increment only in the target table, or // - there is a column in both tables that is auto increment in both tables Column[] targetIdentityColumns = targetTable.getAutoIncrementColumns(); // Sql Server allows only one identity column, so let's take a shortcut here boolean needToAllowIdentityInsert = (targetIdentityColumns.length > 0) && (sourceTable.findColumn(targetIdentityColumns[0].getName(), getPlatform().isDelimitedIdentifierModeOn()) != null); if (needToAllowIdentityInsert) { print("SET IDENTITY_INSERT "); printIdentifier(getTableName(targetTable)); print(" ON"); printEndOfStatement(); } super.copyData(sourceTable, targetTable); // We have to turn it off ASAP because it can be on only for one table per session if (needToAllowIdentityInsert) { print("SET IDENTITY_INSERT "); printIdentifier(getTableName(targetTable)); print(" OFF"); printEndOfStatement(); } }
Example 13
Source File: Oracle8Platform.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ protected TableDefinitionChangesPredicate getTableDefinitionChangesPredicate() { // 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 return new DefaultTableDefinitionChangesPredicate() { protected boolean isSupported(Table intermediateTable, TableChange change) { if ((change instanceof AddPrimaryKeyChange) || (change instanceof RemovePrimaryKeyChange)) { return true; } else if (change instanceof RemoveColumnChange) { // TODO: for now we trigger recreating the table, but ideally we should simply add the necessary pk changes RemoveColumnChange removeColumnChange = (RemoveColumnChange)change; Column column = intermediateTable.findColumn(removeColumnChange.getChangedColumn(), isDelimitedIdentifierModeOn()); return !column.isPrimaryKey(); } else 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 return addColumnChange.isAtEnd() && (!addColumnChange.getNewColumn().isRequired() || (addColumnChange.getNewColumn().getDefaultValue() != null)); } else { return false; } } }; }
Example 14
Source File: SybaseBuilder.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ protected void copyData(Table sourceTable, Table targetTable) throws IOException { // We need to turn on identity override except when the identity column was added to the column Column[] targetAutoIncrCols = targetTable.getAutoIncrementColumns(); boolean needIdentityOverride = false; if (targetAutoIncrCols.length > 0) { needIdentityOverride = true; // Sybase only allows for one identity column per table if (sourceTable.findColumn(targetAutoIncrCols[0].getName(), getPlatform().isDelimitedIdentifierModeOn()) == null) { needIdentityOverride = false; } } if (needIdentityOverride) { print(getEnableIdentityOverrideSql(targetTable)); printEndOfStatement(); } super.copyData(sourceTable, targetTable); if (needIdentityOverride) { print(getDisableIdentityOverrideSql(targetTable)); printEndOfStatement(); } }
Example 15
Source File: MySqlPlatform.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Processes the addition of a column to a table. * * @param currentModel The current database schema * @param params The parameters used in the creation of new tables. Note that for existing * tables, the parameters won't be applied * @param change The change object */ public void processChange(Database currentModel, CreationParameters params, AddColumnChange change) throws IOException { Table changedTable = findChangedTable(currentModel, change); Column prevColumn = null; if (change.getPreviousColumn() != null) { prevColumn = changedTable.findColumn(change.getPreviousColumn(), isDelimitedIdentifierModeOn()); } ((MySqlBuilder)getSqlBuilder()).insertColumn(changedTable, change.getNewColumn(), prevColumn); change.apply(currentModel, isDelimitedIdentifierModeOn()); }
Example 16
Source File: ModelComparator.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Creates change objects for columns that have a different in the given source and target table, and applies them * to the given intermediate model. * * @param sourceModel The source model * @param sourceTable The source table * @param intermediateModel The intermediate model to apply the changes to * @param intermediateTable The table from the intermediate model corresponding to the source table * @param targetModel The target model * @param targetTable The target table * @return The changes */ protected List checkForChangedColumns(Database sourceModel, Table sourceTable, Database intermediateModel, Table intermediateTable, Database targetModel, Table targetTable) { List changes = new ArrayList(); for (int columnIdx = 0; columnIdx < targetTable.getColumnCount(); columnIdx++) { Column targetColumn = targetTable.getColumn(columnIdx); Column sourceColumn = intermediateTable.findColumn(targetColumn.getName(), _caseSensitive); if (sourceColumn != null) { ColumnDefinitionChange change = compareColumns(intermediateTable, sourceColumn, targetTable, targetColumn); if (change != null) { changes.add(change); change.apply(intermediateModel, _caseSensitive); } } } return changes; }
Example 17
Source File: JdbcModelReader.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
/** * Tries to find the schema to which the given table belongs. * * @param connection The database connection * @param schemaPattern The schema pattern to limit the schemas to search in * @param table The table to search for * @return The schema name or <code>null</code> if the schema of the table * could not be found * @deprecated Will be removed once full schema support is in place */ public String determineSchemaOf(Connection connection, String schemaPattern, Table table) throws SQLException { ResultSet tableData = null; ResultSet columnData = null; try { DatabaseMetaDataWrapper metaData = new DatabaseMetaDataWrapper(); metaData.setMetaData(connection.getMetaData()); metaData.setCatalog(getDefaultCatalogPattern()); metaData.setSchemaPattern(schemaPattern == null ? getDefaultSchemaPattern() : schemaPattern); metaData.setTableTypes(getDefaultTableTypes()); String tablePattern = table.getName(); if (getPlatform().isDelimitedIdentifierModeOn()) { tablePattern = tablePattern.toUpperCase(); } tableData = metaData.getTables(metaData.escapeForSearch(tablePattern)); boolean found = false; String schema = null; while (!found && tableData.next()) { Map values = readColumns(tableData, getColumnsForTable()); String tableName = (String)values.get("TABLE_NAME"); if ((tableName != null) && (tableName.length() > 0)) { schema = (String)values.get("TABLE_SCHEM"); columnData = metaData.getColumns(metaData.escapeForSearch(tableName), getDefaultColumnPattern()); found = true; while (found && columnData.next()) { values = readColumns(columnData, getColumnsForColumn()); if (table.findColumn((String)values.get("COLUMN_NAME"), getPlatform().isDelimitedIdentifierModeOn()) == null) { found = false; } } columnData.close(); columnData = null; } } return found ? schema : null; } finally { closeResultSet(columnData); closeResultSet(tableData); } }
Example 18
Source File: MSSqlPlatform.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
/** * {@inheritDoc} */ protected TableDefinitionChangesPredicate getTableDefinitionChangesPredicate() { return new DefaultTableDefinitionChangesPredicate() { protected boolean isSupported(Table intermediateTable, TableChange change) { if ((change instanceof RemoveColumnChange) || (change instanceof AddPrimaryKeyChange) || (change instanceof PrimaryKeyChange) || (change instanceof RemovePrimaryKeyChange)) { return true; } else if (change instanceof AddColumnChange) { AddColumnChange addColumnChange = (AddColumnChange)change; // Sql Server can only add not insert columns, and the cannot be requird unless also // auto increment or with a DEFAULT value return (addColumnChange.getNextColumn() == null) && (!addColumnChange.getNewColumn().isRequired() || addColumnChange.getNewColumn().isAutoIncrement() || !StringUtils.isEmpty(addColumnChange.getNewColumn().getDefaultValue())); } else if (change instanceof ColumnDefinitionChange) { ColumnDefinitionChange colDefChange = (ColumnDefinitionChange)change; Column curColumn = intermediateTable.findColumn(colDefChange.getChangedColumn(), isDelimitedIdentifierModeOn()); Column newColumn = colDefChange.getNewColumn(); // Sql Server has no way of adding or removing an IDENTITY constraint // Also, Sql Server cannot handle reducing the size (even with the CAST in place) return (curColumn.isAutoIncrement() == colDefChange.getNewColumn().isAutoIncrement()) && (curColumn.isRequired() || (curColumn.isRequired() == newColumn.isRequired())) && !ColumnDefinitionChange.isSizeReduced(getPlatformInfo(), curColumn, newColumn); } else { return false; } } }; }
Example 19
Source File: InterbaseModelReader.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
/** * {@inheritDoc} */ public String determineSchemaOf(Connection connection, String schemaPattern, Table table) throws SQLException { ResultSet tableData = null; ResultSet columnData = null; try { DatabaseMetaDataWrapper metaData = new DatabaseMetaDataWrapper(); metaData.setMetaData(connection.getMetaData()); metaData.setCatalog(getDefaultCatalogPattern()); metaData.setSchemaPattern(schemaPattern == null ? getDefaultSchemaPattern() : schemaPattern); metaData.setTableTypes(getDefaultTableTypes()); String tablePattern = table.getName(); if (getPlatform().isDelimitedIdentifierModeOn()) { tablePattern = tablePattern.toUpperCase(); } tableData = metaData.getTables(metaData.escapeForSearch(tablePattern)); boolean found = false; String schema = null; while (!found && tableData.next()) { Map values = readColumns(tableData, getColumnsForTable()); String tableName = (String)values.get("TABLE_NAME"); if ((tableName != null) && (tableName.length() > 0)) { schema = (String)values.get("TABLE_SCHEM"); found = true; if (getPlatform().isDelimitedIdentifierModeOn()) { // Jaybird has a problem when delimited identifiers are used as // it is not able to find the columns for the table // So we have to filter manually below columnData = metaData.getColumns(getDefaultTablePattern(), getDefaultColumnPattern()); } else { columnData = metaData.getColumns(metaData.escapeForSearch(tableName), getDefaultColumnPattern()); } while (found && columnData.next()) { values = readColumns(columnData, getColumnsForColumn()); if (getPlatform().isDelimitedIdentifierModeOn() && !tableName.equals(values.get("TABLE_NAME"))) { continue; } if (table.findColumn((String)values.get("COLUMN_NAME"), getPlatform().isDelimitedIdentifierModeOn()) == null) { found = false; } } columnData.close(); columnData = null; } } return found ? schema : null; } finally { closeResultSet(columnData); closeResultSet(tableData); } }
Example 20
Source File: SapDbPlatform.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
/** * {@inheritDoc} */ protected TableDefinitionChangesPredicate getTableDefinitionChangesPredicate() { return new DefaultTableDefinitionChangesPredicate() { protected boolean isSupported(Table intermediateTable, TableChange change) { if ((change instanceof RemoveColumnChange) || (change instanceof AddPrimaryKeyChange) || (change instanceof PrimaryKeyChange) || (change instanceof RemovePrimaryKeyChange)) { return true; } else if (change instanceof AddColumnChange) { AddColumnChange addColumnChange = (AddColumnChange)change; // SapDB can only add not insert columns, and required columns have to have // a default value or be IDENTITY return (addColumnChange.getNextColumn() == null) && (!addColumnChange.getNewColumn().isRequired() || !StringUtilsExt.isEmpty(addColumnChange.getNewColumn().getDefaultValue())); } else if (change instanceof ColumnDefinitionChange) { ColumnDefinitionChange colChange = (ColumnDefinitionChange)change; // 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 Column curColumn = intermediateTable.findColumn(colChange.getChangedColumn(), isDelimitedIdentifierModeOn()); Column newColumn = colChange.getNewColumn(); // we can however handle the change if only the default value or the required status was changed return ((curColumn.getTypeCode() == newColumn.getTypeCode()) && !ColumnDefinitionChange.isSizeChanged(getPlatformInfo(), curColumn, newColumn) && (curColumn.isAutoIncrement() == newColumn.isAutoIncrement())); } else { return false; } } }; }