Search in sources :

Example 6 with Column$

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

the class Compiler method indexes.

List<CreateStatement> indexes(final $Table table, final Map<String, ColumnRef> columnNameToColumn) {
    final List<CreateStatement> statements = new ArrayList<>();
    if (table.getIndexes() != null) {
        for (final $Table.Indexes.Index index : table.getIndexes().getIndex()) {
            final List<$Named> columns = index.getColumn();
            final int[] columnIndexes = new int[columns.size()];
            for (int c = 0, len = columns.size(); c < len; ++c) {
                final $Named column = columns.get(c);
                columnIndexes[c] = columnNameToColumn.get(column.getName$().text()).index;
            }
            final CreateStatement createIndex = createIndex(index.getUnique$() != null && index.getUnique$().text(), getIndexName(table, index, columnIndexes), index.getType$(), table.getName$().text(), index.getColumn().toArray(new $Named[index.getColumn().size()]));
            if (createIndex != null)
                statements.add(createIndex);
        }
    }
    if (table.getColumn() != null) {
        final List<$Column> columns = table.getColumn();
        for (int c = 0, len = columns.size(); c < len; ++c) {
            final $Column column = columns.get(c);
            if (column.getIndex() != null) {
                final CreateStatement createIndex = createIndex(column.getIndex().getUnique$() != null && column.getIndex().getUnique$().text(), getIndexName(table, column.getIndex(), c), column.getIndex().getType$(), table.getName$().text(), column);
                if (createIndex != null)
                    statements.add(createIndex);
            }
        }
    }
    return statements;
}
Also used : org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Named(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Named) ArrayList(java.util.ArrayList) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Tinyint(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Tinyint) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Bigint(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Bigint) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Smallint(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Smallint) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column)

Example 7 with Column$

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

the class DDLx method topologicalSort.

private static Schema topologicalSort(final Schema schema) {
    final List<$Table> tables = new ArrayList<>(schema.getTable());
    schema.getTable().clear();
    tables.sort(tableNameComparator);
    final RefDigraph<$Table, String> digraph = new RefDigraph<>(table -> table.getName$().text().toLowerCase());
    for (final $Table table : tables) {
        digraph.add(table);
        if (table.getColumn() != null)
            for (final $Column column : table.getColumn()) if (column.getForeignKey() != null)
                digraph.add(table, column.getForeignKey().getReferences$().text().toLowerCase());
        if (table.getConstraints() != null && table.getConstraints().getForeignKey() != null)
            for (final $ForeignKeyComposite foreignKey : table.getConstraints().getForeignKey()) digraph.add(table, foreignKey.getReferences$().text().toLowerCase());
    }
    if (digraph.hasCycle())
        throw new IllegalStateException("Cycle exists in relational model: " + CollectionUtil.toString(digraph.getCycle(), " -> "));
    final List<$Table> topologialOrder = digraph.getTopologicalOrder();
    final ListIterator<$Table> topological = topologialOrder.listIterator(digraph.size());
    while (topological.hasPrevious()) schema.getTable().add(topological.previous());
    return schema;
}
Also used : org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Table(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Table) RefDigraph(org.libj.util.RefDigraph) ArrayList(java.util.ArrayList) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKeyComposite(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKeyComposite) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column)

Example 8 with Column$

use of org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKeyUnary.Column$ 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 9 with Column$

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

the class DerbyDecompiler method getCheckConstraints.

@Override
@SuppressWarnings("null")
Map<String, List<$CheckReference>> getCheckConstraints(final Connection connection) throws SQLException {
    final PreparedStatement statement = connection.prepareStatement(checkSql);
    final ResultSet rows = statement.executeQuery();
    final Map<String, List<$CheckReference>> tableNameToChecks = new HashMap<>();
    String lastTable = null;
    List<$CheckReference> checks = null;
    while (rows.next()) {
        final String tableName = rows.getString(2);
        if (!tableName.equals(lastTable)) {
            lastTable = tableName;
            tableNameToChecks.put(tableName, checks = new ArrayList<>());
        }
        final String checkDefinition = rows.getString(3);
        final String referencedColumns = rows.getString(4);
        if (referencedColumns.contains(","))
            throw new UnsupportedOperationException("Only support single-column check constraints");
        checks.add(makeCheck(checkDefinition));
    }
    return tableNameToChecks;
}
Also used : HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) ArrayList(java.util.ArrayList) List(java.util.List) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$CheckReference(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$CheckReference)

Example 10 with Column$

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

the class DerbyDecompiler method makeColumn.

@Override
$Column makeColumn(final String columnName, final String typeName, final long size, final int decimalDigits, final String _default, final Boolean nullable, final Boolean autoIncrement) {
    final $Column column;
    if ("BIGINT".equals(typeName)) {
        final $Bigint type = newColumn($Bigint.class);
        // type.setPrecision$(new $Bigint.Precision$((byte)size));
        if (_default != null && !"GENERATED_BY_DEFAULT".equals(_default))
            type.setDefault$(new $Bigint.Default$(Long.valueOf(getDefault(_default))));
        if (autoIncrement != null && autoIncrement)
            type.setGenerateOnInsert$(new $Bigint.GenerateOnInsert$($Integer.GenerateOnInsert$.AUTO_5FINCREMENT));
        column = type;
    } else if ("CHAR () FOR BIT DATA".equals(typeName) || "VARCHAR () FOR BIT DATA".equals(typeName)) {
        final $Binary type = newColumn($Binary.class);
        if (typeName.startsWith("VARCHAR"))
            type.setVarying$(new $Binary.Varying$(true));
        type.setLength$(new $Binary.Length$(size));
        column = type;
    } else if ("BLOB".equals(typeName)) {
        final $Blob type = newColumn($Blob.class);
        type.setLength$(new $Blob.Length$(size));
        column = type;
    } else if ("BOOLEAN".equals(typeName)) {
        final $Boolean type = newColumn($Boolean.class);
        if (_default != null)
            type.setDefault$(new $Boolean.Default$(Boolean.valueOf(_default)));
        column = type;
    } else if ("VARCHAR".equals(typeName) || "CHAR".equals(typeName)) {
        final $Char type = newColumn($Char.class);
        if ("VARCHAR".equals(typeName))
            type.setVarying$(new $Char.Varying$(true));
        type.setLength$(new $Char.Length$(size));
        if (_default != null)
            type.setDefault$(new $Char.Default$(_default.substring(1, _default.length() - 1)));
        column = type;
    } else if ("CLOB".equals(typeName)) {
        final $Clob type = newColumn($Clob.class);
        type.setLength$(new $Clob.Length$(size));
        column = type;
    } else if ("DATE".equals(typeName)) {
        final $Date type = newColumn($Date.class);
        if (_default != null)
            type.setDefault$(new $Date.Default$(_default.substring(1, _default.length() - 1)));
        column = type;
    } else if ("TIMESTAMP".equals(typeName)) {
        final $Datetime type = newColumn($Datetime.class);
        // type.setPrecision$(new $Datetime.Precision$((byte)size));
        if (_default != null)
            type.setDefault$(new $Datetime.Default$(_default.substring(1, _default.length() - 1)));
        column = type;
    } else if ("DECIMAL".equals(typeName)) {
        final int precision = (int) size;
        final $Decimal type = newColumn($Decimal.class);
        type.setPrecision$(new $Decimal.Precision$(precision));
        type.setScale$(new $Decimal.Scale$(decimalDigits));
        if (_default != null)
            type.setDefault$(new $Decimal.Default$(new BigDecimal(_default)));
        column = type;
    } else if ("DOUBLE".equals(typeName)) {
        final $Double type = newColumn($Double.class);
        if (_default != null)
            type.setDefault$(new $Double.Default$(Double.valueOf(_default)));
        column = type;
    } else // }
    if ("FLOAT".equals(typeName)) {
        final $Float type = newColumn($Float.class);
        if (_default != null)
            type.setDefault$(new $Float.Default$(Float.valueOf(_default)));
        column = type;
    } else if ("INTEGER".equals(typeName)) {
        final $Int type = newColumn($Int.class);
        type.setPrecision$(new $Int.Precision$((byte) size));
        if (_default != null && !"GENERATED_BY_DEFAULT".equals(_default))
            type.setDefault$(new $Int.Default$(Integer.valueOf(getDefault(_default))));
        if (autoIncrement != null && autoIncrement)
            type.setGenerateOnInsert$(new $Int.GenerateOnInsert$($Integer.GenerateOnInsert$.AUTO_5FINCREMENT));
        column = type;
    } else if ("SMALLINT".equals(typeName)) {
        final $Smallint type = newColumn($Smallint.class);
        type.setPrecision$(new $Smallint.Precision$((byte) size));
        if (_default != null && !"GENERATED_BY_DEFAULT".equals(_default))
            type.setDefault$(new $Smallint.Default$(Short.valueOf(getDefault(_default))));
        if (autoIncrement != null && autoIncrement)
            type.setGenerateOnInsert$(new $Smallint.GenerateOnInsert$($Integer.GenerateOnInsert$.AUTO_5FINCREMENT));
        column = type;
    } else if ("TIME".equals(typeName)) {
        final $Time type = newColumn($Time.class);
        type.setPrecision$(new $Time.Precision$((byte) size));
        if (_default != null)
            type.setDefault$(new $Time.Default$(_default.substring(1, _default.length() - 1)));
        column = type;
    } else // else if ("TINYINT".equals(typeName)) {
    // final $Tinyint type = newColumn($Tinyint.class);
    // type.setPrecision$(new $Tinyint.Precision$((byte)size));
    // if (_default != null && !"GENERATED_BY_DEFAULT".equals(_default))
    // type.setDefault$(new $Tinyint.setDefault$(new BigInteger(getDefault(_default))));
    // 
    // if (autoIncrement != null && autoIncrement)
    // type.GenerateOnInsert$(new $Integer.GenerateOnInsert$($Integer.GenerateOnInsert$.AUTO_5FINCREMENT));
    // 
    // column = type;
    // }
    {
        throw new UnsupportedOperationException("Unsupported column type: " + typeName);
    }
    column.setName$(new $Column.Name$(columnName));
    if (nullable != null && !nullable)
        column.setNull$(new $Column.Null$(false));
    return column;
}
Also used : org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Char(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Char) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Decimal(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Decimal) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Bigint(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Bigint) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Boolean(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Boolean) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Clob(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Clob) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Datetime(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Datetime) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Smallint(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Smallint) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Float(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Float) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Date(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Date) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Double(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Double) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Smallint(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Smallint) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Bigint(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Bigint) BigDecimal(java.math.BigDecimal) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Time(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Time) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Int(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Int) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Blob(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Blob) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Binary(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Binary)

Aggregations

org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column)21 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Bigint (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Bigint)16 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Smallint (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Smallint)16 ArrayList (java.util.ArrayList)13 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Tinyint (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Tinyint)13 HashMap (java.util.HashMap)9 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Integer (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Integer)9 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Decimal (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Decimal)8 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Enum (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Enum)8 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Table (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Table)8 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Boolean (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Boolean)7 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Char (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Char)7 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Double (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Double)7 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Float (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Float)7 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Named (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Named)7 ResultSet (java.sql.ResultSet)6 List (java.util.List)6 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Binary (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Binary)6 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Blob (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Blob)6 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Clob (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Clob)6