Search in sources :

Example 1 with ExecutionException

use of org.hibernate.sql.exec.ExecutionException in project hibernate-orm by hibernate.

the class AbstractJdbcParameter method bindParameterValue.

@Override
public void bindParameterValue(PreparedStatement statement, int startPosition, JdbcParameterBindings jdbcParamBindings, ExecutionContext executionContext) throws SQLException {
    final JdbcParameterBinding binding = jdbcParamBindings.getBinding(AbstractJdbcParameter.this);
    if (binding == null) {
        throw new ExecutionException("JDBC parameter value not bound - " + this);
    }
    final Object bindValue = binding.getBindValue();
    JdbcMapping jdbcMapping = binding.getBindType();
    if (jdbcMapping == null) {
        jdbcMapping = this.jdbcMapping;
    }
    // If the parameter type is not known from the context i.e. null or Object, infer it from the bind value
    if (jdbcMapping == null || jdbcMapping.getMappedJavaType().getJavaTypeClass() == Object.class) {
        jdbcMapping = guessBindType(executionContext, bindValue, jdbcMapping);
    }
    bindParameterValue(jdbcMapping, statement, bindValue, startPosition, executionContext);
}
Also used : JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) JdbcParameterBinding(org.hibernate.sql.exec.spi.JdbcParameterBinding) ExecutionException(org.hibernate.sql.exec.ExecutionException)

Example 2 with ExecutionException

use of org.hibernate.sql.exec.ExecutionException in project hibernate-orm by hibernate.

the class AbstractSqlAstTranslator method renderFetchPlusOffsetExpressionAsSingleParameter.

protected void renderFetchPlusOffsetExpressionAsSingleParameter(Expression fetchClauseExpression, Expression offsetClauseExpression, int offset) {
    if (fetchClauseExpression instanceof Literal) {
        final Number fetchCount = (Number) ((Literal) fetchClauseExpression).getLiteralValue();
        if (offsetClauseExpression instanceof Literal) {
            final Number offsetCount = (Number) ((Literal) offsetClauseExpression).getLiteralValue();
            appendSql(fetchCount.intValue() + offsetCount.intValue() + offset);
        } else {
            appendSql(PARAM_MARKER);
            final JdbcParameter offsetParameter = (JdbcParameter) offsetClauseExpression;
            final int offsetValue = offset + fetchCount.intValue();
            jdbcParameters.addParameter(offsetParameter);
            parameterBinders.add((statement, startPosition, jdbcParameterBindings, executionContext) -> {
                final JdbcParameterBinding binding = jdbcParameterBindings.getBinding(offsetParameter);
                if (binding == null) {
                    throw new ExecutionException("JDBC parameter value not bound - " + offsetParameter);
                }
                final Number bindValue = (Number) binding.getBindValue();
                offsetParameter.getExpressionType().getJdbcMappings().get(0).getJdbcValueBinder().bind(statement, bindValue.intValue() + offsetValue, startPosition, executionContext.getSession());
            });
        }
    } else {
        appendSql(PARAM_MARKER);
        final JdbcParameter offsetParameter = (JdbcParameter) offsetClauseExpression;
        final JdbcParameter fetchParameter = (JdbcParameter) fetchClauseExpression;
        final OffsetReceivingParameterBinder fetchBinder = new OffsetReceivingParameterBinder(offsetParameter, fetchParameter, offset);
        // And in this case, we only want to bind a single JDBC parameter
        if (!(offsetParameter instanceof OffsetJdbcParameter)) {
            jdbcParameters.addParameter(offsetParameter);
            parameterBinders.add((statement, startPosition, jdbcParameterBindings, executionContext) -> {
                final JdbcParameterBinding binding = jdbcParameterBindings.getBinding(offsetParameter);
                if (binding == null) {
                    throw new ExecutionException("JDBC parameter value not bound - " + offsetParameter);
                }
                fetchBinder.dynamicOffset = (Number) binding.getBindValue();
            });
        }
        jdbcParameters.addParameter(fetchParameter);
        parameterBinders.add(fetchBinder);
    }
}
Also used : FilterJdbcParameter(org.hibernate.internal.FilterJdbcParameter) AbstractJdbcParameter(org.hibernate.sql.exec.internal.AbstractJdbcParameter) JdbcParameter(org.hibernate.sql.ast.tree.expression.JdbcParameter) SqlTypedMappingJdbcParameter(org.hibernate.sql.exec.internal.SqlTypedMappingJdbcParameter) JdbcParameterBinding(org.hibernate.sql.exec.spi.JdbcParameterBinding) ConvertedQueryLiteral(org.hibernate.sql.ast.tree.expression.ConvertedQueryLiteral) Literal(org.hibernate.sql.ast.tree.expression.Literal) JdbcLiteral(org.hibernate.sql.ast.tree.expression.JdbcLiteral) QueryLiteral(org.hibernate.sql.ast.tree.expression.QueryLiteral) EntityTypeLiteral(org.hibernate.sql.ast.tree.expression.EntityTypeLiteral) ExecutionException(org.hibernate.sql.exec.ExecutionException)

Aggregations

ExecutionException (org.hibernate.sql.exec.ExecutionException)2 JdbcParameterBinding (org.hibernate.sql.exec.spi.JdbcParameterBinding)2 FilterJdbcParameter (org.hibernate.internal.FilterJdbcParameter)1 JdbcMapping (org.hibernate.metamodel.mapping.JdbcMapping)1 ConvertedQueryLiteral (org.hibernate.sql.ast.tree.expression.ConvertedQueryLiteral)1 EntityTypeLiteral (org.hibernate.sql.ast.tree.expression.EntityTypeLiteral)1 JdbcLiteral (org.hibernate.sql.ast.tree.expression.JdbcLiteral)1 JdbcParameter (org.hibernate.sql.ast.tree.expression.JdbcParameter)1 Literal (org.hibernate.sql.ast.tree.expression.Literal)1 QueryLiteral (org.hibernate.sql.ast.tree.expression.QueryLiteral)1 AbstractJdbcParameter (org.hibernate.sql.exec.internal.AbstractJdbcParameter)1 SqlTypedMappingJdbcParameter (org.hibernate.sql.exec.internal.SqlTypedMappingJdbcParameter)1