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;
}
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;
}
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;
}
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);
}
Aggregations