Search in sources :

Example 46 with TableDefinition

use of org.jooq.meta.TableDefinition in project jOOQ by jOOQ.

the class DerbyDatabase method getTables0.

@Override
protected List<TableDefinition> getTables0() throws SQLException {
    List<TableDefinition> result = new ArrayList<>();
    for (Record record : create().select(SYSTABLES.sysschemas().SCHEMANAME, SYSTABLES.TABLENAME, SYSTABLES.TABLEID, when(SYSTABLES.TABLETYPE.eq(inline("V")), inline(TableType.VIEW.name())).else_(inline(TableType.TABLE.name())).as("table_type"), SYSVIEWS.VIEWDEFINITION).from(SYSTABLES).leftJoin(SYSVIEWS).on(SYSTABLES.TABLEID.eq(SYSVIEWS.TABLEID)).where(SYSTABLES.sysschemas().SCHEMANAME.cast(VARCHAR(32672)).in(getInputSchemata())).orderBy(SYSTABLES.sysschemas().SCHEMANAME, SYSTABLES.TABLENAME)) {
        SchemaDefinition schema = getSchema(record.get(SYSTABLES.sysschemas().SCHEMANAME));
        String name = record.get(SYSTABLES.TABLENAME);
        String id = record.get(SYSTABLES.TABLEID);
        TableType tableType = record.get("table_type", TableType.class);
        String source = record.get(SYSVIEWS.VIEWDEFINITION);
        DerbyTableDefinition table = new DerbyTableDefinition(schema, name, id, tableType, source);
        result.add(table);
    }
    return result;
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) TableType(org.jooq.TableOptions.TableType) ArrayList(java.util.ArrayList) TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record)

Example 47 with TableDefinition

use of org.jooq.meta.TableDefinition in project jOOQ by jOOQ.

the class CUBRIDDatabase method getEnums0.

@Override
protected List<EnumDefinition> getEnums0() throws SQLException {
    List<EnumDefinition> result = new ArrayList<>();
    for (TableDefinition tableDefinition : getTables(getSchemata().get(0))) {
        for (Record record : create().fetch("SHOW COLUMNS FROM {0} WHERE TYPE LIKE 'ENUM(%)'", field(name(tableDefinition.getInputName())))) {
            String table = tableDefinition.getInputName();
            String column = record.get("Field", String.class);
            String columnType = record.get("Type", String.class);
            String name = table + "_" + column;
            ColumnDefinition columnDefinition = tableDefinition.getColumn(column);
            // are explicitly forced to another type
            if (getConfiguredForcedType(columnDefinition) == null) {
                DefaultEnumDefinition definition = new DefaultEnumDefinition(getSchemata().get(0), name, "");
                for (String string : columnType.replaceAll("ENUM\\(|\\)", "").split(",")) {
                    definition.addLiteral(string.trim().replaceAll("'", ""));
                }
                result.add(definition);
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) TableDefinition(org.jooq.meta.TableDefinition) DefaultEnumDefinition(org.jooq.meta.DefaultEnumDefinition) Record(org.jooq.Record) DefaultEnumDefinition(org.jooq.meta.DefaultEnumDefinition) EnumDefinition(org.jooq.meta.EnumDefinition) ColumnDefinition(org.jooq.meta.ColumnDefinition)

Example 48 with TableDefinition

use of org.jooq.meta.TableDefinition in project jOOQ by jOOQ.

the class CUBRIDDatabase method loadUniqueKeys.

@Override
protected void loadUniqueKeys(DefaultRelations relations) throws SQLException {
    for (Record record : fetchKeys(DB_INDEX.IS_PRIMARY_KEY.isTrue())) {
        String key = record.get("constraint_name", String.class);
        String tableName = record.get(DB_CLASS.CLASS_NAME);
        String columnName = record.get(DB_INDEX_KEY.KEY_ATTR_NAME);
        TableDefinition table = getTable(getSchemata().get(0), tableName);
        if (table != null)
            relations.addPrimaryKey(key, table, table.getColumn(columnName));
    }
}
Also used : TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record)

Example 49 with TableDefinition

use of org.jooq.meta.TableDefinition in project jOOQ by jOOQ.

the class CUBRIDDatabase method loadForeignKeys.

@Override
protected void loadForeignKeys(final DefaultRelations relations) throws SQLException {
    create().connection(connection -> {
        DatabaseMetaData meta = connection.getMetaData();
        for (String table : create().selectDistinct(DB_INDEX.CLASS_NAME).from(DB_INDEX).where(DB_INDEX.IS_FOREIGN_KEY.isTrue()).collect(intoList())) {
            for (Record record : create().fetch(meta.getImportedKeys(null, null, table))) {
                String foreignKeyName = record.get("FKTABLE_NAME", String.class) + "__" + record.get("FK_NAME", String.class);
                String foreignKeyTableName = record.get("FKTABLE_NAME", String.class);
                String foreignKeyColumnName = record.get("FKCOLUMN_NAME", String.class);
                String uniqueKeyName = record.get("PKTABLE_NAME", String.class) + "__" + record.get("PK_NAME", String.class);
                String uniqueKeyTableName = record.get("PKTABLE_NAME", String.class);
                TableDefinition foreignKeyTable = getTable(getSchemata().get(0), foreignKeyTableName);
                TableDefinition uniqueKeyTable = getTable(getSchemata().get(0), uniqueKeyTableName);
                if (foreignKeyTable != null && uniqueKeyTable != null)
                    relations.addForeignKey(foreignKeyName, foreignKeyTable, foreignKeyTable.getColumn(foreignKeyColumnName), uniqueKeyName, uniqueKeyTable);
            }
        }
    });
}
Also used : TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 50 with TableDefinition

use of org.jooq.meta.TableDefinition in project jOOQ by jOOQ.

the class CUBRIDDatabase method loadPrimaryKeys.

@Override
protected void loadPrimaryKeys(DefaultRelations relations) throws SQLException {
    for (Record record : fetchKeys(DB_INDEX.IS_UNIQUE.isTrue().and(DB_INDEX.IS_PRIMARY_KEY.isFalse()))) {
        String key = record.get("constraint_name", String.class);
        String tableName = record.get(DB_CLASS.CLASS_NAME);
        String columnName = record.get(DB_INDEX_KEY.KEY_ATTR_NAME);
        TableDefinition table = getTable(getSchemata().get(0), tableName);
        if (table != null)
            relations.addUniqueKey(key, table, table.getColumn(columnName));
    }
}
Also used : TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record)

Aggregations

TableDefinition (org.jooq.meta.TableDefinition)67 SchemaDefinition (org.jooq.meta.SchemaDefinition)47 Record (org.jooq.Record)44 ArrayList (java.util.ArrayList)23 IndexColumnDefinition (org.jooq.meta.IndexColumnDefinition)16 IndexDefinition (org.jooq.meta.IndexDefinition)14 List (java.util.List)12 ColumnDefinition (org.jooq.meta.ColumnDefinition)12 IOException (java.io.IOException)10 DefaultIndexColumnDefinition (org.jooq.meta.DefaultIndexColumnDefinition)10 Result (org.jooq.Result)9 CatalogDefinition (org.jooq.meta.CatalogDefinition)9 EnumDefinition (org.jooq.meta.EnumDefinition)9 RoutineDefinition (org.jooq.meta.RoutineDefinition)9 Arrays.asList (java.util.Arrays.asList)8 TableType (org.jooq.TableOptions.TableType)8 AbstractIndexDefinition (org.jooq.meta.AbstractIndexDefinition)8 DefaultCheckConstraintDefinition (org.jooq.meta.DefaultCheckConstraintDefinition)8 PackageDefinition (org.jooq.meta.PackageDefinition)8 SequenceDefinition (org.jooq.meta.SequenceDefinition)8