Search in sources :

Example 21 with TypedValue

use of org.hibernate.engine.spi.TypedValue in project hibernate-orm by hibernate.

the class Loader method bindNamedParameters.

/**
 * Bind named parameters to the JDBC prepared statement.
 * <p/>
 * This is a generic implementation, the problem being that in the
 * general case we do not know enough information about the named
 * parameters to perform this in a complete manner here.  Thus this
 * is generally overridden on subclasses allowing named parameters to
 * apply the specific behavior.  The most usual limitation here is that
 * we need to assume the type span is always one...
 *
 * @param statement The JDBC prepared statement
 * @param namedParams A map of parameter names to values
 * @param startIndex The position from which to start binding parameter values.
 * @param session The originating session.
 *
 * @return The number of JDBC bind positions actually bound during this method execution.
 *
 * @throws SQLException Indicates problems performing the binding.
 * @throws org.hibernate.HibernateException Indicates problems delegating binding to the types.
 */
protected int bindNamedParameters(final PreparedStatement statement, final Map<String, TypedValue> namedParams, final int startIndex, final SharedSessionContractImplementor session) throws SQLException, HibernateException {
    int result = 0;
    if (CollectionHelper.isEmpty(namedParams)) {
        return result;
    }
    for (String name : namedParams.keySet()) {
        TypedValue typedValue = namedParams.get(name);
        int columnSpan = typedValue.getType().getColumnSpan(getFactory());
        int[] locs = getNamedParameterLocs(name);
        for (int loc : locs) {
            if (DEBUG_ENABLED) {
                LOG.debugf("bindNamedParameters() %s -> %s [%s]", typedValue.getValue(), name, loc + startIndex);
            }
            int start = loc * columnSpan + startIndex;
            typedValue.getType().nullSafeSet(statement, typedValue.getValue(), start, session);
        }
        result += locs.length;
    }
    return result;
}
Also used : TypedValue(org.hibernate.engine.spi.TypedValue)

Example 22 with TypedValue

use of org.hibernate.engine.spi.TypedValue in project hibernate-orm by hibernate.

the class QueryParameterBindingsImpl method collectNamedParameterBindings.

/**
 * @deprecated (since 5.2) expect a different approach to org.hibernate.engine.spi.QueryParameters in 6.0
 */
@Deprecated
public Map<String, TypedValue> collectNamedParameterBindings() {
    final Map<String, TypedValue> collectedBindings = new HashMap<>();
    for (Map.Entry<QueryParameter, QueryParameterBinding> entry : parameterBindingMap.entrySet()) {
        final String key;
        if (entry.getKey().getPosition() != null) {
            key = Integer.toString(entry.getKey().getPosition());
        } else {
            key = entry.getKey().getName();
        }
        Type bindType = entry.getValue().getBindType();
        if (bindType == null) {
            log.debugf("Binding for parameter [%s] did not define type", key);
            bindType = SerializableType.INSTANCE;
        }
        collectedBindings.put(key, new TypedValue(bindType, entry.getValue().getBindValue()));
    }
    return collectedBindings;
}
Also used : QueryParameter(org.hibernate.query.QueryParameter) SerializableType(org.hibernate.type.SerializableType) Type(org.hibernate.type.Type) QueryParameterBinding(org.hibernate.query.spi.QueryParameterBinding) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) TypedValue(org.hibernate.engine.spi.TypedValue)

Example 23 with TypedValue

use of org.hibernate.engine.spi.TypedValue in project hibernate-orm by hibernate.

the class AbstractPersistentCollection method getOrphans.

/**
 * Given a collection of entity instances that used to
 * belong to the collection, and a collection of instances
 * that currently belong, return a collection of orphans
 */
@SuppressWarnings({ "JavaDoc", "unchecked" })
protected static Collection getOrphans(Collection oldElements, Collection currentElements, String entityName, SharedSessionContractImplementor session) throws HibernateException {
    // short-circuit(s)
    if (currentElements.size() == 0) {
        // no new elements, the old list contains only Orphans
        return oldElements;
    }
    if (oldElements.size() == 0) {
        // no old elements, so no Orphans neither
        return oldElements;
    }
    final EntityPersister entityPersister = session.getFactory().getEntityPersister(entityName);
    final Type idType = entityPersister.getIdentifierType();
    final boolean useIdDirect = mayUseIdDirect(idType);
    // create the collection holding the Orphans
    final Collection res = new ArrayList();
    // collect EntityIdentifier(s) of the *current* elements - add them into a HashSet for fast access
    final java.util.Set currentIds = new HashSet();
    final java.util.Set currentSaving = new IdentitySet();
    for (Object current : currentElements) {
        if (current != null && ForeignKeys.isNotTransient(entityName, current, null, session)) {
            final EntityEntry ee = session.getPersistenceContext().getEntry(current);
            if (ee != null && ee.getStatus() == Status.SAVING) {
                currentSaving.add(current);
            } else {
                final Serializable currentId = ForeignKeys.getEntityIdentifierIfNotUnsaved(entityName, current, session);
                currentIds.add(useIdDirect ? currentId : new TypedValue(idType, currentId));
            }
        }
    }
    // iterate over the *old* list
    for (Object old : oldElements) {
        if (!currentSaving.contains(old)) {
            final Serializable oldId = ForeignKeys.getEntityIdentifierIfNotUnsaved(entityName, old, session);
            if (!currentIds.contains(useIdDirect ? oldId : new TypedValue(idType, oldId))) {
                res.add(old);
            }
        }
    }
    return res;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Serializable(java.io.Serializable) IdentitySet(org.hibernate.internal.util.collections.IdentitySet) ArrayList(java.util.ArrayList) PostgresUUIDType(org.hibernate.type.PostgresUUIDType) UUIDBinaryType(org.hibernate.type.UUIDBinaryType) CompositeType(org.hibernate.type.CompositeType) IntegerType(org.hibernate.type.IntegerType) StringType(org.hibernate.type.StringType) LongType(org.hibernate.type.LongType) UUIDCharType(org.hibernate.type.UUIDCharType) Type(org.hibernate.type.Type) EntityEntry(org.hibernate.engine.spi.EntityEntry) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) Collection(java.util.Collection) MarkerObject(org.hibernate.internal.util.MarkerObject) HashSet(java.util.HashSet) TypedValue(org.hibernate.engine.spi.TypedValue)

Example 24 with TypedValue

use of org.hibernate.engine.spi.TypedValue in project hibernate-orm by hibernate.

the class QueryKey method generateQueryKey.

/**
 * Generates a QueryKey.
 *
 * @param queryString The sql query string.
 * @param queryParameters The query parameters
 * @param filterKeys The keys of any enabled filters.
 * @param session The current session.
 * @param customTransformer The result transformer; should be null if data is not transformed before being cached.
 *
 * @return The generate query cache key.
 */
public static QueryKey generateQueryKey(String queryString, QueryParameters queryParameters, Set filterKeys, SharedSessionContractImplementor session, CacheableResultTransformer customTransformer) {
    // disassemble positional parameters
    final int positionalParameterCount = queryParameters.getPositionalParameterTypes().length;
    final Type[] types = new Type[positionalParameterCount];
    final Object[] values = new Object[positionalParameterCount];
    for (int i = 0; i < positionalParameterCount; i++) {
        types[i] = queryParameters.getPositionalParameterTypes()[i];
        values[i] = types[i].disassemble(queryParameters.getPositionalParameterValues()[i], session, null);
    }
    // disassemble named parameters
    final Map<String, TypedValue> namedParameters;
    if (queryParameters.getNamedParameters() == null) {
        namedParameters = null;
    } else {
        namedParameters = CollectionHelper.mapOfSize(queryParameters.getNamedParameters().size());
        for (Map.Entry<String, TypedValue> namedParameterEntry : queryParameters.getNamedParameters().entrySet()) {
            namedParameters.put(namedParameterEntry.getKey(), new TypedValue(namedParameterEntry.getValue().getType(), namedParameterEntry.getValue().getType().disassemble(namedParameterEntry.getValue().getValue(), session, null)));
        }
    }
    // decode row selection...
    final RowSelection selection = queryParameters.getRowSelection();
    final Integer firstRow;
    final Integer maxRows;
    if (selection != null) {
        firstRow = selection.getFirstRow();
        maxRows = selection.getMaxRows();
    } else {
        firstRow = null;
        maxRows = null;
    }
    return new QueryKey(queryString, types, values, namedParameters, firstRow, maxRows, filterKeys, session.getTenantIdentifier(), customTransformer);
}
Also used : RowSelection(org.hibernate.engine.spi.RowSelection) Type(org.hibernate.type.Type) Map(java.util.Map) TypedValue(org.hibernate.engine.spi.TypedValue)

Aggregations

TypedValue (org.hibernate.engine.spi.TypedValue)24 Type (org.hibernate.type.Type)9 Map (java.util.Map)5 ArrayList (java.util.ArrayList)4 Iterator (java.util.Iterator)3 CompositeType (org.hibernate.type.CompositeType)3 QueryException (org.hibernate.QueryException)2 RowSelection (org.hibernate.engine.spi.RowSelection)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 JoinType (org.hibernate.sql.JoinType)2 AssociationType (org.hibernate.type.AssociationType)2 CollectionType (org.hibernate.type.CollectionType)2 StringRepresentableType (org.hibernate.type.StringRepresentableType)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Serializable (java.io.Serializable)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1