borinquenkid commented on code in PR #16816: URL: https://github.com/apache/grails-core/pull/16816#discussion_r4434479539
########## grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/FunctionCallingCriterionHandler.java: ########## @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.orm.hibernate.query; + +import java.util.ArrayList; +import java.util.Collection; + +import jakarta.persistence.criteria.Expression; +import jakarta.persistence.criteria.Predicate; + +import org.hibernate.query.criteria.HibernateCriteriaBuilder; + +import org.springframework.core.convert.ConversionService; + +import org.grails.datastore.mapping.core.exceptions.ConfigurationException; +import org.grails.datastore.mapping.query.Query; +import org.grails.datastore.mapping.query.criteria.FunctionCallingCriterion; + +/** + * Handles criteria that apply a SQL/HQL function to a property before comparison, + * such as {@code upper(name) == 'A'} or {@code year(birthDate) > 2000}. + * + * @since 8.0.1 + */ +public class FunctionCallingCriterionHandler implements InternalCriterionHandler<FunctionCallingCriterion> { + + @Override + @SuppressWarnings("unchecked") + public Predicate handle(CriterionContext context, FunctionCallingCriterion criterion) { + String functionName = criterion.getFunctionName(); + if (criterion.isOnValue()) { + throw new UnsupportedOperationException("A function on the value of a comparison is not supported: " + functionName); + } + Expression<?> propertyPath = context.resolvePath(criterion.getProperty()); + if (propertyPath == null) { + throw new ConfigurationException("Cannot resolve property [" + criterion.getProperty() + "] in a " + functionName + " function call"); + } + HibernateCriteriaBuilder criteriaBuilder = context.getCriteriaBuilder(); + Expression<?> left = applyFunction(criteriaBuilder, functionName, propertyPath); + Query.PropertyCriterion wrapped = criterion.getPropertyCriterion(); + Object value = convertFunctionValue(context.getConversionService(), functionName, wrapped.getValue()); Review Comment: Addressed in d0ff76be16: with is now detected before scalar conversion and builds an subquery predicate. ########## grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/FunctionCallingCriterionHandler.java: ########## @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.orm.hibernate.query; + +import java.util.ArrayList; +import java.util.Collection; + +import jakarta.persistence.criteria.Expression; +import jakarta.persistence.criteria.Predicate; + +import org.hibernate.query.criteria.HibernateCriteriaBuilder; + +import org.springframework.core.convert.ConversionService; + +import org.grails.datastore.mapping.core.exceptions.ConfigurationException; +import org.grails.datastore.mapping.query.Query; +import org.grails.datastore.mapping.query.criteria.FunctionCallingCriterion; + +/** + * Handles criteria that apply a SQL/HQL function to a property before comparison, + * such as {@code upper(name) == 'A'} or {@code year(birthDate) > 2000}. + * + * @since 8.0.1 + */ +public class FunctionCallingCriterionHandler implements InternalCriterionHandler<FunctionCallingCriterion> { + + @Override + @SuppressWarnings("unchecked") + public Predicate handle(CriterionContext context, FunctionCallingCriterion criterion) { + String functionName = criterion.getFunctionName(); + if (criterion.isOnValue()) { + throw new UnsupportedOperationException("A function on the value of a comparison is not supported: " + functionName); + } + Expression<?> propertyPath = context.resolvePath(criterion.getProperty()); + if (propertyPath == null) { + throw new ConfigurationException("Cannot resolve property [" + criterion.getProperty() + "] in a " + functionName + " function call"); + } + HibernateCriteriaBuilder criteriaBuilder = context.getCriteriaBuilder(); + Expression<?> left = applyFunction(criteriaBuilder, functionName, propertyPath); + Query.PropertyCriterion wrapped = criterion.getPropertyCriterion(); + Object value = convertFunctionValue(context.getConversionService(), functionName, wrapped.getValue()); + + if (wrapped instanceof Query.ILike) { + return criteriaBuilder.ilike((Expression<String>) left, (String) value); + } else if (wrapped instanceof Query.RLike rLike) { + return PropertyCriterionHandler.handleRLike(criteriaBuilder, (Expression<String>) left, rLike); + } else if (wrapped instanceof Query.Like) { + return criteriaBuilder.like((Expression<String>) left, (String) value); + } else if (wrapped instanceof Query.Equals) { + return criteriaBuilder.equal(left, value); + } else if (wrapped instanceof Query.NotEquals) { + return criteriaBuilder.notEqual(left, value); Review Comment: Addressed in d0ff76be16: and now handle ( / ), subqueries, and null-inclusive semantics for non-null . -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
