use of org.jooq.Condition in project jOOQ by jOOQ.
the class ParserImpl method parseCreateIndex.
private static final DDLQuery parseCreateIndex(ParserContext ctx) {
boolean ifNotExists = parseKeywordIf(ctx, "IF NOT EXISTS");
Name indexName = parseIndexName(ctx);
parseKeyword(ctx, "ON");
Table<?> tableName = parseTableName(ctx);
parse(ctx, '(');
Field<?>[] fieldNames = Tools.fieldsByName(parseIdentifiers(ctx));
parse(ctx, ')');
Condition condition = parseKeywordIf(ctx, "WHERE") ? parseCondition(ctx) : null;
CreateIndexStep s1 = ifNotExists ? ctx.dsl.createIndexIfNotExists(indexName) : ctx.dsl.createIndex(indexName);
CreateIndexWhereStep s2 = s1.on(tableName, fieldNames);
CreateIndexFinalStep s3 = condition != null ? s2.where(condition) : s2;
return s3;
}
use of org.jooq.Condition in project jOOQ by jOOQ.
the class ParserImpl method parseBooleanFactor.
private static final Condition parseBooleanFactor(ParserContext ctx) {
boolean not = parseKeywordIf(ctx, "NOT");
Condition condition = parseBooleanTest(ctx);
return not ? condition.not() : condition;
}
use of org.jooq.Condition 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();
}
use of org.jooq.Condition in project jOOQ by jOOQ.
the class ParserImpl method parseQueryPrimary.
private static final SelectQueryImpl<Record> parseQueryPrimary(ParserContext ctx) {
if (parseIf(ctx, '(')) {
SelectQueryImpl<Record> result = parseSelect(ctx);
parse(ctx, ')');
return result;
}
parseKeyword(ctx, "SELECT");
boolean distinct = parseKeywordIf(ctx, "DISTINCT") || parseKeywordIf(ctx, "UNIQUE");
Long limit = null;
Long offset = null;
if (!distinct)
parseKeywordIf(ctx, "ALL");
if (parseKeywordIf(ctx, "TOP")) {
limit = parseUnsignedInteger(ctx);
if (parseKeywordIf(ctx, "START AT"))
offset = parseUnsignedInteger(ctx);
}
List<Field<?>> select = parseSelectList(ctx);
List<Table<?>> from = null;
Condition startWith = null;
Condition connectBy = null;
boolean connectByNoCycle = false;
Condition where = null;
List<GroupField> groupBy = null;
Condition having = null;
if (parseKeywordIf(ctx, "FROM"))
from = parseTables(ctx);
// TODO is there a better way?
if (from != null && from.size() == 1 && from.get(0).getName().equalsIgnoreCase("dual"))
from = null;
if (parseKeywordIf(ctx, "START WITH")) {
startWith = parseCondition(ctx);
parseKeyword(ctx, "CONNECT BY");
connectByNoCycle = parseKeywordIf(ctx, "NOCYCLE");
connectBy = parseCondition(ctx);
} else if (parseKeywordIf(ctx, "CONNECT BY")) {
connectByNoCycle = parseKeywordIf(ctx, "NOCYCLE");
connectBy = parseCondition(ctx);
if (parseKeywordIf(ctx, "START WITH"))
startWith = parseCondition(ctx);
}
if (parseKeywordIf(ctx, "WHERE"))
where = parseCondition(ctx);
if (parseKeywordIf(ctx, "GROUP BY")) {
if (parseIf(ctx, '(')) {
parse(ctx, ')');
groupBy = emptyList();
} else if (parseKeywordIf(ctx, "ROLLUP")) {
parse(ctx, '(');
groupBy = singletonList(rollup(parseFields(ctx).toArray(EMPTY_FIELD)));
parse(ctx, ')');
} else if (parseKeywordIf(ctx, "CUBE")) {
parse(ctx, '(');
groupBy = singletonList(cube(parseFields(ctx).toArray(EMPTY_FIELD)));
parse(ctx, ')');
} else if (parseKeywordIf(ctx, "GROUPING SETS")) {
List<List<Field<?>>> fieldSets = new ArrayList<List<Field<?>>>();
parse(ctx, '(');
do {
parse(ctx, '(');
if (parseIf(ctx, ')')) {
fieldSets.add(Collections.<Field<?>>emptyList());
} else {
fieldSets.add(parseFields(ctx));
parse(ctx, ')');
}
} while (parseIf(ctx, ','));
parse(ctx, ')');
groupBy = singletonList(groupingSets(fieldSets.toArray((Collection[]) EMPTY_COLLECTION)));
} else {
groupBy = (List) parseFields(ctx);
if (parseKeywordIf(ctx, "WITH ROLLUP"))
groupBy = singletonList(rollup(groupBy.toArray(EMPTY_FIELD)));
}
}
if (parseKeywordIf(ctx, "HAVING"))
having = parseCondition(ctx);
// TODO support WINDOW
SelectQueryImpl<Record> result = (SelectQueryImpl<Record>) ctx.dsl.selectQuery();
if (distinct)
result.setDistinct(distinct);
if (select.size() > 0)
result.addSelect(select);
if (from != null)
result.addFrom(from);
if (connectBy != null)
if (connectByNoCycle)
result.addConnectByNoCycle(connectBy);
else
result.addConnectBy(connectBy);
if (startWith != null)
result.setConnectByStartWith(startWith);
if (where != null)
result.addConditions(where);
if (groupBy != null)
result.addGroupBy(groupBy);
if (having != null)
result.addHaving(having);
if (limit != null)
if (offset != null)
result.addLimit((int) (long) offset, (int) (long) limit);
else
result.addLimit((int) (long) limit);
return result;
}
use of org.jooq.Condition in project jOOQ by jOOQ.
the class ParserImpl method parseField.
private static final Field<?> parseField(ParserContext ctx, Type type) {
if (B.is(type)) {
Field<?> r = parseFieldAnd(ctx);
Condition c = null;
while (parseKeywordIf(ctx, "OR")) c = ((c == null) ? condition((Field) r) : c).or((Field) parseFieldAnd(ctx));
return c == null ? r : field(c);
} else {
return parseFieldConcat(ctx, type);
}
}
Aggregations