use of org.hibernate.sql.ast.tree.select.QuerySpec in project hibernate-orm by hibernate.
the class InformixSqmToSqlAstConverter method visitQuerySpec.
@Override
public QuerySpec visitQuerySpec(SqmQuerySpec<?> sqmQuerySpec) {
final boolean needsDummy = this.needsDummyTableGroup;
this.needsDummyTableGroup = false;
try {
final QuerySpec querySpec = super.visitQuerySpec(sqmQuerySpec);
if (this.needsDummyTableGroup) {
querySpec.getFromClause().addRoot(new StandardTableGroup(true, null, null, null, new NamedTableReference("(select 1)", "dummy_(x)", false, getCreationContext().getSessionFactory()), null, getCreationContext().getSessionFactory()));
}
return querySpec;
} finally {
this.needsDummyTableGroup = needsDummy;
}
}
use of org.hibernate.sql.ast.tree.select.QuerySpec in project hibernate-orm by hibernate.
the class OracleSqlAstTranslator method getFetchClauseTypeForRowNumbering.
@Override
protected FetchClauseType getFetchClauseTypeForRowNumbering(QueryPart queryPart) {
final FetchClauseType fetchClauseType = super.getFetchClauseTypeForRowNumbering(queryPart);
final boolean hasOffset;
if (queryPart.isRoot() && hasLimit()) {
hasOffset = getLimit().getFirstRow() != null;
} else {
hasOffset = queryPart.getOffsetClauseExpression() != null;
}
if (queryPart instanceof QuerySpec && !hasOffset && fetchClauseType == FetchClauseType.ROWS_ONLY) {
// Note that we also build upon this in #visitOrderBy
return null;
}
return fetchClauseType;
}
use of org.hibernate.sql.ast.tree.select.QuerySpec in project hibernate-orm by hibernate.
the class AbstractSqlAstTranslator method emulateSubQueryRelationalRestrictionPredicate.
protected <X extends Expression> void emulateSubQueryRelationalRestrictionPredicate(Predicate predicate, boolean negated, QueryPart queryPart, X lhsTuple, SubQueryRelationalRestrictionEmulationRenderer<X> renderer, ComparisonOperator tupleComparisonOperator) {
final QuerySpec subQuery;
if (queryPart instanceof QuerySpec && queryPart.getFetchClauseExpression() == null && queryPart.getOffsetClauseExpression() == null) {
subQuery = (QuerySpec) queryPart;
// We can only emulate the tuple sub query predicate as exists predicate when there are no limit/offsets
if (negated) {
appendSql("not ");
}
final QueryPart queryPartForRowNumbering = this.queryPartForRowNumbering;
final int queryPartForRowNumberingClauseDepth = this.queryPartForRowNumberingClauseDepth;
final boolean needsSelectAliases = this.needsSelectAliases;
try {
this.queryPartForRowNumbering = null;
this.queryPartForRowNumberingClauseDepth = -1;
this.needsSelectAliases = false;
queryPartStack.push(subQuery);
appendSql("exists (select 1");
visitFromClause(subQuery.getFromClause());
if (!subQuery.getGroupByClauseExpressions().isEmpty() || subQuery.getHavingClauseRestrictions() != null) {
// If we have a group by or having clause, we have to move the tuple comparison emulation to the HAVING clause
visitWhereClause(subQuery.getWhereClauseRestrictions());
visitGroupByClause(subQuery, SelectItemReferenceStrategy.EXPRESSION);
appendSql(" having ");
clauseStack.push(Clause.HAVING);
try {
renderer.renderComparison(subQuery.getSelectClause().getSqlSelections(), lhsTuple, tupleComparisonOperator);
final Predicate havingClauseRestrictions = subQuery.getHavingClauseRestrictions();
if (havingClauseRestrictions != null) {
appendSql(" and (");
havingClauseRestrictions.accept(this);
appendSql(CLOSE_PARENTHESIS);
}
} finally {
clauseStack.pop();
}
} else {
// If we have no group by or having clause, we can move the tuple comparison emulation to the WHERE clause
appendSql(" where ");
clauseStack.push(Clause.WHERE);
try {
renderer.renderComparison(subQuery.getSelectClause().getSqlSelections(), lhsTuple, tupleComparisonOperator);
final Predicate whereClauseRestrictions = subQuery.getWhereClauseRestrictions();
if (whereClauseRestrictions != null) {
appendSql(" and (");
whereClauseRestrictions.accept(this);
appendSql(CLOSE_PARENTHESIS);
}
} finally {
clauseStack.pop();
}
}
appendSql(CLOSE_PARENTHESIS);
} finally {
queryPartStack.pop();
this.queryPartForRowNumbering = queryPartForRowNumbering;
this.queryPartForRowNumberingClauseDepth = queryPartForRowNumberingClauseDepth;
this.needsSelectAliases = needsSelectAliases;
}
} else {
// TODO: We could use nested queries and use row numbers to emulate this
throw new IllegalArgumentException("Can't emulate in predicate with tuples and limit/offset or set operations: " + predicate);
}
}
use of org.hibernate.sql.ast.tree.select.QuerySpec in project hibernate-orm by hibernate.
the class AbstractSqlAstTranslator method getSelectItemsToInline.
private BitSet getSelectItemsToInline() {
final QuerySpec querySpec = (QuerySpec) getQueryPartStack().getCurrent();
final List<SqlSelection> sqlSelections = querySpec.getSelectClause().getSqlSelections();
final BitSet bitSet = new BitSet(sqlSelections.size());
for (Expression groupByClauseExpression : querySpec.getGroupByClauseExpressions()) {
final SqlSelectionExpression selectItemReference = getSelectItemReference(groupByClauseExpression);
if (selectItemReference != null) {
bitSet.set(sqlSelections.indexOf(selectItemReference.getSelection()));
}
}
return bitSet;
}
use of org.hibernate.sql.ast.tree.select.QuerySpec in project hibernate-orm by hibernate.
the class AbstractCollectionPersister method selectFragment.
/**
* Generate a list of collection index, key and element columns
*/
@Override
public String selectFragment(String alias, String columnSuffix) {
final PluralAttributeMapping attributeMapping = getAttributeMapping();
final QuerySpec rootQuerySpec = new QuerySpec(true);
final LoaderSqlAstCreationState sqlAstCreationState = new LoaderSqlAstCreationState(rootQuerySpec, new SqlAliasBaseManager(), new SimpleFromClauseAccessImpl(), LockOptions.NONE, (fetchParent, querySpec, creationState) -> new ArrayList<>(), true, getFactory());
final NavigablePath entityPath = new NavigablePath(attributeMapping.getRootPathName());
final TableGroup rootTableGroup = attributeMapping.createRootTableGroup(true, entityPath, null, () -> p -> {
}, new SqlAliasBaseConstant(alias), sqlAstCreationState.getSqlExpressionResolver(), sqlAstCreationState.getFromClauseAccess(), getFactory());
rootQuerySpec.getFromClause().addRoot(rootTableGroup);
sqlAstCreationState.getFromClauseAccess().registerTableGroup(entityPath, rootTableGroup);
attributeMapping.createDomainResult(entityPath, rootTableGroup, null, sqlAstCreationState);
// Wrap expressions with aliases
final SelectClause selectClause = rootQuerySpec.getSelectClause();
final java.util.List<SqlSelection> sqlSelections = selectClause.getSqlSelections();
int i = 0;
for (String keyAlias : keyColumnAliases) {
sqlSelections.set(i, new SqlSelectionImpl(i, i + 1, new AliasedExpression(sqlSelections.get(i).getExpression(), keyAlias + columnSuffix)));
i++;
}
if (hasIndex) {
for (String indexAlias : indexColumnAliases) {
sqlSelections.set(i, new SqlSelectionImpl(i, i + 1, new AliasedExpression(sqlSelections.get(i).getExpression(), indexAlias + columnSuffix)));
i++;
}
}
if (hasIdentifier) {
sqlSelections.set(i, new SqlSelectionImpl(i, i + 1, new AliasedExpression(sqlSelections.get(i).getExpression(), identifierColumnAlias + columnSuffix)));
i++;
}
for (int columnIndex = 0; i < sqlSelections.size(); i++, columnIndex++) {
final SqlSelection sqlSelection = sqlSelections.get(i);
sqlSelections.set(i, new SqlSelectionImpl(sqlSelection.getValuesArrayPosition(), sqlSelection.getJdbcResultSetIndex(), new AliasedExpression(sqlSelection.getExpression(), elementColumnAliases[columnIndex] + columnSuffix)));
}
final String sql = getFactory().getJdbcServices().getDialect().getSqlAstTranslatorFactory().buildSelectTranslator(getFactory(), new SelectStatement(rootQuerySpec)).translate(null, QueryOptions.NONE).getSql();
final int fromIndex = sql.lastIndexOf(" from");
final String expression;
if (fromIndex != -1) {
expression = sql.substring("select ".length(), fromIndex);
} else {
expression = sql.substring("select ".length());
}
return expression;
}
Aggregations