Search in sources :

Example 36 with Column

use of com.helger.genericode.v10.Column in project jaxdb by jaxdb.

the class Decompiler method createDDL.

public static Schema createDDL(final Connection connection) throws SQLException {
    final DBVendor vendor = DBVendor.valueOf(connection.getMetaData());
    final Decompiler decompiler = Decompiler.getDecompiler(vendor);
    final DatabaseMetaData metaData = connection.getMetaData();
    try (final ResultSet tableRows = metaData.getTables(null, null, null, new String[] { "TABLE" })) {
        final Schema schema = new Schema();
        final Map<String, List<$CheckReference>> tableNameToChecks = decompiler.getCheckConstraints(connection);
        final Map<String, List<$Table.Constraints.Unique>> tableNameToUniques = decompiler.getUniqueConstraints(connection);
        final Map<String, $Table.Indexes> tableNameToIndexes = decompiler.getIndexes(connection);
        final Map<String, Map<String, $ForeignKeyUnary>> tableNameToForeignKeys = decompiler.getForeignKeys(connection);
        final Map<String, $Column> columnNameToColumn = new HashMap<>();
        final Map<Integer, $Column> columnNumberToColumn = new TreeMap<>();
        final Map<String, TreeMap<Short, String>> indexNameToIndex = new HashMap<>();
        final Map<String, String> indexNameToType = new HashMap<>();
        final Map<String, Boolean> indexNameToUnique = new HashMap<>();
        while (tableRows.next()) {
            final String tableName = tableRows.getString(3);
            final $Table table = new Schema.Table();
            table.setName$(new $Named.Name$(tableName.toLowerCase()));
            schema.addTable(table);
            try (final ResultSet columnRows = metaData.getColumns(null, null, tableName, null)) {
                while (columnRows.next()) {
                    final String columnName = columnRows.getString("COLUMN_NAME").toLowerCase();
                    final String typeName = columnRows.getString("TYPE_NAME");
                    final int columnSize = columnRows.getInt("COLUMN_SIZE");
                    final String _default = columnRows.getString("COLUMN_DEF");
                    final int index = columnRows.getInt("ORDINAL_POSITION");
                    final String nullable = columnRows.getString("IS_NULLABLE");
                    final String autoIncrement = columnRows.getString("IS_AUTOINCREMENT");
                    final int decimalDigits = columnRows.getInt("DECIMAL_DIGITS");
                    final $Column column = decompiler.makeColumn(columnName.toLowerCase(), typeName, columnSize, decimalDigits, _default, nullable.length() == 0 ? null : "YES".equals(nullable), autoIncrement.length() == 0 ? null : "YES".equals(autoIncrement));
                    columnNameToColumn.put(columnName, column);
                    columnNumberToColumn.put(index, column);
                }
                columnNumberToColumn.values().forEach(table::addColumn);
                try (final ResultSet primaryKeyRows = metaData.getPrimaryKeys(null, null, tableName)) {
                    while (primaryKeyRows.next()) {
                        final String columnName = primaryKeyRows.getString("COLUMN_NAME").toLowerCase();
                        if (table.getConstraints() == null)
                            table.setConstraints(new $Table.Constraints());
                        if (table.getConstraints().getPrimaryKey() == null)
                            table.getConstraints().setPrimaryKey(new $Table.Constraints.PrimaryKey());
                        final $Table.Constraints.PrimaryKey.Column column = new $Table.Constraints.PrimaryKey.Column();
                        column.setName$(new $Table.Constraints.PrimaryKey.Column.Name$(columnName));
                        table.getConstraints().getPrimaryKey().addColumn(column);
                    }
                }
                final List<$Table.Constraints.Unique> uniques = tableNameToUniques == null ? null : tableNameToUniques.get(tableName);
                if (uniques != null && uniques.size() > 0) {
                    if (table.getConstraints() == null)
                        table.setConstraints(new $Table.Constraints());
                    for (final $Table.Constraints.Unique unique : uniques) table.getConstraints().addUnique(unique);
                }
                try (final ResultSet indexRows = metaData.getIndexInfo(null, null, tableName, false, true)) {
                    while (indexRows.next()) {
                        final String columnName = indexRows.getString("COLUMN_NAME").toLowerCase();
                        if (columnName == null)
                            continue;
                        final String indexName = indexRows.getString("INDEX_NAME").toLowerCase();
                        TreeMap<Short, String> indexes = indexNameToIndex.get(indexName);
                        if (indexes == null)
                            indexNameToIndex.put(indexName, indexes = new TreeMap<>());
                        final short ordinalPosition = indexRows.getShort("ORDINAL_POSITION");
                        indexes.put(ordinalPosition, columnName);
                        final String type = getType(indexRows.getShort("TYPE"));
                        final String currentType = indexNameToType.get(indexName);
                        if (currentType == null)
                            indexNameToType.put(indexName, type);
                        else if (!type.equals(currentType))
                            throw new IllegalStateException("Expected " + type + " = " + currentType);
                        final boolean unique = !indexRows.getBoolean("NON_UNIQUE");
                        final Boolean currentUnique = indexNameToUnique.get(indexName);
                        if (currentUnique == null)
                            indexNameToUnique.put(indexName, unique);
                        else if (unique != currentUnique)
                            throw new IllegalStateException("Expected " + unique + " = " + currentType);
                    }
                }
                final $Table.Indexes indexes = tableNameToIndexes == null ? null : tableNameToIndexes.get(tableName);
                if (indexes != null)
                    table.setIndexes(indexes);
                final List<$CheckReference> checks = tableNameToChecks == null ? null : tableNameToChecks.get(tableName);
                if (checks != null)
                    for (final $CheckReference check : checks) addCheck(columnNameToColumn.get(check.getColumn$().text()), check);
                final Map<String, $ForeignKeyUnary> foreignKeys = tableNameToForeignKeys == null ? null : tableNameToForeignKeys.get(tableName);
                if (foreignKeys != null)
                    for (final Map.Entry<String, $ForeignKeyUnary> entry : foreignKeys.entrySet()) columnNameToColumn.get(entry.getKey().toLowerCase()).setForeignKey(entry.getValue());
            }
            columnNameToColumn.clear();
            columnNumberToColumn.clear();
            indexNameToIndex.clear();
            indexNameToType.clear();
        }
        return schema;
    }
}
Also used : HashMap(java.util.HashMap) Schema(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.Schema) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Named(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Named) ResultSet(java.sql.ResultSet) List(java.util.List) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Boolean(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Boolean) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column) DatabaseMetaData(java.sql.DatabaseMetaData) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Table(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Table) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKeyUnary(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$ForeignKeyUnary) DBVendor(org.jaxdb.vendor.DBVendor) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$CheckReference(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$CheckReference) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$CheckColumn(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$CheckColumn) org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Table(org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Table) TreeMap(java.util.TreeMap) 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)

Example 37 with Column

use of com.helger.genericode.v10.Column in project jaxdb by jaxdb.

the class DerbyDecompiler method getUniqueConstraints.

@Override
@SuppressWarnings("null")
Map<String, List<$Table.Constraints.Unique>> getUniqueConstraints(final Connection connection) throws SQLException {
    final Map<String, List<String>> tableNameToColumns = getTables(connection);
    final PreparedStatement statement = connection.prepareStatement(constraintsSql);
    final ResultSet rows = statement.executeQuery();
    final Map<String, List<$Table.Constraints.Unique>> tableNameToUniques = new HashMap<>();
    String lastTable = null;
    List<$Table.Constraints.Unique> uniques = null;
    while (rows.next()) {
        final String tableName = rows.getString(2);
        if (!tableName.equals(lastTable)) {
            lastTable = tableName;
            tableNameToUniques.put(tableName, uniques = new ArrayList<>());
        }
        final List<String> columns = tableNameToColumns.get(tableName);
        final String descriptor = rows.getString(3);
        final int close = descriptor.lastIndexOf(')');
        final int open = descriptor.lastIndexOf('(', close - 1);
        final String[] colRefs = descriptor.substring(open + 1, close).split(",");
        final $Table.Constraints.Unique unique = new $Table.Constraints.Unique();
        uniques.add(unique);
        for (int i = 0; i < colRefs.length; ++i) {
            colRefs[i] = columns.get(Integer.valueOf(colRefs[i].trim()) - 1);
            final $Table.Constraints.Unique.Column column = new $Table.Constraints.Unique.Column();
            column.setName$(new $Table.Constraints.Unique.Column.Name$(colRefs[i].toLowerCase()));
            unique.addColumn(column);
        }
    }
    return tableNameToUniques;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) 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) 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 38 with Column

use of com.helger.genericode.v10.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 39 with Column

use of com.helger.genericode.v10.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 40 with Column

use of com.helger.genericode.v10.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)

Aggregations

org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Column)21 ArrayList (java.util.ArrayList)17 HashMap (java.util.HashMap)11 Column (org.molgenis.emx2.Column)11 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Bigint (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Bigint)10 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Smallint (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Smallint)10 Column (com.google.bigtable.v2.Column)9 List (java.util.List)9 Family (com.google.bigtable.v2.Family)8 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Tinyint (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Tinyint)8 Test (org.junit.Test)8 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Table (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Table)7 MolgenisException (org.molgenis.emx2.MolgenisException)6 Map (java.util.Map)4 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Enum (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Enum)4 org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Integer (org.jaxdb.www.ddlx_0_5.xLygluGCXAA.$Integer)4 SqlColumnExecutor.validateColumn (org.molgenis.emx2.sql.SqlColumnExecutor.validateColumn)4 Cell (com.google.bigtable.v2.Cell)3 ResultSet (java.sql.ResultSet)3 ExecutionException (java.util.concurrent.ExecutionException)3