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;
}
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);
}
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;
}
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;
}
}
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);
}
Aggregations