Permalink
Browse files

GRAILS-5854 adds support for case-insensitive eq in criteria builder

  • Loading branch information...
1 parent 8fcebbf commit d3cbbc74ee7f5a609bc607c949df23ed6ebde798 @burtbeckwith burtbeckwith committed Feb 9, 2010
@@ -30,8 +30,11 @@ Refer to the user guide section on [Criteria|guide:criteria] for usage instructi
*between* | Where the property value is between to distinct values | {code}
between("balance", 500, 1000)
{code}
- *eq* | Where a property equals a particular value | {code}
+ *eq* | Where a property equals a particular value.| {code}
eq("branch", "London")
+{code}
+ *eq* (case-insensitive) | A version of *eq* that supports an optional 3rd Map parameter to specify that the query be case-insensitive. | {code}
+eq("branch", "london", [ignoreCase: true])
{code}
*eqProperty* | Where one property must equal another | {code}
eqProperty("lastTransaction","firstTransaction")
@@ -654,22 +654,55 @@ public Object le(String propertyName, Object propertyValue) {
return addToCriteria(Restrictions.le(propertyName, propertyValue));
}
/**
- * Creates an "equals" Criterion based on the specified property name and value
+ * Creates an "equals" Criterion based on the specified property name and value. Case-sensitive.
* @param propertyName The property name
* @param propertyValue The property value
*
* @return A Criterion instance
*/
public Object eq(String propertyName, Object propertyValue) {
- if(!validateSimpleExpression()) {
- throwRuntimeException( new IllegalArgumentException("Call to [eq] with propertyName ["+propertyName+"] and value ["+propertyValue+"] not allowed here."));
- }
- propertyName = calculatePropertyName(propertyName);
- propertyValue = calculatePropertyValue(propertyValue);
- return addToCriteria(Restrictions.eq(propertyName, propertyValue));
+ return eq(propertyName, propertyValue, Collections.emptyMap());
}
/**
+ * Groovy moves the map to the first parameter if using the idiomatic form, e.g.
+ * <code>eq 'firstName', 'Fred', ignoreCase: true</code>.
+ * @param params optional map with customization parameters; currently only 'ignoreCase' is supported.
+ * @param propertyName
+ * @param propertyValue
+ * @return A Criterion instance
+ */
+ public Object eq(Map params, String propertyName, Object propertyValue) {
+ return eq(propertyName, propertyValue, params);
+ }
+
+ /**
+ * Creates an "equals" Criterion based on the specified property name and value.
+ * Supports case-insensitive search if the <code>params</code> map contains <code>true</code>
+ * under the 'ignoreCase' key.
+ * @param propertyName The property name
+ * @param propertyValue The property value
+ * @param params optional map with customization parameters; currently only 'ignoreCase' is supported.
+ *
+ * @return A Criterion instance
+ */
+ public Object eq(String propertyName, Object propertyValue, Map params) {
+ if (!validateSimpleExpression()) {
+ throwRuntimeException( new IllegalArgumentException("Call to [eq] with propertyName ["+propertyName+"] and value ["+propertyValue+"] not allowed here."));
+ }
+ propertyName = calculatePropertyName(propertyName);
+ propertyValue = calculatePropertyValue(propertyValue);
+ SimpleExpression eq = Restrictions.eq(propertyName, propertyValue);
+ if (params != null) {
+ Object ignoreCase = params.get("ignoreCase");
+ if (ignoreCase instanceof Boolean && (Boolean)ignoreCase) {
+ eq = eq.ignoreCase();
+ }
+ }
+ return addToCriteria(eq);
+ }
+
+ /**
* Applies a sql restriction to the results to allow something like:
<pre>
def results = Person.withCriteria {
@@ -7,10 +7,9 @@ import org.codehaus.groovy.grails.commons.DomainClassArtefactHandler
/**
* @author Graeme Rocher
* @since 1.0
- *
+ *
* Created: Jan 14, 2009
*/
-
public class HibernateCriteriaBuilderTests extends AbstractGrailsHibernateTests{
protected void onSetUp() {
@@ -1071,8 +1070,8 @@ class CriteriaBuilderTestClass2 {
assertEquals(1, results.size());
}
- public void testEq() throws Exception {
- GrailsDomainClass domainClass = (GrailsDomainClass) this.grailsApplication.getArtefact(DomainClassArtefactHandler.TYPE,
+ void testEq() throws Exception {
+ GrailsDomainClass domainClass = grailsApplication.getArtefact(DomainClassArtefactHandler.TYPE,
"CriteriaBuilderTestClass");
assertNotNull(domainClass);
@@ -1091,14 +1090,59 @@ class CriteriaBuilderTestClass2 {
obj2.invokeMethod("save", null);
-
List results = (List)parse( "{ " +
"eq('firstName','fred');" +
"}", "Test1","CriteriaBuilderTestClass");
assertEquals(1, results.size());
}
- public void testNe() throws Exception {
+ void testEqCaseInsensitive() throws Exception {
+ GrailsDomainClass domainClass = grailsApplication.getArtefact(DomainClassArtefactHandler.TYPE,
+ "CriteriaBuilderTestClass");
+
+ assertNotNull(domainClass);
+
+ GroovyObject obj = (GroovyObject)domainClass.newInstance();
+ obj.setProperty( "firstName", "fred" );
+ obj.setProperty( "lastName", "flinstone" );
+ obj.setProperty( "age", new Integer(43));
+
+ obj.invokeMethod("save", null);
+
+ GroovyObject obj2 = (GroovyObject)domainClass.newInstance();
+ obj2.setProperty( "firstName", "zulu" );
+ obj2.setProperty( "lastName", "alpha" );
+ obj2.setProperty( "age", new Integer(45));
+
+ obj2.invokeMethod("save", null);
+
+ List results = parse( "{ " +
+ "eq('firstName','Fred');" +
+ "}", "Test1","CriteriaBuilderTestClass");
+ assertEquals 'default not ignoring case', 0, results.size()
+
+ results = parse( "{ " +
+ "eq 'firstName','Fred', ignoreCase: false" +
+ "}", "Test1","CriteriaBuilderTestClass");
+ assertEquals 'explicitly not ignoring case', 0, results.size()
+
+ results = parse( "{ " +
+ "eq 'firstName', 'Fred', ignoreCase: true" +
+ "}", "Test1","CriteriaBuilderTestClass");
+ assertEquals 'ignoring case should match one', 1, results.size()
+
+ results = parse( "{ " +
+ "eq('firstName', 'Fred', [ignoreCase: true])" +
+ "}", "Test1","CriteriaBuilderTestClass");
+ assertEquals 'ignoring case should match one', 1, results.size()
+
+ results = parse( "{ " +
+ "eq 'firstName', 'Fred', dontKnowWhatToDoWithThis: 'foo'" +
+ "}", "Test1","CriteriaBuilderTestClass");
+ assertEquals 'an unknown parameter should be ignored', 0, results.size()
+ }
+
+ public void testNe() throws Exception {
GrailsDomainClass domainClass = (GrailsDomainClass) this.grailsApplication.getArtefact(DomainClassArtefactHandler.TYPE,
"CriteriaBuilderTestClass");

0 comments on commit d3cbbc7

Please sign in to comment.