/** * return true if this foreignkey is the only reference from this table to the same foreign table */ private boolean isUniqueReference(ForeignKey foreignKey) { Iterator foreignKeyIterator = foreignKey.getTable().getForeignKeyIterator(); while (foreignKeyIterator.hasNext()) { ForeignKey element = (ForeignKey) foreignKeyIterator.next(); if (element != foreignKey && element.getReferencedTable().equals(foreignKey.getReferencedTable())) { return false; } } return true; }
private void assertForeignKey( String parentTablename, String childTablename, DefaultGrailsDomainConfiguration config) { boolean fkFound = false; Table childTable = getTableMapping(childTablename, config); for (Iterator<?> fks = childTable.getForeignKeyIterator(); fks.hasNext(); ) { ForeignKey fk = (ForeignKey) fks.next(); if (parentTablename.equals(fk.getReferencedTable().getName())) { fkFound = true; } } assertTrue("FK exists " + childTablename + "->" + parentTablename, fkFound); }
@SuppressWarnings("unchecked") public static boolean isForeignKey(Column column) { Table table = getTable(column); if (table != null) { Iterator<ForeignKey> iter = table.getForeignKeyIterator(); while (iter.hasNext()) { ForeignKey fk = iter.next(); if (fk.containsColumn(column)) { return true; } } } return false; }
// bind collections. private void bindIncomingForeignKeys( PersistentClass rc, Set processed, List foreignKeys, Mapping mapping) { if (foreignKeys != null) { for (Iterator iter = foreignKeys.iterator(); iter.hasNext(); ) { ForeignKey foreignKey = (ForeignKey) iter.next(); if (revengStrategy.excludeForeignKeyAsCollection( foreignKey.getName(), TableIdentifier.create(foreignKey.getTable()), foreignKey.getColumns(), TableIdentifier.create(foreignKey.getReferencedTable()), foreignKey.getReferencedColumns())) { log.debug( "Rev.eng excluded one-to-many or one-to-one for foreignkey " + foreignKey.getName()); } else if (revengStrategy.isOneToOne(foreignKey)) { Property property = bindOneToOne(rc, foreignKey.getTable(), foreignKey, processed, false, true); rc.addProperty(property); } else { Property property = bindOneToMany(rc, foreignKey, processed, mapping); rc.addProperty(property); } } } }
private Property bindOneToOne( PersistentClass rc, Table targetTable, ForeignKey fk, Set processedColumns, boolean constrained, boolean inverseProperty) { OneToOne value = new OneToOne(targetTable, rc); value.setReferencedEntityName( revengStrategy.tableToClassName(TableIdentifier.create(targetTable))); boolean isUnique = isUniqueReference(fk); String propertyName = null; if (inverseProperty) { propertyName = revengStrategy.foreignKeyToInverseEntityName( fk.getName(), TableIdentifier.create(fk.getReferencedTable()), fk.getReferencedColumns(), TableIdentifier.create(targetTable), fk.getColumns(), isUnique); } else { propertyName = revengStrategy.foreignKeyToEntityName( fk.getName(), TableIdentifier.create(fk.getReferencedTable()), fk.getReferencedColumns(), TableIdentifier.create(targetTable), fk.getColumns(), isUnique); } Iterator columns = fk.getColumnIterator(); while (columns.hasNext()) { Column fkcolumn = (Column) columns.next(); checkColumn(fkcolumn); value.addColumn(fkcolumn); processedColumns.add(fkcolumn); } value.setFetchMode(FetchMode.SELECT); value.setConstrained(constrained); value.setForeignKeyType( constrained ? ForeignKeyDirection.FOREIGN_KEY_FROM_PARENT : ForeignKeyDirection.FOREIGN_KEY_TO_PARENT); return makeEntityProperty(propertyName, true, targetTable, fk, value, inverseProperty); // return makeProperty(TableIdentifier.create(targetTable), propertyName, value, // true, true, value.getFetchMode() != FetchMode.JOIN, null, null); }
/** * @param mutable * @param table * @param fk * @param columnsToBind * @param processedColumns * @param rc * @param propName */ private Property bindManyToOne( String propertyName, boolean mutable, Table table, ForeignKey fk, Set processedColumns) { ManyToOne value = new ManyToOne(table); value.setReferencedEntityName(fk.getReferencedEntityName()); Iterator columns = fk.getColumnIterator(); while (columns.hasNext()) { Column fkcolumn = (Column) columns.next(); checkColumn(fkcolumn); value.addColumn(fkcolumn); processedColumns.add(fkcolumn); } value.setFetchMode(FetchMode.SELECT); return makeEntityProperty(propertyName, mutable, table, fk, value, false); }
public String[] getCleanSql() { if (cleanSql == null) { // loop over all foreign key constraints List dropForeignKeysSql = new ArrayList(); List createForeignKeysSql = new ArrayList(); Iterator iter = configuration.getTableMappings(); while (iter.hasNext()) { Table table = (Table) iter.next(); if (table.isPhysicalTable()) { Iterator subIter = table.getForeignKeyIterator(); while (subIter.hasNext()) { ForeignKey fk = (ForeignKey) subIter.next(); if (fk.isPhysicalConstraint()) { // collect the drop key constraint dropForeignKeysSql.add( fk.sqlDropString( dialect, properties.getProperty(Environment.DEFAULT_CATALOG), properties.getProperty(Environment.DEFAULT_SCHEMA))); createForeignKeysSql.add( fk.sqlCreateString( dialect, mapping, properties.getProperty(Environment.DEFAULT_CATALOG), properties.getProperty(Environment.DEFAULT_SCHEMA))); } } } } List deleteSql = new ArrayList(); iter = configuration.getTableMappings(); while (iter.hasNext()) { Table table = (Table) iter.next(); deleteSql.add("delete from " + table.getName()); } List cleanSqlList = new ArrayList(); cleanSqlList.addAll(dropForeignKeysSql); cleanSqlList.addAll(deleteSql); cleanSqlList.addAll(createForeignKeysSql); cleanSql = (String[]) cleanSqlList.toArray(new String[cleanSqlList.size()]); } return cleanSql; }
private List columnMatches(Column[] myPkColumns, int offset, ForeignKey key) { if (key.getColumnSpan() > (myPkColumns.length - offset)) { return null; // not enough columns in the key } List columns = new ArrayList(); for (int j = 0; j < key.getColumnSpan(); j++) { Column column = myPkColumns[j + offset]; if (!column.equals(key.getColumn(j))) { return null; } else { columns.add(column); } } return columns.isEmpty() ? null : columns; }
/** * Basically create an [classname]Id.class and add properties for it. * * @param rc * @param compositeKeyColumns * @param processed * @return */ private SimpleValue handleCompositeKey( RootClass rc, Set processedColumns, List keyColumns, Mapping mapping) { Component pkc = new Component(rc); pkc.setMetaAttributes(Collections.EMPTY_MAP); pkc.setEmbedded(false); String compositeIdName = revengStrategy.tableToCompositeIdName(TableIdentifier.create(rc.getTable())); if (compositeIdName == null) { compositeIdName = revengStrategy.classNameToCompositeIdName(rc.getClassName()); } pkc.setComponentClassName(compositeIdName); Table table = rc.getTable(); List list = null; if (cfg.preferBasicCompositeIds()) { list = new ArrayList(keyColumns); } else { list = findForeignKeys(table.getForeignKeyIterator(), keyColumns); } for (Iterator iter = list.iterator(); iter.hasNext(); ) { Object element = iter.next(); Property property; if (element instanceof Column) { Column column = (Column) element; if (processedColumns.contains(column)) { throw new JDBCBinderException( "Binding column twice for primary key should not happen: " + column); } else { checkColumn(column); String propertyName = revengStrategy.columnToPropertyName(TableIdentifier.create(table), column.getName()); property = bindBasicProperty( makeUnique(pkc, propertyName), table, column, processedColumns, mapping); processedColumns.add(column); } } else if (element instanceof ForeignKeyForColumns) { ForeignKeyForColumns fkfc = (ForeignKeyForColumns) element; ForeignKey foreignKey = fkfc.key; String propertyName = revengStrategy.foreignKeyToEntityName( foreignKey.getName(), TableIdentifier.create(foreignKey.getTable()), foreignKey.getColumns(), TableIdentifier.create(foreignKey.getReferencedTable()), foreignKey.getReferencedColumns(), true); property = bindManyToOne(makeUnique(pkc, propertyName), true, table, foreignKey, processedColumns); processedColumns.addAll(fkfc.columns); } else { throw new JDBCBinderException("unknown thing"); } markAsUseInEquals(property); pkc.addProperty(property); } return pkc; }
/** * bind many-to-ones * * @param table * @param rc * @param primaryKey */ private void bindOutgoingForeignKeys(Table table, RootClass rc, Set processedColumns) { // Iterate the outgoing foreign keys and create many-to-one's for (Iterator iterator = table.getForeignKeyIterator(); iterator.hasNext(); ) { ForeignKey foreignKey = (ForeignKey) iterator.next(); boolean mutable = true; if (contains(foreignKey.getColumnIterator(), processedColumns)) { if (!cfg.preferBasicCompositeIds()) continue; // it's in the pk, so skip this one mutable = false; } if (revengStrategy.excludeForeignKeyAsManytoOne( foreignKey.getName(), TableIdentifier.create(foreignKey.getTable()), foreignKey.getColumns(), TableIdentifier.create(foreignKey.getReferencedTable()), foreignKey.getReferencedColumns())) { // TODO: if many-to-one is excluded should the column be marked as processed so it won't // show up at all ? log.debug("Rev.eng excluded *-to-one for foreignkey " + foreignKey.getName()); } else if (revengStrategy.isOneToOne(foreignKey)) { Property property = bindOneToOne( rc, foreignKey.getReferencedTable(), foreignKey, processedColumns, true, false); rc.addProperty(property); } else { boolean isUnique = isUniqueReference(foreignKey); String propertyName = revengStrategy.foreignKeyToEntityName( foreignKey.getName(), TableIdentifier.create(foreignKey.getTable()), foreignKey.getColumns(), TableIdentifier.create(foreignKey.getReferencedTable()), foreignKey.getReferencedColumns(), isUnique); Property property = bindManyToOne( makeUnique(rc, propertyName), mutable, table, foreignKey, processedColumns); rc.addProperty(property); } } }
private String bindCollection( PersistentClass rc, ForeignKey fromForeignKey, ForeignKey toForeignKey, Collection collection) { ForeignKey targetKey = fromForeignKey; String collectionRole = null; boolean collectionLazy = false; boolean collectionInverse = false; TableIdentifier foreignKeyTable = null; String tableToClassName; if (toForeignKey != null) { targetKey = toForeignKey; } boolean uniqueReference = isUniqueReference(targetKey); // TODO: need to look one step further for many-to-many! foreignKeyTable = TableIdentifier.create(targetKey.getTable()); TableIdentifier foreignKeyReferencedTable = TableIdentifier.create(targetKey.getReferencedTable()); if (toForeignKey == null) { collectionRole = revengStrategy.foreignKeyToCollectionName( fromForeignKey.getName(), foreignKeyTable, fromForeignKey.getColumns(), foreignKeyReferencedTable, fromForeignKey.getReferencedColumns(), uniqueReference); tableToClassName = revengStrategy.tableToClassName(foreignKeyTable); } else { collectionRole = revengStrategy.foreignKeyToManyToManyName( fromForeignKey, TableIdentifier.create(fromForeignKey.getTable()), toForeignKey, uniqueReference); tableToClassName = revengStrategy.tableToClassName(foreignKeyReferencedTable); } collectionInverse = revengStrategy.isForeignKeyCollectionInverse( targetKey.getName(), foreignKeyTable, targetKey.getColumns(), foreignKeyReferencedTable, targetKey.getReferencedColumns()); collectionLazy = revengStrategy.isForeignKeyCollectionLazy( targetKey.getName(), foreignKeyTable, targetKey.getColumns(), foreignKeyReferencedTable, targetKey.getReferencedColumns()); collectionRole = makeUnique(rc, collectionRole); String fullRolePath = StringHelper.qualify(rc.getEntityName(), collectionRole); if (mappings.getCollection(fullRolePath) != null) { log.debug(fullRolePath + " found twice!"); } collection.setRole(fullRolePath); // Master.setOfChildren+ collection.setInverse(collectionInverse); // TODO: allow overriding this collection.setLazy(collectionLazy); collection.setFetchMode(FetchMode.SELECT); return tableToClassName; }
/** * @param rc * @param processed * @param table * @param object */ private Property bindOneToMany( PersistentClass rc, ForeignKey foreignKey, Set processed, Mapping mapping) { Table collectionTable = foreignKey.getTable(); // Collection collection = new org.hibernate.mapping.Set(rc); // MASTER TODO: allow overriding // collection type Collection collection = new org.hibernate.mapping.List(rc); // MASTER TODO: allow overriding collection type collection.setCollectionTable(collectionTable); // CHILD+ boolean manyToMany = revengStrategy.isManyToManyTable(collectionTable); if (manyToMany) { // log.debug("Rev.eng said here is a many-to-many"); // TODO: handle "the other side should influence the name" } if (manyToMany) { ManyToOne element = new ManyToOne(collection.getCollectionTable()); // TODO: find the other foreignkey and choose the other side. Iterator foreignKeyIterator = foreignKey.getTable().getForeignKeyIterator(); List keys = new ArrayList(); while (foreignKeyIterator.hasNext()) { Object next = foreignKeyIterator.next(); if (next != foreignKey) { keys.add(next); } } if (keys.size() > 1) { throw new JDBCBinderException( "more than one other foreign key to choose from!"); // todo: handle better ? } ForeignKey fk = (ForeignKey) keys.get(0); String tableToClassName = bindCollection(rc, foreignKey, fk, collection); element.setReferencedEntityName(tableToClassName); element.addColumn(fk.getColumn(0)); collection.setElement(element); } else { String tableToClassName = bindCollection(rc, foreignKey, null, collection); OneToMany oneToMany = new OneToMany(collection.getOwner()); oneToMany.setReferencedEntityName(tableToClassName); // Child mappings.addSecondPass(new JDBCCollectionSecondPass(mappings, collection)); collection.setElement(oneToMany); } // bind keyvalue KeyValue referencedKeyValue; String propRef = collection.getReferencedPropertyName(); if (propRef == null) { referencedKeyValue = collection.getOwner().getIdentifier(); } else { referencedKeyValue = (KeyValue) collection.getOwner().getProperty(propRef).getValue(); } SimpleValue keyValue = new DependantValue(collectionTable, referencedKeyValue); // keyValue.setForeignKeyName("none"); // Avoid creating the foreignkey // key.setCascadeDeleteEnabled( "cascade".equals( subnode.attributeValue("on-delete") ) ); Iterator columnIterator = foreignKey.getColumnIterator(); while (columnIterator.hasNext()) { Column fkcolumn = (Column) columnIterator.next(); if (fkcolumn.getSqlTypeCode() != null) { // TODO: user defined foreign ref columns does not have a type set. guessAndAlignType( collectionTable, fkcolumn, mapping, false); // needed to ensure foreign key columns has same type as the "property" column. } keyValue.addColumn(fkcolumn); } collection.setKey(keyValue); mappings.addCollection(collection); return makeCollectionProperty( StringHelper.unqualify(collection.getRole()), true, rc.getTable(), foreignKey, collection, true); // return makeProperty(TableIdentifier.create( rc.getTable() ), StringHelper.unqualify( // collection.getRole() ), collection, true, true, true, "none", null); // TODO: cascade isn't // all by default }