Search in sources :

Example 1 with DATA_OMIT_INTO_CLAUSE

use of org.jooq.impl.Tools.BooleanDataKey.DATA_OMIT_INTO_CLAUSE in project jOOQ by jOOQ.

the class SelectQueryImpl method accept0.

final void accept0(Context<?> context) {
    boolean topLevelCte = false;
    // Subquery scopes are started in AbstractContext
    if (context.subqueryLevel() == 0) {
        context.scopeStart();
        if (topLevelCte |= (context.data(DATA_TOP_LEVEL_CTE) == null))
            context.data(DATA_TOP_LEVEL_CTE, new TopLevelCte());
    }
    SQLDialect dialect = context.dialect();
    // [#2791] TODO: Instead of explicitly manipulating these data() objects, future versions
    // of jOOQ should implement a push / pop semantics to clearly delimit such scope.
    Object renderTrailingLimit = context.data(DATA_RENDER_TRAILING_LIMIT_IF_APPLICABLE);
    Object localWindowDefinitions = context.data(DATA_WINDOW_DEFINITIONS);
    Name[] selectAliases = (Name[]) context.data(DATA_SELECT_ALIASES);
    try {
        List<Field<?>> originalFields = null;
        List<Field<?>> alternativeFields = null;
        if (selectAliases != null) {
            context.data().remove(DATA_SELECT_ALIASES);
            alternativeFields = map(originalFields = getSelect(), (f, i) -> i < selectAliases.length ? f.as(selectAliases[i]) : f);
        }
        if (TRUE.equals(renderTrailingLimit))
            context.data().remove(DATA_RENDER_TRAILING_LIMIT_IF_APPLICABLE);
        // [#5127] Lazy initialise this map
        if (localWindowDefinitions != null)
            context.data(DATA_WINDOW_DEFINITIONS, null);
        if (intoTable != null && !TRUE.equals(context.data(DATA_OMIT_INTO_CLAUSE)) && EMULATE_SELECT_INTO_AS_CTAS.contains(dialect)) {
            context.data(DATA_OMIT_INTO_CLAUSE, true, c -> c.visit(createTable(intoTable).as(this)));
            return;
        }
        if (with != null)
            context.visit(with);
        else if (topLevelCte)
            markTopLevelCteAndAccept(context, c -> {
            });
        pushWindow(context);
        Boolean wrapDerivedTables = (Boolean) context.data(DATA_WRAP_DERIVED_TABLES_IN_PARENTHESES);
        if (TRUE.equals(wrapDerivedTables)) {
            context.sqlIndentStart('(').data().remove(DATA_WRAP_DERIVED_TABLES_IN_PARENTHESES);
        }
        switch(dialect) {
            case CUBRID:
            case FIREBIRD:
            case MYSQL:
            case YUGABYTEDB:
                {
                    if (getLimit().isApplicable() && getLimit().withTies())
                        toSQLReferenceLimitWithWindowFunctions(context);
                    else
                        toSQLReferenceLimitDefault(context, originalFields, alternativeFields);
                    break;
                }
            // By default, render the dialect's limit clause
            default:
                {
                    toSQLReferenceLimitDefault(context, originalFields, alternativeFields);
                    break;
                }
        }
        // [#1296] [#7328] FOR UPDATE is emulated in some dialects using hints
        if (forLock != null)
            context.visit(forLock);
        // end-of-query clauses are appended to the end of a query
        if (!StringUtils.isBlank(option))
            context.formatSeparator().sql(option);
        if (TRUE.equals(wrapDerivedTables))
            context.sqlIndentEnd(')').data(DATA_WRAP_DERIVED_TABLES_IN_PARENTHESES, true);
    } finally {
        context.data(DATA_WINDOW_DEFINITIONS, localWindowDefinitions);
        if (renderTrailingLimit != null)
            context.data(DATA_RENDER_TRAILING_LIMIT_IF_APPLICABLE, renderTrailingLimit);
        if (selectAliases != null)
            context.data(DATA_SELECT_ALIASES, selectAliases);
    }
    if (context.subqueryLevel() == 0)
        context.scopeEnd();
}
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) TableField(org.jooq.TableField) GroupField(org.jooq.GroupField) Field(org.jooq.Field) SortField(org.jooq.SortField) SelectField(org.jooq.SelectField) OrderField(org.jooq.OrderField) SQLDialect(org.jooq.SQLDialect) DSL.jsonObject(org.jooq.impl.DSL.jsonObject) DSL.jsonbObject(org.jooq.impl.DSL.jsonbObject) Name(org.jooq.Name) DSL.unquotedName(org.jooq.impl.DSL.unquotedName)

Example 2 with DATA_OMIT_INTO_CLAUSE

use of org.jooq.impl.Tools.BooleanDataKey.DATA_OMIT_INTO_CLAUSE 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)

Aggregations

TRUE (java.lang.Boolean.TRUE)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 ArrayDeque (java.util.ArrayDeque)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Arrays.asList (java.util.Arrays.asList)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 Collections.emptyList (java.util.Collections.emptyList)2 Deque (java.util.Deque)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 Set (java.util.Set)2 BiFunction (java.util.function.BiFunction)2 Consumer (java.util.function.Consumer)2 Function (java.util.function.Function)2 IntStream.range (java.util.stream.IntStream.range)2 Clause (org.jooq.Clause)2