Search in sources :

Example 1 with ScopeContent

use of org.jooq.impl.ScopeMarker.ScopeContent in project jOOQ by jOOQ.

the class DefaultRenderContext method scopeEnd0.

@Override
void scopeEnd0() {
    ScopeMarker[] markers = ScopeMarker.values();
    ScopeStackElement[] beforeFirst = new ScopeStackElement[markers.length];
    ScopeStackElement[] afterLast = new ScopeStackElement[markers.length];
    ScopeContent[] content = new ScopeContent[markers.length];
    for (ScopeMarker marker : markers) {
        if (!marker.topLevelOnly || subqueryLevel() == 0) {
            int i = marker.ordinal();
            ScopeContent o = content[i] = (ScopeContent) data(marker.key);
            if (o != null && !o.isEmpty()) {
                beforeFirst[i] = scopeStack.get(marker.beforeFirst);
                afterLast[i] = scopeStack.get(marker.afterLast);
            }
        }
    }
    outer: for (ScopeStackElement e1 : scopeStack.iterable(e -> e.scopeLevel == scopeStack.scopeLevel())) {
        String replacedSQL = null;
        QueryPartList<Param<?>> insertedBindValues = null;
        if (e1.positions == null) {
            continue outer;
        } else // TODO: subqueryLevel() is lower than scopeLevel if we use implicit join in procedural logic
        if (e1.joinNode != null && !e1.joinNode.children.isEmpty()) {
            replacedSQL = configuration.dsl().renderContext().declareTables(true).sql('(').formatIndentStart(e1.indent).formatIndentStart().formatNewLine().visit(e1.joinNode.joinTree()).formatNewLine().sql(')').render();
        } else {
            elementLoop: for (int i = 0; i < beforeFirst.length; i++) {
                ScopeStackElement e = beforeFirst[i];
                ScopeContent c = content[i];
                if (e1 == e && c != null) {
                    DefaultRenderContext ctx = new DefaultRenderContext(this, false);
                    markers[i].renderer.render((DefaultRenderContext) ctx.formatIndentStart(e.indent), e, afterLast[i], c);
                    replacedSQL = ctx.render();
                    insertedBindValues = ctx.bindValues();
                    break elementLoop;
                }
            }
        }
        if (replacedSQL != null) {
            sql.replace(e1.positions[0], e1.positions[1], replacedSQL);
            int shift = replacedSQL.length() - (e1.positions[1] - e1.positions[0]);
            inner: for (ScopeStackElement e2 : scopeStack) {
                if (e2.positions == null)
                    continue inner;
                if (e2.positions[0] > e1.positions[0]) {
                    e2.positions[0] = e2.positions[0] + shift;
                    e2.positions[1] = e2.positions[1] + shift;
                }
            }
            if (insertedBindValues != null) {
                bindValues.addAll(e1.bindIndex - 1, insertedBindValues);
                inner: for (ScopeStackElement e2 : scopeStack) {
                    if (e2.positions == null)
                        continue inner;
                    if (e2.bindIndex > e1.bindIndex)
                        e2.bindIndex = e2.bindIndex + insertedBindValues.size();
                }
            }
        }
    }
}
Also used : QUOTE_START_DELIMITER(org.jooq.impl.Identifiers.QUOTE_START_DELIMITER) Arrays(java.util.Arrays) RenderKeywordCase(org.jooq.conf.RenderKeywordCase) Table(org.jooq.Table) QUOTES(org.jooq.impl.Identifiers.QUOTES) ForeignKey(org.jooq.ForeignKey) INLINED(org.jooq.conf.ParamType.INLINED) Deque(java.util.Deque) QUOTE_END_DELIMITER(org.jooq.impl.Identifiers.QUOTE_END_DELIMITER) ArrayList(java.util.ArrayList) BindContext(org.jooq.BindContext) HashSet(java.util.HashSet) RenderContext(org.jooq.RenderContext) DATA_COUNT_BIND_VALUES(org.jooq.impl.Tools.BooleanDataKey.DATA_COUNT_BIND_VALUES) SQLITE(org.jooq.SQLDialect.SQLITE) DATA_PREPEND_SQL(org.jooq.impl.Tools.DataKey.DATA_PREPEND_SQL) DATA_APPEND_SQL(org.jooq.impl.Tools.DataKey.DATA_APPEND_SQL) RenderNameCase(org.jooq.conf.RenderNameCase) SQLDialect(org.jooq.SQLDialect) QUOTE_END_DELIMITER_ESCAPED(org.jooq.impl.Identifiers.QUOTE_END_DELIMITER_ESCAPED) Select(org.jooq.Select) ControlFlowSignal(org.jooq.exception.ControlFlowSignal) DataAccessException(org.jooq.exception.DataAccessException) QueryPartInternal(org.jooq.QueryPartInternal) JooqLogger(org.jooq.tools.JooqLogger) SettingsTools(org.jooq.conf.SettingsTools) SettingsTools.renderLocale(org.jooq.conf.SettingsTools.renderLocale) Set(java.util.Set) Constants(org.jooq.Constants) Settings(org.jooq.conf.Settings) Field(org.jooq.Field) StringUtils(org.jooq.tools.StringUtils) QueryPart(org.jooq.QueryPart) ScopeContent(org.jooq.impl.ScopeMarker.ScopeContent) Param(org.jooq.Param) RenderQuotedNames(org.jooq.conf.RenderQuotedNames) Configuration(org.jooq.Configuration) List(java.util.List) Pattern(java.util.regex.Pattern) RenderFormatting(org.jooq.conf.RenderFormatting) ArrayDeque(java.util.ArrayDeque) Query(org.jooq.Query) TRUE(java.lang.Boolean.TRUE) ScopeContent(org.jooq.impl.ScopeMarker.ScopeContent)

Aggregations

TRUE (java.lang.Boolean.TRUE)1 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Deque (java.util.Deque)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 Pattern (java.util.regex.Pattern)1 BindContext (org.jooq.BindContext)1 Configuration (org.jooq.Configuration)1 Constants (org.jooq.Constants)1 Field (org.jooq.Field)1 ForeignKey (org.jooq.ForeignKey)1 Param (org.jooq.Param)1 Query (org.jooq.Query)1 QueryPart (org.jooq.QueryPart)1 QueryPartInternal (org.jooq.QueryPartInternal)1 RenderContext (org.jooq.RenderContext)1 SQLDialect (org.jooq.SQLDialect)1