Search in sources :

Example 1 with Condition

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

the class ParserImpl method parseMerge.

private static final Merge<?> parseMerge(ParserContext ctx) {
    parseKeyword(ctx, "MERGE INTO");
    Table<?> target = parseTableName(ctx);
    parseKeyword(ctx, "USING");
    parse(ctx, '(');
    TableLike<?> using = parseSelect(ctx);
    parse(ctx, ')');
    if (parseKeywordIf(ctx, "AS"))
        using = using.asTable(parseIdentifier(ctx));
    parseKeyword(ctx, "ON");
    Condition on = parseCondition(ctx);
    boolean update = false;
    boolean insert = false;
    List<Field<?>> insertColumns = null;
    List<Field<?>> insertValues = null;
    Map<Field<?>, Object> updateSet = null;
    for (; ; ) {
        if (!update && (update = parseKeywordIf(ctx, "WHEN MATCHED THEN UPDATE SET"))) {
            updateSet = parseSetClauseList(ctx);
        } else if (!insert && (insert = parseKeywordIf(ctx, "WHEN NOT MATCHED THEN INSERT"))) {
            parse(ctx, '(');
            insertColumns = Arrays.asList(Tools.fieldsByName(parseIdentifiers(ctx)));
            parse(ctx, ')');
            parseKeyword(ctx, "VALUES");
            parse(ctx, '(');
            insertValues = parseFields(ctx);
            parse(ctx, ')');
            if (insertColumns.size() != insertValues.size())
                throw ctx.exception();
        } else
            break;
    }
    if (!update && !insert)
        throw ctx.exception();
    // TODO support WHERE
    // TODO support multi clause MERGE
    // TODO support DELETE
    MergeMatchedStep<?> s1 = ctx.dsl.mergeInto(target).using(using).on(on);
    MergeNotMatchedStep<?> s2 = update ? s1.whenMatchedThenUpdate().set(updateSet) : s1;
    MergeFinalStep<?> s3 = insert ? s2.whenNotMatchedThenInsert(insertColumns).values(insertValues) : s2;
    return s3;
}
Also used : Condition(org.jooq.Condition) TableField(org.jooq.TableField) GroupField(org.jooq.GroupField) Field(org.jooq.Field) SortField(org.jooq.SortField)

Example 2 with Condition

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

the class ParserImpl method parseFieldAnd.

private static final Field<?> parseFieldAnd(ParserContext ctx) {
    Field<?> r = parseFieldCondition(ctx);
    Condition c = null;
    while (parseKeywordIf(ctx, "AND")) c = ((c == null) ? condition((Field) r) : c).and((Field) parseFieldCondition(ctx));
    return c == null ? r : field(c);
}
Also used : Condition(org.jooq.Condition) TableField(org.jooq.TableField) GroupField(org.jooq.GroupField) Field(org.jooq.Field) SortField(org.jooq.SortField)

Example 3 with Condition

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

the class ParserImpl method parseCondition.

@Override
public final Condition parseCondition(String sql) {
    ParserContext ctx = new ParserContext(dsl, sql);
    Condition result = parseCondition(ctx);
    if (!ctx.done())
        throw new ParserException(ctx);
    return result;
}
Also used : Condition(org.jooq.Condition)

Example 4 with Condition

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

the class SelectQueryImpl method getWhere.

@SuppressWarnings({ "rawtypes", "unchecked" })
final ConditionProviderImpl getWhere() {
    if (getOrderBy().isEmpty() || getSeek().isEmpty()) {
        return condition;
    } else {
        SortFieldList o = getOrderBy();
        Condition c = null;
        // predicates will become a lot more complicated.
        if (o.nulls()) {
        }
        // databases.
        if (o.size() > 1 && o.uniform()) {
            if (o.get(0).getOrder() == ASC ^ seekBefore) {
                c = row(o.fields()).gt(row(getSeek()));
            } else {
                c = row(o.fields()).lt(row(getSeek()));
            }
        } else // With alternating sorting, the SEEK clause has to be explicitly
        // phrased for each ORDER BY field.
        {
            ConditionProviderImpl or = new ConditionProviderImpl();
            for (int i = 0; i < o.size(); i++) {
                ConditionProviderImpl and = new ConditionProviderImpl();
                for (int j = 0; j < i; j++) {
                    SortFieldImpl<?> s = (SortFieldImpl<?>) o.get(j);
                    and.addConditions(((Field) s.getField()).eq(getSeek().get(j)));
                }
                SortFieldImpl<?> s = (SortFieldImpl<?>) o.get(i);
                if (s.getOrder() == ASC ^ seekBefore) {
                    and.addConditions(((Field) s.getField()).gt(getSeek().get(i)));
                } else {
                    and.addConditions(((Field) s.getField()).lt(getSeek().get(i)));
                }
                or.addConditions(OR, and);
            }
            c = or;
        }
        ConditionProviderImpl result = new ConditionProviderImpl();
        result.addConditions(condition, c);
        return result;
    }
}
Also used : Condition(org.jooq.Condition)

Example 5 with Condition

use of org.jooq.Condition 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)

Aggregations

Condition (org.jooq.Condition)29 Field (org.jooq.Field)14 GroupField (org.jooq.GroupField)11 SortField (org.jooq.SortField)11 TableField (org.jooq.TableField)11 ArrayList (java.util.ArrayList)9 List (java.util.List)5 Arrays.asList (java.util.Arrays.asList)3 Comparator (org.jooq.Comparator)3 QueryPartInternal (org.jooq.QueryPartInternal)3 Record (org.jooq.Record)3 SQLDialect (org.jooq.SQLDialect)3 Collections.emptyList (java.util.Collections.emptyList)2 LinkedHashMap (java.util.LinkedHashMap)2 ConstraintTypeStep (org.jooq.ConstraintTypeStep)2 Name (org.jooq.Name)2 Record1 (org.jooq.Record1)2 Row (org.jooq.Row)2 RowN (org.jooq.RowN)2 Table (org.jooq.Table)2