use of org.jooq.impl.Keywords.K_ORDER_BY in project jOOQ by jOOQ.
the class UpdateQueryImpl method accept0.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
final void accept0(Context<?> ctx) {
boolean declareTables = ctx.declareTables();
ctx.start(UPDATE_UPDATE).visit(K_UPDATE).sql(' ').declareTables(true).visit(table(ctx)).declareTables(declareTables).end(UPDATE_UPDATE);
ctx.formatSeparator().start(UPDATE_SET).visit(K_SET).separatorRequired(true);
// A multi-row update was specified
if (multiRow != null) {
// [#6884] This syntax can be emulated trivially, if the RHS is not a SELECT subquery
if (multiValue != null && !SUPPORT_RVE_SET.contains(ctx.dialect())) {
FieldMapForUpdate map = new FieldMapForUpdate(table(), UPDATE_SET_ASSIGNMENT);
for (int i = 0; i < multiRow.size(); i++) {
Field<?> k = multiRow.field(i);
Field<?> v = multiValue.field(i);
map.put(k, Tools.field(v, k));
}
ctx.formatIndentStart().formatSeparator().visit(map).formatIndentEnd();
} else {
Row row = removeReadonly(ctx, multiRow);
ctx.start(UPDATE_SET_ASSIGNMENT).formatIndentStart().formatSeparator().qualify(false, c -> c.visit(row)).sql(" = ");
// right hand side of a SET clause
if (multiValue != null) {
// single-degree rows. Let's just always render it, here.
if (REQUIRE_RVE_ROW_CLAUSE.contains(ctx.dialect()))
ctx.visit(K_ROW).sql(" ");
ctx.visit(removeReadonly(ctx, multiRow, multiValue));
} else // Subselects or subselect emulations of row value expressions
{
Select<?> select;
if (multiValue != null)
select = select(removeReadonly(ctx, multiRow, multiValue).fields());
else
select = multiSelect;
visitSubquery(ctx, select, false, false, false);
}
ctx.formatIndentEnd().end(UPDATE_SET_ASSIGNMENT);
}
} else // A regular (non-multi-row) update was specified
{
ctx.formatIndentStart().formatSeparator().visit(updateMap).formatIndentEnd();
}
ctx.end(UPDATE_SET);
switch(ctx.family()) {
default:
acceptFrom(ctx);
break;
}
if (limit != null && NO_SUPPORT_LIMIT.contains(ctx.dialect()) || !orderBy.isEmpty() && NO_SUPPORT_ORDER_BY_LIMIT.contains(ctx.dialect())) {
Field<?>[] keyFields = table().getKeys().isEmpty() ? new Field[] { table().rowid() } : (table().getPrimaryKey() != null ? table().getPrimaryKey() : table().getKeys().get(0)).getFieldsArray();
ctx.start(UPDATE_WHERE).formatSeparator().visit(K_WHERE).sql(' ');
if (keyFields.length == 1)
ctx.visit(keyFields[0].in(select((Field) keyFields[0]).from(table()).where(getWhere()).orderBy(orderBy).limit(limit)));
else
ctx.visit(row(keyFields).in(select(keyFields).from(table()).where(getWhere()).orderBy(orderBy).limit(limit)));
ctx.end(UPDATE_WHERE);
} else {
ctx.start(UPDATE_WHERE);
if (hasWhere())
ctx.formatSeparator().visit(K_WHERE).sql(' ').visit(getWhere());
ctx.end(UPDATE_WHERE);
if (!orderBy.isEmpty())
ctx.formatSeparator().visit(K_ORDER_BY).sql(' ').visit(orderBy);
if (limit != null)
ctx.formatSeparator().visit(K_LIMIT).sql(' ').visit(limit);
}
ctx.start(UPDATE_RETURNING);
toSQLReturning(ctx);
ctx.end(UPDATE_RETURNING);
}
use of org.jooq.impl.Keywords.K_ORDER_BY in project jOOQ by jOOQ.
the class WindowSpecificationImpl method accept.
@Override
public final void accept(Context<?> ctx) {
SortFieldList o = orderBy;
// [#8414] [#8593] [#11021] [#11851] Some RDBMS require ORDER BY in some window functions
AbstractWindowFunction<?> w = (AbstractWindowFunction<?>) ctx.data(DATA_WINDOW_FUNCTION);
if (o.isEmpty()) {
boolean ordered = w instanceof Ntile && REQUIRES_ORDER_BY_IN_NTILE.contains(ctx.dialect()) || w instanceof Lead && REQUIRES_ORDER_BY_IN_LEAD_LAG.contains(ctx.dialect()) || w instanceof Lag && REQUIRES_ORDER_BY_IN_LEAD_LAG.contains(ctx.dialect()) || w instanceof Rank && REQUIRES_ORDER_BY_IN_RANK_DENSE_RANK.contains(ctx.dialect()) || w instanceof DenseRank && REQUIRES_ORDER_BY_IN_RANK_DENSE_RANK.contains(ctx.dialect()) || w instanceof PercentRank && REQUIRES_ORDER_BY_IN_PERCENT_RANK_CUME_DIST.contains(ctx.dialect()) || w instanceof CumeDist && REQUIRES_ORDER_BY_IN_PERCENT_RANK_CUME_DIST.contains(ctx.dialect());
if (ordered) {
Field<Integer> constant;
switch(ctx.family()) {
default:
constant = field(select(one()));
break;
}
o = new SortFieldList();
o.add(constant.sortDefault());
}
}
boolean hasWindowDefinitions = windowDefinition != null;
boolean hasPartitionBy = !partitionBy.isEmpty();
boolean hasOrderBy = !o.isEmpty();
boolean hasFrame = frameStart != null;
int clauses = 0;
if (hasWindowDefinitions)
clauses++;
if (hasPartitionBy)
clauses++;
if (hasOrderBy)
clauses++;
if (hasFrame)
clauses++;
boolean indent = clauses > 1;
if (indent)
ctx.formatIndentStart().formatNewLine();
if (windowDefinition != null)
ctx.declareWindows(false, c -> c.visit(windowDefinition));
if (hasPartitionBy) {
// constant expressions in the PARTITION BY clause (HANA)
if (partitionByOne && OMIT_PARTITION_BY_ONE.contains(ctx.dialect())) {
} else {
if (hasWindowDefinitions)
ctx.formatSeparator();
ctx.visit(K_PARTITION_BY).separatorRequired(true).visit(partitionBy);
}
}
if (hasOrderBy) {
if (hasWindowDefinitions || hasPartitionBy)
ctx.formatSeparator();
ctx.visit(K_ORDER_BY).separatorRequired(true).visit(o);
}
if (hasFrame) {
if (hasWindowDefinitions || hasPartitionBy || hasOrderBy)
ctx.formatSeparator();
FrameUnits u = frameUnits;
Integer s = frameStart;
Integer e = frameEnd;
ctx.visit(u.keyword).sql(' ');
if (e != null) {
ctx.visit(K_BETWEEN).sql(' ');
toSQLRows(ctx, s);
ctx.sql(' ').visit(K_AND).sql(' ');
toSQLRows(ctx, e);
} else {
toSQLRows(ctx, s);
}
if (exclude != null)
ctx.sql(' ').visit(K_EXCLUDE).sql(' ').visit(exclude.keyword);
}
if (indent)
ctx.formatIndentEnd().formatNewLine();
}
Aggregations