Search in sources :

Example 1 with FetchClauseType

use of org.hibernate.query.sqm.FetchClauseType 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 2 with FetchClauseType

use of org.hibernate.query.sqm.FetchClauseType in project hibernate-orm by hibernate.

the class AbstractSqlAstTranslator method renderRowNumberingSelectItems.

protected void renderRowNumberingSelectItems(SelectClause selectClause, QueryPart queryPart) {
    final FetchClauseType fetchClauseType = getFetchClauseTypeForRowNumbering(queryPart);
    if (fetchClauseType != null) {
        appendSql(COMA_SEPARATOR_CHAR);
        switch(fetchClauseType) {
            case PERCENT_ONLY:
                appendSql("count(*) over () cnt,");
            case ROWS_ONLY:
                renderRowNumber(selectClause, queryPart);
                appendSql(" rn");
                break;
            case PERCENT_WITH_TIES:
                appendSql("count(*) over () cnt,");
            case ROWS_WITH_TIES:
                if (queryPart.getOffsetClauseExpression() != null) {
                    renderRowNumber(selectClause, queryPart);
                    appendSql(" rn,");
                }
                if (selectClause.isDistinct()) {
                    appendSql("dense_rank()");
                } else {
                    appendSql("rank()");
                }
                visitOverClause(Collections.emptyList(), getSortSpecificationsRowNumbering(selectClause, queryPart));
                appendSql(" rnk");
                break;
        }
    }
}
Also used : FetchClauseType(org.hibernate.query.sqm.FetchClauseType)

Example 3 with FetchClauseType

use of org.hibernate.query.sqm.FetchClauseType in project hibernate-orm by hibernate.

the class SQLServerSqlAstTranslator method visitOffsetFetchClause.

@Override
public void visitOffsetFetchClause(QueryPart queryPart) {
    if (!isRowNumberingCurrentQueryPart()) {
        if (getDialect().getVersion().isBefore(9) && !queryPart.isRoot() && queryPart.getOffsetClauseExpression() != null) {
            throw new IllegalArgumentException("Can't emulate offset clause in subquery");
        }
        // Note that SQL Server is very strict i.e. it requires an order by clause for TOP or OFFSET
        final OffsetFetchClauseMode offsetFetchClauseMode = getOffsetFetchClauseMode(queryPart);
        if (offsetFetchClauseMode == OffsetFetchClauseMode.STANDARD) {
            if (!queryPart.hasSortSpecifications()) {
                appendSql(' ');
                renderEmptyOrderBy();
            }
            final Expression offsetExpression;
            final Expression fetchExpression;
            final FetchClauseType fetchClauseType;
            if (queryPart.isRoot() && hasLimit()) {
                prepareLimitOffsetParameters();
                offsetExpression = getOffsetParameter();
                fetchExpression = getLimitParameter();
                fetchClauseType = FetchClauseType.ROWS_ONLY;
            } else {
                offsetExpression = queryPart.getOffsetClauseExpression();
                fetchExpression = queryPart.getFetchClauseExpression();
                fetchClauseType = queryPart.getFetchClauseType();
            }
            if (offsetExpression == null) {
                appendSql(" offset 0 rows");
            } else {
                renderOffset(offsetExpression, true);
            }
            if (fetchExpression != null) {
                renderFetch(fetchExpression, null, fetchClauseType);
            }
        } else if (offsetFetchClauseMode == OffsetFetchClauseMode.TOP_ONLY && !queryPart.hasSortSpecifications()) {
            appendSql(' ');
            renderEmptyOrderBy();
        }
    }
}
Also used : FetchClauseType(org.hibernate.query.sqm.FetchClauseType) Expression(org.hibernate.sql.ast.tree.expression.Expression)

Aggregations

FetchClauseType (org.hibernate.query.sqm.FetchClauseType)3 Expression (org.hibernate.sql.ast.tree.expression.Expression)1 QuerySpec (org.hibernate.sql.ast.tree.select.QuerySpec)1