use of org.jooq.impl.SQLDataType.BOOLEAN in project jOOQ by jOOQ.
the class DefaultParseContext method parseUpdate.
private final Query parseUpdate(WithImpl with, boolean parseResultQuery) {
parseKeyword("UPDATE", "UPD");
Field<Long> limit = null;
// T-SQL style TOP .. START AT
if (parseKeywordIf("TOP")) {
limit = (Field) parseField();
// [#8623] TODO Support this
// percent = parseKeywordIf("PERCENT") && requireProEdition();
}
Table<?> table = parseTable(() -> peekKeyword(KEYWORDS_IN_UPDATE_FROM));
scope(table);
UpdateSetFirstStep<?> s1 = (with == null ? dsl.update(table) : with.update(table));
List<Table<?>> from = parseKeywordIf("FROM") ? parseList(',', t -> parseTable(() -> peekKeyword(KEYWORDS_IN_UPDATE_FROM))) : null;
parseKeyword("SET");
UpdateFromStep<?> s2;
if (peek('(')) {
Row row = parseRow();
parse('=');
// TODO Can we extract a public API for this?
if (peekSelectOrWith(true))
((UpdateImpl<?>) s1).getDelegate().addValues0(row, parseWithOrSelect(row.size()));
else
((UpdateImpl<?>) s1).getDelegate().addValues0(row, parseRow(row.size()));
s2 = (UpdateFromStep<?>) s1;
} else {
Map<Field<?>, Object> map = parseSetClauseList();
s2 = s1.set(map);
}
UpdateWhereStep<?> s3 = from != null ? s2.from(from) : parseKeywordIf("FROM") ? s2.from(parseList(',', t -> parseTable(() -> peekKeyword(KEYWORDS_IN_UPDATE_FROM)))) : s2;
UpdateOrderByStep<?> s4 = parseKeywordIf("ALL") ? s3 : parseKeywordIf("WHERE") ? s3.where(parseCondition()) : s3;
UpdateLimitStep<?> s5 = parseKeywordIf("ORDER BY") ? s4.orderBy(parseList(',', c -> c.parseSortField())) : s4;
UpdateReturningStep<?> s6 = (limit != null || parseKeywordIf("LIMIT")) ? s5.limit(limit != null ? limit : (Field) parseField()) : s5;
return (parseResultQuery ? parseKeyword("RETURNING") : parseKeywordIf("RETURNING")) ? s6.returning(parseSelectList()) : s6;
}
use of org.jooq.impl.SQLDataType.BOOLEAN in project jOOQ by jOOQ.
the class DefaultParseContext method parseWindowSpecificationIf.
private final WindowSpecification parseWindowSpecificationIf(Name windowName, boolean orderByAllowed) {
final WindowSpecificationOrderByStep s1;
final WindowSpecificationRowsStep s2;
final WindowSpecificationRowsAndStep s3;
final WindowSpecificationExcludeStep s4;
final WindowSpecification result;
s1 = windowName != null ? windowName.as() : parseKeywordIf("PARTITION BY") ? partitionBy(parseList(',', c -> c.parseField())) : null;
if (parseKeywordIf("ORDER BY"))
if (orderByAllowed)
s2 = s1 == null ? orderBy(parseList(',', c -> c.parseSortField())) : s1.orderBy(parseList(',', c -> c.parseSortField()));
else
throw exception("ORDER BY not allowed");
else
s2 = s1;
boolean rows = parseKeywordIf("ROWS");
boolean range = !rows && parseKeywordIf("RANGE");
boolean groups = !rows && !range && parseKeywordIf("GROUPS");
if ((rows || range || groups) && !orderByAllowed)
throw exception("ROWS, RANGE, or GROUPS not allowed");
if (rows || range || groups) {
Long n;
if (parseKeywordIf("BETWEEN")) {
if (parseKeywordIf("UNBOUNDED"))
if (parseKeywordIf("PRECEDING"))
s3 = s2 == null ? rows ? rowsBetweenUnboundedPreceding() : range ? rangeBetweenUnboundedPreceding() : groupsBetweenUnboundedPreceding() : rows ? s2.rowsBetweenUnboundedPreceding() : range ? s2.rangeBetweenUnboundedPreceding() : s2.groupsBetweenUnboundedPreceding();
else if (parseKeywordIf("FOLLOWING"))
s3 = s2 == null ? rows ? rowsBetweenUnboundedFollowing() : range ? rangeBetweenUnboundedFollowing() : groupsBetweenUnboundedFollowing() : rows ? s2.rowsBetweenUnboundedFollowing() : range ? s2.rangeBetweenUnboundedFollowing() : s2.groupsBetweenUnboundedFollowing();
else
throw expected("FOLLOWING", "PRECEDING");
else if (parseKeywordIf("CURRENT ROW"))
s3 = s2 == null ? rows ? rowsBetweenCurrentRow() : range ? rangeBetweenCurrentRow() : groupsBetweenCurrentRow() : rows ? s2.rowsBetweenCurrentRow() : range ? s2.rangeBetweenCurrentRow() : s2.groupsBetweenCurrentRow();
else if ((n = parseUnsignedIntegerLiteralIf()) != null)
if (parseKeywordIf("PRECEDING"))
s3 = s2 == null ? rows ? rowsBetweenPreceding(n.intValue()) : range ? rangeBetweenPreceding(n.intValue()) : groupsBetweenPreceding(n.intValue()) : rows ? s2.rowsBetweenPreceding(n.intValue()) : range ? s2.rangeBetweenPreceding(n.intValue()) : s2.groupsBetweenPreceding(n.intValue());
else if (parseKeywordIf("FOLLOWING"))
s3 = s2 == null ? rows ? rowsBetweenFollowing(n.intValue()) : range ? rangeBetweenFollowing(n.intValue()) : groupsBetweenFollowing(n.intValue()) : rows ? s2.rowsBetweenFollowing(n.intValue()) : range ? s2.rangeBetweenFollowing(n.intValue()) : s2.groupsBetweenFollowing(n.intValue());
else
throw expected("FOLLOWING", "PRECEDING");
else
throw expected("CURRENT ROW", "UNBOUNDED", "integer literal");
parseKeyword("AND");
if (parseKeywordIf("UNBOUNDED"))
if (parseKeywordIf("PRECEDING"))
s4 = s3.andUnboundedPreceding();
else if (parseKeywordIf("FOLLOWING"))
s4 = s3.andUnboundedFollowing();
else
throw expected("FOLLOWING", "PRECEDING");
else if (parseKeywordIf("CURRENT ROW"))
s4 = s3.andCurrentRow();
else if (asTrue(n = parseUnsignedIntegerLiteral()))
if (parseKeywordIf("PRECEDING"))
s4 = s3.andPreceding(n.intValue());
else if (parseKeywordIf("FOLLOWING"))
s4 = s3.andFollowing(n.intValue());
else
throw expected("FOLLOWING", "PRECEDING");
else
throw expected("CURRENT ROW", "UNBOUNDED", "integer literal");
} else if (parseKeywordIf("UNBOUNDED"))
if (parseKeywordIf("PRECEDING"))
s4 = s2 == null ? rows ? rowsUnboundedPreceding() : range ? rangeUnboundedPreceding() : groupsUnboundedPreceding() : rows ? s2.rowsUnboundedPreceding() : range ? s2.rangeUnboundedPreceding() : s2.groupsUnboundedPreceding();
else if (parseKeywordIf("FOLLOWING"))
s4 = s2 == null ? rows ? rowsUnboundedFollowing() : range ? rangeUnboundedFollowing() : groupsUnboundedFollowing() : rows ? s2.rowsUnboundedFollowing() : range ? s2.rangeUnboundedFollowing() : s2.groupsUnboundedFollowing();
else
throw expected("FOLLOWING", "PRECEDING");
else if (parseKeywordIf("CURRENT ROW"))
s4 = s2 == null ? rows ? rowsCurrentRow() : range ? rangeCurrentRow() : groupsCurrentRow() : rows ? s2.rowsCurrentRow() : range ? s2.rangeCurrentRow() : s2.groupsCurrentRow();
else if (asTrue(n = parseUnsignedIntegerLiteral()))
if (parseKeywordIf("PRECEDING"))
s4 = s2 == null ? rows ? rowsPreceding(n.intValue()) : range ? rangePreceding(n.intValue()) : groupsPreceding(n.intValue()) : rows ? s2.rowsPreceding(n.intValue()) : range ? s2.rangePreceding(n.intValue()) : s2.groupsPreceding(n.intValue());
else if (parseKeywordIf("FOLLOWING"))
s4 = s2 == null ? rows ? rowsFollowing(n.intValue()) : range ? rangeFollowing(n.intValue()) : groupsFollowing(n.intValue()) : rows ? s2.rowsFollowing(n.intValue()) : range ? s2.rangeFollowing(n.intValue()) : s2.groupsFollowing(n.intValue());
else
throw expected("FOLLOWING", "PRECEDING");
else
throw expected("BETWEEN", "CURRENT ROW", "UNBOUNDED", "integer literal");
if (parseKeywordIf("EXCLUDE"))
if (parseKeywordIf("CURRENT ROW"))
result = s4.excludeCurrentRow();
else if (parseKeywordIf("TIES"))
result = s4.excludeTies();
else if (parseKeywordIf("GROUP"))
result = s4.excludeGroup();
else if (parseKeywordIf("NO OTHERS"))
result = s4.excludeNoOthers();
else
throw expected("CURRENT ROW", "TIES", "GROUP", "NO OTHERS");
else
result = s4;
} else
result = s2;
if (result != null)
return result;
else if (windowName != null)
return null;
else if ((windowName = parseIdentifierIf()) != null)
return parseWindowSpecificationIf(windowName, orderByAllowed);
else
return null;
}
use of org.jooq.impl.SQLDataType.BOOLEAN in project jOOQ by jOOQ.
the class DefaultParseContext method parseJSONArrayAggFunctionIf.
private final AggregateFilterStep<?> parseJSONArrayAggFunctionIf() {
boolean jsonb = false;
if (parseFunctionNameIf("JSON_ARRAYAGG", "JSON_AGG", "JSON_GROUP_ARRAY") || (jsonb = parseFunctionNameIf("JSONB_AGG"))) {
AggregateFilterStep<?> result;
JSONArrayAggOrderByStep<?> s1;
JSONArrayAggNullStep<?> s2;
JSONArrayAggReturningStep<?> s3;
JSONOnNull onNull;
DataType<?> returning;
parse('(');
result = s3 = s2 = s1 = jsonb ? jsonbArrayAgg(parseField()) : jsonArrayAgg(parseField());
if (parseKeywordIf("ORDER BY"))
result = s3 = s2 = s1.orderBy(parseList(',', c -> c.parseSortField()));
if ((onNull = parseJSONNullTypeIf()) != null)
result = s3 = onNull == ABSENT_ON_NULL ? s2.absentOnNull() : s2.nullOnNull();
if ((returning = parseJSONReturningIf()) != null)
result = s3.returning(returning);
parse(')');
return result;
}
return null;
}
use of org.jooq.impl.SQLDataType.BOOLEAN in project jOOQ by jOOQ.
the class DefaultParseContext method parseFieldJSONObjectConstructorIf.
private final Field<?> parseFieldJSONObjectConstructorIf() {
boolean jsonb = false;
if (parseFunctionNameIf("JSON_OBJECT", "JSON_BUILD_OBJECT") || (jsonb = parseFunctionNameIf("JSONB_BUILD_OBJECT"))) {
parse('(');
if (parseIf(')'))
return jsonb ? jsonbObject() : jsonObject();
List<JSONEntry<?>> result;
JSONOnNull onNull = parseJSONNullTypeIf();
DataType<?> returning = parseJSONReturningIf();
if (onNull == null && returning == null) {
result = parseList(',', c -> parseJSONEntry());
onNull = parseJSONNullTypeIf();
returning = parseJSONReturningIf();
} else
result = new ArrayList<>();
parse(')');
JSONObjectNullStep<?> s1 = jsonb ? jsonbObject(result) : jsonObject(result);
JSONObjectReturningStep<?> s2 = onNull == NULL_ON_NULL ? s1.nullOnNull() : onNull == ABSENT_ON_NULL ? s1.absentOnNull() : s1;
return returning == null ? s2 : s2.returning(returning);
}
return null;
}
use of org.jooq.impl.SQLDataType.BOOLEAN in project jOOQ by jOOQ.
the class DefaultParseContext method parseInsert.
private final Query parseInsert(WithImpl with, boolean parseResultQuery) {
scopeStart();
parseKeyword("INSERT", "INS");
parseKeywordIf("INTO");
Table<?> table = parseTableNameIf();
if (table == null)
table = table(parseSelect());
Name alias;
if (parseKeywordIf("AS"))
table = table.as(parseIdentifier());
else if (!peekKeyword("DEFAULT VALUES", "SEL", "SELECT", "SET", "VALUES") && (alias = parseIdentifierIf()) != null)
table = table.as(alias);
scope(table);
InsertSetStep<?> s1 = (with == null ? dsl.insertInto(table) : with.insertInto(table));
Field<?>[] fields = null;
if (!peekSelectOrWith(true) && parseIf('(') && !parseIf(')')) {
fields = parseList(',', c -> parseField()).toArray(EMPTY_FIELD);
parse(')');
}
InsertOnDuplicateStep<?> onDuplicate;
InsertReturningStep<?> returning;
try {
// [#11821] The Teradata INSERT INTO t (1, 2) syntax can be recognised:
// When there are non-references fields
boolean hasExpressions = anyMatch(fields, f -> !(f instanceof TableField));
if (hasExpressions || parseKeywordIf("VALUES")) {
List<List<Field<?>>> allValues = new ArrayList<>();
if (hasExpressions) {
allValues.add(asList(fields));
fields = null;
}
valuesLoop: do {
if (hasExpressions && !parseIf(','))
break valuesLoop;
parse('(');
// [#6936] MySQL treats an empty VALUES() clause as the same thing as the standard DEFAULT VALUES
if (fields == null && parseIf(')'))
break valuesLoop;
List<Field<?>> values = parseList(',', c -> c.parseKeywordIf("DEFAULT") ? default_() : c.parseField());
if (fields != null && fields.length != values.size())
throw exception("Insert field size (" + fields.length + ") must match values size (" + values.size() + ")");
allValues.add(values);
parse(')');
} while (parseIf(','));
InsertValuesStepN<?> step2 = (fields != null) ? s1.columns(fields) : (InsertValuesStepN<?>) s1;
for (List<Field<?>> values : allValues) step2 = step2.values(values);
returning = onDuplicate = step2;
} else if (parseKeywordIf("SET")) {
Map<Field<?>, Object> map = parseSetClauseList();
returning = onDuplicate = s1.set(map);
} else if (peekSelectOrWith(true)) {
// [#10954] These are moved into the INSERT .. SELECT clause handling. They should not be necessary here
// either, but it seems we currently don't correctly implement nesting scopes?
scopeEnd(null);
scopeStart();
Select<?> select = parseWithOrSelect();
returning = onDuplicate = (fields == null) ? s1.select(select) : s1.columns(fields).select(select);
} else if (parseKeywordIf("DEFAULT VALUES")) {
if (fields != null)
throw notImplemented("DEFAULT VALUES without INSERT field list");
else
returning = onDuplicate = s1.defaultValues();
} else
throw expected("DEFAULT VALUES", "WITH", "SELECT", "SET", "VALUES");
if (parseKeywordIf("ON")) {
if (parseKeywordIf("DUPLICATE KEY UPDATE")) {
parseKeywordIf("SET");
InsertOnConflictWhereStep<?> where = onDuplicate.onDuplicateKeyUpdate().set(parseSetClauseList());
if (parseKeywordIf("WHERE"))
returning = where.where(parseCondition());
else
returning = where;
} else if (parseKeywordIf("DUPLICATE KEY IGNORE")) {
returning = onDuplicate.onDuplicateKeyIgnore();
} else if (parseKeywordIf("CONFLICT")) {
InsertOnConflictDoUpdateStep<?> doUpdate;
if (parseKeywordIf("ON CONSTRAINT")) {
doUpdate = onDuplicate.onConflictOnConstraint(parseName());
} else if (parseIf('(')) {
InsertOnConflictWhereIndexPredicateStep<?> where = onDuplicate.onConflict(parseList(',', c -> parseFieldName()));
parse(')');
doUpdate = parseKeywordIf("WHERE") ? where.where(parseCondition()) : where;
} else {
doUpdate = onDuplicate.onConflict();
}
parseKeyword("DO");
if (parseKeywordIf("NOTHING")) {
returning = doUpdate.doNothing();
} else if (parseKeywordIf("UPDATE SET")) {
InsertOnConflictWhereStep<?> where = doUpdate.doUpdate().set(parseSetClauseList());
if (parseKeywordIf("WHERE"))
returning = where.where(parseCondition());
else
returning = where;
} else
throw expected("NOTHING", "UPDATE");
} else
throw expected("CONFLICT", "DUPLICATE");
}
return (parseResultQuery ? parseKeyword("RETURNING") : parseKeywordIf("RETURNING")) ? returning.returning(parseSelectList()) : returning;
} finally {
scopeEnd(((InsertImpl) s1).getDelegate());
}
}
Aggregations