Search in sources :

Example 1 with PgInherits

use of org.jooq.util.postgres.pg_catalog.tables.PgInherits in project jOOQ by jOOQ.

the class PostgresDatabase method getTables0.

@Override
protected List<TableDefinition> getTables0() throws SQLException {
    List<TableDefinition> result = new ArrayList<TableDefinition>();
    Map<Name, PostgresTableDefinition> map = new HashMap<Name, PostgresTableDefinition>();
    Select<Record6<String, String, String, Boolean, Boolean, String>> empty = select(inline(""), inline(""), inline(""), inline(false), inline(false), inline("")).where(falseCondition());
    for (Record record : create().select().from(select(TABLES.TABLE_SCHEMA, TABLES.TABLE_NAME, TABLES.TABLE_NAME.as("specific_name"), inline(false).as("table_valued_function"), inline(false).as("materialized_view"), PG_DESCRIPTION.DESCRIPTION).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(oid(PG_NAMESPACE))).leftOuterJoin(PG_DESCRIPTION).on(PG_DESCRIPTION.OBJOID.eq(oid(PG_CLASS))).and(PG_DESCRIPTION.OBJSUBID.eq(0)).where(TABLES.TABLE_SCHEMA.in(getInputSchemata())).and(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(oid(PG_NAMESPACE))).where(PG_CLASS.RELKIND.eq(inline("m"))))).unionAll(select(PG_NAMESPACE.NSPNAME, PG_CLASS.RELNAME, PG_CLASS.RELNAME, inline(false).as("table_valued_function"), inline(true).as("materialized_view"), PG_DESCRIPTION.DESCRIPTION).from(PG_CLASS).join(PG_NAMESPACE).on(PG_CLASS.RELNAMESPACE.eq(oid(PG_NAMESPACE))).leftOuterJoin(PG_DESCRIPTION).on(PG_DESCRIPTION.OBJOID.eq(oid(PG_CLASS))).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(true).as("table_valued_function"), inline(false).as("materialized_view"), inline("")).from(ROUTINES).join(PG_NAMESPACE).on(ROUTINES.SPECIFIC_SCHEMA.eq(PG_NAMESPACE.NSPNAME)).join(PG_PROC).on(PG_PROC.PRONAMESPACE.eq(oid(PG_NAMESPACE))).and(PG_PROC.PRONAME.concat("_").concat(oid(PG_PROC)).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);
        boolean tableValuedFunction = record.get("table_valued_function", boolean.class);
        boolean materializedView = record.get("materialized_view", boolean.class);
        String comment = record.get(PG_DESCRIPTION.DESCRIPTION, String.class);
        if (tableValuedFunction) {
            result.add(new PostgresTableValuedFunction(schema, name, record.get(ROUTINES.SPECIFIC_NAME), comment));
        } else if (materializedView) {
            result.add(new PostgresMaterializedViewDefinition(schema, name, comment));
        } else {
            PostgresTableDefinition t = new PostgresTableDefinition(schema, name, comment);
            result.add(t);
            map.put(name(schema.getName(), name), t);
        }
    }
    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(oid(cn))).join(i).on(i.INHRELID.eq(oid(ct))).join(pt).on(i.INHPARENT.eq(oid(pt))).join(pn).on(pt.RELNAMESPACE.eq(oid(pn))).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.util.SchemaDefinition) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Name(org.jooq.Name) PgInherits(org.jooq.util.postgres.pg_catalog.tables.PgInherits) TableDefinition(org.jooq.util.TableDefinition) PgClass(org.jooq.util.postgres.pg_catalog.tables.PgClass) Record6(org.jooq.Record6) Record(org.jooq.Record) PgNamespace(org.jooq.util.postgres.pg_catalog.tables.PgNamespace)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Name (org.jooq.Name)1 Record (org.jooq.Record)1 Record6 (org.jooq.Record6)1 SchemaDefinition (org.jooq.util.SchemaDefinition)1 TableDefinition (org.jooq.util.TableDefinition)1 PgClass (org.jooq.util.postgres.pg_catalog.tables.PgClass)1 PgInherits (org.jooq.util.postgres.pg_catalog.tables.PgInherits)1 PgNamespace (org.jooq.util.postgres.pg_catalog.tables.PgNamespace)1