use of org.hibernate.procedure.spi.FunctionReturnImplementor 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();
}
use of org.hibernate.procedure.spi.FunctionReturnImplementor 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();
}
Aggregations