Search in sources :

Example 1 with QuerySpec

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;
    }
}
Also used : StandardTableGroup(org.hibernate.sql.ast.tree.from.StandardTableGroup) NamedTableReference(org.hibernate.sql.ast.tree.from.NamedTableReference) SqmQuerySpec(org.hibernate.query.sqm.tree.select.SqmQuerySpec) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec)

Example 2 with QuerySpec

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;
}
Also used : FetchClauseType(org.hibernate.query.sqm.FetchClauseType) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec)

Example 3 with QuerySpec

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);
    }
}
Also used : QueryPart(org.hibernate.sql.ast.tree.select.QueryPart) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) SqlFragmentPredicate(org.hibernate.persister.internal.SqlFragmentPredicate) InSubQueryPredicate(org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate) SelfRenderingPredicate(org.hibernate.sql.ast.tree.predicate.SelfRenderingPredicate) FilterPredicate(org.hibernate.sql.ast.tree.predicate.FilterPredicate) NegatedPredicate(org.hibernate.sql.ast.tree.predicate.NegatedPredicate) LikePredicate(org.hibernate.sql.ast.tree.predicate.LikePredicate) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) BetweenPredicate(org.hibernate.sql.ast.tree.predicate.BetweenPredicate) NullnessPredicate(org.hibernate.sql.ast.tree.predicate.NullnessPredicate) GroupedPredicate(org.hibernate.sql.ast.tree.predicate.GroupedPredicate) BooleanExpressionPredicate(org.hibernate.sql.ast.tree.predicate.BooleanExpressionPredicate) ExistsPredicate(org.hibernate.sql.ast.tree.predicate.ExistsPredicate) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate)

Example 4 with QuerySpec

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;
}
Also used : Expression(org.hibernate.sql.ast.tree.expression.Expression) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression) SelfRenderingExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingExpression) OrderedSetAggregateFunctionExpression(org.hibernate.sql.ast.tree.expression.OrderedSetAggregateFunctionExpression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) SqlSelectionExpression(org.hibernate.sql.ast.tree.expression.SqlSelectionExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) ModifiedSubQueryExpression(org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression) BitSet(java.util.BitSet) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) SqlSelectionExpression(org.hibernate.sql.ast.tree.expression.SqlSelectionExpression)

Example 5 with QuerySpec

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;
}
Also used : SelectClause(org.hibernate.sql.ast.tree.select.SelectClause) NavigablePath(org.hibernate.query.spi.NavigablePath) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) SimpleFromClauseAccessImpl(org.hibernate.sql.ast.spi.SimpleFromClauseAccessImpl) SqlAliasBaseManager(org.hibernate.sql.ast.spi.SqlAliasBaseManager) SqlAliasBaseConstant(org.hibernate.sql.ast.spi.SqlAliasBaseConstant) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) AliasedExpression(org.hibernate.sql.ast.tree.expression.AliasedExpression) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) LoaderSqlAstCreationState(org.hibernate.loader.ast.internal.LoaderSqlAstCreationState) SqlSelectionImpl(org.hibernate.sql.results.internal.SqlSelectionImpl) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec)

Aggregations

QuerySpec (org.hibernate.sql.ast.tree.select.QuerySpec)50 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)30 SqlSelectionImpl (org.hibernate.sql.results.internal.SqlSelectionImpl)25 NavigablePath (org.hibernate.query.spi.NavigablePath)22 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)21 Expression (org.hibernate.sql.ast.tree.expression.Expression)21 ArrayList (java.util.ArrayList)19 SelectStatement (org.hibernate.sql.ast.tree.select.SelectStatement)19 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)18 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)16 NamedTableReference (org.hibernate.sql.ast.tree.from.NamedTableReference)16 PluralTableGroup (org.hibernate.sql.ast.tree.from.PluralTableGroup)16 TableReference (org.hibernate.sql.ast.tree.from.TableReference)16 ComparisonPredicate (org.hibernate.sql.ast.tree.predicate.ComparisonPredicate)16 InSubQueryPredicate (org.hibernate.sql.ast.tree.predicate.InSubQueryPredicate)15 List (java.util.List)13 JdbcParameter (org.hibernate.sql.ast.tree.expression.JdbcParameter)13 Collections (java.util.Collections)12 Map (java.util.Map)12 BasicValuedModelPart (org.hibernate.metamodel.mapping.BasicValuedModelPart)12