use of org.jooq.impl.Tools.DataKey.DATA_SELECT_ALIASES 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.impl.Tools.DataKey.DATA_SELECT_ALIASES 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);
}
Aggregations