use of org.jooq.SQL 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.SQL in project jOOQ by jOOQ.
the class DefaultParseContext method parseCreateTable.
private final DDLQuery parseCreateTable(boolean temporary) {
boolean ifNotExists = parseKeywordIf("IF NOT EXISTS");
Table<?> tableName = DSL.table(parseTableName().getQualifiedName());
if (parseKeywordIf("USING"))
parseIdentifier();
CreateTableOnCommitStep onCommitStep;
CreateTableCommentStep commentStep;
List<Field<?>> fields = new ArrayList<>();
List<Constraint> constraints = new ArrayList<>();
List<Index> indexes = new ArrayList<>();
boolean primary = false;
boolean identity = false;
boolean readonly = false;
boolean ctas = false;
if (!peekSelectOrWith(true) && parseIf('(')) {
columnLoop: do {
int p = position();
ConstraintTypeStep constraint = parseConstraintNameSpecification();
if (parsePrimaryKeyClusteredNonClusteredKeywordIf()) {
if (primary)
throw exception("Duplicate primary key specification");
primary = true;
constraints.add(parsePrimaryKeySpecification(constraint));
continue columnLoop;
} else if (parseKeywordIf("UNIQUE")) {
if (!parseKeywordIf("KEY"))
parseKeywordIf("INDEX");
// [#9132] Avoid parsing "using" as an identifier
parseUsingIndexTypeIf();
// [#7268] MySQL has some legacy syntax where an index name
// can override a constraint name
Name index = parseIdentifierIf();
if (index != null)
constraint = constraint(index);
constraints.add(parseUniqueSpecification(constraint));
continue columnLoop;
} else if (parseKeywordIf("FOREIGN KEY")) {
constraints.add(parseForeignKeySpecification(constraint));
continue columnLoop;
} else if (parseKeywordIf("CHECK")) {
constraints.add(parseCheckSpecification(constraint));
continue columnLoop;
} else if (constraint == null && parseIndexOrKeyIf()) {
parseUsingIndexTypeIf();
int p2 = position();
// Look ahead if the next tokens indicate a MySQL index definition
if (parseIf('(') || (parseDataTypeIf(false) == null && parseIdentifierIf() != null && parseUsingIndexTypeIf() && parseIf('('))) {
position(p2);
indexes.add(parseIndexSpecification(tableName));
parseUsingIndexTypeIf();
continue columnLoop;
} else {
position(p);
}
} else if (constraint != null)
throw expected("CHECK", "CONSTRAINT", "FOREIGN KEY", "INDEX", "KEY", "PRIMARY KEY", "UNIQUE");
Name fieldName = parseIdentifier();
boolean skipType = peek(',') || peek(')');
// If only we had multiple return values or destructuring...
ParseInlineConstraints inlineConstraints = parseInlineConstraints(fieldName, !skipType ? parseDataType() : SQLDataType.OTHER, constraints, primary, identity, readonly);
primary = inlineConstraints.primary;
identity = inlineConstraints.identity;
fields.add(field(fieldName, inlineConstraints.type, inlineConstraints.fieldComment));
} while (parseIf(','));
if (fields.isEmpty())
throw expected("At least one column");
parse(')');
} else
ctas = true;
CreateTableElementListStep elementListStep = ifNotExists ? temporary ? dsl.createTemporaryTableIfNotExists(tableName) : dsl.createTableIfNotExists(tableName) : temporary ? dsl.createTemporaryTable(tableName) : dsl.createTable(tableName);
if (!fields.isEmpty())
elementListStep = elementListStep.columns(fields);
CreateTableElementListStep constraintStep = constraints.isEmpty() ? elementListStep : elementListStep.constraints(constraints);
CreateTableAsStep asStep = indexes.isEmpty() ? constraintStep : constraintStep.indexes(indexes);
// [#6133] Historically, the jOOQ API places the ON COMMIT clause after
// the AS clause, which doesn't correspond to dialect implementations
Function<CreateTableOnCommitStep, CreateTableCommentStep> onCommit;
if (temporary && parseKeywordIf("ON COMMIT")) {
if (parseKeywordIf("DELETE ROWS"))
onCommit = CreateTableOnCommitStep::onCommitDeleteRows;
else if (parseKeywordIf("DROP"))
onCommit = CreateTableOnCommitStep::onCommitDrop;
else if (parseKeywordIf("PRESERVE ROWS"))
onCommit = CreateTableOnCommitStep::onCommitPreserveRows;
else
throw unsupportedClause();
} else
onCommit = s -> s;
// keyword only for empty field lists
if (parseKeywordIf("AS") || fields.isEmpty() && peekSelectOrWith(true)) {
boolean previousMetaLookupsForceIgnore = metaLookupsForceIgnore();
CreateTableWithDataStep withDataStep = asStep.as((Select<Record>) metaLookupsForceIgnore(false).parseQuery(true, true));
metaLookupsForceIgnore(previousMetaLookupsForceIgnore);
onCommitStep = parseKeywordIf("WITH DATA") ? withDataStep.withData() : parseKeywordIf("WITH NO DATA") ? withDataStep.withNoData() : withDataStep;
} else if (ctas)
throw expected("AS, WITH, SELECT, or (");
else
onCommitStep = asStep;
commentStep = onCommit.apply(onCommitStep);
List<SQL> storage = new ArrayList<>();
Comment comment = null;
storageLoop: for (boolean first = true; ; first = false) {
boolean optional = first || !parseIf(',');
Keyword keyword = null;
// MySQL storage clauses (see: https://dev.mysql.com/doc/refman/5.7/en/create-table.html)
if ((keyword = parseAndGetKeywordIf("AUTO_INCREMENT")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseFieldUnsignedNumericLiteral(Sign.NONE)));
} else if ((keyword = parseAndGetKeywordIf("AVG_ROW_LENGTH")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseFieldUnsignedNumericLiteral(Sign.NONE)));
} else if ((keyword = parseAndGetKeywordIf("CHARACTER SET")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseIdentifier()));
} else if ((keyword = parseAndGetKeywordIf("DEFAULT CHARACTER SET")) != null || (keyword = parseAndGetKeywordIf("DEFAULT CHARSET")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseIdentifier()));
} else if ((keyword = parseAndGetKeywordIf("CHECKSUM")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseZeroOne()));
} else if ((keyword = parseAndGetKeywordIf("COLLATE")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseIdentifier()));
} else if ((keyword = parseAndGetKeywordIf("DEFAULT COLLATE")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseIdentifier()));
} else // [#10164] In a statement batch, this could already be the next statement
if (!peekKeyword("COMMENT ON") && parseKeywordIf("COMMENT")) {
if (!parseIf('='))
parseKeywordIf("IS");
comment = parseComment();
} else if (peekKeyword("OPTIONS")) {
comment = parseOptionsDescription();
} else if ((keyword = parseAndGetKeywordIf("COMPRESSION")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseStringLiteral()));
} else if ((keyword = parseAndGetKeywordIf("CONNECTION")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseStringLiteral()));
} else if ((keyword = parseAndGetKeywordIf("DATA DIRECTORY")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseStringLiteral()));
} else if ((keyword = parseAndGetKeywordIf("INDEX DIRECTORY")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseStringLiteral()));
} else if ((keyword = parseAndGetKeywordIf("DELAY_KEY_WRITE")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseZeroOne()));
} else if ((keyword = parseAndGetKeywordIf("ENCRYPTION")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseStringLiteral()));
} else if ((keyword = parseAndGetKeywordIf("ENGINE")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseIdentifier()));
} else if ((keyword = parseAndGetKeywordIf("INSERT_METHOD")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseAndGetKeyword("NO", "FIRST", "LAST")));
} else if ((keyword = parseAndGetKeywordIf("KEY_BLOCK_SIZE")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseFieldUnsignedNumericLiteral(Sign.NONE)));
} else if ((keyword = parseAndGetKeywordIf("MAX_ROWS")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseFieldUnsignedNumericLiteral(Sign.NONE)));
} else if ((keyword = parseAndGetKeywordIf("MIN_ROWS")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseFieldUnsignedNumericLiteral(Sign.NONE)));
} else if ((keyword = parseAndGetKeywordIf("PACK_KEYS")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseZeroOneDefault()));
} else if ((keyword = parseAndGetKeywordIf("PASSWORD")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseStringLiteral()));
} else if ((keyword = parseAndGetKeywordIf("ROW_FORMAT")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseAndGetKeyword("DEFAULT", "DYNAMIC", "FIXED", "COMPRESSED", "REDUNDANT", "COMPACT")));
} else if ((keyword = parseAndGetKeywordIf("STATS_AUTO_RECALC")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseZeroOneDefault()));
} else if ((keyword = parseAndGetKeywordIf("STATS_PERSISTENT")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseZeroOneDefault()));
} else if ((keyword = parseAndGetKeywordIf("STATS_SAMPLE_PAGES")) != null) {
parseIf('=');
storage.add(sql("{0} {1}", keyword, parseFieldUnsignedNumericLiteral(Sign.NONE)));
} else if ((keyword = parseAndGetKeywordIf("TABLESPACE")) != null) {
storage.add(sql("{0} {1}", keyword, parseIdentifier()));
if ((keyword = parseAndGetKeywordIf("STORAGE")) != null)
storage.add(sql("{0} {1}", keyword, parseAndGetKeyword("DISK", "MEMORY", "DEFAULT")));
} else if ((keyword = parseAndGetKeywordIf("UNION")) != null) {
parseIf('=');
parse('(');
storage.add(sql("{0} ({1})", keyword, list(parseIdentifiers())));
parse(')');
} else if (optional)
break storageLoop;
else
throw expected("storage clause after ','");
}
CreateTableStorageStep storageStep = comment != null ? commentStep.comment(comment) : commentStep;
if (storage.size() > 0)
return storageStep.storage(new SQLConcatenationImpl(storage.toArray(EMPTY_QUERYPART)));
else
return storageStep;
}
use of org.jooq.SQL in project jOOQ by jOOQ.
the class MergeImpl method toSQLStandard.
private final void toSQLStandard(Context<?> ctx) {
ctx.start(MERGE_MERGE_INTO).visit(K_MERGE_INTO).sql(' ').declareTables(true, c -> c.visit(table)).end(MERGE_MERGE_INTO).formatSeparator().start(MERGE_USING).visit(K_USING).sql(' ');
ctx.declareTables(true, c1 -> c1.data(DATA_WRAP_DERIVED_TABLES_IN_PARENTHESES, true, c2 -> {
// in its MERGE statement.
if (usingDual) {
switch(c2.family()) {
case DERBY:
c2.visit(new Dual());
break;
default:
c2.visit(DSL.selectOne());
break;
}
} else
c2.visit(using);
}));
boolean onParentheses = false;
ctx.end(MERGE_USING).formatSeparator().start(MERGE_ON).visit(K_ON).sql(onParentheses ? " (" : " ").visit(on).sql(onParentheses ? ")" : "").end(MERGE_ON).start(MERGE_WHEN_MATCHED_THEN_UPDATE).start(MERGE_SET);
// [#7291] Multi MATCHED emulation
boolean emulate = false;
boolean requireMatchedConditions = false;
// [#10054] TODO: Skip all WHEN MATCHED clauses after a WHEN MATCHED clause with no search condition
if (NO_SUPPORT_CONDITION_AFTER_NO_CONDITION.contains(ctx.dialect())) {
boolean withoutMatchedConditionFound = false;
for (MatchedClause m : matched) {
if (requireMatchedConditions |= withoutMatchedConditionFound)
break;
withoutMatchedConditionFound |= m.condition instanceof NoCondition;
}
}
emulateCheck: if ((NO_SUPPORT_MULTI.contains(ctx.dialect()) && matched.size() > 1)) {
boolean matchUpdate = false;
boolean matchDelete = false;
for (MatchedClause m : matched) {
if (m.delete) {
if (emulate |= matchDelete)
break emulateCheck;
matchDelete = true;
} else {
if (emulate |= matchUpdate)
break emulateCheck;
matchUpdate = true;
}
}
}
if (emulate) {
MatchedClause update = null;
MatchedClause delete = null;
Condition negate = noCondition();
for (MatchedClause m : matched) {
Condition condition = negate.and(m.condition);
if (m.delete) {
if (delete == null)
delete = new MatchedClause(noCondition(), true);
delete.condition = delete.condition.or(condition);
} else {
if (update == null)
update = new MatchedClause(noCondition());
for (Entry<Field<?>, Field<?>> e : m.updateMap.entrySet()) {
Field<?> exp = update.updateMap.get(e.getKey());
if (exp instanceof CaseConditionStepImpl)
((CaseConditionStepImpl) exp).when(negate.and(condition), e.getValue());
else
update.updateMap.put(e.getKey(), when(negate.and(condition), (Field) e.getValue()).else_(e.getKey()));
}
update.condition = update.condition.or(condition);
}
if (REQUIRE_NEGATION.contains(ctx.dialect()))
negate = negate.andNot(m.condition instanceof NoCondition ? trueCondition() : m.condition);
}
{
if (delete != null)
toSQLMatched(ctx, delete, requireMatchedConditions);
if (update != null)
toSQLMatched(ctx, update, requireMatchedConditions);
}
} else // [#7291] Workaround for https://github.com/h2database/h2database/issues/2552
if (REQUIRE_NEGATION.contains(ctx.dialect())) {
Condition negate = noCondition();
for (MatchedClause m : matched) {
toSQLMatched(ctx, new MatchedClause(negate.and(m.condition), m.delete, m.updateMap), requireMatchedConditions);
negate = negate.andNot(m.condition instanceof NoCondition ? trueCondition() : m.condition);
}
} else {
for (MatchedClause m : matched) toSQLMatched(ctx, m, requireMatchedConditions);
}
ctx.end(MERGE_SET).end(MERGE_WHEN_MATCHED_THEN_UPDATE).start(MERGE_WHEN_NOT_MATCHED_THEN_INSERT);
for (NotMatchedClause m : notMatched) toSQLNotMatched(ctx, m);
ctx.end(MERGE_WHEN_NOT_MATCHED_THEN_INSERT);
}
Aggregations