Search in sources :

Example 1 with FilterImpl

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();
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) ComponentType(org.hibernate.type.ComponentType) Type(org.hibernate.type.Type) FilterImpl(org.hibernate.internal.FilterImpl) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with FilterImpl

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;
}
Also used : FilterImpl(org.hibernate.internal.FilterImpl)

Example 3 with FilterImpl

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());
}
Also used : StringTokenizer(java.util.StringTokenizer) JoinType(org.hibernate.sql.JoinType) Type(org.hibernate.type.Type) FilterImpl(org.hibernate.internal.FilterImpl) Dialect(org.hibernate.dialect.Dialect) Collection(java.util.Collection) DynamicFilterParameterSpecification(org.hibernate.param.DynamicFilterParameterSpecification)

Example 4 with FilterImpl

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]);
}
Also used : FilterImpl(org.hibernate.internal.FilterImpl)

Example 5 with FilterImpl

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"));
    }
}
Also used : FilterImpl(org.hibernate.internal.FilterImpl) Filter(org.hibernate.Filter)

Aggregations

FilterImpl (org.hibernate.internal.FilterImpl)5 Collection (java.util.Collection)2 StringTokenizer (java.util.StringTokenizer)2 Type (org.hibernate.type.Type)2 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Filter (org.hibernate.Filter)1 Dialect (org.hibernate.dialect.Dialect)1 DynamicFilterParameterSpecification (org.hibernate.param.DynamicFilterParameterSpecification)1 JoinType (org.hibernate.sql.JoinType)1 ComponentType (org.hibernate.type.ComponentType)1