use of org.jooq.Context in project jOOQ by jOOQ.
the class InsertQueryImpl method toInsertSelect.
@SuppressWarnings("unchecked")
private final QueryPart toInsertSelect(Context<?> ctx) {
List<List<? extends Field<?>>> keys = conflictingKeys(ctx);
if (!keys.isEmpty()) {
Select<Record> rows = null;
Set<Field<?>> fields = insertMaps.keysFlattened(ctx);
// [#10989] INSERT .. SELECT .. ON DUPLICATE KEY IGNORE
if (select != null) {
Map<Field<?>, Field<?>> map = new HashMap<>();
Field<?>[] names = Tools.fields(select.fields().length);
List<Field<?>> f = new ArrayList<>(fields);
for (int i = 0; i < fields.size() && i < names.length; i++) map.put(f.get(i), names[i]);
rows = (Select<Record>) selectFrom(select.asTable(DSL.table(name("t")), names)).whereNotExists(selectOne().from(table()).where(matchByConflictingKeys(ctx, map)));
} else // [#5089] Multi-row inserts need to explicitly generate UNION ALL
// here. TODO: Refactor this logic to be more generally
// reusable - i.e. ordinary UNION ALL emulation should be
// re-used.
{
for (Map<Field<?>, Field<?>> map : insertMaps.maps()) {
Select<Record> row = select(aliasedFields(map.entrySet().stream().filter(e -> fields.contains(e.getKey())).map(Entry::getValue).collect(toList()))).whereNotExists(selectOne().from(table()).where(matchByConflictingKeys(ctx, map)));
if (rows == null)
rows = row;
else
rows = rows.unionAll(row);
}
}
return ctx.dsl().insertInto(table()).columns(fields).select(selectFrom(rows.asTable("t")));
} else
return DSL.sql("[ The ON DUPLICATE KEY IGNORE/UPDATE clause cannot be emulated when inserting into tables without any known keys : " + table() + " ]");
}
use of org.jooq.Context in project jOOQ by jOOQ.
the class BlockImpl method accept0.
private final void accept0(Context<?> ctx) {
boolean wrapInBeginEnd = alwaysWrapInBeginEnd;
if (wrapInBeginEnd) {
boolean topLevel = ctx.scopeLevel() == -1;
LanguageContext language = ctx.languageContext();
if (topLevel && language == LanguageContext.QUERY)
ctx.languageContext(LanguageContext.BLOCK);
{
begin(ctx, topLevel);
scopeDeclarations(ctx, c -> accept1(c));
end(ctx, topLevel);
}
if (topLevel && language == LanguageContext.QUERY)
ctx.languageContext(language);
} else
accept1(ctx);
}
use of org.jooq.Context in project jOOQ by jOOQ.
the class AbstractRowAsField method acceptMultisetContent.
static final void acceptMultisetContent(Context<?> ctx, Row row, Field<?> field, Consumer<? super Context<?>> acceptDefault) {
Object previous = ctx.data(DATA_MULTISET_CONTENT);
try {
ctx.data(DATA_MULTISET_CONTENT, true);
Name alias = field.getUnqualifiedName();
switch(emulateMultiset(ctx.configuration())) {
case JSON:
switch(ctx.family()) {
default:
ctx.visit(alias(ctx, alias, returningClob(ctx, jsonArray(row.fields()).nullOnNull())));
break;
}
break;
case JSONB:
switch(ctx.family()) {
default:
ctx.visit(alias(ctx, alias, returningClob(ctx, jsonbArray(row.fields()).nullOnNull())));
break;
}
break;
case XML:
switch(ctx.family()) {
default:
ctx.visit(alias(ctx, alias, xmlelement(N_RECORD, map(row.fields(), (f, i) -> xmlelement(fieldNameString(i), f)))));
break;
}
break;
// case ARRAY:
case NATIVE:
default:
acceptDefault.accept(ctx);
break;
}
} finally {
ctx.data(DATA_MULTISET_CONTENT, previous);
}
}
use of org.jooq.Context in project jOOQ by jOOQ.
the class SelectQueryImpl method accept0.
final void accept0(Context<?> context) {
boolean topLevelCte = false;
// Subquery scopes are started in AbstractContext
if (context.subqueryLevel() == 0) {
context.scopeStart();
if (topLevelCte |= (context.data(DATA_TOP_LEVEL_CTE) == null))
context.data(DATA_TOP_LEVEL_CTE, new TopLevelCte());
}
SQLDialect dialect = context.dialect();
// [#2791] TODO: Instead of explicitly manipulating these data() objects, future versions
// of jOOQ should implement a push / pop semantics to clearly delimit such scope.
Object renderTrailingLimit = context.data(DATA_RENDER_TRAILING_LIMIT_IF_APPLICABLE);
Object localWindowDefinitions = context.data(DATA_WINDOW_DEFINITIONS);
Name[] selectAliases = (Name[]) context.data(DATA_SELECT_ALIASES);
try {
List<Field<?>> originalFields = null;
List<Field<?>> alternativeFields = null;
if (selectAliases != null) {
context.data().remove(DATA_SELECT_ALIASES);
alternativeFields = map(originalFields = getSelect(), (f, i) -> i < selectAliases.length ? f.as(selectAliases[i]) : f);
}
if (TRUE.equals(renderTrailingLimit))
context.data().remove(DATA_RENDER_TRAILING_LIMIT_IF_APPLICABLE);
// [#5127] Lazy initialise this map
if (localWindowDefinitions != null)
context.data(DATA_WINDOW_DEFINITIONS, null);
if (intoTable != null && !TRUE.equals(context.data(DATA_OMIT_INTO_CLAUSE)) && EMULATE_SELECT_INTO_AS_CTAS.contains(dialect)) {
context.data(DATA_OMIT_INTO_CLAUSE, true, c -> c.visit(createTable(intoTable).as(this)));
return;
}
if (with != null)
context.visit(with);
else if (topLevelCte)
markTopLevelCteAndAccept(context, c -> {
});
pushWindow(context);
Boolean wrapDerivedTables = (Boolean) context.data(DATA_WRAP_DERIVED_TABLES_IN_PARENTHESES);
if (TRUE.equals(wrapDerivedTables)) {
context.sqlIndentStart('(').data().remove(DATA_WRAP_DERIVED_TABLES_IN_PARENTHESES);
}
switch(dialect) {
case CUBRID:
case FIREBIRD:
case MYSQL:
case YUGABYTEDB:
{
if (getLimit().isApplicable() && getLimit().withTies())
toSQLReferenceLimitWithWindowFunctions(context);
else
toSQLReferenceLimitDefault(context, originalFields, alternativeFields);
break;
}
// By default, render the dialect's limit clause
default:
{
toSQLReferenceLimitDefault(context, originalFields, alternativeFields);
break;
}
}
// [#1296] [#7328] FOR UPDATE is emulated in some dialects using hints
if (forLock != null)
context.visit(forLock);
// end-of-query clauses are appended to the end of a query
if (!StringUtils.isBlank(option))
context.formatSeparator().sql(option);
if (TRUE.equals(wrapDerivedTables))
context.sqlIndentEnd(')').data(DATA_WRAP_DERIVED_TABLES_IN_PARENTHESES, true);
} finally {
context.data(DATA_WINDOW_DEFINITIONS, localWindowDefinitions);
if (renderTrailingLimit != null)
context.data(DATA_RENDER_TRAILING_LIMIT_IF_APPLICABLE, renderTrailingLimit);
if (selectAliases != null)
context.data(DATA_SELECT_ALIASES, selectAliases);
}
if (context.subqueryLevel() == 0)
context.scopeEnd();
}
use of org.jooq.Context in project jOOQ by jOOQ.
the class RowInCondition method accept.
@Override
public final void accept(Context<?> ctx) {
if (EMULATE_IN.contains(ctx.dialect())) {
Condition result = DSL.or(map(right, r -> new RowCondition(left, r, EQUALS)));
if (not)
result = result.not();
ctx.visit(result);
} else {
if (right.size() == 0) {
if (not)
ctx.visit(trueCondition());
else
ctx.visit(falseCondition());
} else {
ctx.visit(left).sql(' ').visit((not ? NOT_IN : IN).toKeyword()).sql(" (").visit(new QueryPartListView<>(AbstractInList.padded(ctx, right))).sql(')');
}
}
}
Aggregations