use of org.hibernate.internal.FilterImpl in project hibernate-orm by hibernate.
the class QueryParameters method processFilters.
@SuppressWarnings({ "unchecked" })
public void processFilters(String sql, Map filters, SessionFactoryImplementor factory) {
if (filters.size() == 0 || !sql.contains(ParserHelper.HQL_VARIABLE_PREFIX)) {
// HELLA IMPORTANT OPTIMIZATION!!!
processedPositionalParameterValues = getPositionalParameterValues();
processedPositionalParameterTypes = getPositionalParameterTypes();
processedSQL = sql;
} else {
final StringTokenizer tokens = new StringTokenizer(sql, SYMBOLS, true);
StringBuilder result = new StringBuilder();
List parameters = new ArrayList();
List parameterTypes = new ArrayList();
int positionalIndex = 0;
while (tokens.hasMoreTokens()) {
final String token = tokens.nextToken();
if (token.startsWith(ParserHelper.HQL_VARIABLE_PREFIX)) {
final String filterParameterName = token.substring(1);
final String[] parts = LoadQueryInfluencers.parseFilterParameterName(filterParameterName);
final FilterImpl filter = (FilterImpl) filters.get(parts[0]);
final Object value = filter.getParameter(parts[1]);
final Type type = filter.getFilterDefinition().getParameterType(parts[1]);
if (value != null && Collection.class.isAssignableFrom(value.getClass())) {
Iterator itr = ((Collection) value).iterator();
while (itr.hasNext()) {
final Object elementValue = itr.next();
result.append('?');
parameters.add(elementValue);
parameterTypes.add(type);
if (itr.hasNext()) {
result.append(", ");
}
}
} else {
result.append('?');
parameters.add(value);
parameterTypes.add(type);
}
} else {
result.append(token);
if ("?".equals(token) && positionalIndex < getPositionalParameterValues().length) {
final Type type = getPositionalParameterTypes()[positionalIndex];
if (type.isComponentType()) {
// should process tokens till reaching the number of "?" corresponding to the
// numberOfParametersCoveredBy of the compositeType
int paramIndex = 1;
final int numberOfParametersCoveredBy = getNumberOfParametersCoveredBy(((ComponentType) type).getSubtypes());
while (paramIndex < numberOfParametersCoveredBy) {
final String nextToken = tokens.nextToken();
if ("?".equals(nextToken)) {
paramIndex++;
}
result.append(nextToken);
}
}
parameters.add(getPositionalParameterValues()[positionalIndex]);
parameterTypes.add(type);
positionalIndex++;
}
}
}
processedPositionalParameterValues = parameters.toArray();
processedPositionalParameterTypes = (Type[]) parameterTypes.toArray(new Type[parameterTypes.size()]);
processedSQL = result.toString();
}
}
use of org.hibernate.internal.FilterImpl in project hibernate-orm by hibernate.
the class LoadQueryInfluencers method enableFilter.
public Filter enableFilter(String filterName) {
FilterImpl filter = new FilterImpl(sessionFactory.getFilterDefinition(filterName));
enabledFilters.put(filterName, filter);
return filter;
}
use of org.hibernate.internal.FilterImpl in project hibernate-orm by hibernate.
the class JoinProcessor method processDynamicFilterParameters.
public static void processDynamicFilterParameters(final String sqlFragment, final ParameterContainer container, final HqlSqlWalker walker) {
if (walker.getEnabledFilters().isEmpty() && (!hasDynamicFilterParam(walker, sqlFragment)) && (!(hasCollectionFilterParam(sqlFragment)))) {
return;
}
Dialect dialect = walker.getDialect();
String symbols = ParserHelper.HQL_SEPARATORS + dialect.openQuote() + dialect.closeQuote();
StringTokenizer tokens = new StringTokenizer(sqlFragment, symbols, true);
StringBuilder result = new StringBuilder();
while (tokens.hasMoreTokens()) {
final String token = tokens.nextToken();
if (token.startsWith(ParserHelper.HQL_VARIABLE_PREFIX)) {
final String filterParameterName = token.substring(1);
final String[] parts = LoadQueryInfluencers.parseFilterParameterName(filterParameterName);
final FilterImpl filter = (FilterImpl) walker.getEnabledFilters().get(parts[0]);
final Object value = filter.getParameter(parts[1]);
final Type type = filter.getFilterDefinition().getParameterType(parts[1]);
final String typeBindFragment = String.join(",", ArrayHelper.fillArray("?", type.getColumnSpan(walker.getSessionFactoryHelper().getFactory())));
final String bindFragment;
if (value != null && Collection.class.isInstance(value)) {
bindFragment = String.join(",", ArrayHelper.fillArray(typeBindFragment, ((Collection) value).size()));
} else {
bindFragment = typeBindFragment;
}
result.append(bindFragment);
container.addEmbeddedParameter(new DynamicFilterParameterSpecification(parts[0], parts[1], type));
} else {
result.append(token);
}
}
container.setText(result.toString());
}
use of org.hibernate.internal.FilterImpl in project hibernate-orm by hibernate.
the class LoadQueryInfluencers method getFilterParameterValue.
public Object getFilterParameterValue(String filterParameterName) {
final String[] parsed = parseFilterParameterName(filterParameterName);
final FilterImpl filter = (FilterImpl) enabledFilters.get(parsed[0]);
if (filter == null) {
throw new IllegalArgumentException("Filter [" + parsed[0] + "] currently not enabled");
}
return filter.getParameter(parsed[1]);
}
use of org.hibernate.internal.FilterImpl in project candlepin by candlepin.
the class PoolCurator method enableIfPrevEnabled.
/**
* @param session
* @param consumerFilter
*/
private void enableIfPrevEnabled(Filter consumerFilter) {
// if filter was previously enabled, restore it.
if (consumerFilter != null) {
FilterImpl filterImpl = (FilterImpl) consumerFilter;
Filter filter = currentSession().enableFilter(CONSUMER_FILTER);
filter.setParameter("consumer_id", filterImpl.getParameter("consumer_id"));
}
}
Aggregations