use of org.jooq.Context 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.Context 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();
}
use of org.jooq.Context in project jOOQ by jOOQ.
the class Alias method acceptDeclareAliasStandard.
private final void acceptDeclareAliasStandard(Context<?> context) {
if (wrapped instanceof TableImpl)
context.scopeMarkStart(wrapping);
SQLDialect dialect = context.dialect();
SQLDialect family = context.family();
boolean emulatedDerivedColumnList = false;
// Hence, wrap the table reference in a subselect
if (fieldAliases != null && (SUPPORT_DERIVED_COLUMN_NAMES_SPECIAL1.contains(dialect)) && (wrapped instanceof TableImpl || wrapped instanceof CommonTableExpressionImpl)) {
visitSubquery(context, select(asterisk()).from(((Table<?>) wrapped).as(alias)), true, false, false);
} else // results using UNION ALL
if (fieldAliases != null && (emulatedDerivedColumnList || SUPPORT_DERIVED_COLUMN_NAMES_SPECIAL2.contains(dialect))) {
emulatedDerivedColumnList = true;
if (wrapped instanceof Values && NO_SUPPORT_VALUES.contains(dialect)) {
context.data(DATA_SELECT_ALIASES, fieldAliases, t -> toSQLWrapped(t));
} else {
// [#3156] Do not SELECT * from derived tables to prevent ambiguously defined columns
// in those derived tables
Select<?> wrappedAsSelect = wrapped instanceof Select ? (Select<?>) wrapped : wrapped instanceof DerivedTable ? ((DerivedTable<?>) wrapped).query() : select(asterisk()).from(((Table<?>) wrapped).as(alias));
List<Field<?>> select = wrappedAsSelect.getSelect();
if (emulatedDerivedColumnList) {
SelectFieldList<Field<?>> fields = new SelectFieldList<>();
for (int i = 0; i < fieldAliases.length; i++) {
switch(family) {
default:
fields.add(field("null").as(fieldAliases[i]));
break;
}
}
visitSubquery(context, select(fields).where(falseCondition()).unionAll(wrappedAsSelect), true, false, false);
}
}
} else
// The default behaviour
toSQLWrapped(context);
// [#291] some aliases cause trouble, if they are not explicitly marked using "as"
toSQLAs(context);
context.sql(' ').qualify(false, c -> c.visit(alias));
// [#1801] Add field aliases to the table alias, if applicable
if (fieldAliases != null && !emulatedDerivedColumnList) {
toSQLDerivedColumnList(context);
} else {
// TODO: Is this still needed?
switch(family) {
case HSQLDB:
case POSTGRES:
case YUGABYTEDB:
{
// The javac compiler doesn't like casting of generics
Object o = wrapped;
if (context.declareTables() && o instanceof ArrayTable)
context.sql('(').visit(wrap(((ArrayTable) o).fields()).qualify(false)).sql(')');
break;
}
}
}
if (wrapped instanceof TableImpl)
context.scopeMarkEnd(wrapping);
}
use of org.jooq.Context in project jOOQ by jOOQ.
the class AlterTableImpl method accept1.
private final void accept1(Context<?> ctx) {
SQLDialect family = ctx.family();
boolean omitAlterTable = (renameConstraint != null && family == HSQLDB) || (renameColumn != null && SUPPORT_RENAME_COLUMN.contains(ctx.dialect()));
boolean renameTable = renameTo != null && SUPPORT_RENAME_TABLE.contains(ctx.dialect());
boolean renameObject = renameTo != null && (false);
if (!omitAlterTable) {
ctx.start(ALTER_TABLE_TABLE).visit(renameObject ? K_RENAME_OBJECT : renameTable ? K_RENAME_TABLE : K_ALTER_TABLE);
if (ifExists && supportsIfExists(ctx))
ctx.sql(' ').visit(K_IF_EXISTS);
ctx.sql(' ').visit(table).end(ALTER_TABLE_TABLE).formatIndentStart().formatSeparator();
}
if (comment != null) {
ctx.visit(K_COMMENT).sql(' ').visit(comment);
} else if (renameTo != null) {
boolean qualify = ctx.qualify();
ctx.start(ALTER_TABLE_RENAME);
if (NO_SUPPORT_RENAME_QUALIFIED_TABLE.contains(ctx.dialect()))
ctx.qualify(false);
ctx.visit(renameObject || renameTable ? K_TO : K_RENAME_TO).sql(' ').visit(renameTo);
if (NO_SUPPORT_RENAME_QUALIFIED_TABLE.contains(ctx.dialect()))
ctx.qualify(qualify);
ctx.end(ALTER_TABLE_RENAME);
} else if (renameColumn != null) {
ctx.start(ALTER_TABLE_RENAME_COLUMN);
switch(ctx.family()) {
case DERBY:
ctx.visit(K_RENAME_COLUMN).sql(' ').visit(renameColumn).formatSeparator().visit(K_TO).sql(' ').qualify(false, c -> c.visit(renameColumnTo));
break;
case H2:
case HSQLDB:
ctx.visit(K_ALTER_COLUMN).sql(' ').qualify(false, c -> c.visit(renameColumn)).formatSeparator().visit(K_RENAME_TO).sql(' ').qualify(false, c -> c.visit(renameColumnTo));
break;
case FIREBIRD:
ctx.visit(K_ALTER_COLUMN).sql(' ').qualify(false, c -> c.visit(renameColumn)).formatSeparator().visit(K_TO).sql(' ').qualify(false, c -> c.visit(renameColumnTo));
break;
default:
ctx.visit(K_RENAME_COLUMN).sql(' ').qualify(false, c -> c.visit(renameColumn)).formatSeparator().visit(K_TO).sql(' ').qualify(false, c -> c.visit(renameColumnTo));
break;
}
ctx.end(ALTER_TABLE_RENAME_COLUMN);
} else if (renameIndex != null) {
ctx.start(ALTER_TABLE_RENAME_INDEX).visit(K_RENAME_INDEX).sql(' ').qualify(false, c -> c.visit(renameIndex)).formatSeparator().visit(K_TO).sql(' ').qualify(false, c -> c.visit(renameIndexTo)).end(ALTER_TABLE_RENAME_INDEX);
} else if (renameConstraint != null) {
ctx.start(ALTER_TABLE_RENAME_CONSTRAINT);
ctx.data(DATA_CONSTRAINT_REFERENCE, true, c1 -> {
if (family == HSQLDB)
c1.visit(K_ALTER_CONSTRAINT).sql(' ').qualify(false, c2 -> c2.visit(renameConstraint)).formatSeparator().visit(K_RENAME_TO).sql(' ').qualify(false, c2 -> c2.visit(renameConstraintTo));
else
c1.visit(K_RENAME_CONSTRAINT).sql(' ').qualify(false, c2 -> c2.visit(renameConstraint)).formatSeparator().visit(K_TO).sql(' ').qualify(false, c2 -> c2.visit(renameConstraintTo));
});
ctx.end(ALTER_TABLE_RENAME_CONSTRAINT);
} else if (add != null) {
boolean multiAdd = REQUIRE_REPEAT_ADD_ON_MULTI_ALTER.contains(ctx.dialect());
boolean parens = !multiAdd;
boolean comma = true;
ctx.start(ALTER_TABLE_ADD).visit(addColumnKeyword(ctx)).sql(' ');
if (parens)
ctx.sql('(');
boolean indent = !multiAdd && add.size() > 1;
if (indent)
ctx.formatIndentStart().formatNewLine();
for (int i = 0; i < add.size(); i++) {
if (i > 0) {
ctx.sql(comma ? "," : "").formatSeparator();
if (multiAdd)
ctx.visit(addColumnKeyword(ctx)).sql(' ');
}
TableElement part = add.get(i);
ctx.qualify(false, c -> c.visit(part));
if (part instanceof Field) {
Field<?> f = (Field<?>) part;
ctx.sql(' ');
toSQLDDLTypeDeclarationForAddition(ctx, f.getDataType());
}
}
if (indent)
ctx.formatIndentEnd().formatNewLine();
if (parens)
ctx.sql(')');
acceptFirstBeforeAfter(ctx);
ctx.end(ALTER_TABLE_ADD);
} else if (addColumn != null) {
ctx.start(ALTER_TABLE_ADD).visit(addColumnKeyword(ctx)).sql(' ');
if (ifNotExistsColumn && supportsIfNotExistsColumn(ctx))
ctx.visit(K_IF_NOT_EXISTS).sql(' ');
ctx.qualify(false, c -> c.visit(addColumn)).sql(' ');
toSQLDDLTypeDeclarationForAddition(ctx, addColumnType);
acceptFirstBeforeAfter(ctx);
ctx.end(ALTER_TABLE_ADD);
} else if (addConstraint != null) {
ctx.start(ALTER_TABLE_ADD);
ctx.visit(K_ADD).sql(' ');
ctx.visit(addConstraint);
ctx.end(ALTER_TABLE_ADD);
} else if (alterConstraint != null) {
ctx.start(ALTER_TABLE_ALTER);
ctx.data(DATA_CONSTRAINT_REFERENCE, true, c -> {
switch(family) {
default:
ctx.visit(K_ALTER);
break;
}
ctx.sql(' ').visit(K_CONSTRAINT).sql(' ').visit(alterConstraint);
ConstraintImpl.acceptEnforced(ctx, alterConstraintEnforced);
});
ctx.end(ALTER_TABLE_ALTER);
} else if (alterColumn != null) {
ctx.start(ALTER_TABLE_ALTER);
switch(family) {
case CUBRID:
case MARIADB:
case MYSQL:
{
// MySQL's CHANGE COLUMN clause has a mandatory RENAMING syntax...
if (alterColumnDefault == null && !alterColumnDropDefault)
ctx.visit(K_CHANGE_COLUMN).sql(' ').qualify(false, c -> c.visit(alterColumn));
else
ctx.visit(K_ALTER_COLUMN);
break;
}
default:
ctx.visit(K_ALTER);
break;
}
ctx.sql(' ');
ctx.qualify(false, c -> c.visit(alterColumn));
if (alterColumnType != null) {
switch(family) {
case DERBY:
ctx.sql(' ').visit(K_SET_DATA_TYPE);
break;
case FIREBIRD:
case POSTGRES:
case YUGABYTEDB:
ctx.sql(' ').visit(K_TYPE);
break;
}
ctx.sql(' ');
toSQLDDLTypeDeclaration(ctx, alterColumnType);
toSQLDDLTypeDeclarationIdentityBeforeNull(ctx, alterColumnType);
// [#3805] Some databases cannot change the type and the NOT NULL constraint in a single statement
if (!NO_SUPPORT_ALTER_TYPE_AND_NULL.contains(ctx.dialect())) {
switch(alterColumnType.nullability()) {
case NULL:
ctx.sql(' ').visit(K_NULL);
break;
case NOT_NULL:
ctx.sql(' ').visit(K_NOT_NULL);
break;
case DEFAULT:
break;
}
}
toSQLDDLTypeDeclarationIdentityAfterNull(ctx, alterColumnType);
} else if (alterColumnDefault != null) {
ctx.start(ALTER_TABLE_ALTER_DEFAULT);
switch(family) {
default:
ctx.sql(' ').visit(K_SET_DEFAULT);
break;
}
ctx.sql(' ').visit(alterColumnDefault).end(ALTER_TABLE_ALTER_DEFAULT);
} else if (alterColumnDropDefault) {
ctx.start(ALTER_TABLE_ALTER_DEFAULT);
switch(family) {
case MARIADB:
case MYSQL:
ctx.sql(' ').visit(K_SET_DEFAULT).sql(' ').visit(K_NULL);
break;
default:
ctx.sql(' ').visit(K_DROP_DEFAULT);
break;
}
ctx.end(ALTER_TABLE_ALTER_DEFAULT);
} else if (alterColumnNullability != null) {
ctx.start(ALTER_TABLE_ALTER_NULL);
switch(ctx.family()) {
default:
ctx.sql(' ').visit(alterColumnNullability.nullable() ? K_DROP_NOT_NULL : K_SET_NOT_NULL);
break;
}
ctx.end(ALTER_TABLE_ALTER_NULL);
}
ctx.end(ALTER_TABLE_ALTER);
} else if (dropColumns != null) {
ctx.start(ALTER_TABLE_DROP);
if (REQUIRE_REPEAT_DROP_ON_MULTI_ALTER.contains(ctx.dialect())) {
String separator = "";
for (Field<?> dropColumn : dropColumns) {
ctx.sql(separator);
acceptDropColumn(ctx);
if (ifExistsColumn && supportsIfExistsColumn(ctx))
ctx.sql(' ').visit(K_IF_EXISTS);
ctx.sql(' ').qualify(false, c -> c.visit(dropColumn));
separator = ", ";
}
} else {
acceptDropColumn(ctx);
if (ifExistsColumn && supportsIfExistsColumn(ctx))
ctx.sql(' ').visit(K_IF_EXISTS);
ctx.sql(' ');
ctx.qualify(false, c -> c.visit(dropColumns));
}
acceptCascade(ctx);
ctx.end(ALTER_TABLE_DROP);
} else if (dropConstraint != null) {
ctx.start(ALTER_TABLE_DROP);
ctx.data(DATA_CONSTRAINT_REFERENCE, true, c -> {
if (dropConstraintType == FOREIGN_KEY && NO_SUPPORT_DROP_CONSTRAINT.contains(c.dialect())) {
c.visit(K_DROP).sql(' ').visit(K_FOREIGN_KEY).sql(' ').visit(dropConstraint);
} else if (dropConstraintType == PRIMARY_KEY && NO_SUPPORT_DROP_CONSTRAINT.contains(c.dialect())) {
c.visit(K_DROP).sql(' ').visit(K_PRIMARY_KEY);
} else {
// [#9382] In some dialects, unnamed UNIQUE constraints can be
// dropped by dropping their declarations.
c.visit(dropConstraint.getUnqualifiedName().empty() ? K_DROP : K_DROP_CONSTRAINT).sql(' ');
if (ifExistsConstraint && !NO_SUPPORT_IF_EXISTS_CONSTRAINT.contains(c.dialect()))
c.visit(K_IF_EXISTS).sql(' ');
c.visit(dropConstraint);
}
acceptCascade(c);
});
ctx.end(ALTER_TABLE_DROP);
} else if (dropConstraintType == PRIMARY_KEY) {
ctx.start(ALTER_TABLE_DROP);
ctx.visit(K_DROP).sql(' ').visit(K_PRIMARY_KEY);
ctx.end(ALTER_TABLE_DROP);
}
if (!omitAlterTable)
ctx.formatIndentEnd();
}
Aggregations