use of org.jooq.Row in project jOOQ by jOOQ.
the class RowInCondition method delegate.
private final QueryPartInternal delegate(Configuration configuration) {
if (asList(DERBY, FIREBIRD, SQLITE).contains(configuration.family())) {
List<Condition> conditions = new ArrayList<Condition>();
for (Row row : right) {
conditions.add(new RowCondition(left, row, EQUALS));
}
Condition result = DSL.or(conditions);
if (comparator == NOT_IN) {
result = result.not();
}
return (QueryPartInternal) result;
} else {
return new Native();
}
}
use of org.jooq.Row in project jOOQ by jOOQ.
the class TableAlias method init.
/**
* Register fields for this table alias
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private final Fields<R> init(String[] fieldAliases) {
List<Field<?>> result = new ArrayList<Field<?>>();
Row row = this.alias.wrapped().fieldsRow();
int size = row.size();
for (int i = 0; i < size; i++) {
Field<?> field = row.field(i);
String name = field.getName();
if (fieldAliases != null && fieldAliases.length > i) {
name = fieldAliases[i];
}
result.add(new TableFieldImpl(name, field.getDataType(), this, field.getComment(), field.getBinding()));
}
return new Fields<R>(result);
}
use of org.jooq.Row 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);
}
Aggregations