setting SQL comments onto dynamic finders

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

setting SQL comments onto dynamic finders

j pimmel
Hi All

I was just wondering whether anyone had a definitive answer regarding whether or not it was possible to set a sqlComment onto dynamic finders in Grails.. This is useful for us for tracing queries back to places in code, which is handy for performance analysis.

What we have done with our custom queries is 

createQuery(SOME_QUERY)
                    .setParameter('foo', 'bar')
                    .setComment('Locate most popular user for site')

A cursory look inside FindAllPersistentMethod, FindAllByPersistentMethod, GetAllPersistenMethod and GetPersistentMethod inside grails-1.0.3/src/persistence/org/codehaus/groovy/grails/orm/hibernate/metaclass

seems to suggest that one can't set the comment in the course of using the dynamic finders / gets 

Anyone know otherwise?

--
| Craft | http://www.linkedin.com/in/jpimmel
|  Vox  | http://act.ualise.com/blogs/continuous-innovation
Reply | Threaded
Open this post in threaded view
|

Re: setting SQL comments onto dynamic finders

Eric Martineau
Weird - my todo list says to ask the exact same question to grails dev list!

In our case, we need to intercept criteria/query objects to inject forced expressions.  We are running a multi-tenant system and wanted to ensure that the results of dynamic methods only contained data for the current tenant.  I did a little spike project where I forced a criteria interceptor into FindAllByPersistentMethod, etc and it worked nicely. 

FindAllByPersistentMethod.java
...
public Object doInHibernate(Session session) throws HibernateException, SQLException {
                Criteria c = session.createCriteria(clazz);
               
               
                if(arguments.length > 0) {
                    if(arguments[0] instanceof Map) {
                        Map argMap = (Map)arguments[0];
                        GrailsHibernateUtil.populateArgumentsForCriteria(clazz, c,argMap);
                    }
                }               
                               
                if(operator.equals(OPERATOR_OR)) {
                    Disjunction dis = Restrictions.disjunction();
                    for (Iterator i = expressions.iterator(); i.hasNext();) {
                        GrailsMethodExpression current = (GrailsMethodExpression) i.next();
                        dis.add( current.getCriterion() );
                    }
                    c.add(dis);
                }

                else {
                    for (Iterator i = expressions.iterator(); i.hasNext();) {
                        GrailsMethodExpression current = (GrailsMethodExpression) i.next();
                        c.add( current.getCriterion() );

                    }
                }

+                final CriteriaInterceptor criteriaInterceptor = StaticCriteriaInterceptorLocator.getCriteriaInterceptor();

+                if (criteriaInterceptor != null) {
+                   criteriaInterceptor.handleCriteria(clazz, crit);
+                }

                return c.list();
            }
        });
    }

I wasn't sure the best way to wire a criteriaInterceptor into *PersistentMethod classes.  Some appeared to be spring aware, others not...

This was the implementation of the CriteriaInterceptor I used:

public class TenantCriteriaInterceptor implements CriteriaInterceptor {
    public Criteria handleCriteria(Class aClass, Criteria criteria) {
        criteria.add(Expression.eq("tenantId", currentTenantId));
        return criteria;
    }
}

Is this something that should be integrated into the grails core?  Seems pretty useful to me...

Eric

On Tue, Jan 20, 2009 at 10:25 AM, j pimmel <[hidden email]> wrote:
Hi All

I was just wondering whether anyone had a definitive answer regarding whether or not it was possible to set a sqlComment onto dynamic finders in Grails.. This is useful for us for tracing queries back to places in code, which is handy for performance analysis.

What we have done with our custom queries is 

createQuery(SOME_QUERY)
                    .setParameter('foo', 'bar')
                    .setComment('Locate most popular user for site')

A cursory look inside FindAllPersistentMethod, FindAllByPersistentMethod, GetAllPersistenMethod and GetPersistentMethod inside grails-1.0.3/src/persistence/org/codehaus/groovy/grails/orm/hibernate/metaclass

seems to suggest that one can't set the comment in the course of using the dynamic finders / gets 

Anyone know otherwise?

--
| Craft | http://www.linkedin.com/in/jpimmel
|  Vox  | http://act.ualise.com/blogs/continuous-innovation