use of org.jooq.Condition in project jOOQ by jOOQ.
the class MergeImpl method getStandardMerge.
// -------------------------------------------------------------------------
// QueryPart API
// -------------------------------------------------------------------------
/**
* Return a standard MERGE statement emulating the H2-specific syntax
*/
private final QueryPart getStandardMerge() {
// The SRC for the USING() clause:
// ------------------------------
Table<?> src;
if (upsertSelect != null) {
List<Field<?>> v = new ArrayList<Field<?>>();
Row row = upsertSelect.fieldsRow();
for (int i = 0; i < row.size(); i++) {
v.add(row.field(i).as("s" + (i + 1)));
}
// [#579] TODO: Currently, this syntax may require aliasing
// on the call-site
src = DSL.select(v).from(upsertSelect).asTable("src");
} else {
List<Field<?>> v = new ArrayList<Field<?>>();
for (int i = 0; i < getUpsertValues().size(); i++) {
v.add(getUpsertValues().get(i).as("s" + (i + 1)));
}
src = DSL.select(v).asTable("src");
}
// The condition for the ON clause:
// --------------------------------
Set<Field<?>> onFields = new HashSet<Field<?>>();
Condition condition = null;
if (getUpsertKeys().isEmpty()) {
UniqueKey<?> key = table.getPrimaryKey();
if (key != null) {
onFields.addAll(key.getFields());
for (int i = 0; i < key.getFields().size(); i++) {
Condition rhs = key.getFields().get(i).equal((Field) src.field(i));
if (condition == null) {
condition = rhs;
} else {
condition = condition.and(rhs);
}
}
} else // This should probably execute an INSERT statement
{
throw new IllegalStateException("Cannot omit KEY() clause on a non-Updatable Table");
}
} else {
for (int i = 0; i < getUpsertKeys().size(); i++) {
int matchIndex = getUpsertFields().indexOf(getUpsertKeys().get(i));
if (matchIndex == -1) {
throw new IllegalStateException("Fields in KEY() clause must be part of the fields specified in MERGE INTO table (...)");
}
onFields.addAll(getUpsertKeys());
Condition rhs = getUpsertKeys().get(i).equal((Field) src.field(matchIndex));
if (condition == null) {
condition = rhs;
} else {
condition = condition.and(rhs);
}
}
}
// INSERT and UPDATE clauses
// -------------------------
Map<Field<?>, Field<?>> update = new LinkedHashMap<Field<?>, Field<?>>();
Map<Field<?>, Field<?>> insert = new LinkedHashMap<Field<?>, Field<?>>();
for (int i = 0; i < src.fieldsRow().size(); i++) {
// Oracle does not allow to update fields from the ON clause
if (!onFields.contains(getUpsertFields().get(i))) {
update.put(getUpsertFields().get(i), src.field(i));
}
insert.put(getUpsertFields().get(i), src.field(i));
}
return DSL.mergeInto(table).using(src).on(condition).whenMatchedThenUpdate().set(update).whenNotMatchedThenInsert().set(insert);
}
use of org.jooq.Condition in project jOOQ by jOOQ.
the class ParserImpl method parseDelete.
private static final Delete<?> parseDelete(ParserContext ctx) {
parseKeyword(ctx, "DELETE");
parseKeywordIf(ctx, "FROM");
Table<?> tableName = parseTableName(ctx);
boolean where = parseKeywordIf(ctx, "WHERE");
Condition condition = where ? parseCondition(ctx) : null;
// TODO Implement returning
// boolean returning = parseKeywordIf(ctx, "RETURNING");
DeleteWhereStep<?> s1;
DeleteFinalStep<?> s2;
s1 = ctx.dsl.delete(tableName);
s2 = where ? s1.where(condition) : s1;
return s2;
}
use of org.jooq.Condition in project jOOQ by jOOQ.
the class ParserImpl method parseUpdate.
private static final Update<?> parseUpdate(ParserContext ctx) {
parseKeyword(ctx, "UPDATE");
Table<?> tableName = parseTableName(ctx);
parseKeyword(ctx, "SET");
// TODO Row value expression updates
Map<Field<?>, Object> map = parseSetClauseList(ctx);
// TODO support FROM
Condition condition = parseKeywordIf(ctx, "WHERE") ? parseCondition(ctx) : null;
// TODO support RETURNING
return condition == null ? ctx.dsl.update(tableName).set(map) : ctx.dsl.update(tableName).set(map).where(condition);
}
use of org.jooq.Condition in project jOOQ by jOOQ.
the class ParserImpl method parseAggregateFunctionIf.
private static final Field<?> parseAggregateFunctionIf(ParserContext ctx) {
AggregateFunction<?> agg;
WindowBeforeOverStep<?> over;
Field<?> result;
Condition filter;
over = agg = parseCountIf(ctx);
if (agg == null)
over = agg = parseGeneralSetFunctionIf(ctx);
if (agg == null)
over = agg = parseBinarySetFunctionIf(ctx);
if (agg == null)
over = parseOrderedSetFunctionIf(ctx);
if (agg == null && over == null)
return null;
if (agg != null && parseKeywordIf(ctx, "FILTER")) {
parse(ctx, '(');
parseKeyword(ctx, "WHERE");
filter = parseCondition(ctx);
parse(ctx, ')');
result = over = agg.filterWhere(filter);
} else if (agg != null)
result = agg;
else
result = over;
// TODO parse WITHIN GROUP (ORDER BY) where applicable
if (parseKeywordIf(ctx, "OVER")) {
Object nameOrSpecification = parseWindowNameOrSpecification(ctx, agg != null);
if (nameOrSpecification instanceof Name)
result = over.over((Name) nameOrSpecification);
else if (nameOrSpecification instanceof WindowSpecification)
result = over.over((WindowSpecification) nameOrSpecification);
else
result = over.over();
}
return result;
}
use of org.jooq.Condition in project jOOQ by jOOQ.
the class RowIsNull method delegate.
private final QueryPartInternal delegate(Configuration configuration) {
// Informix doesn't implement the RVE IS NULL predicate.
if (asList(CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, SQLITE).contains(configuration.family())) {
List<Condition> conditions = new ArrayList<Condition>();
for (Field<?> field : row.fields()) {
conditions.add(isNull ? field.isNull() : field.isNotNull());
}
Condition result = DSL.and(conditions);
return (QueryPartInternal) result;
} else {
return new Native();
}
}
Aggregations