Search in sources :

Example 1 with QueryParameterBinding

use of org.hibernate.query.spi.QueryParameterBinding in project hibernate-orm by hibernate.

the class QueryParameterBindingsImpl method collectPositionalBindTypes.

/**
	 * @deprecated (since 5.2) expect a different approach to org.hibernate.engine.spi.QueryParameters in 6.0
	 */
@Deprecated
public Type[] collectPositionalBindTypes() {
    Type[] types = new Type[positionalParameterBindings.size()];
    for (Map.Entry<Integer, QueryParameterBinding> entry : positionalParameterBindings.entrySet()) {
        final int position = entry.getKey();
        Type type = entry.getValue().getBindType();
        if (type == null) {
            log.debugf("Binding for positional-parameter [%s] did not define type, using SerializableType", position);
            type = SerializableType.INSTANCE;
        }
        types[position] = type;
    }
    return types;
}
Also used : SerializableType(org.hibernate.type.SerializableType) Type(org.hibernate.type.Type) QueryParameterBinding(org.hibernate.query.spi.QueryParameterBinding) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 2 with QueryParameterBinding

use of org.hibernate.query.spi.QueryParameterBinding in project hibernate-orm by hibernate.

the class QueryParameterBindingsImpl method getBinding.

public QueryParameterBinding getBinding(int position) {
    int positionAdjustment = 0;
    if (!parameterMetadata.isOrdinalParametersZeroBased()) {
        positionAdjustment = -1;
    }
    QueryParameterBinding binding = null;
    if (parameterMetadata != null) {
        if (!parameterMetadata.hasPositionalParameters()) {
            // no positional parameters, assume jpa named.
            binding = locateBinding(Integer.toString(position));
        } else {
            try {
                binding = positionalParameterBindings.get(position + positionAdjustment);
                if (binding == null) {
                    binding = makeBinding(parameterMetadata.getQueryParameter(position));
                    positionalParameterBindings.put(position + positionAdjustment, binding);
                }
            } catch (QueryParameterException e) {
            // treat this as null binding
            }
        }
    }
    if (binding == null) {
        throw new IllegalArgumentException("Unknown parameter position: " + position);
    }
    return binding;
}
Also used : QueryParameterException(org.hibernate.QueryParameterException) QueryParameterBinding(org.hibernate.query.spi.QueryParameterBinding)

Example 3 with QueryParameterBinding

use of org.hibernate.query.spi.QueryParameterBinding in project hibernate-orm by hibernate.

the class QueryParameterBindingsImpl method verifyParametersBound.

public void verifyParametersBound(boolean reserveFirstParameter) {
    // verify named parameters bound
    for (Map.Entry<QueryParameter, QueryParameterBinding> bindEntry : parameterBindingMap.entrySet()) {
        if (!bindEntry.getValue().isBound()) {
            if (bindEntry.getKey().getName() != null) {
                throw new QueryException("Named parameter [" + bindEntry.getKey().getName() + "] not set");
            } else {
                throw new QueryException("Parameter memento [" + bindEntry.getKey() + "] not set");
            }
        }
    }
    // verify position parameters bound
    int startIndex = 0;
    if (!parameterMetadata.isOrdinalParametersZeroBased()) {
        startIndex = 1;
    }
    for (int i = startIndex; i < positionalParameterBindings.size(); i++) {
        QueryParameterBinding binding = null;
        if (parameterMetadata.isOrdinalParametersZeroBased()) {
            binding = positionalParameterBindings.get(i);
        } else {
            binding = positionalParameterBindings.get(i - 1);
        }
        if (binding == null || !binding.isBound()) {
            throw new QueryException("Positional parameter [" + i + "] not set");
        }
    }
    // verify position parameter count is correct
    final int positionalValueSpan = calculatePositionalValueSpan(reserveFirstParameter);
    final int positionCounts = parameterMetadata.getPositionalParameterCount();
    if (positionCounts != positionalValueSpan) {
        if (reserveFirstParameter && positionCounts - 1 != positionalValueSpan) {
            throw new QueryException("Expected positional parameter count: " + (positionCounts - 1) + ", actually detected " + positionalValueSpan);
        } else if (!reserveFirstParameter) {
            throw new QueryException("Expected positional parameter count: " + (positionCounts) + ", actually detected " + positionalValueSpan);
        }
    }
}
Also used : QueryParameter(org.hibernate.query.QueryParameter) QueryException(org.hibernate.QueryException) QueryParameterBinding(org.hibernate.query.spi.QueryParameterBinding) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 4 with QueryParameterBinding

use of org.hibernate.query.spi.QueryParameterBinding in project hibernate-orm by hibernate.

the class QueryParameterBindingsImpl method calculatePositionalValueSpan.

private int calculatePositionalValueSpan(boolean reserveFirstParameter) {
    int positionalValueSpan = 0;
    for (QueryParameterBinding binding : positionalParameterBindings.values()) {
        if (binding.isBound()) {
            Type bindType = binding.getBindType();
            if (bindType == null) {
                bindType = SerializableType.INSTANCE;
            }
            positionalValueSpan += bindType.getColumnSpan(sessionFactory);
        }
    }
    return positionalValueSpan;
}
Also used : SerializableType(org.hibernate.type.SerializableType) Type(org.hibernate.type.Type) QueryParameterBinding(org.hibernate.query.spi.QueryParameterBinding)

Example 5 with QueryParameterBinding

use of org.hibernate.query.spi.QueryParameterBinding in project hibernate-orm by hibernate.

the class QueryParameterBindingsImpl method makeBinding.

@SuppressWarnings("WeakerAccess")
protected QueryParameterBinding makeBinding(QueryParameter queryParameter) {
    assert !parameterBindingMap.containsKey(queryParameter);
    if (!parameterMetadata.containsReference(queryParameter)) {
        throw new IllegalArgumentException("Cannot create binding for parameter reference [" + queryParameter + "] - reference is not a parameter of this query");
    }
    final QueryParameterBinding binding = makeBinding(queryParameter.getType());
    parameterBindingMap.put(queryParameter, binding);
    return binding;
}
Also used : QueryParameterBinding(org.hibernate.query.spi.QueryParameterBinding)

Aggregations

QueryParameterBinding (org.hibernate.query.spi.QueryParameterBinding)22 HashMap (java.util.HashMap)4 Map (java.util.Map)4 TreeMap (java.util.TreeMap)4 QueryParameterException (org.hibernate.QueryParameterException)3 QueryParameter (org.hibernate.query.QueryParameter)3 SerializableType (org.hibernate.type.SerializableType)3 Type (org.hibernate.type.Type)3 QueryParameterListBinding (org.hibernate.query.spi.QueryParameterListBinding)2 Collection (java.util.Collection)1 QueryException (org.hibernate.QueryException)1 Dialect (org.hibernate.dialect.Dialect)1 NamedParameterDescriptor (org.hibernate.engine.query.spi.NamedParameterDescriptor)1 OrdinalParameterDescriptor (org.hibernate.engine.query.spi.OrdinalParameterDescriptor)1 TypedValue (org.hibernate.engine.spi.TypedValue)1