Search in sources :

Example 6 with org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKeyUnary

use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKeyUnary in project jaxdb by jaxdb.

the class DerbyDecompiler method getForeignKeys.

@Override
@SuppressWarnings("null")
Map<String, Map<String, $ForeignKeyUnary>> getForeignKeys(final Connection connection) throws SQLException {
    final Map<String, List<String>> tableNameToColumns = getTables(connection);
    final PreparedStatement statement = connection.prepareStatement(foreignKeySql);
    final ResultSet rows = statement.executeQuery();
    final Map<String, Map<String, $ForeignKeyUnary>> tableNameToForeignKeys = new HashMap<>();
    String lastTable = null;
    Map<String, $ForeignKeyUnary> columnNameToForeignKey = null;
    while (rows.next()) {
        final String tableName = rows.getString(2);
        final List<String> columnNames = tableNameToColumns.get(tableName);
        if (!tableName.equals(lastTable)) {
            lastTable = tableName;
            tableNameToForeignKeys.put(tableName, columnNameToForeignKey = new HashMap<>());
        }
        final String primaryTable = rows.getString(6);
        final String primaryDescriptor = rows.getString(7);
        final String primaryColumn = tableNameToColumns.get(primaryTable).get(Integer.valueOf(primaryDescriptor.substring(primaryDescriptor.lastIndexOf('(') + 1, primaryDescriptor.lastIndexOf(')'))) - 1);
        final $ForeignKeyUnary foreignKey = new $Column.ForeignKey();
        foreignKey.setReferences$(new References$(primaryTable.toLowerCase()));
        foreignKey.setColumn$(new Column$(primaryColumn.toLowerCase()));
        final String deleteRule = rows.getString(4);
        final $ChangeRule.Enum onDelete = deleteRule == null ? null : "S".equals(deleteRule) ? $ChangeRule.RESTRICT : "C".equals(deleteRule) ? $ChangeRule.CASCADE : "U".equals(deleteRule) ? $ChangeRule.SET_20NULL : null;
        if (onDelete != null)
            foreignKey.setOnDelete$(new OnDelete$(onDelete));
        final String updateRule = rows.getString(5);
        final $ChangeRule.Enum onUpdate = updateRule == null ? null : "S".equals(updateRule) ? $ChangeRule.RESTRICT : null;
        if (onUpdate != null)
            foreignKey.setOnUpdate$(new OnUpdate$(onUpdate));
        final String foreignDescriptor = rows.getString(3);
        final String foreignColumn = columnNames.get(Integer.valueOf(foreignDescriptor.substring(foreignDescriptor.lastIndexOf('(') + 1, foreignDescriptor.lastIndexOf(')'))) - 1);
        columnNameToForeignKey.put(foreignColumn, foreignKey);
    }
    return tableNameToForeignKeys;
}
Also used : OnUpdate$(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKey.OnUpdate$) HashMap(java.util.HashMap) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKeyUnary(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKeyUnary) PreparedStatement(java.sql.PreparedStatement) References$(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKey.References$) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ChangeRule(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ChangeRule) ResultSet(java.sql.ResultSet) OnDelete$(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKey.OnDelete$) ArrayList(java.util.ArrayList) List(java.util.List) Column$(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKeyUnary.Column$) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Bigint (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Bigint)4 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKeyUnary (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKeyUnary)4 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Named (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Named)4 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Smallint (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Smallint)4 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Tinyint (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Tinyint)4 ResultSet (java.sql.ResultSet)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Boolean (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Boolean)3 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Char (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Char)3 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Decimal (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Decimal)3 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Double (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Double)3 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Float (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Float)3 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Int (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Int)3 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Integer (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Integer)3 DatabaseMetaData (java.sql.DatabaseMetaData)2 List (java.util.List)2 TreeMap (java.util.TreeMap)2 GeneratorExecutionException (org.jaxdb.ddlx.GeneratorExecutionException)2 org.jaxdb.jsql.data (org.jaxdb.jsql.data)2