Search in sources :

Example 1 with Record6

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

Example 2 with Record6

use of org.jooq.Record6 in project jOOQ by jOOQ.

the class MySQLDatabase method getRoutines0.

@Override
protected List<RoutineDefinition> getRoutines0() throws SQLException {
    List<RoutineDefinition> result = new ArrayList<RoutineDefinition>();
    try {
        create(true).fetchCount(PROC);
    } catch (DataAccessException e) {
        log.warn("Table unavailable", "The `mysql`.`proc` table is unavailable. Stored procedures cannot be loaded. Check if you have sufficient grants");
        return result;
    }
    Result<Record6<String, String, String, byte[], byte[], ProcType>> records = create().select(Proc.DB, Proc.NAME, Proc.COMMENT, Proc.PARAM_LIST, Proc.RETURNS, Proc.TYPE).from(PROC).where(DB.in(getInputSchemata())).orderBy(DB, Proc.NAME).fetch();
    Map<Record, Result<Record6<String, String, String, byte[], byte[], ProcType>>> groups = records.intoGroups(new Field[] { Proc.DB, Proc.NAME });
    // procedures and functions with the same signature.
    for (Entry<Record, Result<Record6<String, String, String, byte[], byte[], ProcType>>> entry : groups.entrySet()) {
        Result<?> overloads = entry.getValue();
        for (int i = 0; i < overloads.size(); i++) {
            Record record = overloads.get(i);
            SchemaDefinition schema = getSchema(record.get(DB));
            String name = record.get(Proc.NAME);
            String comment = record.get(Proc.COMMENT);
            String params = new String(record.get(Proc.PARAM_LIST));
            String returns = new String(record.get(Proc.RETURNS));
            ProcType type = record.get(Proc.TYPE);
            if (overloads.size() > 1) {
                result.add(new MySQLRoutineDefinition(schema, name, comment, params, returns, type, "_" + type.name()));
            } else {
                result.add(new MySQLRoutineDefinition(schema, name, comment, params, returns, type, null));
            }
        }
    }
    return result;
}
Also used : RoutineDefinition(org.jooq.util.RoutineDefinition) SchemaDefinition(org.jooq.util.SchemaDefinition) ArrayList(java.util.ArrayList) Result(org.jooq.Result) ProcType(org.jooq.util.mysql.mysql.enums.ProcType) Record6(org.jooq.Record6) Record(org.jooq.Record) DataAccessException(org.jooq.exception.DataAccessException)

Example 3 with Record6

use of org.jooq.Record6 in project jOOQ by jOOQ.

the class MySQLDatabase method getRoutines0.

@Override
protected List<RoutineDefinition> getRoutines0() throws SQLException {
    List<RoutineDefinition> result = new ArrayList<>();
    Result<Record6<String, String, String, byte[], byte[], ProcType>> records = is8() ? create().select(ROUTINES.ROUTINE_SCHEMA, ROUTINES.ROUTINE_NAME, ROUTINES.ROUTINE_COMMENT, inline(new byte[0]).as(PROC.PARAM_LIST), inline(new byte[0]).as(PROC.RETURNS), ROUTINES.ROUTINE_TYPE.coerce(PROC.TYPE).as(ROUTINES.ROUTINE_TYPE)).from(ROUTINES).where(ROUTINES.ROUTINE_SCHEMA.in(getInputSchemata())).orderBy(1, 2, 6).fetch() : create().select(PROC.DB.as(ROUTINES.ROUTINE_SCHEMA), PROC.NAME.as(ROUTINES.ROUTINE_NAME), PROC.COMMENT.as(ROUTINES.ROUTINE_COMMENT), PROC.PARAM_LIST, PROC.RETURNS, PROC.TYPE.as(ROUTINES.ROUTINE_TYPE)).from(PROC).where(PROC.DB.in(getInputSchemata())).orderBy(1, 2, 6).fetch();
    Map<Record, Result<Record6<String, String, String, byte[], byte[], ProcType>>> groups = records.intoGroups(new Field[] { ROUTINES.ROUTINE_SCHEMA, ROUTINES.ROUTINE_NAME });
    // [#1908] This indirection is necessary as MySQL allows for overloading
    // procedures and functions with the same signature.
    groups.forEach((k, overloads) -> {
        overloads.forEach(record -> {
            SchemaDefinition schema = getSchema(record.get(ROUTINES.ROUTINE_SCHEMA));
            String name = record.get(ROUTINES.ROUTINE_NAME);
            String comment = record.get(ROUTINES.ROUTINE_COMMENT);
            String params = is8() ? "" : new String(record.get(PROC.PARAM_LIST));
            String returns = is8() ? "" : new String(record.get(PROC.RETURNS));
            ProcType type = record.get(ROUTINES.ROUTINE_TYPE.coerce(PROC.TYPE).as(ROUTINES.ROUTINE_TYPE));
            if (overloads.size() > 1)
                result.add(new MySQLRoutineDefinition(schema, name, comment, params, returns, type, "_" + type.name()));
            else
                result.add(new MySQLRoutineDefinition(schema, name, comment, params, returns, type, null));
        });
    });
    return result;
}
Also used : RoutineDefinition(org.jooq.meta.RoutineDefinition) SchemaDefinition(org.jooq.meta.SchemaDefinition) ArrayList(java.util.ArrayList) ProcType(org.jooq.meta.mysql.mysql.enums.ProcType) Record6(org.jooq.Record6) Record(org.jooq.Record) Result(org.jooq.Result)

Example 4 with Record6

use of org.jooq.Record6 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 Record6

use of org.jooq.Record6 in project jOOQ by jOOQ.

the class PostgresDatabase method getIndexes0.

@Override
protected List<IndexDefinition> getIndexes0() throws SQLException {
    List<IndexDefinition> result = new ArrayList<>();
    PgIndex i = PG_INDEX.as("i");
    PgClass trel = PG_CLASS.as("trel");
    PgConstraint c = PG_CONSTRAINT.as("c");
    indexLoop: for (Record6<String, String, String, Boolean, String[], Integer[]> record : create().select(trel.pgNamespace().NSPNAME, trel.RELNAME, i.indexClass().RELNAME, i.INDISUNIQUE, array(select(field("pg_get_indexdef({0}, k + 1, true)", String.class, i.INDEXRELID)).from("generate_subscripts({0}, 1) as k", i.INDKEY).orderBy(field("k"))).as("columns"), field("{0}::int[]", Integer[].class, i.INDOPTION).as("asc_or_desc")).from(i).join(trel).on(trel.OID.eq(i.INDRELID)).where(trel.pgNamespace().NSPNAME.in(getInputSchemata())).and(getIncludeSystemIndexes() ? noCondition() : row(trel.pgNamespace().NSPNAME, i.indexClass().RELNAME).notIn(select(c.pgNamespace().NSPNAME, c.CONNAME).from(c))).orderBy(1, 2, 3)) {
        final SchemaDefinition tableSchema = getSchema(record.get(trel.pgNamespace().NSPNAME));
        if (tableSchema == null)
            continue indexLoop;
        final String indexName = record.get(i.indexClass().RELNAME);
        final String tableName = record.get(trel.RELNAME);
        final String[] columns = record.value5();
        final Integer[] options = record.value6();
        final TableDefinition table = getTable(tableSchema, tableName);
        if (table == null)
            continue indexLoop;
        final boolean unique = record.get(i.INDISUNIQUE);
        for (int k = 0; k < columns.length; k++) // the column expression, because it might be quoted
        if (table.getColumn(columns[k]) == null && table.getColumn(columns[k] = tryParseColumnName(columns[k])) == null)
            continue indexLoop;
        else // columns without options
        if (k >= options.length)
            continue indexLoop;
        result.add(new AbstractIndexDefinition(tableSchema, indexName, table, unique) {

            List<IndexColumnDefinition> indexColumns = new ArrayList<>();

            {
                for (int ordinal = 0; ordinal < columns.length; ordinal++) {
                    ColumnDefinition column = table.getColumn(columns[ordinal]);
                    // [#6307] Some background info on this bitwise operation here:
                    // https://stackoverflow.com/a/18128104/521799
                    SortOrder order = (options[ordinal] & 1) == 1 ? SortOrder.DESC : SortOrder.ASC;
                    indexColumns.add(new DefaultIndexColumnDefinition(this, column, order, ordinal + 1));
                }
            }

            @Override
            protected List<IndexColumnDefinition> getIndexColumns0() {
                return indexColumns;
            }
        });
    }
    return result;
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) ArrayList(java.util.ArrayList) SortOrder(org.jooq.SortOrder) PgConstraint(org.jooq.meta.postgres.pg_catalog.tables.PgConstraint) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) ColumnDefinition(org.jooq.meta.ColumnDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition) BigInteger(java.math.BigInteger) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition) AbstractIndexDefinition(org.jooq.meta.AbstractIndexDefinition) IndexDefinition(org.jooq.meta.IndexDefinition) PgConstraint(org.jooq.meta.postgres.pg_catalog.tables.PgConstraint) PgIndex(org.jooq.meta.postgres.pg_catalog.tables.PgIndex) AbstractIndexDefinition(org.jooq.meta.AbstractIndexDefinition) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) PgClass(org.jooq.meta.postgres.pg_catalog.tables.PgClass) TableDefinition(org.jooq.meta.TableDefinition) Record6(org.jooq.Record6) Records.intoList(org.jooq.Records.intoList) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList)

Aggregations

Record6 (org.jooq.Record6)6 ArrayList (java.util.ArrayList)5 Record (org.jooq.Record)4 SchemaDefinition (org.jooq.meta.SchemaDefinition)3 BigInteger (java.math.BigInteger)2 HashMap (java.util.HashMap)2 Name (org.jooq.Name)2 Result (org.jooq.Result)2 TableDefinition (org.jooq.meta.TableDefinition)2 PgClass (org.jooq.meta.postgres.pg_catalog.tables.PgClass)2 SchemaDefinition (org.jooq.util.SchemaDefinition)2 Arrays.asList (java.util.Arrays.asList)1 List (java.util.List)1 Collectors.toList (java.util.stream.Collectors.toList)1 Measurable (org.finos.waltz.schema.tables.Measurable)1 ScenarioAxisItem (org.finos.waltz.schema.tables.ScenarioAxisItem)1 Condition (org.jooq.Condition)1 Records.intoList (org.jooq.Records.intoList)1 SortOrder (org.jooq.SortOrder)1 TableType (org.jooq.TableOptions.TableType)1