Java Code Examples for org.apache.ddlutils.model.Table#getColumn()
The following examples show how to use
org.apache.ddlutils.model.Table#getColumn() .
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: 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 2
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 3
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 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: 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 6
Source File: TestDatabaseIO.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Tests a database model with a table with an autoincrement column. */ public void testAutoIncrementColumn() throws Exception { Database model = readModel( "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='test'>\n" + " <table name='SomeTable'\n" + " description='Some table'>\n" + " <column name='ID'\n" + " type='INTEGER'\n" + " autoIncrement='true'/>\n" + " </table>\n" + "</database>"); assertEquals("test", model.getName()); assertEquals(1, model.getTableCount()); Table table = model.getTable(0); assertEquals("SomeTable", "Some table", 1, 0, 1, 0, 0, table); Column column = table.getColumn(0); assertEquals("ID", Types.INTEGER, 0, 0, null, null, null, false, false, true, column); assertEquals(column, table.getAutoIncrementColumns()[0]); assertEquals( "<?xml version='1.0' encoding='UTF-8'?>\n" + "<database xmlns=\"" + DatabaseIO.DDLUTILS_NAMESPACE + "\" name=\"test\">\n" + " <table name=\"SomeTable\" description=\"Some table\">\n" + " <column name=\"ID\" primaryKey=\"false\" required=\"false\" type=\"INTEGER\" autoIncrement=\"true\" />\n" + " </table>\n" + "</database>\n", model); }
Example 7
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 8
Source File: DataWriter.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Writes the given bean. * * @param bean The bean to write */ public void write(SqlDynaBean bean) throws DataWriterException { SqlDynaClass dynaClass = (SqlDynaClass)bean.getDynaClass(); Table table = dynaClass.getTable(); TableXmlWriter tableWriter = new TableXmlWriter(table); List columnWriters = new ArrayList(); for (int idx = 0; idx < table.getColumnCount(); idx++) { Column column = table.getColumn(idx); Object value = bean.get(column.getName()); SqlTypeConverter converter = _converterConf.getRegisteredConverter(table, column); String valueAsText = null; if (converter == null) { if (value != null) { valueAsText = value.toString(); } } else { valueAsText = converter.convertToString(value, column.getTypeCode()); } if (valueAsText != null) { columnWriters.add(new ColumnXmlWriter(column, valueAsText)); } } tableWriter.write(columnWriters, this); }
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: DataWriter.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Writes the given bean. * * @param bean The bean to write */ public void write(SqlDynaBean bean) throws DataWriterException { SqlDynaClass dynaClass = (SqlDynaClass)bean.getDynaClass(); Table table = dynaClass.getTable(); TableXmlWriter tableWriter = new TableXmlWriter(table); List columnWriters = new ArrayList(); for (int idx = 0; idx < table.getColumnCount(); idx++) { Column column = table.getColumn(idx); Object value = bean.get(column.getName()); SqlTypeConverter converter = _converterConf.getRegisteredConverter(table, column); String valueAsText = null; if (converter == null) { if (value != null) { valueAsText = value.toString(); } } else { valueAsText = converter.convertToString(value, column.getTypeCode()); } if (valueAsText != null) { columnWriters.add(new ColumnXmlWriter(column, valueAsText)); } } tableWriter.write(columnWriters, this); }
Example 11
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 12
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 13
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 14
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 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: DataReader.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
/** * Reads a bean from the given xml stream reader. * * @param xmlReader The reader */ private void readBean(XMLStreamReader xmlReader) throws XMLStreamException, DdlUtilsXMLException { QName elemQName = xmlReader.getName(); Location location = xmlReader.getLocation(); Map attributes = new HashMap(); String tableName = null; for (int idx = 0; idx < xmlReader.getAttributeCount(); idx++) { QName attrQName = xmlReader.getAttributeName(idx); attributes.put(isCaseSensitive() ? attrQName.getLocalPart() : attrQName.getLocalPart().toLowerCase(), xmlReader.getAttributeValue(idx)); } readColumnSubElements(xmlReader, attributes); if ("table".equals(elemQName.getLocalPart())) { tableName = (String)attributes.get("table-name"); } else { tableName = elemQName.getLocalPart(); } Table table = _model.findTable(tableName, isCaseSensitive()); if (table == null) { _log.warn("Data XML contains an element " + elemQName + " at location " + location + " but there is no table defined with this name. This element will be ignored."); } else { DynaBean bean = _model.createDynaBeanFor(table); for (int idx = 0; idx < table.getColumnCount(); idx++) { Column column = table.getColumn(idx); String value = (String)attributes.get(isCaseSensitive() ? column.getName() : column.getName().toLowerCase()); if (value != null) { setColumnValue(bean, table, column, value); } } getSink().addBean(bean); consumeRestOfElement(xmlReader); } }
Example 17
Source File: DataReader.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
/** * Reads a bean from the given xml stream reader. * * @param xmlReader The reader */ private void readBean(XMLStreamReader xmlReader) throws XMLStreamException, DdlUtilsXMLException { QName elemQName = xmlReader.getName(); Location location = xmlReader.getLocation(); Map attributes = new HashMap(); String tableName = null; for (int idx = 0; idx < xmlReader.getAttributeCount(); idx++) { QName attrQName = xmlReader.getAttributeName(idx); attributes.put(isCaseSensitive() ? attrQName.getLocalPart() : attrQName.getLocalPart().toLowerCase(), xmlReader.getAttributeValue(idx)); } readColumnSubElements(xmlReader, attributes); if ("table".equals(elemQName.getLocalPart())) { tableName = (String)attributes.get("table-name"); } else { tableName = elemQName.getLocalPart(); } Table table = _model.findTable(tableName, isCaseSensitive()); if (table == null) { _log.warn("Data XML contains an element " + elemQName + " at location " + location + " but there is no table defined with this name. This element will be ignored."); } else { DynaBean bean = _model.createDynaBeanFor(table); for (int idx = 0; idx < table.getColumnCount(); idx++) { Column column = table.getColumn(idx); String value = (String)attributes.get(isCaseSensitive() ? column.getName() : column.getName().toLowerCase()); if (value != null) { setColumnValue(bean, table, column, value); } } getSink().addBean(bean); consumeRestOfElement(xmlReader); } }
Example 18
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 19
Source File: TestDatabaseIO.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
/** * Tests a database model containing a single foreignkey. */ public void testSingleForeignkey() throws Exception { Database model = readModel( "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='test'>\n" + " <table name='SomeTable'\n" + " description='Some table'>\n" + " <column name='ID'\n" + " type='VARCHAR'\n" + " size='16'\n" + " primaryKey='true'\n" + " required='true'\n" + " description='The primary key'/>\n" + " </table>\n" + " <table name='AnotherTable'\n" + " description='And another table'>\n" + " <column name='Some_ID'\n" + " type='VARCHAR'\n" + " size='16'\n" + " description='The foreign key'/>\n" + " <foreign-key foreignTable='SomeTable'>\n" + " <reference local='Some_ID' foreign='ID'/>\n" + " </foreign-key>\n" + " </table>\n" + "</database>"); assertEquals("test", model.getName()); assertEquals(2, model.getTableCount()); Table someTable = model.getTable(0); assertEquals("SomeTable", "Some table", 1, 1, 0, 0, 0, someTable); Column pkColumn = someTable.getColumn(0); assertEquals("ID", Types.VARCHAR, 16, 0, null, "The primary key", null, true, true, false, pkColumn); Table anotherTable = model.getTable(1); assertEquals("AnotherTable", "And another table", 1, 0, 0, 1, 0, anotherTable); Column fkColumn = anotherTable.getColumn(0); assertEquals("Some_ID", Types.VARCHAR, 16, 0, null, "The foreign key", null, false, false, false, fkColumn); ForeignKey fk = anotherTable.getForeignKey(0); assertEquals(null, CascadeActionEnum.NONE, CascadeActionEnum.NONE, someTable, 1, fk); assertEquals(fkColumn, pkColumn, fk.getFirstReference()); assertEquals( "<?xml version='1.0' encoding='UTF-8'?>\n" + "<database xmlns=\"" + DatabaseIO.DDLUTILS_NAMESPACE + "\" name=\"test\">\n" + " <table name=\"SomeTable\" description=\"Some table\">\n" + " <column name=\"ID\" primaryKey=\"true\" required=\"true\" type=\"VARCHAR\" size=\"16\" autoIncrement=\"false\" description=\"The primary key\" />\n" + " </table>\n" + " <table name=\"AnotherTable\" description=\"And another table\">\n" + " <column name=\"Some_ID\" primaryKey=\"false\" required=\"false\" type=\"VARCHAR\" size=\"16\" autoIncrement=\"false\" description=\"The foreign key\" />\n" + " <foreign-key foreignTable=\"SomeTable\">\n" + " <reference local=\"Some_ID\" foreign=\"ID\" />\n" + " </foreign-key>\n" + " </table>\n" + "</database>\n", model); }
Example 20
Source File: SqlBuilder.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
/** * Writes a statement that copies the data from the source to the target table. Note * that this copies only those columns that are in both tables. * Database-specific implementations might redefine this method though it usually * suffices to redefine the {@link #writeCastExpression(Column, Column)} method. * * @param sourceTable The source table * @param targetTable The target table */ protected void copyData(Table sourceTable, Table targetTable) throws IOException { ListOrderedMap columns = new ListOrderedMap(); for (int idx = 0; idx < sourceTable.getColumnCount(); idx++) { Column sourceColumn = sourceTable.getColumn(idx); Column targetColumn = targetTable.findColumn(sourceColumn.getName(), getPlatform().isDelimitedIdentifierModeOn()); if (targetColumn != null) { columns.put(sourceColumn, targetColumn); } } print("INSERT INTO "); printIdentifier(getTableName(targetTable)); print(" ("); for (Iterator columnIt = columns.keySet().iterator(); columnIt.hasNext();) { printIdentifier(getColumnName((Column)columnIt.next())); if (columnIt.hasNext()) { print(","); } } print(") SELECT "); for (Iterator columnsIt = columns.entrySet().iterator(); columnsIt.hasNext();) { Map.Entry entry = (Map.Entry)columnsIt.next(); writeCastExpression((Column)entry.getKey(), (Column)entry.getValue()); if (columnsIt.hasNext()) { print(","); } } print(" FROM "); printIdentifier(getTableName(sourceTable)); printEndOfStatement(); }