Search in sources :

Example 1 with DynamicFilterParameterSpecification

use of org.hibernate.param.DynamicFilterParameterSpecification 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(sqlFragment)) && (!(hasCollectionFilterParam(sqlFragment)))) {
        return;
    }
    Dialect dialect = walker.getSessionFactoryHelper().getFactory().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 = StringHelper.join(",", ArrayHelper.fillArray("?", type.getColumnSpan(walker.getSessionFactoryHelper().getFactory())));
            final String bindFragment;
            if (value != null && Collection.class.isInstance(value)) {
                bindFragment = StringHelper.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)

Aggregations

Collection (java.util.Collection)1 StringTokenizer (java.util.StringTokenizer)1 Dialect (org.hibernate.dialect.Dialect)1 FilterImpl (org.hibernate.internal.FilterImpl)1 DynamicFilterParameterSpecification (org.hibernate.param.DynamicFilterParameterSpecification)1 JoinType (org.hibernate.sql.JoinType)1 Type (org.hibernate.type.Type)1