Search in sources :

Example 26 with Column$

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

Example 27 with Column$

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

the class DerbyDecompiler method getIndexes.

@Override
@SuppressWarnings("null")
Map<String, $Table.Indexes> getIndexes(final Connection connection) throws SQLException {
    final Map<String, List<String>> tableNameToColumns = getTables(connection);
    final PreparedStatement statement = connection.prepareStatement(indexSql);
    final ResultSet rows = statement.executeQuery();
    final Map<String, $Table.Indexes> tableNameToIndexes = new HashMap<>();
    String lastTable = null;
    $Table.Indexes indexes = null;
    while (rows.next()) {
        final String tableName = rows.getString(2);
        final List<String> columnNames = tableNameToColumns.get(tableName);
        if (!tableName.equals(lastTable)) {
            lastTable = tableName;
            tableNameToIndexes.put(tableName, indexes = new $Table.Indexes());
        }
        final String descriptor = rows.getString(3);
        final boolean unique = descriptor.startsWith("UNIQUE");
        final $IndexType.Enum type = descriptor.startsWith("HASH") ? $Index.Type$.HASH : $Index.Type$.BTREE;
        final $Table.Indexes.Index index = new $Table.Indexes.Index();
        indexes.addIndex(index);
        if (!$Index.Type$.BTREE.equals(type))
            index.setType$(new $Index.Type$(type));
        if (unique)
            index.setUnique$(new $Index.Unique$(unique));
        final String[] columnNumbers = descriptor.substring(descriptor.lastIndexOf('(') + 1, descriptor.lastIndexOf(')')).split(",");
        for (final String columnNumber : columnNumbers) {
            final String columnName = columnNames.get(Integer.valueOf(columnNumber.trim()) - 1);
            final $Table.Indexes.Index.Column column = new $Table.Indexes.Index.Column();
            column.setName$(new $Table.Indexes.Index.Column.Name$(columnName.toLowerCase()));
            index.addColumn(column);
        }
    }
    return tableNameToIndexes;
}
Also used : HashMap(java.util.HashMap) PreparedStatement(java.sql.PreparedStatement) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Index(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Index) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$IndexType(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$IndexType) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Table(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Table) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List)

Example 28 with Column$

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

the class Generator method registerColumns.

private static void registerColumns(final $Table table, final Set<? super String> tableNames, final Map<? super String, ColumnRef> columnNameToColumn) throws GeneratorExecutionException {
    final String tableName = table.getName$().text();
    final List<String> violations = new ArrayList<>();
    String nameViolation = checkNameViolation(tableName);
    if (nameViolation != null)
        violations.add(nameViolation);
    if (tableNames.contains(tableName))
        throw new GeneratorExecutionException("Circular table dependency detected: " + tableName);
    tableNames.add(tableName);
    final List<$Column> columns = table.getColumn();
    if (columns != null) {
        for (int c = 0, len = columns.size(); c < len; ++c) {
            final $Column column = columns.get(c);
            final String columnName = column.getName$().text();
            nameViolation = checkNameViolation(columnName);
            if (nameViolation != null)
                violations.add(nameViolation);
            final ColumnRef existing = columnNameToColumn.get(columnName);
            if (existing != null)
                throw new GeneratorExecutionException("Duplicate column definition: " + tableName + "." + columnName);
            columnNameToColumn.put(columnName, new ColumnRef(column, c));
        }
    }
    if (violations.size() > 0)
        violations.forEach(logger::warn);
}
Also used : ArrayList(java.util.ArrayList) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column)

Example 29 with Column$

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

the class Generator method parse.

public LinkedHashSet<Statement> parse(final DBVendor vendor) throws GeneratorExecutionException {
    final Map<String, LinkedHashSet<DropStatement>> dropTableStatements = new HashMap<>();
    final Map<String, LinkedHashSet<DropStatement>> dropTypeStatements = new HashMap<>();
    final Map<String, LinkedHashSet<CreateStatement>> createTableStatements = new HashMap<>();
    final Schema normalized = ddlx.getNormalizedSchema();
    // The following code resolves a problem with ENUM types. The DDLx is generated from merged schema, whereby the original owner
    // of the ENUM type is lost. The jSQL, however, is generated from the normalized schema, where the owner of the ENUM type is
    // present. The `tableNameToEnumToOwner` variable is a map for each table linking each table's ENUMs to their original owners.
    final Map<String, $Table> tableNameToTable = new HashMap<>();
    final Map<String, Map<String, String>> tableNameToEnumToOwner = new HashMap<String, Map<String, String>>() {

        @Override
        public Map<String, String> get(final Object key) {
            final String tableName = (String) key;
            Map<String, String> map = super.get(key);
            if (map == null)
                put(tableName, map = new HashMap<>());
            return map;
        }
    };
    for (final $Table table : normalized.getTable()) tableNameToTable.put(table.getName$().text(), table);
    for ($Table table : normalized.getTable()) {
        if (table.getAbstract$().text())
            continue;
        final Map<String, String> colNameToOwnerTable = tableNameToEnumToOwner.get(table.getName$().text());
        do {
            for (final $Column column : table.getColumn()) if (column instanceof $Enum)
                colNameToOwnerTable.put(column.getName$().text(), table.getName$().text());
            table = table.getExtends$() != null ? tableNameToTable.get(table.getExtends$().text()) : null;
        } while (table != null);
    }
    final Set<String> skipTables = new HashSet<>();
    final Schema merged = ddlx.getMergedSchema();
    final List<$Table> tables = merged.getTable();
    for (final $Table table : tables) {
        if (table.getSkip$().text()) {
            skipTables.add(table.getName$().text());
        } else if (!table.getAbstract$().text()) {
            dropTableStatements.put(table.getName$().text(), Compiler.getCompiler(vendor).dropTable(table));
            dropTypeStatements.put(table.getName$().text(), Compiler.getCompiler(vendor).dropTypes(table, tableNameToEnumToOwner));
        }
    }
    final Set<String> tableNames = new HashSet<>();
    for (final $Table table : tables) if (!table.getAbstract$().text())
        createTableStatements.put(table.getName$().text(), parseTable(vendor, table, tableNames, tableNameToEnumToOwner));
    final LinkedHashSet<Statement> statements = new LinkedHashSet<>();
    final CreateStatement createSchema = Compiler.getCompiler(vendor).createSchemaIfNotExists(merged);
    if (createSchema != null)
        statements.add(createSchema);
    final ListIterator<$Table> listIterator = tables.listIterator(tables.size());
    while (listIterator.hasPrevious()) {
        final $Table table = listIterator.previous();
        final String tableName = table.getName$().text();
        if (!skipTables.contains(tableName) && !table.getAbstract$().text())
            statements.addAll(dropTableStatements.get(tableName));
    }
    for (final $Table table : tables) {
        final String tableName = table.getName$().text();
        if (!skipTables.contains(tableName) && !table.getAbstract$().text())
            statements.addAll(dropTypeStatements.get(tableName));
    }
    for (final $Table table : tables) {
        final String tableName = table.getName$().text();
        if (!skipTables.contains(tableName) && !table.getAbstract$().text())
            statements.addAll(createTableStatements.get(tableName));
    }
    return statements;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) Schema(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.Schema) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Table(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Table) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Enum(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Enum) HashMap(java.util.HashMap) Map(java.util.Map) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 30 with Column$

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

the class OracleCompiler method dropTypes.

@Override
LinkedHashSet<DropStatement> dropTypes(final $Table table, final Map<String, Map<String, String>> tableNameToEnumToOwner) {
    final LinkedHashSet<DropStatement> statements = super.dropTypes(table, tableNameToEnumToOwner);
    if (table.getColumn() != null) {
        for (final $Column column : table.getColumn()) {
            if (column instanceof $Integer) {
                final $Integer type = ($Integer) column;
                if (Generator.isAuto(type)) {
                    statements.add(new DropStatement("BEGIN EXECUTE IMMEDIATE 'DROP SEQUENCE " + q(getSequenceName(table, type)) + "'; EXCEPTION WHEN OTHERS THEN IF SQLCODE != -2289 THEN RAISE; END IF; END;"));
                    statements.add(new DropStatement("BEGIN EXECUTE IMMEDIATE 'DROP TRIGGER " + q(getTriggerName(table, type)) + "'; EXCEPTION WHEN OTHERS THEN IF SQLCODE != -4080 THEN RAISE; END IF; END;"));
                }
            }
        }
    }
    return statements;
}
Also used : org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Integer(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Integer) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column)

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