use of org.obeonetwork.dsl.database.ForeignKeyElement in project InformationSystem by ObeoNetwork.
the class DefaultDataBaseBuilder method buildForeignKeys.
protected void buildForeignKeys(Table table) {
Map foreignKeys = new HashMap();
ResultSet rs = null;
try {
rs = metaData.getImportedKeys(null, schemaName, table.getName());
while (rs.next()) {
String pkSchemaName = rs.getString(2);
String pkTableName = rs.getString(3);
String fkName = rs.getString(12);
String pkName = rs.getString(13);
String foreignKeyId = tableContainer.getName() + "::" + table.getName() + "::" + fkName;
ForeignKey foreignKey = (ForeignKey) foreignKeys.get(foreignKeyId);
if (foreignKey == null) {
foreignKey = CreationUtils.createForeignKey(table);
foreignKey.setName(fkName);
foreignKeys.put(foreignKeyId, foreignKey);
}
int keySeq = rs.getInt(9);
ForeignKeyElement foreignKeyElement = CreationUtils.createForeignKeyElement(keySeq, foreignKey);
foreignKey.getElements().add(foreignKeyElement);
String pkColumnName = rs.getString(4);
Column pkColumn = queries.getColumn(tableContainer, pkTableName, pkColumnName);
if (pkColumn != null) {
foreignKeyElement.setPkColumn(pkColumn);
} else {
AbstractTable pkTable = queries.getTable(pkTableName);
if (pkTable == null) {
pkTable = CreationUtils.createTable(tableContainer, pkTableName);
pkColumn = CreationUtils.createColumn(pkTable, pkColumnName);
} else {
pkColumn = queries.getColumn(pkTable, pkColumnName);
}
foreignKeyElement.setPkColumn(pkColumn);
}
String fkSchemaName = rs.getString(6);
String fkTableName = rs.getString(7);
String fkColumnName = rs.getString(8);
Column fkColumn = queries.getColumn(tableContainer, fkTableName, fkColumnName);
foreignKeyElement.setFkColumn(fkColumn);
// Set target table
if (foreignKey.getTarget() == null) {
Table targetTable = (Table) pkColumn.getOwner();
foreignKey.setTarget(targetTable);
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
JdbcUtils.closeResultSet(rs);
}
}
Aggregations