Search in sources :

Example 1 with OnUpdate$

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

the class Decompiler method getForeignKeys.

@SuppressWarnings("null")
// FIXME: This does not support composite foreign keys
Map<String, Map<String, $ForeignKeyUnary>> getForeignKeys(final Connection connection) throws SQLException {
    final DatabaseMetaData metaData = connection.getMetaData();
    try (final ResultSet foreignKeyRows = metaData.getImportedKeys(null, null, null)) {
        final Map<String, Map<String, $ForeignKeyUnary>> tableNameToForeignKeys = new HashMap<>();
        String lastTable = null;
        Map<String, $ForeignKeyUnary> columnNameToForeignKey = null;
        while (foreignKeyRows.next()) {
            final String tableName = foreignKeyRows.getString("FKTABLE_NAME").toLowerCase();
            if (!tableName.equals(lastTable)) {
                lastTable = tableName;
                tableNameToForeignKeys.put(tableName, columnNameToForeignKey = new HashMap<>());
            }
            final String primaryTable = foreignKeyRows.getString("PKTABLE_NAME").toLowerCase();
            final String primaryColumn = foreignKeyRows.getString("PKCOLUMN_NAME").toLowerCase();
            final String columnName = foreignKeyRows.getString("FKCOLUMN_NAME").toLowerCase();
            final short updateRule = foreignKeyRows.getShort("UPDATE_RULE");
            final short deleteRule = foreignKeyRows.getShort("DELETE_RULE");
            final $ForeignKeyUnary foreignKey = new $Column.ForeignKey();
            foreignKey.setReferences$(new References$(primaryTable));
            foreignKey.setColumn$(new Column$(primaryColumn));
            final $ChangeRule.Enum onUpdate = toBinding(updateRule);
            if (onUpdate != null)
                foreignKey.setOnUpdate$(new OnUpdate$(onUpdate));
            final $ChangeRule.Enum onDelete = toBinding(deleteRule);
            if (onDelete != null)
                foreignKey.setOnDelete$(new OnDelete$(onDelete));
            columnNameToForeignKey.put(columnName, 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) DatabaseMetaData(java.sql.DatabaseMetaData) 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$) Column$(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKeyUnary.Column$) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Example 2 with OnUpdate$

use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKey.OnUpdate$ 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

ResultSet (java.sql.ResultSet)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ChangeRule (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ChangeRule)2 OnDelete$ (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKey.OnDelete$)2 OnUpdate$ (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKey.OnUpdate$)2 References$ (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKey.References$)2 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKeyUnary (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKeyUnary)2 Column$ (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKeyUnary.Column$)2 DatabaseMetaData (java.sql.DatabaseMetaData)1 PreparedStatement (java.sql.PreparedStatement)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1