Search in sources :

Example 11 with ForeignKeyElement

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);
    }
}
Also used : ForeignKeyElement(org.obeonetwork.dsl.database.ForeignKeyElement) AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Table(org.obeonetwork.dsl.database.Table) AbstractTable(org.obeonetwork.dsl.database.AbstractTable) HashMap(java.util.HashMap) Column(org.obeonetwork.dsl.database.Column) ResultSet(java.sql.ResultSet) ForeignKey(org.obeonetwork.dsl.database.ForeignKey) HashMap(java.util.HashMap) Map(java.util.Map) SQLException(java.sql.SQLException)

Aggregations

ForeignKeyElement (org.obeonetwork.dsl.database.ForeignKeyElement)11 ForeignKey (org.obeonetwork.dsl.database.ForeignKey)8 Table (org.obeonetwork.dsl.database.Table)7 Column (org.obeonetwork.dsl.database.Column)6 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 EObject (org.eclipse.emf.ecore.EObject)3 Map (java.util.Map)2 IStatus (org.eclipse.core.runtime.IStatus)2 Status (org.eclipse.core.runtime.Status)2 EObjectFlatComboSettings (org.eclipse.emf.eef.runtime.ui.widgets.eobjflatcombo.EObjectFlatComboSettings)2 Viewer (org.eclipse.jface.viewers.Viewer)2 ViewerFilter (org.eclipse.jface.viewers.ViewerFilter)2 AbstractTable (org.obeonetwork.dsl.database.AbstractTable)2 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 List (java.util.List)1 EReference (org.eclipse.emf.ecore.EReference)1 EObjectPropertiesEditionContext (org.eclipse.emf.eef.runtime.context.impl.EObjectPropertiesEditionContext)1