Search in sources :

Example 56 with TableDefinition

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

the class H2Database method getInlineEnums2_0.

private void getInlineEnums2_0(List<EnumDefinition> result) {
    // TODO: Re-generate the H2 schema and use generated code to run this query
    create().resultQuery(("" + "select c.TABLE_SCHEMA , c.TABLE_NAME , c.COLUMN_NAME , array_agg(ev.value_name order by ev.VALUE_ORDINAL)\n" + "from INFORMATION_SCHEMA.ENUM_VALUES ev\n" + "join information_schema.columns c\n" + "on ev.OBJECT_SCHEMA = c.TABLE_SCHEMA\n" + "and ev.OBJECT_NAME = c.TABLE_NAME\n" + "and ev.ENUM_IDENTIFIER = c.DTD_IDENTIFIER\n" + "where ev.OBJECT_TYPE = 'TABLE'\n" + "and c.DOMAIN_NAME IS NULL\n" + "group by c.TABLE_SCHEMA , c.TABLE_NAME , c.COLUMN_NAME\n" + "order by 1, 2, 3\n" + "")).coerce(COLUMNS.TABLE_SCHEMA, COLUMNS.TABLE_NAME, COLUMNS.COLUMN_NAME, arrayAgg(COLUMNS.COLUMN_NAME)).forEach(r -> {
        SchemaDefinition schema = getSchema(r.value1());
        if (schema == null)
            return;
        // [#1237] Don't generate enum classes for columns in MySQL tables
        // that are excluded from code generation
        TableDefinition tableDefinition = getTable(schema, r.value2());
        if (tableDefinition != null) {
            ColumnDefinition columnDefinition = tableDefinition.getColumn(r.value3());
            if (columnDefinition != null) {
                // are explicitly forced to another type
                if (getConfiguredForcedType(columnDefinition, columnDefinition.getType()) == null) {
                    String name = r.value2() + "_" + r.value3();
                    DefaultEnumDefinition definition = new DefaultEnumDefinition(schema, name, "");
                    for (String string : r.value4()) definition.addLiteral(string);
                    result.add(definition);
                }
            }
        }
    });
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) TableDefinition(org.jooq.meta.TableDefinition) DefaultEnumDefinition(org.jooq.meta.DefaultEnumDefinition) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) ColumnDefinition(org.jooq.meta.ColumnDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition)

Example 57 with TableDefinition

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

the class H2Database method loadUniqueKeys.

@Override
protected void loadUniqueKeys(DefaultRelations relations) throws SQLException {
    for (Record record : uniqueKeys(getInputSchemata())) {
        SchemaDefinition schema = getSchema(record.get(CONSTRAINTS.TABLE_SCHEMA));
        if (schema != null) {
            String tableName = record.get(CONSTRAINTS.TABLE_NAME);
            String primaryKey = record.get(CONSTRAINTS.CONSTRAINT_NAME);
            String columnName = record.get(INDEXES.COLUMN_NAME);
            TableDefinition table = getTable(schema, tableName);
            if (table != null)
                relations.addUniqueKey(primaryKey, table, table.getColumn(columnName));
        }
    }
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record)

Example 58 with TableDefinition

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

the class HSQLDBDatabase method loadCheckConstraints.

@Override
protected void loadCheckConstraints(DefaultRelations relations) throws SQLException {
    CheckConstraints cc = CHECK_CONSTRAINTS.as("cc");
    Columns c = COLUMNS.as("c");
    // [#2808] [#3019] Workaround for bad handling of JOIN .. USING
    Field<String> constraintName = field(name(cc.CONSTRAINT_NAME.getName()), String.class);
    for (Record record : create().select(cc.tableConstraints().TABLE_SCHEMA, cc.tableConstraints().TABLE_NAME, constraintName, cc.CHECK_CLAUSE).from(cc).where(cc.tableConstraints().TABLE_SCHEMA.in(getInputSchemata())).and(getIncludeSystemCheckConstraints() ? noCondition() : cc.tableConstraints().CONSTRAINT_NAME.notLike("SYS!_CT!_%", '!').or(cc.CHECK_CLAUSE.notIn(// TODO: Should we ever quote these?
    select(c.TABLE_SCHEMA.concat(inline('.')).concat(c.TABLE_NAME).concat(inline('.')).concat(c.COLUMN_NAME).concat(inline(" IS NOT NULL"))).from(c).where(c.TABLE_SCHEMA.eq(cc.tableConstraints().TABLE_SCHEMA)).and(c.TABLE_NAME.eq(cc.tableConstraints().TABLE_NAME)))))) {
        SchemaDefinition schema = getSchema(record.get(cc.tableConstraints().TABLE_SCHEMA));
        TableDefinition table = getTable(schema, record.get(cc.tableConstraints().TABLE_NAME));
        if (table != null) {
            relations.addCheckConstraint(table, new DefaultCheckConstraintDefinition(schema, table, record.get(constraintName), record.get(cc.CHECK_CLAUSE)));
        }
    }
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) Columns(org.jooq.meta.hsqldb.information_schema.tables.Columns) TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record) DefaultCheckConstraintDefinition(org.jooq.meta.DefaultCheckConstraintDefinition) CheckConstraints(org.jooq.meta.hsqldb.information_schema.tables.CheckConstraints)

Example 59 with TableDefinition

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

the class HSQLDBDatabase method loadForeignKeys.

@Override
protected void loadForeignKeys(DefaultRelations relations) throws SQLException {
    KeyColumnUsage fkKcu = KEY_COLUMN_USAGE.as("fk_kcu");
    KeyColumnUsage pkKcu = KEY_COLUMN_USAGE.as("pk_kcu");
    Result<?> result = create().select(REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_NAME, REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_SCHEMA, TABLE_CONSTRAINTS.TABLE_NAME, fkKcu.CONSTRAINT_NAME, fkKcu.TABLE_SCHEMA, fkKcu.TABLE_NAME, fkKcu.COLUMN_NAME, pkKcu.COLUMN_NAME).from(REFERENTIAL_CONSTRAINTS).join(fkKcu).on(fkKcu.CONSTRAINT_SCHEMA.equal(REFERENTIAL_CONSTRAINTS.CONSTRAINT_SCHEMA)).and(fkKcu.CONSTRAINT_NAME.equal(REFERENTIAL_CONSTRAINTS.CONSTRAINT_NAME)).join(TABLE_CONSTRAINTS).on(TABLE_CONSTRAINTS.CONSTRAINT_SCHEMA.eq(REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_SCHEMA)).and(TABLE_CONSTRAINTS.CONSTRAINT_NAME.eq(REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_NAME)).join(pkKcu).on(pkKcu.CONSTRAINT_SCHEMA.eq(TABLE_CONSTRAINTS.CONSTRAINT_SCHEMA)).and(pkKcu.CONSTRAINT_NAME.eq(TABLE_CONSTRAINTS.CONSTRAINT_NAME)).and(pkKcu.ORDINAL_POSITION.eq(fkKcu.POSITION_IN_UNIQUE_CONSTRAINT)).where(fkKcu.TABLE_SCHEMA.in(getInputSchemata())).orderBy(fkKcu.TABLE_SCHEMA.asc(), fkKcu.TABLE_NAME.asc(), fkKcu.CONSTRAINT_NAME.asc(), fkKcu.ORDINAL_POSITION.asc()).fetch();
    for (Record record : result) {
        SchemaDefinition foreignKeySchema = getSchema(record.get(fkKcu.TABLE_SCHEMA));
        SchemaDefinition uniqueKeySchema = getSchema(record.get(REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_SCHEMA));
        String foreignKey = record.get(fkKcu.CONSTRAINT_NAME);
        String foreignKeyTableName = record.get(fkKcu.TABLE_NAME);
        String foreignKeyColumn = record.get(fkKcu.COLUMN_NAME);
        String uniqueKey = record.get(REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_NAME);
        String uniqueKeyTableName = record.get(TABLE_CONSTRAINTS.TABLE_NAME);
        String uniqueKeyColumn = record.get(pkKcu.COLUMN_NAME);
        TableDefinition foreignKeyTable = getTable(foreignKeySchema, foreignKeyTableName);
        TableDefinition uniqueKeyTable = getTable(uniqueKeySchema, uniqueKeyTableName);
        if (foreignKeyTable != null && uniqueKeyTable != null)
            relations.addForeignKey(foreignKey, foreignKeyTable, foreignKeyTable.getColumn(foreignKeyColumn), uniqueKey, uniqueKeyTable, uniqueKeyTable.getColumn(uniqueKeyColumn), true);
    }
}
Also used : KeyColumnUsage(org.jooq.meta.hsqldb.information_schema.tables.KeyColumnUsage) SchemaDefinition(org.jooq.meta.SchemaDefinition) TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record)

Example 60 with TableDefinition

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

the class SQLiteDatabase method getTables0.

@Override
protected List<TableDefinition> getTables0() throws SQLException {
    List<TableDefinition> result = new ArrayList<>();
    CommonTableExpression<Record1<String>> virtualTables = name("virtual_tables").fields("name").as(select(coalesce(SQLiteMaster.NAME, inline(""))).from(SQLITE_MASTER).where(SQLiteMaster.SQL.likeIgnoreCase(inline("%create virtual table%"))));
    for (Record record : create().with(virtualTables).select(SQLiteMaster.NAME, when(SQLiteMaster.TYPE.eq(inline("view")), inline(TableType.VIEW.name())).else_(inline(TableType.TABLE.name())).as("table_type"), SQLiteMaster.SQL).from(SQLITE_MASTER).where(SQLiteMaster.TYPE.in("table", "view")).and(getIncludeSystemTables() ? noCondition() : SQLiteMaster.NAME.notLike(all(inline("%!_content"), inline("%!_segments"), inline("%!_segdir"), inline("%!_docsize"), inline("%!_stat"))).escape('!').or(SQLiteMaster.NAME.like(inline("%!_content")).escape('!').and(replace(SQLiteMaster.NAME, inline("_content")).notIn(selectFrom(virtualTables)))).or(SQLiteMaster.NAME.like(inline("%!_segments")).escape('!').and(replace(SQLiteMaster.NAME, inline("_segments")).notIn(selectFrom(virtualTables)))).or(SQLiteMaster.NAME.like(inline("%!_segdir")).escape('!').and(replace(SQLiteMaster.NAME, inline("_segdir")).notIn(selectFrom(virtualTables)))).or(SQLiteMaster.NAME.like(inline("%!_docsize")).escape('!').and(replace(SQLiteMaster.NAME, inline("_docsize")).notIn(selectFrom(virtualTables)))).or(SQLiteMaster.NAME.like(inline("%!_stat")).escape('!').and(replace(SQLiteMaster.NAME, inline("_stat")).notIn(selectFrom(virtualTables))))).orderBy(SQLiteMaster.NAME)) {
        String name = record.get(SQLiteMaster.NAME);
        TableType tableType = record.get("table_type", TableType.class);
        String source = record.get(SQLiteMaster.SQL);
        result.add(new SQLiteTableDefinition(getSchemata().get(0), name, "", tableType, source));
    }
    return result;
}
Also used : TableType(org.jooq.TableOptions.TableType) ArrayList(java.util.ArrayList) TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record) Record1(org.jooq.Record1)

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