Search in sources :

Example 1 with Comparator

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

the class ParserImpl method parseBooleanPrimary.

private static final Condition parseBooleanPrimary(ParserContext ctx) {
    if (parseIf(ctx, '(')) {
        Condition result = parseCondition(ctx);
        parse(ctx, ')');
        return result;
    }
    TruthValue truth = parseTruthValueIf(ctx);
    if (truth != null) {
        Comparator comp = parseComparatorIf(ctx);
        switch(truth) {
            case TRUE:
                return comp == null ? condition(true) : inline(true).compare(comp, (Field<Boolean>) parseField(ctx));
            case FALSE:
                return comp == null ? condition(false) : inline(false).compare(comp, (Field<Boolean>) parseField(ctx));
            case NULL:
                return comp == null ? condition((Boolean) null) : inline((Boolean) null).compare(comp, (Field<Boolean>) parseField(ctx));
            default:
                throw ctx.exception();
        }
    }
    return parsePredicate(ctx);
}
Also used : Condition(org.jooq.Condition) TableField(org.jooq.TableField) GroupField(org.jooq.GroupField) Field(org.jooq.Field) SortField(org.jooq.SortField) Comparator(org.jooq.Comparator)

Example 2 with Comparator

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

the class CompareCondition method accept.

@Override
public final void accept(Context<?> ctx) {
    SQLDialect family = ctx.family();
    Field<?> lhs = field1;
    Field<?> rhs = field2;
    Comparator op = comparator;
    // [#293] TODO: This could apply to other operators, too
    if ((op == LIKE || op == NOT_LIKE) && field1.getType() != String.class && asList(DERBY, POSTGRES).contains(family)) {
        lhs = lhs.cast(String.class);
    } else // need to emulate this as LOWER(lhs) LIKE LOWER(rhs)
    if ((op == LIKE_IGNORE_CASE || op == NOT_LIKE_IGNORE_CASE) && POSTGRES != family) {
        lhs = lhs.lower();
        rhs = rhs.lower();
        op = (op == LIKE_IGNORE_CASE ? LIKE : NOT_LIKE);
    }
    ctx.visit(lhs).sql(' ');
    boolean castRhs = false;
    ParamType previousParamType = ctx.paramType();
    ParamType forcedParamType = previousParamType;
    ctx.keyword(op.toSQL()).sql(' ');
    if (castRhs)
        ctx.keyword("cast").sql('(');
    ctx.paramType(forcedParamType).visit(rhs).paramType(previousParamType);
    if (castRhs)
        ctx.sql(' ').keyword("as").sql(' ').keyword("varchar").sql("(4000))");
    if (escape != null) {
        ctx.sql(' ').keyword("escape").sql(' ').visit(inline(escape));
    }
}
Also used : SQLDialect(org.jooq.SQLDialect) ParamType(org.jooq.conf.ParamType) Comparator(org.jooq.Comparator)

Example 3 with Comparator

use of org.jooq.Comparator 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 4 with Comparator

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

the class ParserImpl method parsePredicate.

private static final Condition parsePredicate(ParserContext ctx) {
    if (parseKeywordIf(ctx, "EXISTS")) {
        parse(ctx, '(');
        Select<?> select = parseSelect(ctx);
        parse(ctx, ')');
        return exists(select);
    } else {
        // TODO row value expressions
        Field left;
        Comparator comp;
        boolean not;
        left = parseFieldConcat(ctx, null);
        not = parseKeywordIf(ctx, "NOT");
        if (!not && (comp = parseComparatorIf(ctx)) != null) {
            boolean all = parseKeywordIf(ctx, "ALL");
            boolean any = !all && (parseKeywordIf(ctx, "ANY") || parseKeywordIf(ctx, "SOME"));
            if (all || any)
                parse(ctx, '(');
            Condition result = all ? left.compare(comp, DSL.all(parseSelect(ctx))) : any ? left.compare(comp, DSL.any(parseSelect(ctx))) : left.compare(comp, parseFieldConcat(ctx, null));
            if (all || any)
                parse(ctx, ')');
            return result;
        } else if (!not && parseKeywordIf(ctx, "IS")) {
            not = parseKeywordIf(ctx, "NOT");
            if (parseKeywordIf(ctx, "NULL"))
                return not ? left.isNotNull() : left.isNull();
            parseKeyword(ctx, "DISTINCT FROM");
            Field right = parseFieldConcat(ctx, null);
            return not ? left.isNotDistinctFrom(right) : left.isDistinctFrom(right);
        } else if (parseKeywordIf(ctx, "IN")) {
            Condition result;
            parse(ctx, '(');
            if (peekKeyword(ctx, "SELECT"))
                result = not ? left.notIn(parseSelect(ctx)) : left.in(parseSelect(ctx));
            else
                result = not ? left.notIn(parseFields(ctx)) : left.in(parseFields(ctx));
            parse(ctx, ')');
            return result;
        } else if (parseKeywordIf(ctx, "BETWEEN")) {
            boolean symmetric = parseKeywordIf(ctx, "SYMMETRIC");
            Field r1 = parseFieldConcat(ctx, null);
            parseKeyword(ctx, "AND");
            Field r2 = parseFieldConcat(ctx, null);
            return symmetric ? not ? left.notBetweenSymmetric(r1, r2) : left.betweenSymmetric(r1, r2) : not ? left.notBetween(r1, r2) : left.between(r1, r2);
        } else if (parseKeywordIf(ctx, "LIKE")) {
            Field right = parseFieldConcat(ctx, null);
            boolean escape = parseKeywordIf(ctx, "ESCAPE");
            char character = escape ? parseCharacterLiteral(ctx) : ' ';
            return escape ? not ? left.notLike(right, character) : left.like(right, character) : not ? left.notLike(right) : left.like(right);
        }
    }
    throw ctx.exception();
}
Also used : Condition(org.jooq.Condition) TableField(org.jooq.TableField) GroupField(org.jooq.GroupField) Field(org.jooq.Field) SortField(org.jooq.SortField) Comparator(org.jooq.Comparator)

Aggregations

Comparator (org.jooq.Comparator)4 Condition (org.jooq.Condition)3 Field (org.jooq.Field)3 GroupField (org.jooq.GroupField)2 SQLDialect (org.jooq.SQLDialect)2 SortField (org.jooq.SortField)2 TableField (org.jooq.TableField)2 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 List (java.util.List)1 QueryPartInternal (org.jooq.QueryPartInternal)1 ParamType (org.jooq.conf.ParamType)1