Search in sources :

Example 1 with ParameterBind

use of org.hibernate.procedure.ParameterBind in project hibernate-orm by hibernate.

the class ProcedureParameterImpl method prepare.

@Override
public void prepare(CallableStatement statement, int startIndex) throws SQLException {
    // initially set up the Type we will use for binding as the explicit type.
    Type typeToUse = getHibernateType();
    int[] sqlTypesToUse = sqlTypes;
    final ParameterBind bind = getBind();
    // however, for Calendar binding with an explicit TemporalType we may need to adjust this...
    if (bind != null && bind.getExplicitTemporalType() != null) {
        if (Calendar.class.isInstance(bind.getValue())) {
            switch(bind.getExplicitTemporalType()) {
                case TIMESTAMP:
                    {
                        typeToUse = CalendarType.INSTANCE;
                        sqlTypesToUse = typeToUse.sqlTypes(procedureCall.getSession().getFactory());
                        break;
                    }
                case DATE:
                    {
                        typeToUse = CalendarDateType.INSTANCE;
                        sqlTypesToUse = typeToUse.sqlTypes(procedureCall.getSession().getFactory());
                        break;
                    }
                case TIME:
                    {
                        typeToUse = CalendarTimeType.INSTANCE;
                        sqlTypesToUse = typeToUse.sqlTypes(procedureCall.getSession().getFactory());
                        break;
                    }
            }
        }
    }
    this.startIndex = startIndex;
    if (mode == ParameterMode.IN || mode == ParameterMode.INOUT || mode == ParameterMode.OUT) {
        if (mode == ParameterMode.INOUT || mode == ParameterMode.OUT) {
            if (sqlTypesToUse.length > 1) {
                // there is more than one column involved; see if the Hibernate Type can handle
                // multi-param extraction...
                final boolean canHandleMultiParamExtraction = ProcedureParameterExtractionAware.class.isInstance(typeToUse) && ((ProcedureParameterExtractionAware) typeToUse).canDoExtraction();
                if (!canHandleMultiParamExtraction) {
                    // it cannot...
                    throw new UnsupportedOperationException("Type [" + typeToUse + "] does support multi-parameter value extraction");
                }
            }
            // e.g., Oracle.
            if (sqlTypesToUse.length == 1 && procedureCall.getParameterStrategy() == ParameterStrategy.NAMED && canDoNameParameterBinding(typeToUse)) {
                statement.registerOutParameter(getName(), sqlTypesToUse[0]);
            } else {
                for (int i = 0; i < sqlTypesToUse.length; i++) {
                    statement.registerOutParameter(startIndex + i, sqlTypesToUse[i]);
                }
            }
        }
        if (mode == ParameterMode.INOUT || mode == ParameterMode.IN) {
            if (bind == null || bind.getValue() == null) {
                // parameter defines a default value.  Deferring to that information would be the best option
                if (isPassNullsEnabled()) {
                    log.debugf("Stored procedure [%s] IN/INOUT parameter [%s] not bound and `passNulls` was set to true; binding NULL", procedureCall.getProcedureName(), this);
                    if (this.procedureCall.getParameterStrategy() == ParameterStrategy.NAMED && canDoNameParameterBinding(typeToUse)) {
                        ((ProcedureParameterNamedBinder) typeToUse).nullSafeSet(statement, null, this.getName(), procedureCall.getSession());
                    } else {
                        typeToUse.nullSafeSet(statement, null, startIndex, procedureCall.getSession());
                    }
                } else {
                    log.debugf("Stored procedure [%s] IN/INOUT parameter [%s] not bound and `passNulls` was set to false; assuming procedure defines default value", procedureCall.getProcedureName(), this);
                }
            } else {
                if (this.procedureCall.getParameterStrategy() == ParameterStrategy.NAMED && canDoNameParameterBinding(typeToUse)) {
                    ((ProcedureParameterNamedBinder) typeToUse).nullSafeSet(statement, bind.getValue(), this.getName(), procedureCall.getSession());
                } else {
                    typeToUse.nullSafeSet(statement, bind.getValue(), startIndex, procedureCall.getSession());
                }
            }
        }
    } else {
        // we have a REF_CURSOR type param
        if (procedureCall.getParameterStrategy() == ParameterStrategy.NAMED) {
            procedureCall.getSession().getFactory().getServiceRegistry().getService(RefCursorSupport.class).registerRefCursorParameter(statement, getName());
        } else {
            procedureCall.getSession().getFactory().getServiceRegistry().getService(RefCursorSupport.class).registerRefCursorParameter(statement, startIndex);
        }
    }
}
Also used : RefCursorSupport(org.hibernate.engine.jdbc.cursor.spi.RefCursorSupport) TemporalType(javax.persistence.TemporalType) CalendarTimeType(org.hibernate.type.CalendarTimeType) CalendarType(org.hibernate.type.CalendarType) CalendarDateType(org.hibernate.type.CalendarDateType) Type(org.hibernate.type.Type) ProcedureParameterNamedBinder(org.hibernate.type.ProcedureParameterNamedBinder) ParameterBind(org.hibernate.procedure.ParameterBind) ProcedureParameterExtractionAware(org.hibernate.type.ProcedureParameterExtractionAware)

Example 2 with ParameterBind

use of org.hibernate.procedure.ParameterBind in project hibernate-orm by hibernate.

the class ProcedureParamBindings method getBinding.

@Override
public <T> QueryParameterBinding<T> getBinding(QueryParameter<T> parameter) {
    final ProcedureParameterImplementor<T> procParam = parameterMetadata.resolve(parameter);
    ParameterBind binding = bindingMap.get(procParam);
    if (binding == null) {
        if (!parameterMetadata.containsReference(parameter)) {
            throw new IllegalArgumentException("Passed parameter is not registered with this query");
        }
        binding = new ParameterBindImpl(procParam, this);
        bindingMap.put(procParam, binding);
    }
    return binding;
}
Also used : ParameterBindImpl(org.hibernate.procedure.internal.ParameterBindImpl) ParameterBind(org.hibernate.procedure.ParameterBind)

Aggregations

ParameterBind (org.hibernate.procedure.ParameterBind)2 TemporalType (javax.persistence.TemporalType)1 RefCursorSupport (org.hibernate.engine.jdbc.cursor.spi.RefCursorSupport)1 ParameterBindImpl (org.hibernate.procedure.internal.ParameterBindImpl)1 CalendarDateType (org.hibernate.type.CalendarDateType)1 CalendarTimeType (org.hibernate.type.CalendarTimeType)1 CalendarType (org.hibernate.type.CalendarType)1 ProcedureParameterExtractionAware (org.hibernate.type.ProcedureParameterExtractionAware)1 ProcedureParameterNamedBinder (org.hibernate.type.ProcedureParameterNamedBinder)1 Type (org.hibernate.type.Type)1