Search in sources :

Example 1 with FilterPredicate

use of org.hibernate.sql.ast.tree.predicate.FilterPredicate in project hibernate-orm by hibernate.

the class FilterHelper method generateFilterPredicate.

private FilterPredicate generateFilterPredicate(FilterAliasGenerator aliasGenerator, Map<String, Filter> enabledFilters) {
    final FilterPredicate filterPredicate = new FilterPredicate();
    for (int i = 0, max = filterNames.length; i < max; i++) {
        final String filterName = filterNames[i];
        final FilterImpl enabledFilter = (FilterImpl) enabledFilters.get(filterName);
        if (enabledFilter != null) {
            String condition = render(aliasGenerator, i);
            final List<String> filterParameterNames = parameterNames[i];
            if (CollectionHelper.isNotEmpty(filterParameterNames)) {
                for (int paramPos = 0; paramPos < filterParameterNames.size(); paramPos++) {
                    final String parameterName = filterParameterNames.get(paramPos);
                    final JdbcMapping jdbcMapping = enabledFilter.getFilterDefinition().getParameterJdbcMapping(parameterName);
                    final Object parameterValue = enabledFilter.getParameter(parameterName);
                    if (parameterValue == null) {
                        throw new MappingException(String.format("unknown parameter [%s] for filter [%s]", parameterName, filterName));
                    }
                    final StringBuilder paramMarkers = new StringBuilder("?");
                    if (parameterValue instanceof Iterable && !jdbcMapping.getJavaTypeDescriptor().isInstance(parameterValue)) {
                        final Iterator<?> iterator = ((Iterable<?>) parameterValue).iterator();
                        if (iterator.hasNext()) {
                            final Object value = iterator.next();
                            final FilterJdbcParameter jdbcParameter = new FilterJdbcParameter(jdbcMapping, value);
                            filterPredicate.applyParameter(jdbcParameter);
                            while (iterator.hasNext()) {
                                paramMarkers.append(",?");
                                filterPredicate.applyParameter(new FilterJdbcParameter(jdbcMapping, iterator.next()));
                            }
                        } else {
                            // We need a dummy value if the list is empty
                            filterPredicate.applyParameter(new FilterJdbcParameter(jdbcMapping, null));
                        }
                    } else {
                        filterPredicate.applyParameter(new FilterJdbcParameter(jdbcMapping, parameterValue));
                    }
                    final String marker = ":" + filterNames[i] + "." + parameterName;
                    condition = condition.replaceAll(marker, paramMarkers.toString());
                }
            }
            filterPredicate.applyFragment(condition);
        }
    }
    if (filterPredicate.isEmpty()) {
        return null;
    }
    return filterPredicate;
}
Also used : JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) FilterPredicate(org.hibernate.sql.ast.tree.predicate.FilterPredicate) MappingException(org.hibernate.MappingException)

Aggregations

MappingException (org.hibernate.MappingException)1 JdbcMapping (org.hibernate.metamodel.mapping.JdbcMapping)1 FilterPredicate (org.hibernate.sql.ast.tree.predicate.FilterPredicate)1