org.hibernate.procedure.internal.ProcedureOutputsImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-core Show documentation
Show all versions of hibernate-core Show documentation
Hibernate's core ORM functionality
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or .
*/
package org.hibernate.procedure.internal;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.hibernate.engine.jdbc.cursor.spi.RefCursorSupport;
import org.hibernate.metamodel.model.domain.AllowableOutputParameterType;
import org.hibernate.metamodel.model.domain.AllowableParameterType;
import org.hibernate.procedure.ParameterMisuseException;
import org.hibernate.procedure.ProcedureOutputs;
import org.hibernate.procedure.spi.ParameterStrategy;
import org.hibernate.procedure.spi.ProcedureParameterImplementor;
import org.hibernate.query.procedure.ProcedureParameter;
import org.hibernate.result.Output;
import org.hibernate.result.internal.OutputsImpl;
import org.hibernate.sql.exec.ExecutionException;
import jakarta.persistence.ParameterMode;
/**
* Implementation of ProcedureResult. Defines centralized access to all of the results of a procedure call.
*
* @author Steve Ebersole
*/
public class ProcedureOutputsImpl extends OutputsImpl implements ProcedureOutputs {
private final ProcedureCallImpl procedureCall;
private final CallableStatement callableStatement;
private final ProcedureParameterImplementor[] refCursorParameters;
private int refCursorParamIndex;
ProcedureOutputsImpl(ProcedureCallImpl procedureCall, CallableStatement callableStatement) {
super( procedureCall, callableStatement );
this.procedureCall = procedureCall;
this.callableStatement = callableStatement;
this.refCursorParameters = procedureCall.collectRefCursorParameters();
}
@Override
public T getOutputParameterValue(ProcedureParameter parameter) {
final AllowableParameterType hibernateType = parameter.getHibernateType();
if ( parameter.getMode() == ParameterMode.IN ) {
throw new ParameterMisuseException( "IN parameter not valid for output extraction" );
}
try {
if ( parameter.getMode() == ParameterMode.REF_CURSOR ) {
if ( parameter.getPosition() != null ) {
return (T) callableStatement.getObject( parameter.getPosition() );
}
else {
return (T) callableStatement.getObject( parameter.getName() );
}
}
else if ( hibernateType instanceof AllowableOutputParameterType ) {
//noinspection unchecked
if ( parameter.getPosition() != null ) {
return (T) ( (AllowableOutputParameterType) hibernateType ).extract(
callableStatement,
parameter.getPosition(),
procedureCall.getSession()
);
}
else {
return (T) ( (AllowableOutputParameterType) hibernateType ).extract(
callableStatement,
parameter.getName(),
procedureCall.getSession()
);
}
}
else {
throw new ParameterMisuseException( "Parameter type cannot extract procedure output parameters" );
}
}
catch (SQLException e) {
throw new ExecutionException(
"Error extracting procedure output parameter value ["
+ parameter.getPosition() != null ?
String.valueOf( parameter.getPosition() ) :
parameter.getName() + "]",
e
);
}
}
@Override
public Object getOutputParameterValue(String name) {
return getOutputParameterValue( procedureCall.getParameterMetadata().getQueryParameter( name ) );
}
@Override
public Object getOutputParameterValue(int position) {
return getOutputParameterValue( procedureCall.getParameterMetadata().getQueryParameter( position ) );
}
@Override
protected CurrentReturnState buildCurrentReturnState(boolean isResultSet, int updateCount) {
return new ProcedureCurrentReturnState( isResultSet, updateCount, refCursorParamIndex );
}
protected class ProcedureCurrentReturnState extends CurrentReturnState {
private final int refCursorParamIndex;
private ProcedureCurrentReturnState(boolean isResultSet, int updateCount, int refCursorParamIndex) {
super( isResultSet, updateCount );
this.refCursorParamIndex = refCursorParamIndex;
}
@Override
public boolean indicatesMoreOutputs() {
return super.indicatesMoreOutputs()
|| ProcedureOutputsImpl.this.refCursorParamIndex < ProcedureOutputsImpl.this.refCursorParameters.length;
}
@Override
protected boolean hasExtendedReturns() {
return refCursorParamIndex < refCursorParameters.length;
}
@Override
protected Output buildExtendedReturn() {
ProcedureOutputsImpl.this.refCursorParamIndex++;
final ProcedureParameterImplementor refCursorParam = ProcedureOutputsImpl.this.refCursorParameters[refCursorParamIndex];
ResultSet resultSet;
if ( refCursorParam.getName() != null ) {
resultSet = ProcedureOutputsImpl.this.procedureCall.getSession().getFactory().getServiceRegistry()
.getService( RefCursorSupport.class )
.getResultSet( ProcedureOutputsImpl.this.callableStatement, refCursorParam.getName() );
}
else {
resultSet = ProcedureOutputsImpl.this.procedureCall.getSession().getFactory().getServiceRegistry()
.getService( RefCursorSupport.class )
.getResultSet( ProcedureOutputsImpl.this.callableStatement, refCursorParam.getPosition() );
}
return buildResultSetOutput( () -> extractResults( resultSet ) );
}
}
}