Search in sources :

Example 1 with QueryPartInternal

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

the class RowInCondition method delegate.

private final QueryPartInternal delegate(Configuration configuration) {
    if (asList(DERBY, FIREBIRD, SQLITE).contains(configuration.family())) {
        List<Condition> conditions = new ArrayList<Condition>();
        for (Row row : right) {
            conditions.add(new RowCondition(left, row, EQUALS));
        }
        Condition result = DSL.or(conditions);
        if (comparator == NOT_IN) {
            result = result.not();
        }
        return (QueryPartInternal) result;
    } else {
        return new Native();
    }
}
Also used : DSL.falseCondition(org.jooq.impl.DSL.falseCondition) Condition(org.jooq.Condition) DSL.trueCondition(org.jooq.impl.DSL.trueCondition) QueryPartInternal(org.jooq.QueryPartInternal) ArrayList(java.util.ArrayList) Row(org.jooq.Row)

Example 2 with QueryPartInternal

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

the class QueryPartCollectionView method accept.

@Override
public /* non-final */
void accept(Context<?> ctx) {
    BitSet rendersContent = new BitSet(size());
    int i = 0;
    for (T e : this) rendersContent.set(i++, ((QueryPartInternal) e).rendersContent(ctx));
    int size = rendersContent.cardinality();
    boolean format = ctx.format() && (size >= 2 && !isSimple(ctx) || size > 4);
    boolean previousQualify = ctx.qualify();
    boolean previousAlreadyIndented = TRUE.equals(ctx.data(DATA_LIST_ALREADY_INDENTED));
    boolean indent = format && !previousAlreadyIndented;
    if (previousAlreadyIndented)
        ctx.data(DATA_LIST_ALREADY_INDENTED, false);
    if (qualify != null)
        ctx.qualify(qualify);
    if (indent)
        ctx.formatIndentStart();
    if (ctx.separatorRequired())
        if (format)
            ctx.formatSeparator();
        else
            ctx.sql(' ');
    // Some lists render different SQL when empty
    if (size == 0) {
        toSQLEmptyList(ctx);
    } else {
        int j = 0;
        int k = 0;
        T prev = null;
        for (T part : this) {
            try {
                int j0 = j;
                if (!rendersContent.get(j++))
                    continue;
                if (mapper != null)
                    part = mapper.apply(part, j0);
                if (k++ > 0) {
                    // [#3607] Procedures and functions are not separated by comma
                    if (!(prev instanceof SeparatedQueryPart && ((SeparatedQueryPart) prev).rendersSeparator()))
                        ctx.sql(separator);
                    if (format)
                        ctx.formatSeparator();
                    else
                        ctx.sql(' ');
                } else if (indent)
                    ctx.formatNewLine();
                if (indent) {
                    T t = part;
                    ctx.data(DATA_LIST_ALREADY_INDENTED, t instanceof QueryPartCollectionView && ((QueryPartCollectionView<?>) t).size() > 1, c -> acceptElement(c, t));
                } else
                    acceptElement(ctx, part);
            } finally {
                prev = part;
            }
        }
    }
    if (indent)
        ctx.formatIndentEnd().formatNewLine();
    if (qualify != null)
        ctx.qualify(previousQualify);
    if (previousAlreadyIndented)
        ctx.data(DATA_LIST_ALREADY_INDENTED, previousAlreadyIndented);
}
Also used : QueryPartInternal(org.jooq.QueryPartInternal) EMPTY_QUERYPART(org.jooq.impl.Tools.EMPTY_QUERYPART) BitSet(java.util.BitSet)

Example 3 with QueryPartInternal

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

the class AbstractContext method visit0.

// ------------------------------------------------------------------------
// XXX Context API
// ------------------------------------------------------------------------
private final C visit0(QueryPart part) {
    if (part != null) {
        QueryPartInternal internal = (QueryPartInternal) part;
        // We're declaring fields, but "part" does not declare fields
        if (declareFields() && !internal.declaresFields()) {
            boolean aliases = declareAliases();
            declareFields(false);
            visit0(internal);
            declareFields(true);
            declareAliases(aliases);
        } else // We're declaring tables, but "part" does not declare tables
        if (declareTables() && !internal.declaresTables()) {
            boolean aliases = declareAliases();
            declareTables(false);
            visit0(internal);
            declareTables(true);
            declareAliases(aliases);
        } else // We're declaring windows, but "part" does not declare windows
        if (declareWindows() && !internal.declaresWindows()) {
            declareWindows(false);
            visit0(internal);
            declareWindows(true);
        } else // We're declaring cte, but "part" does not declare cte
        if (declareCTE() && !internal.declaresCTE()) {
            declareCTE(false);
            visit0(internal);
            declareCTE(true);
        } else if (!castModeOverride && castMode() != CastMode.DEFAULT && !internal.generatesCast()) {
            CastMode previous = castMode();
            castMode(CastMode.DEFAULT);
            visit0(internal);
            castMode(previous);
        } else // We're not declaring, or "part" can declare
        {
            visit0(internal);
        }
    }
    return (C) this;
}
Also used : QueryPartInternal(org.jooq.QueryPartInternal) CastMode(org.jooq.RenderContext.CastMode) ParamCastMode(org.jooq.conf.ParamCastMode)

Example 4 with QueryPartInternal

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

the class RowCondition method delegate.

private final QueryPartInternal delegate(Configuration configuration) {
    SQLDialect dialect = configuration.dialect();
    // Regular comparison predicate emulation
    if (asList(EQUALS, NOT_EQUALS).contains(comparator) && asList(DERBY, FIREBIRD, SQLITE).contains(dialect.family())) {
        List<Condition> conditions = new ArrayList<Condition>();
        Field<?>[] leftFields = left.fields();
        Field<?>[] rightFields = right.fields();
        for (int i = 0; i < leftFields.length; i++) {
            conditions.add(leftFields[i].equal((Field) rightFields[i]));
        }
        Condition result = DSL.and(conditions);
        if (comparator == NOT_EQUALS) {
            result = result.not();
        }
        return (QueryPartInternal) result;
    } else // Ordering comparison predicate emulation
    if (asList(GREATER, GREATER_OR_EQUAL, LESS, LESS_OR_EQUAL).contains(comparator) && asList(DERBY, CUBRID, FIREBIRD, SQLITE).contains(dialect.family())) {
        // The order component of the comparator (stripping the equal component)
        Comparator order = (comparator == GREATER) ? GREATER : (comparator == GREATER_OR_EQUAL) ? GREATER : (comparator == LESS) ? LESS : (comparator == LESS_OR_EQUAL) ? LESS : null;
        // [#2658] The factored order component of the comparator (enforcing the equal component)
        Comparator factoredOrder = (comparator == GREATER) ? GREATER_OR_EQUAL : (comparator == GREATER_OR_EQUAL) ? GREATER_OR_EQUAL : (comparator == LESS) ? LESS_OR_EQUAL : (comparator == LESS_OR_EQUAL) ? LESS_OR_EQUAL : null;
        // Whether the comparator has an equal component
        boolean equal = (comparator == GREATER_OR_EQUAL) || (comparator == LESS_OR_EQUAL);
        // The following algorithm emulates the equivalency of these expressions:
        // (A, B, C) > (X, Y, Z)
        // (A > X) OR (A = X AND B > Y) OR (A = X AND B = Y AND C > Z)
        List<Condition> outer = new ArrayList<Condition>();
        Field<?>[] leftFields = left.fields();
        Field<?>[] rightFields = right.fields();
        for (int i = 0; i < leftFields.length; i++) {
            List<Condition> inner = new ArrayList<Condition>();
            for (int j = 0; j < i; j++) {
                inner.add(leftFields[j].equal((Field) rightFields[j]));
            }
            inner.add(leftFields[i].compare(order, (Field) rightFields[i]));
            outer.add(DSL.and(inner));
        }
        if (equal) {
            outer.add(new RowCondition(left, right, Comparator.EQUALS));
        }
        Condition result = DSL.or(outer);
        // (A >= X) AND ((A > X) OR (A = X AND B > Y) OR (A = X AND B = Y AND C > Z))
        if (leftFields.length > 1) {
            result = leftFields[0].compare(factoredOrder, (Field) rightFields[0]).and(result);
        }
        return (QueryPartInternal) result;
    } else {
        return new Native();
    }
}
Also used : Condition(org.jooq.Condition) ArrayList(java.util.ArrayList) Comparator(org.jooq.Comparator) QueryPartInternal(org.jooq.QueryPartInternal) Field(org.jooq.Field) SQLDialect(org.jooq.SQLDialect) ArrayList(java.util.ArrayList) List(java.util.List) Arrays.asList(java.util.Arrays.asList)

Example 5 with QueryPartInternal

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

the class RowIsNull method delegate.

private final QueryPartInternal delegate(Configuration configuration) {
    // Informix doesn't implement the RVE IS NULL predicate.
    if (asList(CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, SQLITE).contains(configuration.family())) {
        List<Condition> conditions = new ArrayList<Condition>();
        for (Field<?> field : row.fields()) {
            conditions.add(isNull ? field.isNull() : field.isNotNull());
        }
        Condition result = DSL.and(conditions);
        return (QueryPartInternal) result;
    } else {
        return new Native();
    }
}
Also used : Condition(org.jooq.Condition) QueryPartInternal(org.jooq.QueryPartInternal) ArrayList(java.util.ArrayList)

Aggregations

QueryPartInternal (org.jooq.QueryPartInternal)6 ArrayList (java.util.ArrayList)3 Condition (org.jooq.Condition)3 CastMode (org.jooq.RenderContext.CastMode)2 ParamCastMode (org.jooq.conf.ParamCastMode)2 Arrays.asList (java.util.Arrays.asList)1 BitSet (java.util.BitSet)1 List (java.util.List)1 Clause (org.jooq.Clause)1 Comparator (org.jooq.Comparator)1 Field (org.jooq.Field)1 QueryPart (org.jooq.QueryPart)1 Row (org.jooq.Row)1 SQLDialect (org.jooq.SQLDialect)1 DSL.falseCondition (org.jooq.impl.DSL.falseCondition)1 DSL.trueCondition (org.jooq.impl.DSL.trueCondition)1 EMPTY_QUERYPART (org.jooq.impl.Tools.EMPTY_QUERYPART)1