package org.hibernate.procedure.internal;
import java.sql.CallableStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import javax.persistence.ParameterMode;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.procedure.spi.CallableStatementSupport;
import org.hibernate.procedure.spi.ParameterRegistrationImplementor;
import org.hibernate.procedure.spi.ParameterStrategy;
public class PostgresCallableStatementSupport implements CallableStatementSupport {
public static final PostgresCallableStatementSupport INSTANCE = new PostgresCallableStatementSupport();
@Override
public String renderCallableStatement(
String procedureName,
ParameterStrategy parameterStrategy,
List<ParameterRegistrationImplementor<?>> parameterRegistrations,
SharedSessionContractImplementor session) {
final boolean firstParamIsRefCursor = ! parameterRegistrations.isEmpty()
&& parameterRegistrations.get( 0 ).getMode() == ParameterMode.REF_CURSOR;
if ( firstParamIsRefCursor ) {
if ( parameterStrategy == ParameterStrategy.NAMED ) {
throw new HibernateException( "Cannot mix named parameters and REF_CURSOR parameter on PostgreSQL" );
}
}
final StringBuilder buffer;
if ( firstParamIsRefCursor ) {
buffer = new StringBuilder().append( "{? = call " );
}
else {
buffer = new StringBuilder().append( "{call " );
}
buffer.append( procedureName ).append( "(" );
String sep = "";
final int startIndex = firstParamIsRefCursor ? 1 : 0;
for ( int i = startIndex; i < parameterRegistrations.size(); i++ ) {
final ParameterRegistrationImplementor parameter = parameterRegistrations.get( i );
if ( parameter.getMode() == ParameterMode.REF_CURSOR ) {
throw new HibernateException( "PostgreSQL supports only one REF_CURSOR parameter, but multiple were registered" );
}
for ( int ignored : parameter.getSqlTypes() ) {
buffer.append( sep ).append( "?" );
sep = ",";
}
}
return buffer.append( ")}" ).toString();
}
@Override
public void registerParameters(
String procedureName,
CallableStatement statement,
ParameterStrategy parameterStrategy,
List<ParameterRegistrationImplementor<?>> parameterRegistrations,
SharedSessionContractImplementor session) {
int i = 1;
try {
for ( ParameterRegistrationImplementor parameter : parameterRegistrations ) {
if ( parameter.getMode() == ParameterMode.REF_CURSOR ) {
statement.registerOutParameter( i, Types.OTHER );
i++;
}
else {
parameter.prepare( statement, i );
i += parameter.getSqlTypes().length;
}
}
}
catch (SQLException e) {
throw session.getJdbcServices().getSqlExceptionHelper().convert(
e,
"Error registering CallableStatement parameters",
procedureName
);
}
}
}