Quantcast

[grails-dev] The SqlWhereVisiter Revisited

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

[grails-dev] The SqlWhereVisiter Revisited

graemer
Hi guys,

I ran into this description of GroovySql on the strings page of the
Groovy docs: http://groovy.codehaus.org/Strings

And the interesting thing for me is that it converts GString
references into ? marks and preserves the types by settng parameters
in a PreparedStatement.

I believe this could be a great technical for integrating a easy to
using query language that maps to HQL.

eg:

Account.find( "number=${number} and
                     branch=${branch}")

would be converted to:

from Account as a where a.number = ? and a.branch = ?

The parameters would then be set using the Query object of Hibernate

What do you think? as the current mechanism of using HQL queries
supports parameters like this:

Account("from Account as a where a.number = ? and a.branch = ?", [
number,branch ] )

Graeme
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: [grails-dev] The SqlWhereVisiter Revisited

Dierk König
Yes, while this is simply GString support that makes GString
statements into PreparedStatements.
The SqlWhereVisitor is much more elaborate. It turns the AST of
an expression like { a=='x' || b=='y' } into the SQL syntax for
"a = 'x' or b = 'y' ".

For simple references like
 "number=$number and branch=$branch"
HQL supports named queries with the :name syntax.
Not sure whether be should use that or directly go for
the question-mark solution.

For general expressions like
  "number=${look.up.number}"
we could use the simple GString mechanism in the same
way groovy.sql.Sql does, i.e. produce the question-mark version
which will make Hibernate using PreparedStatements.

Converting general groovy expressions into HQL queries would require
writing a HQLWhereVisitor, which would not be too complicated
either.

cheers
Mittie

> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]]On Behalf
> Of Graeme Rocher
> Sent: Sonntag, 27. November 2005 11:43
> To: [hidden email]
> Subject: [grails-dev] The SqlWhereVisiter Revisited
>
>
> Hi guys,
>
> I ran into this description of GroovySql on the strings page of the
> Groovy docs: http://groovy.codehaus.org/Strings
>
> And the interesting thing for me is that it converts GString
> references into ? marks and preserves the types by settng parameters
> in a PreparedStatement.
>
> I believe this could be a great technical for integrating a easy to
> using query language that maps to HQL.
>
> eg:
>
> Account.find( "number=${number} and
>                      branch=${branch}")
>
> would be converted to:
>
> from Account as a where a.number = ? and a.branch = ?
>
> The parameters would then be set using the Query object of Hibernate
>
> What do you think? as the current mechanism of using HQL queries
> supports parameters like this:
>
> Account("from Account as a where a.number = ? and a.branch = ?", [
> number,branch ] )
>
> Graeme
Loading...