Java Code Examples for org.apache.ddlutils.model.Table#getForeignKey()
The following examples show how to use
org.apache.ddlutils.model.Table#getForeignKey() .
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: DataToDatabaseSink.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Updates the values of the columns constituting the indicated foreign key with the values * of the given identity. * * @param bean The bean whose columns shall be updated * @param fkName The name of the foreign key * @param identity The target identity */ private void updateFKColumns(DynaBean bean, String fkName, Identity identity) { Table sourceTable = ((SqlDynaClass)bean.getDynaClass()).getTable(); Table targetTable = identity.getTable(); ForeignKey fk = null; for (int idx = 0; idx < sourceTable.getForeignKeyCount(); idx++) { ForeignKey curFk = sourceTable.getForeignKey(idx); if (curFk.getForeignTableName().equalsIgnoreCase(targetTable.getQualifiedName())) { if (fkName.equals(getFKName(sourceTable, curFk))) { fk = curFk; break; } } } if (fk != null) { for (int idx = 0; idx < fk.getReferenceCount(); idx++) { Reference curRef = fk.getReference(idx); Column sourceColumn = curRef.getLocalColumn(); Column targetColumn = curRef.getForeignColumn(); bean.set(sourceColumn.getName(), identity.getColumnValue(targetColumn.getName())); } } }
Example 2
Source File: MySqlModelComparator.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Returns remove and add changes for the foreign keys that reference the indicated columns as a local column. * * @param intermediateTable The intermediate table * @param targetTable The target table * @param columnNames The names of the columns to look for * @return The additional changes */ private List getForeignKeyRecreationChanges(Table intermediateTable, Table targetTable, Set columnNames) { List newChanges = new ArrayList(); for (int fkIdx = 0; fkIdx < targetTable.getForeignKeyCount(); fkIdx++) { ForeignKey targetFk = targetTable.getForeignKey(fkIdx); ForeignKey intermediateFk = intermediateTable.findForeignKey(targetFk, isCaseSensitive()); if (intermediateFk != null) { for (int refIdx = 0; refIdx < intermediateFk.getReferenceCount(); refIdx++) { Reference ref = intermediateFk.getReference(refIdx); for (Iterator colNameIt = columnNames.iterator(); colNameIt.hasNext();) { if (StringUtilsExt.equals(ref.getLocalColumnName(), (String)colNameIt.next(), isCaseSensitive())) { newChanges.add(new RemoveForeignKeyChange(intermediateTable.getName(), intermediateFk)); newChanges.add(new AddForeignKeyChange(intermediateTable.getName(), intermediateFk)); } } } } } return newChanges; }
Example 3
Source File: MSSqlModelComparator.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ protected List checkForAddedForeignKeys(Database sourceModel, Database intermediateModel, Database targetModel) { List changes = super.checkForAddedForeignKeys(sourceModel, intermediateModel, targetModel); List additionalChanges = new ArrayList(); // re-adding all foreign keys that are maintained and that use a changed column for (int tableIdx = 0; tableIdx < targetModel.getTableCount(); tableIdx++) { Table targetTable = targetModel.getTable(tableIdx); Table sourceTable = sourceModel.findTable(targetTable.getName(), isCaseSensitive()); Table intermediateTable = intermediateModel.findTable(targetTable.getName(), isCaseSensitive()); if (sourceTable != null) { for (int fkIdx = 0; fkIdx < targetTable.getForeignKeyCount(); fkIdx++) { ForeignKey targetFk = targetTable.getForeignKey(fkIdx); ForeignKey sourceFk = findCorrespondingForeignKey(sourceTable, targetFk); ForeignKey intermediateFk = findCorrespondingForeignKey(intermediateTable, targetFk); if ((sourceFk != null) && (intermediateFk == null)) { additionalChanges.add(new AddForeignKeyChange(intermediateTable.getName(), targetFk)); } } } } for (Iterator changeIt = additionalChanges.iterator(); changeIt.hasNext();) { ((AddForeignKeyChange)changeIt.next()).apply(intermediateModel, isCaseSensitive()); } changes.addAll(additionalChanges); return changes; }
Example 4
Source File: DataToDatabaseSink.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ public void start() throws DataSinkException { _fkTables.clear(); _waitingObjects.clear(); if (_ensureFkOrder) { for (int tableIdx = 0; tableIdx < _model.getTableCount(); tableIdx++) { Table table = _model.getTable(tableIdx); for (int fkIdx = 0; fkIdx < table.getForeignKeyCount(); fkIdx++) { ForeignKey curFk = table.getForeignKey(fkIdx); _fkTables.add(curFk.getForeignTable()); } } } try { _connection = _platform.borrowConnection(); } catch (DatabaseOperationException ex) { throw new DataSinkException(ex); } }
Example 5
Source File: ModelComparator.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Creates change objects for foreign keys that are not present in the given source model but are in the target * model, and applies them to the given intermediate model. * * @param sourceModel The source model * @param intermediateModel The intermediate model to apply the changes to * @param targetModel The target model * @return The changes */ protected List checkForAddedForeignKeys(Database sourceModel, Database intermediateModel, Database targetModel) { List changes = new ArrayList(); for (int tableIdx = 0; tableIdx < targetModel.getTableCount(); tableIdx++) { Table targetTable = targetModel.getTable(tableIdx); Table intermediateTable = intermediateModel.findTable(targetTable.getQualifiedName(), _caseSensitive); for (int fkIdx = 0; fkIdx < targetTable.getForeignKeyCount(); fkIdx++) { ForeignKey targetFk = targetTable.getForeignKey(fkIdx); ForeignKey intermediateFk = findCorrespondingForeignKey(intermediateTable, targetFk); if (intermediateFk == null) { if (_log.isInfoEnabled()) { _log.info("Foreign key " + targetFk + " needs to be added to table " + intermediateTable.getQualifiedName()); } intermediateFk = _cloneHelper.clone(targetFk, intermediateTable, intermediateModel, _caseSensitive); AddForeignKeyChange fkChange = new AddForeignKeyChange(intermediateTable.getQualifiedName(), intermediateFk); changes.add(fkChange); fkChange.apply(intermediateModel, _caseSensitive); } } } return changes; }
Example 6
Source File: DataToDatabaseSink.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Updates the values of the columns constituting the indicated foreign key with the values * of the given identity. * * @param bean The bean whose columns shall be updated * @param fkName The name of the foreign key * @param identity The target identity */ private void updateFKColumns(DynaBean bean, String fkName, Identity identity) { Table sourceTable = ((SqlDynaClass)bean.getDynaClass()).getTable(); Table targetTable = identity.getTable(); ForeignKey fk = null; for (int idx = 0; idx < sourceTable.getForeignKeyCount(); idx++) { ForeignKey curFk = sourceTable.getForeignKey(idx); if (curFk.getForeignTableName().equalsIgnoreCase(targetTable.getQualifiedName())) { if (fkName.equals(getFKName(sourceTable, curFk))) { fk = curFk; break; } } } if (fk != null) { for (int idx = 0; idx < fk.getReferenceCount(); idx++) { Reference curRef = fk.getReference(idx); Column sourceColumn = curRef.getLocalColumn(); Column targetColumn = curRef.getForeignColumn(); bean.set(sourceColumn.getName(), identity.getColumnValue(targetColumn.getName())); } } }
Example 7
Source File: ForeignKeyChangeImplBase.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ public ForeignKey findChangedForeignKey(Database model, boolean caseSensitive) { Table table = findChangedTable(model, caseSensitive); if (table != null) { for (int fkIdx = 0; fkIdx < table.getForeignKeyCount(); fkIdx++) { ForeignKey curFk = table.getForeignKey(fkIdx); if (curFk.getReferenceCount() == _referenceColumnNames.size()) { for (int refIdx = 0; refIdx < curFk.getReferenceCount(); refIdx++) { Reference ref = curFk.getReference(refIdx); Pair colNames = (Pair)_referenceColumnNames.get(refIdx); if (caseSensitive) { if (ref.getLocalColumnName().equals((String)colNames.getFirst()) && ref.getForeignColumnName().equals((String)colNames.getSecond())) { return curFk; } } else { if (ref.getLocalColumnName().equalsIgnoreCase((String)colNames.getFirst()) && ref.getForeignColumnName().equalsIgnoreCase((String)colNames.getSecond())) { return curFk; } } } } } } return null; }
Example 8
Source File: DataToDatabaseSink.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ public void start() throws DataSinkException { _fkTables.clear(); _waitingObjects.clear(); if (_ensureFkOrder) { for (int tableIdx = 0; tableIdx < _model.getTableCount(); tableIdx++) { Table table = _model.getTable(tableIdx); for (int fkIdx = 0; fkIdx < table.getForeignKeyCount(); fkIdx++) { ForeignKey curFk = table.getForeignKey(fkIdx); _fkTables.add(curFk.getForeignTable()); } } } try { _connection = _platform.borrowConnection(); } catch (DatabaseOperationException ex) { throw new DataSinkException(ex); } }
Example 9
Source File: ModelComparator.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Searches in the given table for a corresponding foreign key. If the given key * has no name, then a foreign key to the same table with the same columns (but not * necessarily in the same order) is searched. If the given key has a name, then the * corresponding key also needs to have the same name, or no name at all, but not a * different one. * * @param table The table to search in * @param fk The original foreign key * @return The corresponding foreign key if found */ protected ForeignKey findCorrespondingForeignKey(Table table, ForeignKey fk) { for (int fkIdx = 0; fkIdx < table.getForeignKeyCount(); fkIdx++) { ForeignKey curFk = table.getForeignKey(fkIdx); if ((_caseSensitive && fk.equals(curFk)) || (!_caseSensitive && fk.equalsIgnoreCase(curFk))) { return curFk; } } return null; }
Example 10
Source File: ModelComparator.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Creates change objects for foreign keys that are not present in the given source model but are in the target * model, and applies them to the given intermediate model. * * @param sourceModel The source model * @param intermediateModel The intermediate model to apply the changes to * @param targetModel The target model * @return The changes */ protected List checkForAddedForeignKeys(Database sourceModel, Database intermediateModel, Database targetModel) { List changes = new ArrayList(); for (int tableIdx = 0; tableIdx < targetModel.getTableCount(); tableIdx++) { Table targetTable = targetModel.getTable(tableIdx); Table intermediateTable = intermediateModel.findTable(targetTable.getQualifiedName(), _caseSensitive); for (int fkIdx = 0; fkIdx < targetTable.getForeignKeyCount(); fkIdx++) { ForeignKey targetFk = targetTable.getForeignKey(fkIdx); ForeignKey intermediateFk = findCorrespondingForeignKey(intermediateTable, targetFk); if (intermediateFk == null) { if (_log.isInfoEnabled()) { _log.info("Foreign key " + targetFk + " needs to be added to table " + intermediateTable.getQualifiedName()); } intermediateFk = _cloneHelper.clone(targetFk, intermediateTable, intermediateModel, _caseSensitive); AddForeignKeyChange fkChange = new AddForeignKeyChange(intermediateTable.getQualifiedName(), intermediateFk); changes.add(fkChange); fkChange.apply(intermediateModel, _caseSensitive); } } } return changes; }
Example 11
Source File: ModelComparator.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Searches in the given table for a corresponding foreign key. If the given key * has no name, then a foreign key to the same table with the same columns (but not * necessarily in the same order) is searched. If the given key has a name, then the * corresponding key also needs to have the same name, or no name at all, but not a * different one. * * @param table The table to search in * @param fk The original foreign key * @return The corresponding foreign key if found */ protected ForeignKey findCorrespondingForeignKey(Table table, ForeignKey fk) { for (int fkIdx = 0; fkIdx < table.getForeignKeyCount(); fkIdx++) { ForeignKey curFk = table.getForeignKey(fkIdx); if ((_caseSensitive && fk.equals(curFk)) || (!_caseSensitive && fk.equalsIgnoreCase(curFk))) { return curFk; } } return null; }
Example 12
Source File: MySqlModelComparator.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Returns remove and add changes for the foreign keys that reference the indicated columns as a local column. * * @param intermediateTable The intermediate table * @param targetTable The target table * @param columnNames The names of the columns to look for * @return The additional changes */ private List getForeignKeyRecreationChanges(Table intermediateTable, Table targetTable, Set columnNames) { List newChanges = new ArrayList(); for (int fkIdx = 0; fkIdx < targetTable.getForeignKeyCount(); fkIdx++) { ForeignKey targetFk = targetTable.getForeignKey(fkIdx); ForeignKey intermediateFk = intermediateTable.findForeignKey(targetFk, isCaseSensitive()); if (intermediateFk != null) { for (int refIdx = 0; refIdx < intermediateFk.getReferenceCount(); refIdx++) { Reference ref = intermediateFk.getReference(refIdx); for (Iterator colNameIt = columnNames.iterator(); colNameIt.hasNext();) { if (StringUtilsExt.equals(ref.getLocalColumnName(), (String)colNameIt.next(), isCaseSensitive())) { newChanges.add(new RemoveForeignKeyChange(intermediateTable.getName(), intermediateFk)); newChanges.add(new AddForeignKeyChange(intermediateTable.getName(), intermediateFk)); } } } } } return newChanges; }
Example 13
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 14
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 15
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 16
Source File: TestDatabaseIO.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
/** * Tests a database model containing a foreignkey with two references. */ public void testForeignkeyWithTwoReferences() 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" + " <column name='VALUE1'\n" + " type='INTEGER'\n" + " required='false'\n" + " description='A value'/>\n" + " <column name='VALUE2'\n" + " type='DOUBLE'\n" + " required='false'\n" + " description='Another value'/>\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" + " <column name='Some_Value'\n" + " type='DOUBLE'/>\n" + " <foreign-key foreignTable='SomeTable'>\n" + " <reference local='Some_ID' foreign='ID'/>\n" + " <reference local='Some_Value' foreign='VALUE2'/>\n" + " </foreign-key>\n" + " </table>\n" + "</database>"); assertEquals("test", model.getName()); assertEquals(2, model.getTableCount()); Table someTable = model.getTable(0); assertEquals("SomeTable", "Some table", 3, 1, 0, 0, 0, someTable); assertEquals("ID", Types.VARCHAR, 16, 0, null, "The primary key", null, true, true, false, someTable.getColumn(0)); Table anotherTable = model.getTable(1); assertEquals("AnotherTable", "And another table", 2, 0, 0, 1, 0, anotherTable); assertEquals("Some_ID", Types.VARCHAR, 16, 0, null, "The foreign key", null, false, false, false, anotherTable.getColumn(0)); ForeignKey fk = anotherTable.getForeignKey(0); assertEquals(null, CascadeActionEnum.NONE, CascadeActionEnum.NONE, someTable, 2, fk); assertEquals(anotherTable.getColumn(0), someTable.getColumn(0), fk.getReference(0)); assertEquals(anotherTable.getColumn(1), someTable.getColumn(2), fk.getReference(1)); 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" + " <column name=\"VALUE1\" primaryKey=\"false\" required=\"false\" type=\"INTEGER\" autoIncrement=\"false\" description=\"A value\" />\n" + " <column name=\"VALUE2\" primaryKey=\"false\" required=\"false\" type=\"DOUBLE\" autoIncrement=\"false\" description=\"Another value\" />\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" + " <column name=\"Some_Value\" primaryKey=\"false\" required=\"false\" type=\"DOUBLE\" autoIncrement=\"false\" />\n" + " <foreign-key foreignTable=\"SomeTable\">\n" + " <reference local=\"Some_ID\" foreign=\"ID\" />\n" + " <reference local=\"Some_Value\" foreign=\"VALUE2\" />\n" + " </foreign-key>\n" + " </table>\n" + "</database>\n", model); }
Example 17
Source File: TestDatabaseIO.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
/** * Tests a database model containing a named foreignkey. */ public void testNamedForeignkey() 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 name='The foreignkey' 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); assertEquals("ID", Types.VARCHAR, 16, 0, null, "The primary key", null, true, true, false, someTable.getColumn(0)); Table anotherTable = model.getTable(1); assertEquals("AnotherTable", "And another table", 1, 0, 0, 1, 0, anotherTable); assertEquals("Some_ID", Types.VARCHAR, 16, 0, null, "The foreign key", null, false, false, false, anotherTable.getColumn(0)); ForeignKey fk = anotherTable.getForeignKey(0); assertEquals("The foreignkey", CascadeActionEnum.NONE, CascadeActionEnum.NONE, someTable, 1, fk); assertEquals(anotherTable.getColumn(0), someTable.getColumn(0), fk.getReference(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=\"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\" name=\"The foreignkey\">\n" + " <reference local=\"Some_ID\" foreign=\"ID\" />\n" + " </foreign-key>\n" + " </table>\n" + "</database>\n", model); }
Example 18
Source File: TestDatabaseIO.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
/** * Tests a database model containing two foreignkeys. */ public void testTwoForeignkeys() 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" + " <column name='VALUE1'\n" + " type='INTEGER'\n" + " required='false'\n" + " description='A value'/>\n" + " <column name='VALUE2'\n" + " type='DOUBLE'\n" + " required='false'\n" + " description='Another value'/>\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" + " <column name='Some_Value'\n" + " type='DOUBLE'/>\n" + " <foreign-key foreignTable='SomeTable'>\n" + " <reference local='Some_ID' foreign='ID'/>\n" + " </foreign-key>\n" + " <foreign-key foreignTable='SomeTable'>\n" + " <reference local='Some_Value' foreign='VALUE2'/>\n" + " </foreign-key>\n" + " </table>\n" + "</database>"); assertEquals("test", model.getName()); assertEquals(2, model.getTableCount()); Table someTable = model.getTable(0); assertEquals("SomeTable", "Some table", 3, 1, 0, 0, 0, someTable); assertEquals("ID", Types.VARCHAR, 16, 0, null, "The primary key", null, true, true, false, someTable.getColumn(0)); Table anotherTable = model.getTable(1); assertEquals("AnotherTable", "And another table", 2, 0, 0, 2, 0, anotherTable); assertEquals("Some_ID", Types.VARCHAR, 16, 0, null, "The foreign key", null, false, false, false, anotherTable.getColumn(0)); ForeignKey fk = anotherTable.getForeignKey(0); assertEquals(null, CascadeActionEnum.NONE, CascadeActionEnum.NONE, someTable, 1, fk); assertEquals(anotherTable.getColumn(0), someTable.getColumn(0), fk.getReference(0)); fk = anotherTable.getForeignKey(1); assertEquals(null, CascadeActionEnum.NONE, CascadeActionEnum.NONE, someTable, 1, fk); assertEquals(anotherTable.getColumn(1), someTable.getColumn(2), fk.getReference(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=\"true\" required=\"true\" type=\"VARCHAR\" size=\"16\" autoIncrement=\"false\" description=\"The primary key\" />\n" + " <column name=\"VALUE1\" primaryKey=\"false\" required=\"false\" type=\"INTEGER\" autoIncrement=\"false\" description=\"A value\" />\n" + " <column name=\"VALUE2\" primaryKey=\"false\" required=\"false\" type=\"DOUBLE\" autoIncrement=\"false\" description=\"Another value\" />\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" + " <column name=\"Some_Value\" primaryKey=\"false\" required=\"false\" type=\"DOUBLE\" autoIncrement=\"false\" />\n" + " <foreign-key foreignTable=\"SomeTable\">\n" + " <reference local=\"Some_ID\" foreign=\"ID\" />\n" + " </foreign-key>\n" + " <foreign-key foreignTable=\"SomeTable\">\n" + " <reference local=\"Some_Value\" foreign=\"VALUE2\" />\n" + " </foreign-key>\n" + " </table>\n" + "</database>\n", model); }
Example 19
Source File: DataToDatabaseSink.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
/** * {@inheritDoc} */ public void addBean(DynaBean bean) throws DataSinkException { Table table = _model.getDynaClassFor(bean).getTable(); Identity origIdentity = buildIdentityFromPKs(table, bean); // GemStone changes BEGIN if (_useBatchMode) { if (_lastTable != null) { if (table != _lastTable) { purgeBatchQueue(); } } else if (_platform.getPlatformInfo() .isIdentityValueReadableInBatchUsingStatement()) { _origIdentityMap = new HashMap<DynaBean, Identity>(); } _lastTable = table; } // GemStone changes END if (_ensureFkOrder && (table.getForeignKeyCount() > 0)) { WaitingObject waitingObj = new WaitingObject(bean, origIdentity); for (int idx = 0; idx < table.getForeignKeyCount(); idx++) { ForeignKey fk = table.getForeignKey(idx); Identity fkIdentity = buildIdentityFromFK(table, fk, bean); if ((fkIdentity != null) && !fkIdentity.equals(origIdentity)) { Identity processedIdentity = (Identity)_identityMap.get(fkIdentity); if (processedIdentity != null) { updateFKColumns(bean, fkIdentity.getForeignKeyName(), processedIdentity); } else { waitingObj.addPendingFK(fkIdentity); } } } if (waitingObj.hasPendingFKs()) { if (_log.isDebugEnabled()) { StringBuilder msg = new StringBuilder(); msg.append("Defering insertion of row "); msg.append(buildIdentityFromPKs(table, bean).toString()); msg.append(" because it is waiting for:"); for (Iterator it = waitingObj.getPendingFKs(); it.hasNext();) { msg.append("\n "); msg.append(it.next().toString()); } _log.debug(msg.toString()); } _waitingObjects.add(waitingObj); return; } } insertBeanIntoDatabase(table, bean); if (_log.isDebugEnabled()) { _log.debug("Inserted bean " + origIdentity); } // GemStone changes BEGIN if (_origIdentityMap == null) { populateIdentityMap(table, bean, origIdentity); } else { _origIdentityMap.put(bean, origIdentity); } }
Example 20
Source File: TestDatabaseIO.java From gemfirexd-oss with Apache License 2.0 | 4 votes |
/** * Tests a database model containing a named foreignkey. */ public void testNamedForeignkey() 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 name='The foreignkey' 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); assertEquals("ID", Types.VARCHAR, 16, 0, null, "The primary key", null, true, true, false, someTable.getColumn(0)); Table anotherTable = model.getTable(1); assertEquals("AnotherTable", "And another table", 1, 0, 0, 1, 0, anotherTable); assertEquals("Some_ID", Types.VARCHAR, 16, 0, null, "The foreign key", null, false, false, false, anotherTable.getColumn(0)); ForeignKey fk = anotherTable.getForeignKey(0); assertEquals("The foreignkey", CascadeActionEnum.NONE, CascadeActionEnum.NONE, someTable, 1, fk); assertEquals(anotherTable.getColumn(0), someTable.getColumn(0), fk.getReference(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=\"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\" name=\"The foreignkey\">\n" + " <reference local=\"Some_ID\" foreign=\"ID\" />\n" + " </foreign-key>\n" + " </table>\n" + "</database>\n", model); }