Search in sources :

Example 1 with ParameterStrategy

use of org.hibernate.procedure.spi.ParameterStrategy in project hibernate-orm by hibernate.

the class StandardCallableStatementSupport method interpretCall.

@Override
public JdbcCall interpretCall(ProcedureCallImplementor<?> procedureCall) {
    final String procedureName = procedureCall.getProcedureName();
    final FunctionReturnImplementor functionReturn = procedureCall.getFunctionReturn();
    final ProcedureParameterMetadataImplementor parameterMetadata = procedureCall.getParameterMetadata();
    final SharedSessionContractImplementor session = procedureCall.getSession();
    final List<? extends ProcedureParameterImplementor<?>> registrations = parameterMetadata.getRegistrationsAsList();
    final ParameterStrategy parameterStrategy = functionReturn == null && parameterMetadata.hasNamedParameters() ? ParameterStrategy.NAMED : ParameterStrategy.POSITIONAL;
    final JdbcCallImpl.Builder builder = new JdbcCallImpl.Builder(parameterStrategy);
    final StringBuilder buffer;
    final int offset;
    if (functionReturn != null && !implicitReturn) {
        offset = 2;
        buffer = new StringBuilder(11 + procedureName.length() + registrations.size() * 2).append("{?=call ");
        builder.setFunctionReturn(functionReturn.toJdbcFunctionReturn(session));
    } else {
        offset = 1;
        buffer = new StringBuilder(9 + procedureName.length() + registrations.size() * 2).append("{call ");
    }
    buffer.append(procedureName).append("(");
    String sep = "";
    for (int i = 0; i < registrations.size(); i++) {
        final ProcedureParameterImplementor<?> parameter = registrations.get(i);
        if (parameter.getMode() == ParameterMode.REF_CURSOR) {
            verifyRefCursorSupport(session.getJdbcServices().getJdbcEnvironment().getDialect());
        }
        buffer.append(sep);
        final JdbcCallParameterRegistration registration = parameter.toJdbcParameterRegistration(i + offset, procedureCall);
        if (registration.getName() != null) {
            buffer.append(':').append(registration.getName());
        } else {
            buffer.append("?");
        }
        sep = ",";
        builder.addParameterRegistration(registration);
    }
    buffer.append(")}");
    builder.setCallableName(buffer.toString());
    return builder.buildJdbcCall();
}
Also used : SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) ParameterStrategy(org.hibernate.procedure.spi.ParameterStrategy) JdbcCallImpl(org.hibernate.sql.exec.internal.JdbcCallImpl) JdbcCallParameterRegistration(org.hibernate.sql.exec.spi.JdbcCallParameterRegistration) ProcedureParameterMetadataImplementor(org.hibernate.query.spi.ProcedureParameterMetadataImplementor) FunctionReturnImplementor(org.hibernate.procedure.spi.FunctionReturnImplementor)

Example 2 with ParameterStrategy

use of org.hibernate.procedure.spi.ParameterStrategy in project hibernate-orm by hibernate.

the class PostgresCallableStatementSupport method interpretCall.

@Override
public JdbcCall interpretCall(ProcedureCallImplementor<?> procedureCall) {
    final String procedureName = procedureCall.getProcedureName();
    final FunctionReturnImplementor functionReturn = procedureCall.getFunctionReturn();
    final ProcedureParameterMetadataImplementor parameterMetadata = procedureCall.getParameterMetadata();
    final SharedSessionContractImplementor session = procedureCall.getSession();
    final boolean firstParamIsRefCursor = parameterMetadata.getParameterCount() != 0 && isFirstParameterModeRefCursor(parameterMetadata);
    if (firstParamIsRefCursor || functionReturn != null) {
        // validate that the parameter strategy is positional (cannot mix, and REF_CURSOR is inherently positional)
        if (parameterMetadata.hasNamedParameters()) {
            throw new HibernateException("Cannot mix named parameters and REF_CURSOR parameter on PostgreSQL");
        }
    }
    final List<? extends ProcedureParameterImplementor<?>> registrations = parameterMetadata.getRegistrationsAsList();
    final ParameterStrategy parameterStrategy = parameterMetadata.hasNamedParameters() ? ParameterStrategy.NAMED : ParameterStrategy.POSITIONAL;
    final JdbcCallImpl.Builder builder = new JdbcCallImpl.Builder(parameterStrategy);
    final StringBuilder buffer;
    final int offset;
    final int startIndex;
    if (functionReturn != null) {
        offset = 2;
        startIndex = 0;
        buffer = new StringBuilder(11 + procedureName.length() + registrations.size() * 2).append("{?=call ");
        builder.setFunctionReturn(functionReturn.toJdbcFunctionReturn(session));
    } else if (firstParamIsRefCursor) {
        offset = 1;
        startIndex = 1;
        buffer = new StringBuilder(11 + procedureName.length() + registrations.size() * 2).append("{?=call ");
        builder.addParameterRegistration(registrations.get(0).toJdbcParameterRegistration(1, procedureCall));
    } else {
        offset = 1;
        startIndex = 0;
        buffer = new StringBuilder(9 + procedureName.length() + registrations.size() * 2).append("{call ");
    }
    buffer.append(procedureName).append("(");
    String sep = "";
    for (int i = startIndex; i < registrations.size(); i++) {
        final ProcedureParameterImplementor<?> parameter = registrations.get(i);
        if (parameter.getMode() == ParameterMode.REF_CURSOR) {
            throw new HibernateException("PostgreSQL supports only one REF_CURSOR parameter, but multiple were registered");
        }
        buffer.append(sep);
        final JdbcCallParameterRegistration registration = parameter.toJdbcParameterRegistration(i + offset, procedureCall);
        if (registration.getName() != null) {
            buffer.append(':').append(registration.getName());
        } else {
            buffer.append("?");
        }
        sep = ",";
        builder.addParameterRegistration(registration);
    }
    buffer.append(")}");
    builder.setCallableName(buffer.toString());
    return builder.buildJdbcCall();
}
Also used : HibernateException(org.hibernate.HibernateException) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) ParameterStrategy(org.hibernate.procedure.spi.ParameterStrategy) JdbcCallImpl(org.hibernate.sql.exec.internal.JdbcCallImpl) JdbcCallParameterRegistration(org.hibernate.sql.exec.spi.JdbcCallParameterRegistration) ProcedureParameterMetadataImplementor(org.hibernate.query.spi.ProcedureParameterMetadataImplementor) FunctionReturnImplementor(org.hibernate.procedure.spi.FunctionReturnImplementor)

Aggregations

SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)2 FunctionReturnImplementor (org.hibernate.procedure.spi.FunctionReturnImplementor)2 ParameterStrategy (org.hibernate.procedure.spi.ParameterStrategy)2 ProcedureParameterMetadataImplementor (org.hibernate.query.spi.ProcedureParameterMetadataImplementor)2 JdbcCallImpl (org.hibernate.sql.exec.internal.JdbcCallImpl)2 JdbcCallParameterRegistration (org.hibernate.sql.exec.spi.JdbcCallParameterRegistration)2 HibernateException (org.hibernate.HibernateException)1