Search in sources :

Example 21 with Table

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

the class JavaGenerator method generateSchema.

protected void generateSchema(SchemaDefinition schema, JavaWriter out) {
    final String catalogId = out.ref(getStrategy().getFullJavaIdentifier(schema.getCatalog()), 2);
    final String schemaId = getStrategy().getJavaIdentifier(schema);
    final String schemaName = !schema.getQualifiedOutputName().isEmpty() ? schema.getQualifiedOutputName() : schemaId;
    final String className = getStrategy().getJavaClassName(schema);
    final List<String> interfaces = out.ref(getStrategy().getJavaClassImplements(schema, Mode.DEFAULT));
    printPackage(out, schema);
    if (scala) {
        out.println("object %s {", className);
        out.javadoc("The reference instance of <code>%s</code>", schemaName);
        out.println("val %s = new %s", schemaId, className);
        out.println("}");
        out.println();
    }
    generateSchemaClassJavadoc(schema, out);
    printClassAnnotations(out, schema, Mode.DEFAULT);
    if (scala) {
        out.println("%sclass %s extends %s(\"%s\", %s)[[before= with ][separator= with ][%s]] {", visibility(), className, SchemaImpl.class, escapeString(schema.getOutputName()), catalogId, interfaces);
    } else if (kotlin) {
        out.println("%sopen class %s : %s(\"%s\", %s)[[before=, ][%s]] {", visibility(), className, SchemaImpl.class, escapeString(schema.getOutputName()), catalogId, interfaces);
        out.println("public companion object {");
        out.javadoc("The reference instance of <code>%s</code>", schemaName);
        out.println("%sval %s: %s = %s()", visibility(), scalaWhitespaceSuffix(schemaId), className, className);
        out.println("}");
    } else {
        out.println("%sclass %s extends %s[[before= implements ][%s]] {", visibility(), className, SchemaImpl.class, interfaces);
        out.printSerial();
        out.javadoc("The reference instance of <code>%s</code>", schemaName);
        out.println("%sstatic final %s %s = new %s();", visibility(), className, schemaId, className);
    }
    if (generateGlobalTableReferences()) {
        Set<String> memberNames = getMemberNames(schema);
        for (TableDefinition table : schema.getTables()) {
            // reference in the schema, and the function call
            if (scala && table.isTableValuedFunction() && table.getParameters().isEmpty())
                continue;
            final String tableClassName = out.ref(getStrategy().getFullJavaClassName(table));
            final String tableId = getStrategy().getJavaIdentifier(table);
            final String tableShortId = getShortId(out, memberNames, table);
            final String tableComment = escapeEntities(comment(table));
            out.javadoc(isBlank(tableComment) ? "The table <code>" + table.getQualifiedOutputName() + "</code>." : tableComment);
            if (scala)
                out.println("%sdef %s = %s", visibility(), tableId, tableShortId);
            else if (kotlin)
                out.println("%sval %s: %s get() = %s", visibility(), scalaWhitespaceSuffix(tableId), tableClassName, tableShortId);
            else
                out.println("%sfinal %s %s = %s;", visibility(), tableClassName, tableId, tableShortId);
            // globalObjectReferences
            if (table.isTableValuedFunction())
                printTableValuedFunction(out, table, getStrategy().getJavaIdentifier(table));
        }
    }
    if (!scala && !kotlin) {
        out.javadoc(NO_FURTHER_INSTANCES_ALLOWED);
        out.println("private %s() {", className);
        out.println("super(\"%s\", null);", escapeString(schema.getOutputName()));
        out.println("}");
    }
    out.println();
    if (scala) {
        out.println("%soverride def getCatalog: %s = %s", visibilityPublic(), Catalog.class, catalogId);
    } else if (kotlin) {
        out.println("%soverride fun getCatalog(): %s = %s", visibilityPublic(), Catalog.class, catalogId);
    } else {
        out.overrideInherit();
        printNonnullAnnotation(out);
        out.println("%s%s getCatalog() {", visibilityPublic(), Catalog.class);
        out.println("return %s;", catalogId);
        out.println("}");
    }
    // [#2255] Avoid referencing sequence literals, if they're not generated
    if (generateGlobalSequenceReferences())
        printReferences(out, database.getSequences(schema), Sequence.class, true);
    // [#681] Avoid referencing domain literals, if they're not generated
    if (generateGlobalDomainReferences())
        printReferences(out, database.getDomains(schema), Domain.class, true);
    // [#9685] Avoid referencing table literals if they're not generated
    if (generateTables())
        printReferences(out, database.getTables(schema), Table.class, true);
    // [#9685] Avoid referencing UDT literals if they're not generated
    if (generateUDTs())
        printReferences(out, database.getUDTs(schema), UDT.class, true);
    generateSchemaClassFooter(schema, out);
    out.println("}");
}
Also used : SchemaImpl(org.jooq.impl.SchemaImpl) Table(org.jooq.Table) UDT(org.jooq.UDT) TableDefinition(org.jooq.meta.TableDefinition) Sequence(org.jooq.Sequence) Domain(org.jooq.Domain) Catalog(org.jooq.Catalog)

Example 22 with Table

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

the class SelectQueryImpl method toSQLReference0.

/**
 * This method renders the main part of a query without the LIMIT clause.
 * This part is common to any type of limited query
 */
@SuppressWarnings("unchecked")
private final void toSQLReference0(Context<?> context, List<Field<?>> originalFields, List<Field<?>> alternativeFields) {
    SQLDialect family = context.family();
    boolean qualify = context.qualify();
    int unionOpSize = unionOp.size();
    boolean unionParensRequired = false;
    boolean unionOpNesting = false;
    // The SQL standard specifies:
    // 
    // <query expression> ::=
    // [ <with clause> ] <query expression body>
    // [ <order by clause> ] [ <result offset clause> ] [ <fetch first clause> ]
    // 
    // Depending on the dialect and on various syntax elements, parts of the above must be wrapped in
    // synthetic parentheses
    boolean wrapQueryExpressionInDerivedTable;
    boolean wrapQueryExpressionBodyInDerivedTable;
    boolean applySeekOnDerivedTable = applySeekOnDerivedTable();
    wrapQueryExpressionInDerivedTable = false;
    if (wrapQueryExpressionInDerivedTable)
        context.visit(K_SELECT).sql(" *").formatSeparator().visit(K_FROM).sql(" (").formatIndentStart().formatNewLine();
    wrapQueryExpressionBodyInDerivedTable = false || // predicate must be applied on a derived table, not on the individual subqueries
    applySeekOnDerivedTable;
    if (wrapQueryExpressionBodyInDerivedTable) {
        context.visit(K_SELECT).sql(' ');
        context.formatIndentStart().formatNewLine().sql("t.*");
        if (alternativeFields != null && originalFields.size() < alternativeFields.size())
            context.sql(", ").formatSeparator().declareFields(true, c -> c.visit(alternativeFields.get(alternativeFields.size() - 1)));
        context.formatIndentEnd().formatSeparator().visit(K_FROM).sql(" (").formatIndentStart().formatNewLine();
    }
    // all databases, we need to wrap relevant subqueries in parentheses.
    if (unionOpSize > 0) {
        if (!TRUE.equals(context.data(DATA_NESTED_SET_OPERATIONS)))
            context.data(DATA_NESTED_SET_OPERATIONS, unionOpNesting = unionOpNesting());
        for (int i = unionOpSize - 1; i >= 0; i--) {
            switch(unionOp.get(i)) {
                case EXCEPT:
                    context.start(SELECT_EXCEPT);
                    break;
                case EXCEPT_ALL:
                    context.start(SELECT_EXCEPT_ALL);
                    break;
                case INTERSECT:
                    context.start(SELECT_INTERSECT);
                    break;
                case INTERSECT_ALL:
                    context.start(SELECT_INTERSECT_ALL);
                    break;
                case UNION:
                    context.start(SELECT_UNION);
                    break;
                case UNION_ALL:
                    context.start(SELECT_UNION_ALL);
                    break;
            }
            // [#3676] There might be cases where nested set operations do not
            // imply required parentheses in some dialects, but better
            // play safe than sorry
            unionParenthesis(context, '(', alternativeFields != null ? alternativeFields : getSelect(), derivedTableRequired(context, this), unionParensRequired = unionOpNesting || unionParensRequired(context));
        }
    }
    traverseJoins(getFrom(), t -> {
        if (t instanceof TableImpl)
            context.scopeRegister(t, true);
    });
    for (Entry<QueryPart, QueryPart> entry : localQueryPartMapping.entrySet()) context.scopeRegister(entry.getKey(), true, entry.getValue());
    // SELECT clause
    // -------------
    context.start(SELECT_SELECT).visit(K_SELECT).separatorRequired(true);
    // [#1493] Oracle hints come directly after the SELECT keyword
    if (!StringUtils.isBlank(hint))
        context.sql(' ').sql(hint).separatorRequired(true);
    if (Tools.isNotEmpty(distinctOn))
        context.visit(K_DISTINCT_ON).sql(" (").visit(distinctOn).sql(')').separatorRequired(true);
    else if (distinct)
        context.visit(K_DISTINCT).separatorRequired(true);
    if (TRUE.equals(context.data(BooleanDataKey.DATA_RENDERING_DATA_CHANGE_DELTA_TABLE)))
        context.qualify(false);
    context.declareFields(true);
    // non-ambiguous column names as ambiguous column names are not allowed in subqueries
    if (alternativeFields != null)
        if (wrapQueryExpressionBodyInDerivedTable && originalFields.size() < alternativeFields.size())
            context.visit(new SelectFieldList<>(alternativeFields.subList(0, originalFields.size())));
        else
            context.visit(new SelectFieldList<>(alternativeFields));
    else
        // The default behaviour
        context.visit(getSelectResolveUnsupportedAsterisks(context.configuration()));
    if (TRUE.equals(context.data(BooleanDataKey.DATA_RENDERING_DATA_CHANGE_DELTA_TABLE)))
        context.qualify(qualify);
    context.declareFields(false).end(SELECT_SELECT);
    // only in top level SELECTs
    if (!context.subquery()) {
        context.start(SELECT_INTO);
        QueryPart actualIntoTable = (QueryPart) context.data(DATA_SELECT_INTO_TABLE);
        if (actualIntoTable == null)
            actualIntoTable = intoTable;
        if (actualIntoTable != null && !TRUE.equals(context.data(DATA_OMIT_INTO_CLAUSE)) && (SUPPORT_SELECT_INTO_TABLE.contains(context.dialect()) || !(actualIntoTable instanceof Table))) {
            context.formatSeparator().visit(K_INTO).sql(' ').visit(actualIntoTable);
        }
        context.end(SELECT_INTO);
    }
    // FROM and JOIN clauses
    // ---------------------
    context.start(SELECT_FROM).declareTables(true);
    // [#....] Some SQL dialects do not require a FROM clause. Others do and
    // jOOQ generates a "DUAL" table or something equivalent.
    // See also org.jooq.impl.Dual for details.
    boolean hasFrom = !getFrom().isEmpty() || !OPTIONAL_FROM_CLAUSE.contains(context.dialect());
    List<Condition> semiAntiJoinPredicates = null;
    ConditionProviderImpl where = getWhere(context);
    if (hasFrom) {
        Object previousCollect = context.data(DATA_COLLECT_SEMI_ANTI_JOIN, true);
        Object previousCollected = context.data(DATA_COLLECTED_SEMI_ANTI_JOIN, null);
        TableList tablelist = getFrom();
        tablelist = transformInlineDerivedTables(tablelist, where);
        context.formatSeparator().visit(K_FROM).separatorRequired(true).visit(tablelist);
        semiAntiJoinPredicates = (List<Condition>) context.data(DATA_COLLECTED_SEMI_ANTI_JOIN, previousCollected);
        context.data(DATA_COLLECT_SEMI_ANTI_JOIN, previousCollect);
    }
    context.declareTables(false).end(SELECT_FROM);
    // WHERE clause
    // ------------
    context.start(SELECT_WHERE);
    if (TRUE.equals(context.data().get(BooleanDataKey.DATA_SELECT_NO_DATA)))
        context.formatSeparator().visit(K_WHERE).sql(' ').visit(falseCondition());
    else if (!where.hasWhere() && semiAntiJoinPredicates == null)
        ;
    else {
        ConditionProviderImpl actual = new ConditionProviderImpl();
        if (semiAntiJoinPredicates != null)
            actual.addConditions(semiAntiJoinPredicates);
        if (where.hasWhere())
            actual.addConditions(where.getWhere());
        context.formatSeparator().visit(K_WHERE).sql(' ').visit(actual);
    }
    context.end(SELECT_WHERE);
    // GROUP BY and HAVING clause
    // --------------------------
    context.start(SELECT_GROUP_BY);
    if (!getGroupBy().isEmpty()) {
        context.formatSeparator().visit(K_GROUP_BY);
        if (groupByDistinct)
            context.sql(' ').visit(K_DISTINCT);
        context.separatorRequired(true);
        context.visit(groupBy);
    }
    context.end(SELECT_GROUP_BY);
    // HAVING clause
    // -------------
    context.start(SELECT_HAVING);
    if (getHaving().hasWhere())
        context.formatSeparator().visit(K_HAVING).sql(' ').visit(getHaving());
    context.end(SELECT_HAVING);
    // WINDOW clause
    // -------------
    context.start(SELECT_WINDOW);
    if (Tools.isNotEmpty(window) && !NO_SUPPORT_WINDOW_CLAUSE.contains(context.dialect()))
        context.formatSeparator().visit(K_WINDOW).separatorRequired(true).declareWindows(true, c -> c.visit(window));
    context.end(SELECT_WINDOW);
    if (getQualify().hasWhere())
        context.formatSeparator().visit(K_QUALIFY).sql(' ').visit(getQualify());
    // ORDER BY clause for local subselect
    // -----------------------------------
    toSQLOrderBy(context, originalFields, alternativeFields, false, wrapQueryExpressionBodyInDerivedTable, orderBy, limit);
    // --------------------------------------------
    if (unionOpSize > 0) {
        unionParenthesis(context, ')', null, derivedTableRequired(context, this), unionParensRequired);
        for (int i = 0; i < unionOpSize; i++) {
            CombineOperator op = unionOp.get(i);
            for (Select<?> other : union.get(i)) {
                boolean derivedTableRequired = derivedTableRequired(context, other);
                context.formatSeparator().visit(op.toKeyword(family));
                if (unionParensRequired)
                    context.sql(' ');
                else
                    context.formatSeparator();
                unionParenthesis(context, '(', other.getSelect(), derivedTableRequired, unionParensRequired);
                context.visit(other);
                unionParenthesis(context, ')', null, derivedTableRequired, unionParensRequired);
            }
            // [#1658] Close parentheses opened previously
            if (i < unionOpSize - 1)
                unionParenthesis(context, ')', null, derivedTableRequired(context, this), unionParensRequired);
            switch(unionOp.get(i)) {
                case EXCEPT:
                    context.end(SELECT_EXCEPT);
                    break;
                case EXCEPT_ALL:
                    context.end(SELECT_EXCEPT_ALL);
                    break;
                case INTERSECT:
                    context.end(SELECT_INTERSECT);
                    break;
                case INTERSECT_ALL:
                    context.end(SELECT_INTERSECT_ALL);
                    break;
                case UNION:
                    context.end(SELECT_UNION);
                    break;
                case UNION_ALL:
                    context.end(SELECT_UNION_ALL);
                    break;
            }
        }
        if (unionOpNesting)
            context.data().remove(DATA_NESTED_SET_OPERATIONS);
    }
    if (wrapQueryExpressionBodyInDerivedTable) {
        context.formatIndentEnd().formatNewLine().sql(") t");
        if (applySeekOnDerivedTable) {
            context.formatSeparator().visit(K_WHERE).sql(' ').qualify(false, c -> c.visit(getSeekCondition()));
        }
    }
    // ORDER BY clause for UNION
    // -------------------------
    context.qualify(false, c -> toSQLOrderBy(context, originalFields, alternativeFields, wrapQueryExpressionInDerivedTable, wrapQueryExpressionBodyInDerivedTable, unionOrderBy, unionLimit));
}
Also used : Internal.isub(org.jooq.impl.Internal.isub) Tools.aliased(org.jooq.impl.Tools.aliased) UnmodifiableList(org.jooq.impl.QOM.UnmodifiableList) DSL.emptyGroupingSet(org.jooq.impl.DSL.emptyGroupingSet) K_BY(org.jooq.impl.Keywords.K_BY) Arrays.asList(java.util.Arrays.asList) DSL.createTable(org.jooq.impl.DSL.createTable) Map(java.util.Map) QualifiedAsterisk(org.jooq.QualifiedAsterisk) SQLDialect(org.jooq.SQLDialect) Scope(org.jooq.Scope) Select(org.jooq.Select) SELECT_START_WITH(org.jooq.Clause.SELECT_START_WITH) DSL.partitionBy(org.jooq.impl.DSL.partitionBy) QueryPartCollectionView.wrap(org.jooq.impl.QueryPartCollectionView.wrap) Tools.isWindow(org.jooq.impl.Tools.isWindow) DataExtendedKey(org.jooq.impl.Tools.DataExtendedKey) DATA_TRANSFORM_ROWNUM_TO_LIMIT(org.jooq.impl.Tools.DataExtendedKey.DATA_TRANSFORM_ROWNUM_TO_LIMIT) TableField(org.jooq.TableField) SUPPORT_NATIVE_EXCEPT(org.jooq.impl.AsteriskImpl.SUPPORT_NATIVE_EXCEPT) DSL.noCondition(org.jooq.impl.DSL.noCondition) K_START_WITH(org.jooq.impl.Keywords.K_START_WITH) DATA_INSERT_SELECT(org.jooq.impl.Tools.BooleanDataKey.DATA_INSERT_SELECT) TRUE(java.lang.Boolean.TRUE) DERBY(org.jooq.SQLDialect.DERBY) EXCEPT_ALL(org.jooq.impl.CombineOperator.EXCEPT_ALL) DATA_RENDER_TRAILING_LIMIT_IF_APPLICABLE(org.jooq.impl.Tools.BooleanDataKey.DATA_RENDER_TRAILING_LIMIT_IF_APPLICABLE) DSL.jsonArrayAgg(org.jooq.impl.DSL.jsonArrayAgg) K_WITH_READ_ONLY(org.jooq.impl.Keywords.K_WITH_READ_ONLY) LinkedHashMap(java.util.LinkedHashMap) K_ORDER_BY(org.jooq.impl.Keywords.K_ORDER_BY) Tools.autoAlias(org.jooq.impl.Tools.autoAlias) INTERSECT(org.jooq.impl.CombineOperator.INTERSECT) VARCHAR(org.jooq.impl.SQLDataType.VARCHAR) XML(org.jooq.impl.SQLDataType.XML) DATA_OMIT_INTO_CLAUSE(org.jooq.impl.Tools.BooleanDataKey.DATA_OMIT_INTO_CLAUSE) DATA_DML_TARGET_TABLE(org.jooq.impl.Tools.DataKey.DATA_DML_TARGET_TABLE) MYSQL(org.jooq.SQLDialect.MYSQL) DATA_COLLECTED_SEMI_ANTI_JOIN(org.jooq.impl.Tools.DataKey.DATA_COLLECTED_SEMI_ANTI_JOIN) K_WITH_CHECK_OPTION(org.jooq.impl.Keywords.K_WITH_CHECK_OPTION) K_FROM(org.jooq.impl.Keywords.K_FROM) Comparator(org.jooq.Comparator) SelectFieldOrAsterisk(org.jooq.SelectFieldOrAsterisk) DSL.jsonbArrayAgg(org.jooq.impl.DSL.jsonbArrayAgg) DATA_COLLECT_SEMI_ANTI_JOIN(org.jooq.impl.Tools.BooleanDataKey.DATA_COLLECT_SEMI_ANTI_JOIN) FIREBIRD(org.jooq.SQLDialect.FIREBIRD) K_WHERE(org.jooq.impl.Keywords.K_WHERE) SELECT(org.jooq.Clause.SELECT) DSL.xmlattributes(org.jooq.impl.DSL.xmlattributes) JoinType(org.jooq.JoinType) IGNITE(org.jooq.SQLDialect.IGNITE) MARIADB(org.jooq.SQLDialect.MARIADB) NO_SUPPORT_ABSENT_ON_NULL(org.jooq.impl.JSONNull.NO_SUPPORT_ABSENT_ON_NULL) K_DISTINCT_ON(org.jooq.impl.Keywords.K_DISTINCT_ON) SelectQuery(org.jooq.SelectQuery) DSL.table(org.jooq.impl.DSL.table) INTERSECT_ALL(org.jooq.impl.CombineOperator.INTERSECT_ALL) Row(org.jooq.Row) IntStream.range(java.util.stream.IntStream.range) SELECT_WINDOW(org.jooq.Clause.SELECT_WINDOW) BiFunction(java.util.function.BiFunction) Table(org.jooq.Table) DSL.rowNumber(org.jooq.impl.DSL.rowNumber) Operator(org.jooq.Operator) ForLockWaitMode(org.jooq.impl.ForLock.ForLockWaitMode) SelectLimitPercentStep(org.jooq.SelectLimitPercentStep) Clause(org.jooq.Clause) SELECT_SELECT(org.jooq.Clause.SELECT_SELECT) NO_SUPPORT_UNQUALIFIED_COMBINED(org.jooq.impl.AsteriskImpl.NO_SUPPORT_UNQUALIFIED_COMBINED) JSONB(org.jooq.impl.SQLDataType.JSONB) DataKey(org.jooq.impl.Tools.DataKey) Tools.aliasedFields(org.jooq.impl.Tools.aliasedFields) SELECT_FROM(org.jooq.Clause.SELECT_FROM) K_TOP(org.jooq.impl.Keywords.K_TOP) GroupField(org.jooq.GroupField) OR(org.jooq.Operator.OR) DataType(org.jooq.DataType) DSL.name(org.jooq.impl.DSL.name) K_HAVING(org.jooq.impl.Keywords.K_HAVING) SELECT_CONNECT_BY(org.jooq.Clause.SELECT_CONNECT_BY) Name(org.jooq.Name) Collection(java.util.Collection) SelectLimitStep(org.jooq.SelectLimitStep) Field(org.jooq.Field) DATA_INSERT_SELECT_WITHOUT_INSERT_COLUMN_LIST(org.jooq.impl.Tools.BooleanDataKey.DATA_INSERT_SELECT_WITHOUT_INSERT_COLUMN_LIST) CUBRID(org.jooq.SQLDialect.CUBRID) SELECT_UNION(org.jooq.Clause.SELECT_UNION) K_QUALIFY(org.jooq.impl.Keywords.K_QUALIFY) EXCEPT(org.jooq.impl.CombineOperator.EXCEPT) K_INTO(org.jooq.impl.Keywords.K_INTO) Tools.containsUnaliasedTable(org.jooq.impl.Tools.containsUnaliasedTable) SelectWithTiesStep(org.jooq.SelectWithTiesStep) JSON(org.jooq.impl.SQLDataType.JSON) Entry(java.util.Map.Entry) K_DISTINCT(org.jooq.impl.Keywords.K_DISTINCT) Tools.anyMatch(org.jooq.impl.Tools.anyMatch) ResultSetMetaData(java.sql.ResultSetMetaData) K_SELECT(org.jooq.impl.Keywords.K_SELECT) K_SIBLINGS(org.jooq.impl.Keywords.K_SIBLINGS) SELECT_HAVING(org.jooq.Clause.SELECT_HAVING) INLINED(org.jooq.conf.ParamType.INLINED) Function(java.util.function.Function) K_CONNECT_BY(org.jooq.impl.Keywords.K_CONNECT_BY) TableLike(org.jooq.TableLike) Tools.fieldArray(org.jooq.impl.Tools.fieldArray) DSL.inline(org.jooq.impl.DSL.inline) K_PERCENT(org.jooq.impl.Keywords.K_PERCENT) SelectHavingStep(org.jooq.SelectHavingStep) DataAccessException(org.jooq.exception.DataAccessException) DATA_NESTED_SET_OPERATIONS(org.jooq.impl.Tools.BooleanDataKey.DATA_NESTED_SET_OPERATIONS) SELECT_INTERSECT(org.jooq.Clause.SELECT_INTERSECT) Consumer(java.util.function.Consumer) SortField(org.jooq.SortField) Multiset.returningClob(org.jooq.impl.Multiset.returningClob) H2(org.jooq.SQLDialect.H2) Tools.unalias(org.jooq.impl.Tools.unalias) K_WINDOW(org.jooq.impl.Keywords.K_WINDOW) Arrays(java.util.Arrays) DSL.orderBy(org.jooq.impl.DSL.orderBy) POSTGRES(org.jooq.SQLDialect.POSTGRES) DATA_UNALIAS_ALIASED_EXPRESSIONS(org.jooq.impl.Tools.BooleanDataKey.DATA_UNALIAS_ALIASED_EXPRESSIONS) JOIN(org.jooq.JoinType.JOIN) Condition(org.jooq.Condition) DSL.trueCondition(org.jooq.impl.DSL.trueCondition) EMULATE_WITH_GROUP_CONCAT(org.jooq.impl.JSONArrayAgg.EMULATE_WITH_GROUP_CONCAT) SelectField(org.jooq.SelectField) Tools.unqualified(org.jooq.impl.Tools.unqualified) CommonTableExpressionList.markTopLevelCteAndAccept(org.jooq.impl.CommonTableExpressionList.markTopLevelCteAndAccept) JSONObjectNullStep(org.jooq.JSONObjectNullStep) K_NOCYCLE(org.jooq.impl.Keywords.K_NOCYCLE) Set(java.util.Set) Tools.camelCase(org.jooq.impl.Tools.camelCase) QueryPart(org.jooq.QueryPart) Tools.traverseJoins(org.jooq.impl.Tools.traverseJoins) SELECT_UNION_ALL(org.jooq.Clause.SELECT_UNION_ALL) Tools.qualify(org.jooq.impl.Tools.qualify) N_LEVEL(org.jooq.impl.Names.N_LEVEL) Materialized(org.jooq.impl.QOM.Materialized) DSL.jsonObject(org.jooq.impl.DSL.jsonObject) K_AND(org.jooq.impl.Keywords.K_AND) DSL.xmlagg(org.jooq.impl.DSL.xmlagg) DATA_SELECT_INTO_TABLE(org.jooq.impl.Tools.DataKey.DATA_SELECT_INTO_TABLE) XML(org.jooq.XML) K_GROUP_BY(org.jooq.impl.Keywords.K_GROUP_BY) CompareCondition(org.jooq.impl.QOM.CompareCondition) DSL.row(org.jooq.impl.DSL.row) DSL.xmlelement(org.jooq.impl.DSL.xmlelement) ArrayList(java.util.ArrayList) Tools.isNotEmpty(org.jooq.impl.Tools.isNotEmpty) SELECT_INTO(org.jooq.Clause.SELECT_INTO) Transformations.transformRownum(org.jooq.impl.Transformations.transformRownum) SQLITE(org.jooq.SQLDialect.SQLITE) SELECT_WHERE(org.jooq.Clause.SELECT_WHERE) Record(org.jooq.Record) RIGHT_OUTER_JOIN(org.jooq.JoinType.RIGHT_OUTER_JOIN) JSONObjectReturningStep(org.jooq.JSONObjectReturningStep) K_MATERIALIZE(org.jooq.impl.Keywords.K_MATERIALIZE) K_ORDER(org.jooq.impl.Keywords.K_ORDER) DATA_TOP_LEVEL_CTE(org.jooq.impl.Tools.DataKey.DATA_TOP_LEVEL_CTE) SELECT_INTERSECT_ALL(org.jooq.Clause.SELECT_INTERSECT_ALL) StringUtils(org.jooq.tools.StringUtils) YUGABYTEDB(org.jooq.SQLDialect.YUGABYTEDB) UNION_ALL(org.jooq.impl.CombineOperator.UNION_ALL) ArrayDeque(java.util.ArrayDeque) ForLockMode(org.jooq.impl.ForLock.ForLockMode) TableOnStep(org.jooq.TableOnStep) Tools.map(org.jooq.impl.Tools.map) WindowDefinition(org.jooq.WindowDefinition) DSL.jsonbObject(org.jooq.impl.DSL.jsonbObject) Transformations.transformQualify(org.jooq.impl.Transformations.transformQualify) SelectOffsetStep(org.jooq.SelectOffsetStep) DATA_WRAP_DERIVED_TABLES_IN_PARENTHESES(org.jooq.impl.Tools.BooleanDataKey.DATA_WRAP_DERIVED_TABLES_IN_PARENTHESES) K_INLINE(org.jooq.impl.Keywords.K_INLINE) DSL.regexpReplaceAll(org.jooq.impl.DSL.regexpReplaceAll) DATA_SELECT_ALIASES(org.jooq.impl.Tools.DataKey.DATA_SELECT_ALIASES) JSONArrayAgg.patchOracleArrayAggBug(org.jooq.impl.JSONArrayAgg.patchOracleArrayAggBug) SELECT_EXCEPT(org.jooq.Clause.SELECT_EXCEPT) DEFAULT(org.jooq.SQLDialect.DEFAULT) Tools.selectQueryImpl(org.jooq.impl.Tools.selectQueryImpl) SELECT_GROUP_BY(org.jooq.Clause.SELECT_GROUP_BY) JooqLogger(org.jooq.tools.JooqLogger) Collections.emptyList(java.util.Collections.emptyList) DSL.falseCondition(org.jooq.impl.DSL.falseCondition) JSONEntry(org.jooq.JSONEntry) DATA_WINDOW_DEFINITIONS(org.jooq.impl.Tools.DataKey.DATA_WINDOW_DEFINITIONS) HSQLDB(org.jooq.SQLDialect.HSQLDB) List(java.util.List) Context(org.jooq.Context) DATA_OVERRIDE_ALIASES_IN_ORDER_BY(org.jooq.impl.Tools.DataKey.DATA_OVERRIDE_ALIASES_IN_ORDER_BY) TablePartitionByStep(org.jooq.TablePartitionByStep) Tools.recordType(org.jooq.impl.Tools.recordType) DSL.key(org.jooq.impl.DSL.key) ForeignKey(org.jooq.ForeignKey) Deque(java.util.Deque) BooleanDataKey(org.jooq.impl.Tools.BooleanDataKey) UNION(org.jooq.impl.CombineOperator.UNION) DSL.asterisk(org.jooq.impl.DSL.asterisk) SELECT_EXCEPT_ALL(org.jooq.Clause.SELECT_EXCEPT_ALL) Tools.hasAmbiguousNames(org.jooq.impl.Tools.hasAmbiguousNames) DSL.unquotedName(org.jooq.impl.DSL.unquotedName) Tools.isEmpty(org.jooq.impl.Tools.isEmpty) DESC(org.jooq.SortOrder.DESC) N_ROWNUM(org.jooq.impl.Names.N_ROWNUM) DSL.one(org.jooq.impl.DSL.one) Configuration(org.jooq.Configuration) TableOptionalOnStep(org.jooq.TableOptionalOnStep) SELECT_ORDER_BY(org.jooq.Clause.SELECT_ORDER_BY) LEFT_OUTER_JOIN(org.jooq.JoinType.LEFT_OUTER_JOIN) DSL.generateSeries(org.jooq.impl.DSL.generateSeries) OrderField(org.jooq.OrderField) Collections(java.util.Collections) DSL.noCondition(org.jooq.impl.DSL.noCondition) Condition(org.jooq.Condition) DSL.trueCondition(org.jooq.impl.DSL.trueCondition) CompareCondition(org.jooq.impl.QOM.CompareCondition) DSL.falseCondition(org.jooq.impl.DSL.falseCondition) DSL.createTable(org.jooq.impl.DSL.createTable) Table(org.jooq.Table) Tools.containsUnaliasedTable(org.jooq.impl.Tools.containsUnaliasedTable) QueryPart(org.jooq.QueryPart) SQLDialect(org.jooq.SQLDialect) DSL.jsonObject(org.jooq.impl.DSL.jsonObject) DSL.jsonbObject(org.jooq.impl.DSL.jsonbObject)

Example 23 with Table

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

the class SelectQueryImpl method getSelectResolveSomeAsterisks0.

private final SelectFieldList<SelectFieldOrAsterisk> getSelectResolveSomeAsterisks0(Configuration c, boolean resolveSupported) {
    SelectFieldList<SelectFieldOrAsterisk> result = new SelectFieldList<>();
    // [#7921] Only H2 supports the * EXCEPT (..) syntax
    boolean resolveExcept = resolveSupported || !SUPPORT_NATIVE_EXCEPT.contains(c.dialect());
    boolean resolveUnqualifiedCombined = resolveSupported || NO_SUPPORT_UNQUALIFIED_COMBINED.contains(c.dialect());
    // [#7921] TODO Find a better, more efficient way to resolve asterisks
    SelectFieldList<SelectFieldOrAsterisk> list = getSelectResolveImplicitAsterisks();
    for (SelectFieldOrAsterisk s : list) if (s instanceof Field)
        result.add(getResolveProjection(c, (Field<?>) s));
    else if (s instanceof QualifiedAsteriskImpl) {
        QualifiedAsteriskImpl q = (QualifiedAsteriskImpl) s;
        if (q.fields.isEmpty())
            if (resolveSupported)
                result.addAll(Arrays.asList(q.qualifier().fields()));
            else
                result.add(s);
        else if (resolveExcept)
            result.addAll(subtract(Arrays.asList(((QualifiedAsterisk) s).qualifier().fields()), (((QualifiedAsteriskImpl) s).fields)));
        else
            result.add(s);
    } else if (s instanceof AsteriskImpl) {
        AsteriskImpl a = (AsteriskImpl) s;
        if (a.fields.isEmpty())
            if (resolveSupported || resolveUnqualifiedCombined && list.size() > 1)
                result.addAll(resolveAsterisk(new QueryPartList<>()));
            else
                result.add(s);
        else if (resolveExcept)
            result.addAll(resolveAsterisk(new QueryPartList<>(), a.fields));
        else
            result.add(s);
    } else if (s instanceof Row)
        result.add(getResolveProjection(c, new RowAsField<Row, Record>((Row) s)));
    else if (s instanceof Table)
        result.add(getResolveProjection(c, new TableAsField<>((Table<?>) s)));
    else
        throw new AssertionError("Type not supported: " + s);
    return result;
}
Also used : DSL.createTable(org.jooq.impl.DSL.createTable) Table(org.jooq.Table) Tools.containsUnaliasedTable(org.jooq.impl.Tools.containsUnaliasedTable) TableField(org.jooq.TableField) GroupField(org.jooq.GroupField) Field(org.jooq.Field) SortField(org.jooq.SortField) SelectField(org.jooq.SelectField) OrderField(org.jooq.OrderField) Record(org.jooq.Record) QualifiedAsterisk(org.jooq.QualifiedAsterisk) Row(org.jooq.Row) SelectFieldOrAsterisk(org.jooq.SelectFieldOrAsterisk)

Example 24 with Table

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

the class InformationSchemaMetaImpl method init.

@SuppressWarnings({ "unchecked", "rawtypes" })
private final void init(InformationSchema meta) {
    List<String> errors = new ArrayList<>();
    // Catalogs
    // -------------------------------------------------------------------------------------------------------------
    boolean hasCatalogs = false;
    for (org.jooq.util.xml.jaxb.Catalog xc : meta.getCatalogs()) {
        InformationSchemaCatalog ic = new InformationSchemaCatalog(xc.getCatalogName(), xc.getComment());
        catalogs.add(ic);
        catalogsByName.put(name(xc.getCatalogName()), ic);
        hasCatalogs = true;
    }
    // -------------------------------------------------------------------------------------------------------------
    schemaLoop: for (org.jooq.util.xml.jaxb.Schema xs : meta.getSchemata()) {
        // [#6662] This is kept for backwards compatibility reasons
        if (!hasCatalogs) {
            InformationSchemaCatalog ic = new InformationSchemaCatalog(xs.getCatalogName(), null);
            if (!catalogs.contains(ic)) {
                catalogs.add(ic);
                catalogsByName.put(name(xs.getCatalogName()), ic);
            }
        }
        Name catalogName = name(xs.getCatalogName());
        Catalog catalog = catalogsByName.get(catalogName);
        if (catalog == null) {
            errors.add("Catalog " + catalogName + " not defined for schema " + xs.getSchemaName());
            continue schemaLoop;
        }
        InformationSchemaSchema is = new InformationSchemaSchema(xs.getSchemaName(), catalog, xs.getComment());
        schemas.add(is);
        schemasByName.put(name(xs.getCatalogName(), xs.getSchemaName()), is);
    }
    // -------------------------------------------------------------------------------------------------------------
    domainLoop: for (org.jooq.util.xml.jaxb.Domain d : meta.getDomains()) {
        Name schemaName = name(d.getDomainCatalog(), d.getDomainSchema());
        Schema schema = schemasByName.get(schemaName);
        if (schema == null) {
            errors.add("Schema " + schemaName + " not defined for domain " + d.getDomainName());
            continue domainLoop;
        }
        Name domainName = name(d.getDomainCatalog(), d.getDomainSchema(), d.getDomainName());
        int length = d.getCharacterMaximumLength() == null ? 0 : d.getCharacterMaximumLength();
        int precision = d.getNumericPrecision() == null ? 0 : d.getNumericPrecision();
        int scale = d.getNumericScale() == null ? 0 : d.getNumericScale();
        // TODO [#10239] Support NOT NULL constraints
        boolean nullable = true;
        List<Check<?>> checks = new ArrayList<>();
        for (org.jooq.util.xml.jaxb.DomainConstraint dc : meta.getDomainConstraints()) {
            if (domainName.equals(name(dc.getDomainCatalog(), dc.getDomainSchema(), dc.getDomainName()))) {
                Name constraintName = name(dc.getConstraintCatalog(), dc.getConstraintSchema(), dc.getConstraintName());
                for (org.jooq.util.xml.jaxb.CheckConstraint cc : meta.getCheckConstraints()) if (constraintName.equals(name(cc.getConstraintCatalog(), cc.getConstraintSchema(), cc.getConstraintName())))
                    checks.add(new CheckImpl<>(null, constraintName, DSL.condition(cc.getCheckClause()), true));
            }
        }
        InformationSchemaDomain<?> id = new InformationSchemaDomain<Object>(schema, name(d.getDomainName()), (DataType) type(d.getDataType(), length, precision, scale, nullable, false, null, null), checks.toArray(EMPTY_CHECK));
        domains.add(id);
        domainsByName.put(domainName, id);
    }
    // -------------------------------------------------------------------------------------------------------------
    tableLoop: for (org.jooq.util.xml.jaxb.Table xt : meta.getTables()) {
        Name schemaName = name(xt.getTableCatalog(), xt.getTableSchema());
        Schema schema = schemasByName.get(schemaName);
        if (schema == null) {
            errors.add("Schema " + schemaName + " not defined for table " + xt.getTableName());
            continue tableLoop;
        }
        TableType tableType;
        switch(xt.getTableType()) {
            case GLOBAL_TEMPORARY:
                tableType = TableType.TEMPORARY;
                break;
            case VIEW:
                tableType = TableType.VIEW;
                break;
            case BASE_TABLE:
            default:
                tableType = TableType.TABLE;
                break;
        }
        String sql = null;
        if (tableType == TableType.VIEW) {
            viewLoop: for (org.jooq.util.xml.jaxb.View vt : meta.getViews()) {
                if (StringUtils.equals(defaultIfNull(xt.getTableCatalog(), ""), defaultIfNull(vt.getTableCatalog(), "")) && StringUtils.equals(defaultIfNull(xt.getTableSchema(), ""), defaultIfNull(vt.getTableSchema(), "")) && StringUtils.equals(defaultIfNull(xt.getTableName(), ""), defaultIfNull(vt.getTableName(), ""))) {
                    sql = vt.getViewDefinition();
                    break viewLoop;
                }
            }
        }
        InformationSchemaTable it = new InformationSchemaTable(xt.getTableName(), schema, xt.getComment(), tableType, sql);
        tables.add(it);
        tablesByName.put(name(xt.getTableCatalog(), xt.getTableSchema(), xt.getTableName()), it);
    }
    // Columns
    // -------------------------------------------------------------------------------------------------------------
    List<Column> columns = new ArrayList<>(meta.getColumns());
    columns.sort((o1, o2) -> {
        Integer p1 = o1.getOrdinalPosition();
        Integer p2 = o2.getOrdinalPosition();
        if (Objects.equals(p1, p2))
            return 0;
        if (p1 == null)
            return -1;
        if (p2 == null)
            return 1;
        return p1.compareTo(p2);
    });
    columnLoop: for (Column xc : columns) {
        String typeName = xc.getDataType();
        int length = xc.getCharacterMaximumLength() == null ? 0 : xc.getCharacterMaximumLength();
        int precision = xc.getNumericPrecision() == null ? 0 : xc.getNumericPrecision();
        int scale = xc.getNumericScale() == null ? 0 : xc.getNumericScale();
        boolean nullable = !FALSE.equals(xc.isIsNullable());
        boolean readonly = TRUE.equals(xc.isReadonly());
        Field<?> generatedAlwaysAs = TRUE.equals(xc.isIsGenerated()) ? DSL.field(xc.getGenerationExpression()) : null;
        GenerationOption generationOption = TRUE.equals(xc.isIsGenerated()) ? "STORED".equalsIgnoreCase(xc.getGenerationOption()) ? STORED : "VIRTUAL".equalsIgnoreCase(xc.getGenerationOption()) ? VIRTUAL : null : null;
        // TODO: Exception handling should be moved inside SQLDataType
        Name tableName = name(xc.getTableCatalog(), xc.getTableSchema(), xc.getTableName());
        InformationSchemaTable table = tablesByName.get(tableName);
        if (table == null) {
            errors.add("Table " + tableName + " not defined for column " + xc.getColumnName());
            continue columnLoop;
        }
        AbstractTable.createField(name(xc.getColumnName()), type(typeName, length, precision, scale, nullable, readonly, generatedAlwaysAs, generationOption), table, xc.getComment());
    }
    // Indexes
    // -------------------------------------------------------------------------------------------------------------
    Map<Name, List<SortField<?>>> columnsByIndex = new HashMap<>();
    List<IndexColumnUsage> indexColumnUsages = new ArrayList<>(meta.getIndexColumnUsages());
    indexColumnUsages.sort(comparingInt(IndexColumnUsage::getOrdinalPosition));
    indexColumnLoop: for (IndexColumnUsage ic : indexColumnUsages) {
        Name indexName = name(ic.getIndexCatalog(), ic.getIndexSchema(), ic.getTableName(), ic.getIndexName());
        List<SortField<?>> fields = columnsByIndex.computeIfAbsent(indexName, k -> new ArrayList<>());
        Name tableName = name(ic.getTableCatalog(), ic.getTableSchema(), ic.getTableName());
        InformationSchemaTable table = tablesByName.get(tableName);
        if (table == null) {
            errors.add("Table " + tableName + " not defined for index " + indexName);
            continue indexColumnLoop;
        }
        TableField<Record, ?> field = (TableField<Record, ?>) table.field(ic.getColumnName());
        if (field == null) {
            errors.add("Column " + ic.getColumnName() + " not defined for table " + tableName);
            continue indexColumnLoop;
        }
        fields.add(Boolean.TRUE.equals(ic.isIsDescending()) ? field.desc() : field.asc());
    }
    indexLoop: for (org.jooq.util.xml.jaxb.Index i : meta.getIndexes()) {
        Name tableName = name(i.getTableCatalog(), i.getTableSchema(), i.getTableName());
        Name indexName = name(i.getIndexCatalog(), i.getIndexSchema(), i.getTableName(), i.getIndexName());
        InformationSchemaTable table = tablesByName.get(tableName);
        if (table == null) {
            errors.add("Table " + tableName + " not defined for index " + indexName);
            continue indexLoop;
        }
        List<SortField<?>> c = columnsByIndex.get(indexName);
        if (c == null || c.isEmpty()) {
            errors.add("No columns defined for index " + indexName);
            continue indexLoop;
        }
        IndexImpl index = (IndexImpl) Internal.createIndex(i.getIndexName(), table, c.toArray(EMPTY_SORTFIELD), Boolean.TRUE.equals(i.isIsUnique()));
        table.indexes.add(index);
        indexesByName.put(indexName, index);
    }
    // Constraints
    // -------------------------------------------------------------------------------------------------------------
    Map<Name, List<TableField<Record, ?>>> columnsByConstraint = new HashMap<>();
    List<KeyColumnUsage> keyColumnUsages = new ArrayList<>(meta.getKeyColumnUsages());
    keyColumnUsages.sort(comparing(KeyColumnUsage::getOrdinalPosition));
    keyColumnLoop: for (KeyColumnUsage xc : keyColumnUsages) {
        Name constraintName = name(xc.getConstraintCatalog(), xc.getConstraintSchema(), xc.getConstraintName());
        List<TableField<Record, ?>> fields = columnsByConstraint.computeIfAbsent(constraintName, k -> new ArrayList<>());
        Name tableName = name(xc.getTableCatalog(), xc.getTableSchema(), xc.getTableName());
        InformationSchemaTable table = tablesByName.get(tableName);
        if (table == null) {
            errors.add("Table " + tableName + " not defined for constraint " + constraintName);
            continue keyColumnLoop;
        }
        TableField<Record, ?> field = (TableField<Record, ?>) table.field(xc.getColumnName());
        if (field == null) {
            errors.add("Column " + xc.getColumnName() + " not defined for table " + tableName);
            continue keyColumnLoop;
        }
        fields.add(field);
    }
    tableConstraintLoop: for (TableConstraint xc : meta.getTableConstraints()) {
        switch(xc.getConstraintType()) {
            case PRIMARY_KEY:
            case UNIQUE:
                {
                    Name tableName = name(xc.getTableCatalog(), xc.getTableSchema(), xc.getTableName());
                    Name constraintName = name(xc.getConstraintCatalog(), xc.getConstraintSchema(), xc.getConstraintName());
                    InformationSchemaTable table = tablesByName.get(tableName);
                    if (table == null) {
                        errors.add("Table " + tableName + " not defined for constraint " + constraintName);
                        continue tableConstraintLoop;
                    }
                    List<TableField<Record, ?>> c = columnsByConstraint.get(constraintName);
                    if (c == null || c.isEmpty()) {
                        errors.add("No columns defined for constraint " + constraintName);
                        continue tableConstraintLoop;
                    }
                    UniqueKeyImpl<Record> key = (UniqueKeyImpl<Record>) Internal.createUniqueKey(table, xc.getConstraintName(), c.toArray(new TableField[0]));
                    if (xc.getConstraintType() == PRIMARY_KEY) {
                        table.primaryKey = key;
                        primaryKeys.add(key);
                    } else
                        table.uniqueKeys.add(key);
                    keysByName.put(constraintName, key);
                    break;
                }
        }
    }
    for (ReferentialConstraint xr : meta.getReferentialConstraints()) {
        referentialKeys.put(name(xr.getConstraintCatalog(), xr.getConstraintSchema(), xr.getConstraintName()), name(xr.getUniqueConstraintCatalog(), xr.getUniqueConstraintSchema(), xr.getUniqueConstraintName()));
    }
    tableConstraintLoop: for (TableConstraint xc : meta.getTableConstraints()) {
        switch(xc.getConstraintType()) {
            case FOREIGN_KEY:
                {
                    Name tableName = name(xc.getTableCatalog(), xc.getTableSchema(), xc.getTableName());
                    Name constraintName = name(xc.getConstraintCatalog(), xc.getConstraintSchema(), xc.getConstraintName());
                    InformationSchemaTable table = tablesByName.get(tableName);
                    if (table == null) {
                        errors.add("Table " + tableName + " not defined for constraint " + constraintName);
                        continue tableConstraintLoop;
                    }
                    List<TableField<Record, ?>> c = columnsByConstraint.get(constraintName);
                    if (c == null || c.isEmpty()) {
                        errors.add("No columns defined for constraint " + constraintName);
                        continue tableConstraintLoop;
                    }
                    UniqueKeyImpl<Record> uniqueKey = keysByName.get(referentialKeys.get(constraintName));
                    if (uniqueKey == null) {
                        errors.add("No unique key defined for foreign key " + constraintName);
                        continue tableConstraintLoop;
                    }
                    ForeignKey<Record, Record> key = Internal.createForeignKey(uniqueKey, table, xc.getConstraintName(), c.toArray(new TableField[0]));
                    table.foreignKeys.add(key);
                    break;
                }
        }
    }
    tableConstraintLoop: for (TableConstraint xc : meta.getTableConstraints()) {
        switch(xc.getConstraintType()) {
            case CHECK:
                {
                    Name tableName = name(xc.getTableCatalog(), xc.getTableSchema(), xc.getTableName());
                    Name constraintName = name(xc.getConstraintCatalog(), xc.getConstraintSchema(), xc.getConstraintName());
                    InformationSchemaTable table = tablesByName.get(tableName);
                    if (table == null) {
                        errors.add("Table " + tableName + " not defined for constraint " + constraintName);
                        continue tableConstraintLoop;
                    }
                    for (CheckConstraint cc : meta.getCheckConstraints()) {
                        if (constraintName.equals(name(cc.getConstraintCatalog(), cc.getConstraintSchema(), cc.getConstraintName()))) {
                            table.checks.add(new CheckImpl<>(table, constraintName, DSL.condition(cc.getCheckClause()), true));
                            continue tableConstraintLoop;
                        }
                    }
                    errors.add("No check clause found for check constraint " + constraintName);
                    continue tableConstraintLoop;
                }
        }
    }
    // -------------------------------------------------------------------------------------------------------------
    sequenceLoop: for (org.jooq.util.xml.jaxb.Sequence xs : meta.getSequences()) {
        Name schemaName = name(xs.getSequenceCatalog(), xs.getSequenceSchema());
        Schema schema = schemasByName.get(schemaName);
        if (schema == null) {
            errors.add("Schema " + schemaName + " not defined for sequence " + xs.getSequenceName());
            continue sequenceLoop;
        }
        String typeName = xs.getDataType();
        int length = xs.getCharacterMaximumLength() == null ? 0 : xs.getCharacterMaximumLength();
        int precision = xs.getNumericPrecision() == null ? 0 : xs.getNumericPrecision();
        int scale = xs.getNumericScale() == null ? 0 : xs.getNumericScale();
        boolean nullable = true;
        BigInteger startWith = xs.getStartValue();
        BigInteger incrementBy = xs.getIncrement();
        BigInteger minvalue = xs.getMinimumValue();
        BigInteger maxvalue = xs.getMaximumValue();
        Boolean cycle = xs.isCycleOption();
        BigInteger cache = xs.getCache();
        InformationSchemaSequence is = new InformationSchemaSequence(xs.getSequenceName(), schema, type(typeName, length, precision, scale, nullable, false, null, null), startWith, incrementBy, minvalue, maxvalue, cycle, cache);
        sequences.add(is);
    }
    // -------------------------------------------------------------------------------------------------------------
    for (Schema s : schemas) initLookup(schemasPerCatalog, s.getCatalog(), s);
    for (InformationSchemaDomain<?> d : domains) initLookup(domainsPerSchema, d.getSchema(), d);
    for (InformationSchemaTable t : tables) initLookup(tablesPerSchema, t.getSchema(), t);
    for (Sequence<?> q : sequences) initLookup(sequencesPerSchema, q.getSchema(), q);
    if (!errors.isEmpty())
        throw new IllegalArgumentException(errors.toString());
}
Also used : UniqueKey(org.jooq.UniqueKey) EMPTY_SORTFIELD(org.jooq.impl.Tools.EMPTY_SORTFIELD) Table(org.jooq.Table) HashMap(java.util.HashMap) STORED(org.jooq.impl.QOM.GenerationOption.STORED) ForeignKey(org.jooq.ForeignKey) Sequence(org.jooq.Sequence) ArrayList(java.util.ArrayList) InformationSchema(org.jooq.util.xml.jaxb.InformationSchema) Index(org.jooq.Index) Map(java.util.Map) Schema(org.jooq.Schema) BigInteger(java.math.BigInteger) Comparator.comparing(java.util.Comparator.comparing) KeyColumnUsage(org.jooq.util.xml.jaxb.KeyColumnUsage) EMPTY_CHECK(org.jooq.impl.Tools.EMPTY_CHECK) Domain(org.jooq.Domain) FALSE(java.lang.Boolean.FALSE) Record(org.jooq.Record) Comparator.comparingInt(java.util.Comparator.comparingInt) DataType(org.jooq.DataType) DSL.name(org.jooq.impl.DSL.name) SQLDialectNotSupportedException(org.jooq.exception.SQLDialectNotSupportedException) Collections.emptyList(java.util.Collections.emptyList) Name(org.jooq.Name) CheckConstraint(org.jooq.util.xml.jaxb.CheckConstraint) ReferentialConstraint(org.jooq.util.xml.jaxb.ReferentialConstraint) Check(org.jooq.Check) Field(org.jooq.Field) StringUtils(org.jooq.tools.StringUtils) PRIMARY_KEY(org.jooq.util.xml.jaxb.TableConstraintType.PRIMARY_KEY) GenerationOption(org.jooq.impl.QOM.GenerationOption) Objects(java.util.Objects) TableType(org.jooq.TableOptions.TableType) Configuration(org.jooq.Configuration) List(java.util.List) Catalog(org.jooq.Catalog) SortField(org.jooq.SortField) VIRTUAL(org.jooq.impl.QOM.GenerationOption.VIRTUAL) TableField(org.jooq.TableField) TableConstraint(org.jooq.util.xml.jaxb.TableConstraint) TableOptions(org.jooq.TableOptions) StringUtils.defaultIfNull(org.jooq.tools.StringUtils.defaultIfNull) IndexColumnUsage(org.jooq.util.xml.jaxb.IndexColumnUsage) TRUE(java.lang.Boolean.TRUE) Collections(java.util.Collections) Column(org.jooq.util.xml.jaxb.Column) HashMap(java.util.HashMap) InformationSchema(org.jooq.util.xml.jaxb.InformationSchema) Schema(org.jooq.Schema) ArrayList(java.util.ArrayList) Index(org.jooq.Index) Name(org.jooq.Name) KeyColumnUsage(org.jooq.util.xml.jaxb.KeyColumnUsage) GenerationOption(org.jooq.impl.QOM.GenerationOption) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) CheckConstraint(org.jooq.util.xml.jaxb.CheckConstraint) Sequence(org.jooq.Sequence) TableField(org.jooq.TableField) Catalog(org.jooq.Catalog) BigInteger(java.math.BigInteger) Domain(org.jooq.Domain) IndexColumnUsage(org.jooq.util.xml.jaxb.IndexColumnUsage) Field(org.jooq.Field) SortField(org.jooq.SortField) TableField(org.jooq.TableField) Column(org.jooq.util.xml.jaxb.Column) DataType(org.jooq.DataType) Record(org.jooq.Record) Table(org.jooq.Table) TableType(org.jooq.TableOptions.TableType) ReferentialConstraint(org.jooq.util.xml.jaxb.ReferentialConstraint) BigInteger(java.math.BigInteger) TableConstraint(org.jooq.util.xml.jaxb.TableConstraint)

Example 25 with Table

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

the class InformationSchemaExport method exportTable0.

private static final void exportTable0(Configuration configuration, InformationSchema result, Table<?> t, Set<Table<?>> includedTables) {
    org.jooq.util.xml.jaxb.Table it = new org.jooq.util.xml.jaxb.Table();
    String catalogName = catalogName(t);
    String schemaName = schemaName(t);
    if (!isBlank(catalogName))
        it.setTableCatalog(catalogName);
    if (!isBlank(schemaName))
        it.setTableSchema(schemaName);
    switch(t.getOptions().type()) {
        case MATERIALIZED_VIEW:
        case VIEW:
            it.setTableType(TableType.VIEW);
            break;
        case TEMPORARY:
            it.setTableType(TableType.GLOBAL_TEMPORARY);
            break;
        case FUNCTION:
        case TABLE:
        case EXPRESSION:
        case UNKNOWN:
        default:
            it.setTableType(TableType.BASE_TABLE);
            break;
    }
    it.setTableName(t.getName());
    it.setComment(t.getComment());
    result.getTables().add(it);
    if (t.getOptions().type() == org.jooq.TableOptions.TableType.VIEW) {
        org.jooq.util.xml.jaxb.View iv = new org.jooq.util.xml.jaxb.View();
        if (!isBlank(catalogName))
            iv.setTableCatalog(catalogName);
        if (!isBlank(schemaName))
            iv.setTableSchema(schemaName);
        iv.setTableName(t.getName());
        iv.setViewDefinition(t.getOptions().source());
        result.getViews().add(iv);
    }
    Field<?>[] fields = t.fields();
    for (int i = 0; i < fields.length; i++) {
        Field<?> f = fields[i];
        DataType<?> type = f.getDataType();
        Column ic = new Column();
        if (!isBlank(catalogName))
            ic.setTableCatalog(catalogName);
        if (!isBlank(schemaName))
            ic.setTableSchema(schemaName);
        ic.setTableName(t.getName());
        ic.setColumnName(f.getName());
        ic.setComment(f.getComment());
        ic.setDataType(type.getTypeName(configuration));
        if (type.lengthDefined())
            ic.setCharacterMaximumLength(type.length());
        if (type.precisionDefined())
            ic.setNumericPrecision(type.precision());
        if (type.scaleDefined())
            ic.setNumericScale(type.scale());
        ic.setColumnDefault(DSL.using(configuration).render(type.defaultValue()));
        ic.setIsNullable(type.nullable());
        ic.setOrdinalPosition(i + 1);
        ic.setReadonly(type.readonly());
        if (type.computed()) {
            ic.setIsGenerated(type.computed());
            ic.setGenerationExpression(DSL.using(configuration).render(type.generatedAlwaysAs()));
            ic.setGenerationOption(type.generationOption() == VIRTUAL ? "VIRTUAL" : type.generationOption() == STORED ? "STORED" : null);
        }
        result.getColumns().add(ic);
    }
    for (UniqueKey<?> key : t.getKeys()) exportKey0(result, t, key, key.isPrimary() ? PRIMARY_KEY : UNIQUE);
    for (ForeignKey<?, ?> fk : t.getReferences()) if (includedTables.contains(fk.getKey().getTable()))
        exportKey0(result, t, fk, FOREIGN_KEY);
    for (Check<?> chk : t.getChecks()) if (includedTables.contains(chk.getTable()))
        exportCheck0(configuration, result, t, chk);
    for (Index index : t.getIndexes()) exportIndex0(result, t, index);
}
Also used : Table(org.jooq.Table) Index(org.jooq.Index) CheckConstraint(org.jooq.util.xml.jaxb.CheckConstraint) ReferentialConstraint(org.jooq.util.xml.jaxb.ReferentialConstraint) TableConstraint(org.jooq.util.xml.jaxb.TableConstraint) Field(org.jooq.Field) SortField(org.jooq.SortField) Column(org.jooq.util.xml.jaxb.Column)

Aggregations

Table (org.jooq.Table)31 Field (org.jooq.Field)20 List (java.util.List)19 Record (org.jooq.Record)19 ArrayList (java.util.ArrayList)17 Set (java.util.Set)16 SQLDialect (org.jooq.SQLDialect)16 Map (java.util.Map)15 Collection (java.util.Collection)14 Condition (org.jooq.Condition)14 Select (org.jooq.Select)14 Configuration (org.jooq.Configuration)13 Name (org.jooq.Name)13 TableField (org.jooq.TableField)13 Arrays (java.util.Arrays)12 DSL.name (org.jooq.impl.DSL.name)12 Schema (org.jooq.Schema)11 StringUtils (org.jooq.tools.StringUtils)11 Catalog (org.jooq.Catalog)10 Row (org.jooq.Row)10