Java Code Examples for org.apache.ddlutils.model.Table#getColumnCount()
The following examples show how to use
org.apache.ddlutils.model.Table#getColumnCount() .
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: ColumnOrderChange.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} */ public void apply(Database database, boolean caseSensitive) { Table table = findChangedTable(database, caseSensitive); ArrayList newColumns = new ArrayList(); for (int idx = 0; idx < table.getColumnCount(); idx++) { newColumns.add(table.getColumn(idx)); } for (int idx = 0; idx < table.getColumnCount(); idx++) { Column column = table.getColumn(idx); int newPos = getNewPosition(column.getName(), caseSensitive); if (newPos >= 0) { newColumns.set(newPos, column); } } table.removeAllColumns(); table.addColumns(newColumns); }
Example 2
Source File: MSSqlModelComparator.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
/** * Returns all columns that are changed in a way that makes it necessary to recreate foreign keys and * indexes using them. * * @param sourceTable The source table * @param targetTable The target table * @return The columns (from the target table) */ private List getRelevantChangedColumns(Table sourceTable, Table targetTable) { List result = new ArrayList(); for (int columnIdx = 0; columnIdx < targetTable.getColumnCount(); columnIdx++) { Column targetColumn = targetTable.getColumn(columnIdx); Column sourceColumn = sourceTable.findColumn(targetColumn.getName(), isCaseSensitive()); if (sourceColumn != null) { int targetTypeCode = getPlatformInfo().getTargetJdbcType(targetColumn.getTypeCode()); if ((targetTypeCode != sourceColumn.getTypeCode()) || ColumnDefinitionChange.isSizeChanged(getPlatformInfo(), sourceColumn, targetColumn)) { result.add(targetColumn); } } } return result; }
Example 3
Source File: MSSqlModelComparator.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
/** * Returns all columns that are changed in a way that makes it necessary to recreate foreign keys and * indexes using them. * * @param sourceTable The source table * @param targetTable The target table * @return The columns (from the target table) */ private List getRelevantChangedColumns(Table sourceTable, Table targetTable) { List result = new ArrayList(); for (int columnIdx = 0; columnIdx < targetTable.getColumnCount(); columnIdx++) { Column targetColumn = targetTable.getColumn(columnIdx); Column sourceColumn = sourceTable.findColumn(targetColumn.getName(), isCaseSensitive()); if (sourceColumn != null) { int targetTypeCode = getPlatformInfo().getTargetJdbcType(targetColumn.getTypeCode()); if ((targetTypeCode != sourceColumn.getTypeCode()) || ColumnDefinitionChange.isSizeChanged(getPlatformInfo(), sourceColumn, targetColumn)) { result.add(targetColumn); } } } return result; }
Example 4
Source File: PlatformImplBase.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Allows the platform to postprocess the model just read from the database. * * @param model The model */ protected void postprocessModelFromDatabase(Database model) { // Default values for CHAR/VARCHAR/LONGVARCHAR columns have quotation marks // around them which we'll remove now for (int tableIdx = 0; tableIdx < model.getTableCount(); tableIdx++) { Table table = model.getTable(tableIdx); for (int columnIdx = 0; columnIdx < table.getColumnCount(); columnIdx++) { Column column = table.getColumn(columnIdx); if (TypeMap.isTextType(column.getTypeCode()) || TypeMap.isDateTimeType(column.getTypeCode())) { String defaultValue = column.getDefaultValue(); if ((defaultValue != null) && (defaultValue.length() >= 2) && defaultValue.startsWith("'") && defaultValue.endsWith("'")) { defaultValue = defaultValue.substring(1, defaultValue.length() - 1); column.setDefaultValue(defaultValue); } } } } }
Example 5
Source File: ModelComparator.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Creates change objects for columns that are not present in the given source table but are in the 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 checkForAddedColumns(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) { String prevColumn = (columnIdx > 0 ? intermediateTable.getColumn(columnIdx - 1).getName() : null); String nextColumn = (columnIdx < intermediateTable.getColumnCount() ? intermediateTable.getColumn(columnIdx).getName() : null); Column clonedColumn = _cloneHelper.clone(targetColumn, false); AddColumnChange change = new AddColumnChange(intermediateTable.getQualifiedName(), clonedColumn, prevColumn, nextColumn); changes.add(change); change.apply(intermediateModel, _caseSensitive); } } return changes; }
Example 6
Source File: DatabaseIO.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Writes the table object to the given XML writer. * * @param table The table object * @param xmlWriter The XML writer */ private void writeTableElement(Table table, PrettyPrintingXmlWriter xmlWriter) throws DdlUtilsXMLException { xmlWriter.indentIfPrettyPrinting(1); writeElementStart(xmlWriter, QNAME_ELEMENT_TABLE); // GemStone changes BEGIN // write fully qualified table name final String tableName = table.getQualifiedName(); writeAttribute(xmlWriter, QNAME_ATTRIBUTE_NAME, tableName); /* (original code) writeAttribute(xmlWriter, QNAME_ATTRIBUTE_NAME, table.getName()); */ // GemStone changes END writeAttribute(xmlWriter, QNAME_ATTRIBUTE_DESCRIPTION, table.getDescription()); if ((table.getColumnCount() > 0) || (table.getForeignKeyCount() > 0) || (table.getIndexCount() > 0)) { xmlWriter.printlnIfPrettyPrinting(); for (int idx = 0; idx < table.getColumnCount(); idx++) { writeColumnElement(table.getColumn(idx), xmlWriter); } for (int idx = 0; idx < table.getForeignKeyCount(); idx++) { writeForeignKeyElement(table.getForeignKey(idx), xmlWriter); } for (int idx = 0; idx < table.getIndexCount(); idx++) { writeIndexElement(table.getIndex(idx), xmlWriter); } xmlWriter.indentIfPrettyPrinting(1); } writeElementEnd(xmlWriter); }
Example 7
Source File: TestAgainstLiveDatabaseBase.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Updates the row in the designated table. * * @param tableName The name of the table (case insensitive) * @param oldBean The bean representing the current row * @param columnValues The values for the columns in order of definition * @return The dyna bean for the new row */ protected DynaBean updateRow(String tableName, DynaBean oldBean, Object[] columnValues) { Table table = getModel().findTable(tableName); DynaBean bean = getModel().createDynaBeanFor(table); for (int idx = 0; (idx < table.getColumnCount()) && (idx < columnValues.length); idx++) { Column column = table.getColumn(idx); bean.set(column.getName(), columnValues[idx]); } getPlatform().update(getModel(), oldBean, bean); return bean; }
Example 8
Source File: DatabaseIO.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Writes the table object to the given XML writer. * * @param table The table object * @param xmlWriter The XML writer */ private void writeTableElement(Table table, PrettyPrintingXmlWriter xmlWriter) throws DdlUtilsXMLException { xmlWriter.indentIfPrettyPrinting(1); writeElementStart(xmlWriter, QNAME_ELEMENT_TABLE); // GemStone changes BEGIN // write fully qualified table name final String tableName = table.getQualifiedName(); writeAttribute(xmlWriter, QNAME_ATTRIBUTE_NAME, tableName); /* (original code) writeAttribute(xmlWriter, QNAME_ATTRIBUTE_NAME, table.getName()); */ // GemStone changes END writeAttribute(xmlWriter, QNAME_ATTRIBUTE_DESCRIPTION, table.getDescription()); if ((table.getColumnCount() > 0) || (table.getForeignKeyCount() > 0) || (table.getIndexCount() > 0)) { xmlWriter.printlnIfPrettyPrinting(); for (int idx = 0; idx < table.getColumnCount(); idx++) { writeColumnElement(table.getColumn(idx), xmlWriter); } for (int idx = 0; idx < table.getForeignKeyCount(); idx++) { writeForeignKeyElement(table.getForeignKey(idx), xmlWriter); } for (int idx = 0; idx < table.getIndexCount(); idx++) { writeIndexElement(table.getIndex(idx), xmlWriter); } xmlWriter.indentIfPrettyPrinting(1); } writeElementEnd(xmlWriter); }
Example 9
Source File: TestAgainstLiveDatabaseBase.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Inserts a row into the designated table. * * @param tableName The name of the table (case insensitive) * @param columnValues The values for the columns in order of definition * @return The dyna bean for the row */ protected DynaBean insertRow(String tableName, Object[] columnValues) { Table table = getModel().findTable(tableName); DynaBean bean = getModel().createDynaBeanFor(table); for (int idx = 0; (idx < table.getColumnCount()) && (idx < columnValues.length); idx++) { Column column = table.getColumn(idx); bean.set(column.getName(), columnValues[idx]); } getPlatform().insert(getModel(), bean); return bean; }
Example 10
Source File: PostgreSqlModelReader.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ protected Table readTable(DatabaseMetaDataWrapper metaData, Map values) throws SQLException { Table table = super.readTable(metaData, values); if (table != null) { // PostgreSQL also returns unique indexes for pk and non-pk auto-increment columns // which are of the form "[table]_[column]_key" HashMap uniquesByName = new HashMap(); for (int indexIdx = 0; indexIdx < table.getIndexCount(); indexIdx++) { Index index = table.getIndex(indexIdx); if (index.isUnique() && (index.getName() != null)) { uniquesByName.put(index.getName(), index); } } for (int columnIdx = 0; columnIdx < table.getColumnCount(); columnIdx++) { Column column = table.getColumn(columnIdx); if (column.isAutoIncrement()) { String indexName = table.getName() + "_" + column.getName() + "_key"; if (uniquesByName.containsKey(indexName)) { table.removeIndex((Index)uniquesByName.get(indexName)); uniquesByName.remove(indexName); } } } } return table; }
Example 11
Source File: ModelComparator.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Creates change objects for columns that are not present in the given source table but are in the 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 checkForAddedColumns(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) { String prevColumn = (columnIdx > 0 ? intermediateTable.getColumn(columnIdx - 1).getName() : null); String nextColumn = (columnIdx < intermediateTable.getColumnCount() ? intermediateTable.getColumn(columnIdx).getName() : null); Column clonedColumn = _cloneHelper.clone(targetColumn, false); AddColumnChange change = new AddColumnChange(intermediateTable.getQualifiedName(), clonedColumn, prevColumn, nextColumn); changes.add(change); change.apply(intermediateModel, _caseSensitive); } } return changes; }
Example 12
Source File: PostgreSqlBuilder.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ public void createTable(Database database, Table table, Map parameters) throws IOException { for (int idx = 0; idx < table.getColumnCount(); idx++) { Column column = table.getColumn(idx); if (column.isAutoIncrement()) { createAutoIncrementSequence(table, column); } } super.createTable(database, table, parameters); }
Example 13
Source File: SqlDynaClass.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Factory method for creating and initializing a new dyna class instance * for the given table. * * @param table The table * @return The dyna class for the table */ public static SqlDynaClass newInstance(Table table) { List properties = new ArrayList(); for (int idx = 0; idx < table.getColumnCount(); idx++) { properties.add(new SqlDynaProperty(table.getColumn(idx))); } SqlDynaProperty[] array = new SqlDynaProperty[properties.size()]; properties.toArray(array); return new SqlDynaClass(table, array); }
Example 14
Source File: SqlBuilder.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Writes the columns of the given table. * * @param table The table */ protected void writeColumns(Table table) throws IOException { for (int idx = 0; idx < table.getColumnCount(); idx++) { printIndent(); writeColumn(table, table.getColumn(idx)); if (idx < table.getColumnCount() - 1) { println(","); } } }
Example 15
Source File: PostgreSqlBuilder.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ public void createTable(Database database, Table table, Map parameters) throws IOException { for (int idx = 0; idx < table.getColumnCount(); idx++) { Column column = table.getColumn(idx); if (column.isAutoIncrement()) { createAutoIncrementSequence(table, column); } } super.createTable(database, table, parameters); }
Example 16
Source File: SqlBuilder.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Writes the columns of the given table. * * @param table The table */ protected void writeColumns(Table table) throws IOException { for (int idx = 0; idx < table.getColumnCount(); idx++) { printIndent(); writeColumn(table, table.getColumn(idx)); if (idx < table.getColumnCount() - 1) { println(","); } } }
Example 17
Source File: SqlDynaClass.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Factory method for creating and initializing a new dyna class instance * for the given table. * * @param table The table * @return The dyna class for the table */ public static SqlDynaClass newInstance(Table table) { List properties = new ArrayList(); for (int idx = 0; idx < table.getColumnCount(); idx++) { properties.add(new SqlDynaProperty(table.getColumn(idx))); } SqlDynaProperty[] array = new SqlDynaProperty[properties.size()]; properties.toArray(array); return new SqlDynaClass(table, array); }
Example 18
Source File: TestAgainstLiveDatabaseBase.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
/** * Asserts that the two given database tables are equal. * * @param expected The expected table * @param actual The actual table * @param caseSensitive Whether case matters when comparing */ protected void assertEquals(Table expected, Table actual, boolean caseSensitive) { if (caseSensitive) { assertEquals("Table names do not match.", getPlatform().getSqlBuilder().shortenName(expected.getName(), getSqlBuilder().getMaxTableNameLength()), getPlatform().getSqlBuilder().shortenName(actual.getName(), getSqlBuilder().getMaxTableNameLength())); } else { assertEquals("Table names do not match (ignoring case).", getPlatform().getSqlBuilder().shortenName(expected.getName().toUpperCase(), getSqlBuilder().getMaxTableNameLength()), getPlatform().getSqlBuilder().shortenName(actual.getName().toUpperCase(), getSqlBuilder().getMaxTableNameLength())); } assertEquals("Not the same number of columns in table "+actual.getName()+".", expected.getColumnCount(), actual.getColumnCount()); for (int columnIdx = 0; columnIdx < actual.getColumnCount(); columnIdx++) { assertEquals(expected.getColumn(columnIdx), actual.getColumn(columnIdx), caseSensitive); } assertEquals("Not the same number of foreign keys in table "+actual.getName()+".", expected.getForeignKeyCount(), actual.getForeignKeyCount()); // order is not assumed with the way foreignkeys are returned. for (int expectedFkIdx = 0; expectedFkIdx < expected.getForeignKeyCount(); expectedFkIdx++) { ForeignKey expectedFk = expected.getForeignKey(expectedFkIdx); String expectedName = getPlatform().getSqlBuilder().shortenName(expectedFk.getName(), getSqlBuilder().getMaxForeignKeyNameLength()); for (int actualFkIdx = 0; actualFkIdx < actual.getForeignKeyCount(); actualFkIdx++) { ForeignKey actualFk = actual.getForeignKey(actualFkIdx); String actualName = getPlatform().getSqlBuilder().shortenName(actualFk.getName(), getSqlBuilder().getMaxForeignKeyNameLength()); if (StringUtilsExt.equals(expectedName, actualName, caseSensitive)) { assertEquals(expectedFk, actualFk, caseSensitive); } } } assertEquals("Not the same number of indices in table "+actual.getName()+".", expected.getIndexCount(), actual.getIndexCount()); for (int indexIdx = 0; indexIdx < actual.getIndexCount(); indexIdx++) { assertEquals(expected.getIndex(indexIdx), actual.getIndex(indexIdx), caseSensitive); } }
Example 19
Source File: TestAgainstLiveDatabaseBase.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
/** * Returns a copy of the given model adjusted for type changes because of the native type mappings * which when read back from the database will map to different types. * * @param sourceModel The source model * @return The adjusted model */ protected Database adjustModel(Database sourceModel) { Database model = new CloneHelper().clone(sourceModel); for (int tableIdx = 0; tableIdx < model.getTableCount(); tableIdx++) { Table table = model.getTable(tableIdx); for (int columnIdx = 0; columnIdx < table.getColumnCount(); columnIdx++) { Column column = table.getColumn(columnIdx); int origType = column.getTypeCode(); int targetType = getPlatformInfo().getTargetJdbcType(origType); // we adjust the column types if the native type would back-map to a // different jdbc type if (targetType != origType) { column.setTypeCode(targetType); // we should also adapt the default value if (column.getDefaultValue() != null) { DefaultValueHelper helper = getPlatform().getSqlBuilder().getDefaultValueHelper(); column.setDefaultValue(helper.convert(column.getDefaultValue(), origType, targetType)); } } // we also promote the default size if the column has no size // spec of its own if ((column.getSize() == null) && getPlatformInfo().hasSize(targetType)) { Integer defaultSize = getPlatformInfo().getDefaultSize(targetType); if (defaultSize != null) { column.setSize(defaultSize.toString()); } } // finally the platform might return a synthetic default value if the column // is a primary key column if (getPlatformInfo().isSyntheticDefaultValueForRequiredReturned() && (column.getDefaultValue() == null) && column.isRequired() && !column.isAutoIncrement()) { switch (column.getTypeCode()) { case Types.TINYINT: case Types.SMALLINT: case Types.INTEGER: case Types.BIGINT: column.setDefaultValue("0"); break; case Types.REAL: case Types.FLOAT: case Types.DOUBLE: column.setDefaultValue("0.0"); break; case Types.BIT: column.setDefaultValue("false"); break; default: column.setDefaultValue(""); break; } } if (column.isPrimaryKey() && getPlatformInfo().isPrimaryKeyColumnAutomaticallyRequired()) { column.setRequired(true); } if (column.isAutoIncrement() && getPlatformInfo().isIdentityColumnAutomaticallyRequired()) { column.setRequired(true); } } // we also add the default names to foreign keys that are initially unnamed for (int fkIdx = 0; fkIdx < table.getForeignKeyCount(); fkIdx++) { ForeignKey fk = table.getForeignKey(fkIdx); if (fk.getName() == null) { fk.setName(getPlatform().getSqlBuilder().getForeignKeyName(table, fk)); } } } return model; }
Example 20
Source File: TestAgainstLiveDatabaseBase.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
/** * Asserts that the two given database tables are equal. * * @param expected The expected table * @param actual The actual table * @param caseSensitive Whether case matters when comparing */ protected void assertEquals(Table expected, Table actual, boolean caseSensitive) { if (caseSensitive) { assertEquals("Table names do not match.", getPlatform().getSqlBuilder().shortenName(expected.getName(), getSqlBuilder().getMaxTableNameLength()), getPlatform().getSqlBuilder().shortenName(actual.getName(), getSqlBuilder().getMaxTableNameLength())); } else { assertEquals("Table names do not match (ignoring case).", getPlatform().getSqlBuilder().shortenName(expected.getName().toUpperCase(), getSqlBuilder().getMaxTableNameLength()), getPlatform().getSqlBuilder().shortenName(actual.getName().toUpperCase(), getSqlBuilder().getMaxTableNameLength())); } assertEquals("Not the same number of columns in table "+actual.getName()+".", expected.getColumnCount(), actual.getColumnCount()); for (int columnIdx = 0; columnIdx < actual.getColumnCount(); columnIdx++) { assertEquals(expected.getColumn(columnIdx), actual.getColumn(columnIdx), caseSensitive); } assertEquals("Not the same number of foreign keys in table "+actual.getName()+".", expected.getForeignKeyCount(), actual.getForeignKeyCount()); // order is not assumed with the way foreignkeys are returned. for (int expectedFkIdx = 0; expectedFkIdx < expected.getForeignKeyCount(); expectedFkIdx++) { ForeignKey expectedFk = expected.getForeignKey(expectedFkIdx); String expectedName = getPlatform().getSqlBuilder().shortenName(expectedFk.getName(), getSqlBuilder().getMaxForeignKeyNameLength()); for (int actualFkIdx = 0; actualFkIdx < actual.getForeignKeyCount(); actualFkIdx++) { ForeignKey actualFk = actual.getForeignKey(actualFkIdx); String actualName = getPlatform().getSqlBuilder().shortenName(actualFk.getName(), getSqlBuilder().getMaxForeignKeyNameLength()); if (StringUtilsExt.equals(expectedName, actualName, caseSensitive)) { assertEquals(expectedFk, actualFk, caseSensitive); } } } assertEquals("Not the same number of indices in table "+actual.getName()+".", expected.getIndexCount(), actual.getIndexCount()); for (int indexIdx = 0; indexIdx < actual.getIndexCount(); indexIdx++) { assertEquals(expected.getIndex(indexIdx), actual.getIndex(indexIdx), caseSensitive); } }