Search in sources :

Example 1 with TableType

use of org.jooq.TableOptions.TableType in project jOOQ by jOOQ.

the class HSQLDBDatabase method getTables0.

@Override
protected List<TableDefinition> getTables0() throws SQLException {
    List<TableDefinition> result = new ArrayList<>();
    for (Record record : create().select(SYSTEM_TABLES.TABLE_SCHEM, SYSTEM_TABLES.TABLE_NAME, inline("").as(ROUTINES.SPECIFIC_NAME), SYSTEM_TABLES.REMARKS, when(SYSTEM_TABLES.TABLE_TYPE.eq(inline("VIEW")), inline(TableType.VIEW.name())).else_(inline(TableType.TABLE.name())).trim().as("table_type"), when(VIEWS.VIEW_DEFINITION.lower().like(inline("create%")), VIEWS.VIEW_DEFINITION).else_(inline("create view \"").concat(SYSTEM_TABLES.TABLE_NAME).concat("\" as ").concat(VIEWS.VIEW_DEFINITION)).as(VIEWS.VIEW_DEFINITION)).from(SYSTEM_TABLES).leftJoin(VIEWS).on(SYSTEM_TABLES.TABLE_SCHEM.eq(VIEWS.TABLE_SCHEMA)).and(SYSTEM_TABLES.TABLE_NAME.eq(VIEWS.TABLE_NAME)).where(SYSTEM_TABLES.TABLE_SCHEM.in(getInputSchemata())).unionAll(tableValuedFunctions() ? select(ROUTINES.ROUTINE_SCHEMA, ROUTINES.ROUTINE_NAME, ROUTINES.SPECIFIC_NAME, inline(""), inline(TableType.FUNCTION.name()), ROUTINES.ROUTINE_DEFINITION).from(ROUTINES).where(ROUTINES.ROUTINE_SCHEMA.in(getInputSchemata())).and(ROUTINES.ROUTINE_TYPE.eq(inline("FUNCTION"))).and(ROUTINES.DATA_TYPE.startsWith(inline("ROW("))) : select(inline(""), inline(""), inline(""), inline(""), inline(TableType.FUNCTION.name()), inline("")).where(falseCondition())).orderBy(SYSTEM_TABLES.TABLE_SCHEM, SYSTEM_TABLES.TABLE_NAME).fetch()) {
        SchemaDefinition schema = getSchema(record.get(SYSTEM_TABLES.TABLE_SCHEM));
        String name = record.get(SYSTEM_TABLES.TABLE_NAME);
        String specificName = record.get(ROUTINES.SPECIFIC_NAME);
        String comment = record.get(SYSTEM_TABLES.REMARKS);
        TableType tableType = record.get("table_type", TableType.class);
        String source = record.get(VIEWS.VIEW_DEFINITION);
        if (tableType == TableType.FUNCTION)
            result.add(new HSQLDBTableValuedFunction(schema, name, specificName, comment, source));
        else
            result.add(new HSQLDBTableDefinition(schema, name, comment, tableType, source));
    }
    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 2 with TableType

use of org.jooq.TableOptions.TableType in project jOOQ by jOOQ.

the class XMLDatabase method getTables0.

@Override
protected List<TableDefinition> getTables0() {
    List<TableDefinition> result = new ArrayList<>();
    for (Table table : info().getTables()) {
        if (getInputSchemata().contains(table.getTableSchema())) {
            SchemaDefinition schema = getSchema(table.getTableSchema());
            TableType tableType;
            switch(table.getTableType()) {
                case GLOBAL_TEMPORARY:
                    tableType = TableType.TEMPORARY;
                    break;
                case VIEW:
                    tableType = TableType.VIEW;
                    break;
                case BASE_TABLE:
                default:
                    tableType = TableType.TABLE;
                    break;
            }
            String source = null;
            if (tableType == TableType.VIEW) {
                viewLoop: for (View view : info().getViews()) {
                    if (StringUtils.equals(defaultIfNull(table.getTableCatalog(), ""), defaultIfNull(view.getTableCatalog(), "")) && StringUtils.equals(defaultIfNull(table.getTableSchema(), ""), defaultIfNull(view.getTableSchema(), "")) && StringUtils.equals(defaultIfNull(table.getTableName(), ""), defaultIfNull(view.getTableName(), ""))) {
                        source = view.getViewDefinition();
                        break viewLoop;
                    }
                }
            }
            result.add(new XMLTableDefinition(schema, info(), table, table.getComment(), tableType, source));
        }
    }
    return result;
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) Table(org.jooq.util.xml.jaxb.Table) TableType(org.jooq.TableOptions.TableType) ArrayList(java.util.ArrayList) TableDefinition(org.jooq.meta.TableDefinition) View(org.jooq.util.xml.jaxb.View)

Example 3 with TableType

use of org.jooq.TableOptions.TableType in project jOOQ by jOOQ.

the class MySQLDatabase method getTables0.

@Override
protected List<TableDefinition> getTables0() throws SQLException {
    List<TableDefinition> result = new ArrayList<>();
    for (Record record : create().select(TABLES.TABLE_SCHEMA, TABLES.TABLE_NAME, TABLES.TABLE_COMMENT, when(TABLES.TABLE_TYPE.eq(inline("VIEW")), inline(TableType.VIEW.name())).else_(inline(TableType.TABLE.name())).as("table_type"), when(VIEWS.VIEW_DEFINITION.lower().like(inline("create%")), VIEWS.VIEW_DEFINITION).else_(inline("create view `").concat(TABLES.TABLE_NAME).concat("` as ").concat(VIEWS.VIEW_DEFINITION)).as(VIEWS.VIEW_DEFINITION)).from(TABLES).leftJoin(VIEWS).on(TABLES.TABLE_SCHEMA.eq(VIEWS.TABLE_SCHEMA)).and(TABLES.TABLE_NAME.eq(VIEWS.TABLE_NAME)).where(TABLES.TABLE_SCHEMA.in(workaroundFor5213(getInputSchemata()))).and(TABLES.TABLE_TYPE.ne(inline("SEQUENCE"))).orderBy(TABLES.TABLE_SCHEMA, TABLES.TABLE_NAME)) {
        SchemaDefinition schema = getSchema(record.get(TABLES.TABLE_SCHEMA));
        String name = record.get(TABLES.TABLE_NAME);
        String comment = record.get(TABLES.TABLE_COMMENT);
        TableType tableType = record.get("table_type", TableType.class);
        String source = record.get(VIEWS.VIEW_DEFINITION);
        MySQLTableDefinition table = new MySQLTableDefinition(schema, name, comment, 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 4 with TableType

use of org.jooq.TableOptions.TableType in project jOOQ by jOOQ.

the class PostgresDatabase method getTables0.

@Override
protected List<TableDefinition> getTables0() throws SQLException {
    List<TableDefinition> result = new ArrayList<>();
    Map<Name, PostgresTableDefinition> map = new HashMap<>();
    Select<Record6<String, String, String, String, String, String>> empty = select(inline(""), inline(""), inline(""), inline(""), inline(""), inline("")).where(falseCondition());
    for (Record record : create().select().from(select(TABLES.TABLE_SCHEMA, TABLES.TABLE_NAME, TABLES.TABLE_NAME.as("specific_name"), PG_DESCRIPTION.DESCRIPTION, when(TABLES.TABLE_TYPE.eq(inline("VIEW")), inline(TableType.VIEW.name())).else_(inline(TableType.TABLE.name())).as("table_type"), VIEWS.VIEW_DEFINITION).from(TABLES).join(PG_NAMESPACE).on(TABLES.TABLE_SCHEMA.eq(PG_NAMESPACE.NSPNAME)).join(PG_CLASS).on(PG_CLASS.RELNAME.eq(TABLES.TABLE_NAME)).and(PG_CLASS.RELNAMESPACE.eq(PG_NAMESPACE.OID)).leftJoin(PG_DESCRIPTION).on(PG_DESCRIPTION.OBJOID.eq(PG_CLASS.OID)).and(PG_DESCRIPTION.OBJSUBID.eq(0)).leftJoin(VIEWS).on(TABLES.TABLE_SCHEMA.eq(VIEWS.TABLE_SCHEMA)).and(TABLES.TABLE_NAME.eq(VIEWS.TABLE_NAME)).where(TABLES.TABLE_SCHEMA.in(getInputSchemata())).and(canUseTupleInPredicates() ? row(TABLES.TABLE_SCHEMA, TABLES.TABLE_NAME).notIn(select(PG_NAMESPACE.NSPNAME, PG_CLASS.RELNAME).from(PG_CLASS).join(PG_NAMESPACE).on(PG_CLASS.RELNAMESPACE.eq(PG_NAMESPACE.OID)).where(PG_CLASS.RELKIND.eq(inline("m")))) : noCondition()).unionAll(select(field("{0}::varchar", PG_NAMESPACE.NSPNAME.getDataType(), PG_NAMESPACE.NSPNAME), field("{0}::varchar", PG_CLASS.RELNAME.getDataType(), PG_CLASS.RELNAME), field("{0}::varchar", PG_CLASS.RELNAME.getDataType(), PG_CLASS.RELNAME), PG_DESCRIPTION.DESCRIPTION, inline(TableType.MATERIALIZED_VIEW.name()).as("table_type"), inline("")).from(PG_CLASS).join(PG_NAMESPACE).on(PG_CLASS.RELNAMESPACE.eq(PG_NAMESPACE.OID)).leftOuterJoin(PG_DESCRIPTION).on(PG_DESCRIPTION.OBJOID.eq(PG_CLASS.OID)).and(PG_DESCRIPTION.OBJSUBID.eq(0)).where(PG_NAMESPACE.NSPNAME.in(getInputSchemata())).and(PG_CLASS.RELKIND.eq(inline("m")))).unionAll(tableValuedFunctions() ? select(ROUTINES.ROUTINE_SCHEMA, ROUTINES.ROUTINE_NAME, ROUTINES.SPECIFIC_NAME, inline(""), inline(TableType.FUNCTION.name()).as("table_type"), inline("")).from(ROUTINES).join(PG_NAMESPACE).on(ROUTINES.SPECIFIC_SCHEMA.eq(PG_NAMESPACE.NSPNAME)).join(PG_PROC).on(PG_PROC.PRONAMESPACE.eq(PG_NAMESPACE.OID)).and(PG_PROC.PRONAME.concat("_").concat(PG_PROC.OID).eq(ROUTINES.SPECIFIC_NAME)).where(ROUTINES.ROUTINE_SCHEMA.in(getInputSchemata())).and(PG_PROC.PRORETSET) : empty).asTable("tables")).orderBy(1, 2).fetch()) {
        SchemaDefinition schema = getSchema(record.get(TABLES.TABLE_SCHEMA));
        String name = record.get(TABLES.TABLE_NAME);
        String comment = record.get(PG_DESCRIPTION.DESCRIPTION, String.class);
        TableType tableType = record.get("table_type", TableType.class);
        String source = record.get(VIEWS.VIEW_DEFINITION);
        if (source != null && !source.toLowerCase().startsWith("create"))
            source = "create view \"" + name + "\" as " + source;
        switch(tableType) {
            case FUNCTION:
                result.add(new PostgresTableValuedFunction(schema, name, record.get(ROUTINES.SPECIFIC_NAME), comment));
                break;
            case MATERIALIZED_VIEW:
                result.add(new PostgresMaterializedViewDefinition(schema, name, comment));
                break;
            default:
                PostgresTableDefinition t = new PostgresTableDefinition(schema, name, comment, tableType, source);
                result.add(t);
                map.put(name(schema.getName(), name), t);
                break;
        }
    }
    PgClass ct = PG_CLASS.as("ct");
    PgNamespace cn = PG_NAMESPACE.as("cn");
    PgInherits i = PG_INHERITS.as("i");
    PgClass pt = PG_CLASS.as("pt");
    PgNamespace pn = PG_NAMESPACE.as("pn");
    // don't execute the following query:
    if (is84()) {
        for (Record5<String, String, String, String, Integer> inheritance : create().select(cn.NSPNAME, ct.RELNAME, pn.NSPNAME, pt.RELNAME, max(i.INHSEQNO).over().partitionBy(i.INHRELID).as("m")).from(ct).join(cn).on(ct.RELNAMESPACE.eq(cn.OID)).join(i).on(i.INHRELID.eq(ct.OID)).join(pt).on(i.INHPARENT.eq(pt.OID)).join(pn).on(pt.RELNAMESPACE.eq(pn.OID)).where(cn.NSPNAME.in(getInputSchemata())).and(pn.NSPNAME.in(getInputSchemata())).fetch()) {
            Name child = name(inheritance.value1(), inheritance.value2());
            Name parent = name(inheritance.value3(), inheritance.value4());
            if (inheritance.value5() > 1) {
                log.info("Multiple inheritance", "Multiple inheritance is not supported by jOOQ: " + child + " inherits from " + parent);
            } else {
                PostgresTableDefinition childTable = map.get(child);
                PostgresTableDefinition parentTable = map.get(parent);
                if (childTable != null && parentTable != null) {
                    childTable.setParentTable(parentTable);
                    parentTable.getChildTables().add(childTable);
                }
            }
        }
    }
    return result;
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) TableType(org.jooq.TableOptions.TableType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Name(org.jooq.Name) BigInteger(java.math.BigInteger) PgInherits(org.jooq.meta.postgres.pg_catalog.tables.PgInherits) TableDefinition(org.jooq.meta.TableDefinition) PgClass(org.jooq.meta.postgres.pg_catalog.tables.PgClass) Record6(org.jooq.Record6) Record(org.jooq.Record) PgNamespace(org.jooq.meta.postgres.pg_catalog.tables.PgNamespace)

Example 5 with TableType

use of org.jooq.TableOptions.TableType 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)

Aggregations

ArrayList (java.util.ArrayList)7 TableType (org.jooq.TableOptions.TableType)7 Record (org.jooq.Record)6 TableDefinition (org.jooq.meta.TableDefinition)6 SchemaDefinition (org.jooq.meta.SchemaDefinition)5 BigInteger (java.math.BigInteger)2 HashMap (java.util.HashMap)2 Name (org.jooq.Name)2 FALSE (java.lang.Boolean.FALSE)1 TRUE (java.lang.Boolean.TRUE)1 Collections (java.util.Collections)1 Collections.emptyList (java.util.Collections.emptyList)1 Comparator.comparing (java.util.Comparator.comparing)1 Comparator.comparingInt (java.util.Comparator.comparingInt)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Catalog (org.jooq.Catalog)1 Check (org.jooq.Check)1 Configuration (org.jooq.Configuration)1