All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.procedure.internal.ProcedureResultImpl Maven / Gradle / Ivy

There is a newer version: 7.0.0.Alpha2
Show newest version
/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * Copyright (c) 2012, Red Hat Inc. or third-party contributors as
 * indicated by the @author tags or express copyright attribution
 * statements applied by the authors.  All third-party contributions are
 * distributed under license by Red Hat Inc.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.hibernate.procedure.internal;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.hibernate.JDBCException;
import org.hibernate.engine.jdbc.cursor.spi.RefCursorSupport;
import org.hibernate.procedure.ParameterRegistration;
import org.hibernate.procedure.ProcedureResult;
import org.hibernate.result.Return;
import org.hibernate.result.internal.ResultImpl;

/**
 * Implementation of ProcedureResult.  Defines centralized access to all of the results of a procedure call.
 *
 * @author Steve Ebersole
 */
public class ProcedureResultImpl extends ResultImpl implements ProcedureResult {
	private final ProcedureCallImpl procedureCall;
	private final CallableStatement callableStatement;

	private final ParameterRegistrationImplementor[] refCursorParameters;
	private int refCursorParamIndex;

	ProcedureResultImpl(ProcedureCallImpl procedureCall, CallableStatement callableStatement) {
		super( procedureCall, callableStatement );
		this.procedureCall = procedureCall;
		this.callableStatement = callableStatement;

		this.refCursorParameters = procedureCall.collectRefCursorParameters();
	}

	@Override
	public  T getOutputParameterValue(ParameterRegistration parameterRegistration) {
		return ( (ParameterRegistrationImplementor) parameterRegistration ).extract( callableStatement );
	}

	@Override
	public Object getOutputParameterValue(String name) {
		return procedureCall.getParameterRegistration( name ).extract( callableStatement );
	}

	@Override
	public Object getOutputParameterValue(int position) {
		return procedureCall.getParameterRegistration( position ).extract( callableStatement );
	}

	@Override
	protected CurrentReturnDescriptor buildCurrentReturnDescriptor(boolean isResultSet, int updateCount) {
		return new ProcedureCurrentReturnDescriptor( isResultSet, updateCount, refCursorParamIndex );
	}

	protected boolean hasMoreReturns(CurrentReturnDescriptor descriptor) {
		return super.hasMoreReturns( descriptor )
				|| ( (ProcedureCurrentReturnDescriptor) descriptor ).refCursorParamIndex < refCursorParameters.length;
	}

	@Override
	protected Return buildExtendedReturn(CurrentReturnDescriptor returnDescriptor) {
		this.refCursorParamIndex++;
		final int refCursorParamIndex = ( (ProcedureCurrentReturnDescriptor) returnDescriptor ).refCursorParamIndex;
		final ParameterRegistrationImplementor refCursorParam = refCursorParameters[refCursorParamIndex];
		ResultSet resultSet;
		if ( refCursorParam.getName() != null ) {
			resultSet = procedureCall.getSession().getFactory().getServiceRegistry()
					.getService( RefCursorSupport.class )
					.getResultSet( callableStatement, refCursorParam.getName() );
		}
		else {
			resultSet = procedureCall.getSession().getFactory().getServiceRegistry()
					.getService( RefCursorSupport.class )
					.getResultSet( callableStatement, refCursorParam.getPosition() );
		}
		return new ResultSetReturn( this, resultSet );
	}

	protected JDBCException convert(SQLException e, String message) {
		return procedureCall.getSession().getFactory().getSQLExceptionHelper().convert(
				e,
				message,
				procedureCall.getProcedureName()
		);
	}

	protected static class ProcedureCurrentReturnDescriptor extends CurrentReturnDescriptor {
		private final int refCursorParamIndex;

		private ProcedureCurrentReturnDescriptor(boolean isResultSet, int updateCount, int refCursorParamIndex) {
			super( isResultSet, updateCount );
			this.refCursorParamIndex = refCursorParamIndex;
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy