At the codehaus HausParty James Strachan, myself and a few others got to thinking about O/R mapping and Groovy.
We came up with the idea of extending the GPath syntax to allow us to specify strongly typed queries.
For example, assume that you have a collection of Person objects called people. In Groovy you can do the following to get all people with the surname ‘Bloggs’ who are over 21:
bloggsOfAge = people.findAll{it.lasName=='Bloggs' && it.age>21}
//iterate through the returned values
bloggsOfAge.each {
//do something
}
What we are trying to allow in Groovy is to specialise the collection class that will at runtime turn this into an SQL query:
SELECT * FROM Person WHERE Person.lastName='Bloggs' AND Person.age > 21
Its a fairly simple transformation, and what it gives you is very powerful — a query that is checked at compile time, not runtime.
Of course we could also turn this into some kind of xslt/xpath:
<xsl:for-each match="//Person[@lastName=='Bloggs && @age>21']">
<xsl:apply-templates select="."/>
</xsl:for-each>
But it gets cooler. In Groovy and similar languages you can build these objects dynamically. Groovy GPath works on HashMaps for example as well as real objects. And I don’t think its clear that using real objects (aka O/R mapping) is the best way to do this kind of db stuff. Maybe this kind of dynamic object construction is better?
Note that the key difference with current query syntaxes like JDO/EJBQL is taht they are just strings and therefore not checked by the compiler. There is a lot more difference between:
doQuery("it.lasName=='Bloggs' && it.age>21");
and
findAll {it.lasName=='Bloggs' && it.age>21}
than a mere absence of quotes.
James tells me that he’s been talking to people at the Apache Conference about the idea. I think if the idea flies we whould be able to write trivial adapters for other persistence technologies like Prevayler and OJB
Here’s a bit more of the zeitgeist:
Ted Neward
James Strachan
Smalltalkers with the same idea
The GPath wiki page
James Strachan’s post explaining GDO