GORM Where query composition on return value - grails

I noticed some behaviour I don't quite understand. It's possible to chain where queries as long as the preceding where query wasn't returned from a function.
def w = Subscription.where { topicId == 1 }
w.where { user.id == 1 }.findAll()
//works as expected
def f() {
Subscription.where { topicId == 1 }
}
f().where { user.id == 1 }.findAll()
//doesn't filter by user.id
def f() {
Subscription.where { topicId == 1 }
}
f().build { eq 'user.id', 1L }
//works
I don't mind using DetachedCriteria#build(). I'd just like to understand :-D
--- Edit
Grails 2.4.2

Grails contains a global AST transformation...
that transforms where methods into detached criteria queries.
It essentially looks for uses of where() and changes the code so that it uses detached criterias. Most likely, calling where() as you are, on the return of a method, particularly a method which returns Object, is not being acknowledged up by the AST, so the query basically ends up being unchanged and doing nothing.
It's nothing that Groovy is doing, but rather something Grails-specific. I do not claim a full, nor anything near full, understanding of what's happening, but you can get the gist of it by looking at the DetachedCriteriaTransformer source code.

Related

Groovy removeAll closure not removing objects in Set

I'm using data binding with parent/child relationships in Grails 2.3.7 and am having trouble with deletes. The form has many optional children, and to keep the database tidy I'd like to purge blank (null) values. I've found some nice articles which suggest using removeAll to filter my entries but I can't get remove or removeAll to work!
For example... (Parent has 10 children, 5 are blank)
def update(Parent parent) {
parent.children.getClass() // returns org.hibernate.collection.PersistentSet
parent.children.size() // returns 10
parent.children.findAll{ it.value == null }.size() // returns 5
parent.children.removeAll{ it.value == null } // returns TRUE
parent.children.size() // Still returns 10!!!
}
I've read PersistentSet is finicky about equals() and hashCode() being implemented manually, which I've done in every domain class. What baffles me is how removeAll can return true, indicating the Collection has changed, yet it hasn't. I've been stuck on this for a couple days now so any tips would be appreciated. Thanks.
Update:
I've been experimenting with the Child hashcode and that seems to be the culprit. If I make a bare-bones hashcode based on the id (bad practice) then removeAll works, but if I include the value it stops working again. For example...
// Sample 1: Works with removeAll
int hashCode() {
int hash1 = id.hashCode()
return hash1
}
// Sample 2: Doesn't work with removeAll
int hashCode() {
int hash1 = id.hashCode()
int hash2 = value == null ? 0 : value.hashCode()
return hash1 + hash2
}
// Sample Domain classes (thanks Burt)
class Parent {
static hasMany = [children: Child]
}
class Child {
String name
String value
static constraints = {
value nullable: true
}
}
This behavior is explained by the data binding step updating data, making it dirty. (ie: child.value.isDirty() == true) Here's how I understand it.
First Grails data binding fetches the Parent and children, and the hashcode of each Child is calculated. Next, data updates are applied which makes child.value dirty (if it changed) but the Set's hashcodes remain unchanged. When removeAll finds a match it builds a hashCode with the dirty data, but that hashcode is NOT found in the Set so it can't remove it. Essentially removeAll will only work if ALL of my hashCode variables are clean.
So if the data must be clean to remove it, one solution is to save it twice. Like this...
// Parent Controller
def update(Parent parent) {
parent.children.removeAll{ it.value == null } // Removes CLEAN children with no value
parent.save(flush:true)
parent.refresh() // parent.children is now clean
parent.children.removeAll{ it.value == null } // Removes (formerly dirty) children
parent.save(flush:true) // Success!
}
This works though it's not ideal. First I must allow null values in the database, though they only exist briefly and I don't want them. And second it's kinda inefficient to do two saves. Surely there must be a better way?
hashCode and equals weirdness aren't an issue here - there are no contains calls or something similar that would use the hashCode value and potentially miss the actual data. If you look at the implementation of removeAll you can see that it uses an Iterator to call your closure on every instance and remove any where the closure result is truthy, and return true if at least one was removed. Using this Parent class
class Parent {
static hasMany = [children: Child]
}
and this Child
class Child {
String name
String value
static constraints = {
value nullable: true
}
}
and this code to create test instances:
def parent = new Parent()
5.times {
parent.addToChildren(name: 'c' + it)
}
5.times {
parent.addToChildren(name: 'c2' + it, value: 'asd')
}
parent.save()
it prints 5 for the final size(). So there's probably something else affecting this. You shouldn't have to, but you can create your own removeAll that does the same thing, and if you throw in some println calls you might figure out what's up:
boolean removeAll(collection, Closure remove) {
boolean atLeastOne = false
Iterator iter = collection.iterator()
while (iter.hasNext()) {
def c = iter.next()
if (remove(c)) {
iter.remove()
atLeastOne = true
}
}
atLeastOne
}
Invoke this as
println removeAll(parent.children) { it.value == null }

Grails: How to fetch a single item via named queries and additional criteria closure

I am using Grails 2.4.3 and having trouble with named queries.
For example I have this
class Product {
Customer customer
...
static namedQueries = {
byCustomer { Customer c ->
eq('customer', c)
}
}
...
}
Now I can do
Product.byCustomer(customer).list()
I event can do
Product.byCustomer(customer).list(pagination + sorting ) {
...
gt('price', 23.5)
}
If I want a single object from gorm with criterias on it, I usually do
Product.createCriteria().get {
....
eq('name', 'foo')
}
This will return the first matching Product with name == 'foo'
Now, what I want to do is this:
Product.byCustomer(customer).get {
...
eq('type', 'bar')
}
This gives me:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server
version for the right syntax to use near 'from product this_)' at line 1. Stacktrace follows:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax;
check the manual that corresponds to your MariaDB server version for the right syntax to use near
'from product this_)' at line 1
I also tried this:
Product.byCustomer(customer) {
...
eq('type', 'bar')
}.get()
which does also not work, since byCustomer(...) returns a collection.
What do I miss here ? I am really wondering cause all other methods seem to work, except .get() :
Product.byCustomer(customer).count {
...
eq('type', 'bar')
}
(this is working)
I really appreciate any help. Thanks!
UPDATE:
As pointed out there are several options are given if you just want to query a fixed set of properties:
With where clause:
Product.byCustomer(customer).findWhere([type: 'bar'])
With another named query for "type"
Product.byCustomer(customer).byType('bar').get()
Problem: I have a dynamic set of criterias I want to add - and cannot create a named queries for all properties in my domain class.
UPDATE 2:
Example how I want to dynamically build by criteria, based on conditions:
Product p = Product.byCustomer(customer).get() {
if (condition) {
eq('a', params.int('bar'))
gt('b', params.int('foo'))
items {
eq('c', 'baz')
}
} else {
...
}
}
ANSWER
I think I found the way this will work for me. As I use Grails > 2.0 I can use
where-queries which will return DetachedCriteria
DetachedCriteria has the missing get() method and I even can mix findWhere - like syntax with builder syntax - check this out:
Product.where { foo == '5' && bar > 8 }.get()
Or
Product.where { foo == '5' && bar > 8 }.build {
items {
eq('baz', 5)
}
}.get()
In both closures (where + build) I can dynamically add conditions.
Now the final part - reuse the namedQueries which already exists in the above Query
Product.where { foo == '5' && bar > 8 }.build {
items {
eq('baz', 5)
}
Product.byCustomer(customer)
}.get()
Sweet! Unfortunately I cannot answer my own question yet :)
UPDATE
The last part is NOT working. How can I mix DetachedCriteria and named queries ?
Since the result of a named query with a closure argument is a java.util.ArrayList you should be able to do
Product.byCustomer(customer){
...
eq('type', 'bar')
}.getAt(0)
If the result is an empty list it will return null, otherwise it will return the first entry (in your case the only one). Tried with Grails 2.4.3.

What is the difference between withCriteria and CreateCriteria in Grails?

What is their difference and why and where we need to use them,i think it seems like they have no difference at all to me ?
withCriteria { ... } is essentially shorthand for createCriteria().list { ... }. If you need to use any of the other criteria methods (get, count, ...) or pass pagination parameters to list then you have to use the long-hand form.
SomeDomain.createCriteria().list(max:10, offset:50) {
// ...
}
It's worth adding what I just came across in the grails documentation for createCriteria().
Because that query includes pagination parameters (max and offset), this will return a PagedResultList which has a getTotalCount() method to return the total number of matching records for pagination. Two queries are still run, but they are run for you and the results and total count are combined in the PagedResultList.
Source
This means you can use getTotalCount() without having to initiate the call (it's made for you). This is very helpful. The example documentation shows:
def c = Account.createCriteria()
def results = c.list (max: 10, offset: 10) {
like("holderFirstName", "Fred%")
and {
between("balance", 500, 1000)
eq("branch", "London")
}
order("holderLastName", "desc")
}
println "Rendering ${results.size()} Accounts of ${results.totalCount}"
This capability is not available when using withCriteria().
Example of createCriteria():
def criteria = OfferCredit.createCriteria {
offer {
eq('status', LeverageUtils.ACTIVE_STATUS)
ge('expirationDate', new Date())
}
user {
eq('userId', userId)
}
eq('status', LeverageUtils.ACTIVE_STATUS)
order('creationDate', 'asc')
}
criteria.list()
Example of withCriteria():
List<Supermarket> results = Supermarket.withCriteria {
like("sp_street", params.street)
productSupermarket {
product {
idEq(params.product)
}
// or just eq('product', someProduct)
}
maxResults(10)
}
withCriteria executes and returns the list. It provides a closure using which you can customize the criteria before it gets executed.
createCriteria just creates a criteria object which you can modify and then explicitly call the list method to execute.
If criteria is simple or if it is defined in a single place it is better to use withCriteria.
If you need to pass the criteria around (create it in one function and pass it to others) createCriteria would be better. I think withCriteria support is limited.
withCriteria ->
Purpose -> Allows inline execution of Criteria queries.
If no matching records are found, an empty List is returned.
If a projection is specified:
returns a single value if it only contains one field
a List in case there are multiple fields in the projection

list.find(closure) and executing against that value

Really my question is "Can the code sample below be even smaller? Basically the code sample is designed to first look through a list of objects, find the most granular (in this case it is branch) and then query backwards depending on what object it finds.
1 - If it finds a branch, return the findAllBy against the branch
2 - If it finds a department, return the findAllBy against the department
3 - If it finds an organization, return the findAllBy against the organization
The goal is to find the most granular object (which is why order is important), but do I need to have two separate blocks (one to define the objects, the other to check if they exist)? Or can those two executions be made into one command...
def resp
def srt = [sort:"name", order:"asc"]
def branch = listObjects.find{it instanceof Branch}
def department = listObjects.find{it instanceof Department}
def organization = listObjects.find{it instanceof Organization}
resp = !resp && branch ? Employees.findAllByBranch(branch,srt) : resp
resp = !resp && department ? Employees.findAllByDepartment(department,srt) : resp
resp = !resp && organization ? Employees.findAllByOrganization(organization,srt) : resp
return resp
What I'm thinking is something along the lines of this:
def resp
resp = Employees.findAllByBranch(listObjects.find{it instanceof Branch})
resp = !resp ? Employees.findAllByDepartment(listObjects.find{it instanceof Department}) : resp
resp = !resp ? Employees.findAllByOrganization(listObjects.find{it instanceof Organization}) : resp
But I believe that will throw an exception since those objects might be null
You can shorten it up a bit more with findResult instead of a for in loop with a variable you need to def outside:
def listObjects // = some predetermined list that you've apparently created
def srt = [sort:"name", order:"asc"]
def result = [Branch, Department, Organization].findResult { clazz ->
listObjects?.find { it.class.isAssignableFrom(clazz) }?.with { foundObj ->
Employees."findAllBy${clazz.name}"(foundObj, srt)
}
}
findResult is similar to find, but it returns the result from the first non-null item rather than the item itself. It avoids the need for a separate collection variable outside of the loop.
Edit: what I had previously didn't quite match the behavior that I think you were looking for (I don't think the other answers do either, but I could be misunderstanding). You have to ensure that there's something found in the list before doing the findAllBy or else you could pull back null items which is not what you're looking for.
In real, production code, I'd actually do things a bit differently though. I'd leverage the JVM type system to only have to spin through the listObjects once and short circuit when it found the first Branch/Department/Organization like this:
def listObjects
def sort = [sort:"name", order:"asc"]
def result = listObjects?.findResult { findEmployeesFor(it, sort) }
... // then have these methods to actually exercise the type specific findEmployeesFor
def findEmployeesFor(Branch branch, sort) { Employees.findAllByBranch(branch, sort) }
def findEmployeesFor(Department department, sort { Employees.findAllByDepartment(department, sort)}
def findEmployeesFor(Organization organization, sort { Employees.findAllByOrganization(organization, sort)}
def findEmployeesFor(Object obj, sort) { return null } // if listObjects can hold non/branch/department/organization objects
I think that this code is actually clearer and it reduces the number of times we iterate over the list and the number of reflection calls we need to make.
Edit:
A for in loop is more efficient, since you want to break processing on first non-null result (i.e. in Groovy we cannot break out of a closure iteration with "return" or "break").
def resp
for(clazz in [Branch,Department,Organization]) {
resp = Employees."findAllBy${clazz.name}"(listObjects?.find{it instanceof $clazz})
if(resp) return
}
if(resp) // do something...
Original:
List results = [Branch,Department,Organization].collect{clazz->
Employees."findAllBy${clazz.name}"(listObjects?.find{it instanceof $clazz})
}
Enjoy Groovy ;--)
I think #virtualeyes nearly had it, but instead of a collect (which as he says you can't break out of), you want to use a find, as that stops running the first valid result it gets:
List results = [Branch,Department,Organization].find { clazz->
Employees."findAllBy${clazz.name}"(listObjects?.find{it instanceof clazz})
}

Can I append a closure to another in Groovy?

I have two very similar methods in Grails, something like "calculate statistics by os" and "calculate statistics by browser" - effectively both prepare some things, then run a similar query on the DB, then do things with the results. The only part where the methods differ is the query they run in the middle of my method -
def summary = c.list {
eq('browser', Browser.get(1)) // OR eq('os', OS.get(1))
between('date', dates.start, dates.end)
}
It occurred to me that the ideal way to refactor it would be to pass in the first line of the closure as a method parameter. Like
doStats (Closure query) {
...
def summary = c.list {
query
between('date', dates.start, dates.end)
}
}
I tried this but "query" gets ignored. I tried query() instead but then the query clause is executed where defined, so this doesn't work either. I suppose I could just pass the whole closure as a parameter but that seems wrong - the query might also get more complicated in future.
Anyone have any better ideas?
I found leftShift operator useful for composing closure from two separate ones. What you can do is:
Closure a = { /*...*/ }
Closure b = { /*...*/ }
Closure c = a << b
Take a look at this example:
def criteria = {
projection Projections.distinct(Projections.property('id'))
and {
eq 'owner.id', userDetails.id
if (filter.groupId) {
eq 'group.id', filter.groupId
}
}
}
List<Long> ids = Contact.createCriteria().list(criteria << {
maxResults filter.max
firstResult filter.offset
})
Integer totalCount = Contact.createCriteria().count(criteria)
What you can see here is that I'm creating a criteria for listing ant counting GORM objects. Criterias for both cases are almost the same, but for listing purposes I also need to include limit and offset from command object.
You're using the criteria DSL which might be different than plain groovy closures.
To do what you're asking, you can use the method described here -
http://mrhaki.blogspot.com/2010/06/grails-goodness-refactoring-criteria.html
and put your query in to private method.
The more elegant solution for this is to use named queries in grails -
http://grails.org/doc/latest/ref/Domain%20Classes/namedQueries.html
Look at the
recentPublicationsWithBookInTitle {
// calls to other named queries…
recentPublications()
publicationsWithBookInTitle()
}
example -
Not sure about with the Grails Criteria builder, but with other builders, you can do something like:
doStats (Closure query) {
def summary = c.list {
query( it )
between('date', dates.start, dates.end)
}
}
And call this via:
def f = { criteria ->
criteria.eq( 'browser', Browser.get( 1 ) )
}
doStats( f )
If not, you're probably best looking at named queries like tomas says

Resources