Skip to content

Commit

Permalink
GRAILS-6868 - improve the handling of named queries with names that s…
Browse files Browse the repository at this point in the history
…tart with a lower case letter followed by an upper case letter
  • Loading branch information
Jeff Brown committed Oct 22, 2010
1 parent 8c260b6 commit 19d3808
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
11 changes: 9 additions & 2 deletions src/java/grails/util/GrailsNameUtils.java
Expand Up @@ -43,8 +43,15 @@ public static String getSetterName(String propertyName) {
* @return The name for the getter method for this property, if it were to exist, i.e. getConstraints
*/
public static String getGetterName(String propertyName) {
return "get" + Character.toUpperCase(propertyName.charAt(0))
+ propertyName.substring(1);
final String suffix;
if(propertyName.length() > 1 &&
Character.isLowerCase(propertyName.charAt(0)) &&
Character.isUpperCase(propertyName.charAt(1))) {
suffix = propertyName;
} else {
suffix = Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
}
return "get" + suffix;
}

/**
Expand Down
Expand Up @@ -19,6 +19,7 @@ import java.lang.reflect.Modifier
import org.codehaus.groovy.grails.orm.hibernate.metaclass.*
import org.codehaus.groovy.grails.plugins.orm.hibernate.HibernatePluginSupport
import org.hibernate.criterion.CriteriaSpecification
import grails.util.GrailsNameUtils

/**
* A builder that implements the ORM named queries DSL.
Expand Down Expand Up @@ -61,16 +62,16 @@ class HibernateNamedQueriesBuilder {
}

private handleMethodMissing = {String name, args ->
def propertyName = name[0].toUpperCase() + name[1..-1]

def classesToAugment = [domainClass]

def subClasses = domainClass.subClasses
if(subClasses) {
classesToAugment += subClasses
}

def getterName = GrailsNameUtils.getGetterName(name)
classesToAugment.each { clz ->
clz.metaClass.static."get${propertyName}" = {->
clz.metaClass.static."${getterName}" = {->
// creating a new proxy each time because the proxy class has
// some state that cannot be shared across requests (namedCriteriaParams)
new NamedCriteriaProxy(criteriaClosure: args[0], domainClass: clz, dynamicMethods: dynamicMethods)
Expand Down
4 changes: 4 additions & 0 deletions src/test/grails/util/GrailsNameUtilsTests.java
Expand Up @@ -21,6 +21,10 @@
*/
public class GrailsNameUtilsTests extends TestCase {

public void testGetGetterNameForPropertyThatBeginsWithASingleLowerCaseLetter() {
assertEquals("getaPaperback", GrailsNameUtils.getGetterName("aPaperback"));
}

public void testGetClassNameRepresentation() {
assertEquals("MyClass", GrailsNameUtils.getClassNameRepresentation("my-class"));
assertEquals("MyClass", GrailsNameUtils.getClassNameRepresentation("MyClass"));
Expand Down
Expand Up @@ -63,6 +63,10 @@ class Publication {
Boolean paperback = true
static namedQueries = {
aPaperback {
eq 'paperback', true
}
lastPublishedBefore { date ->
uniqueResult = true
le 'datePublished', date
Expand Down Expand Up @@ -514,6 +518,23 @@ class Publication {
}
assertEquals 0, results?.size()
}

void testPropertyCapitalization() {
def publicationClass = ga.getDomainClass("Publication").clazz

def now = new Date()
[true, false].each { isPaperback ->
3.times {
assert publicationClass.newInstance(title: "Some Book",
datePublished: now - 10, paperback: isPaperback).save()
}
}
session.clear()

def results = publicationClass.aPaperback.list()

assertEquals 3, results.size()
}

void testChainingNamedQueries() {
def publicationClass = ga.getDomainClass("Publication").clazz
Expand Down

0 comments on commit 19d3808

Please sign in to comment.