y.codehaus.org'] sql.execute 'insert into PROJECT (id, name, url) values (?, ?, ?)', params Or insert a row using GString syntax:
def map = [id:20, name:'Grails', url:'http://grails.codehaus.org'] sql.execute "insert into PROJECT (id, name, url) values ($map.id, $map.name, $map.url)" Or a row update:
def newUrl = 'http://grails.org' def project = 'Grails' sql.executeUpdate "update PROJECT set url=$newUrl where name=$project" Now try a query using
eachRow
:
println 'Some GR8 projects:' sql.eachRow('select * from PROJECT') { row -> println "${row.name.padRight(10)} ($row.url)" } Which will produce something like this:
Some GR8 projects: Groovy (http://groovy.codehaus.org) Grails (http://grails.org) Griffon (http://griffon.codehaus.org) Gradle (http://gradle.org) Now try a query using
rows
:
def rows = sql.rows("select * from PROJECT where name like 'Gra%'") assert rows.size() == 2 println rows.join('\n') with output like this:
[ID:20, NAME:Grails, URL:http://grails.org] [ID:40, NAME:Gradle, URL:http://gradle.org] Also,
eachRow
and
rows
support paging. Here's an example:
sql.eachRow('select * from PROJECT', 2, 2) { row -> println "${row.name.padRight(10)} ($row.url)" } Which will start at the second row and return a maximum of 2 rows. Here's an example result:
Grails (http://grails.org) Griffon (http://griffon.codehaus.org) Finally, we should clean up:
sql.close() If we are using a DataSource and we haven't enabled statement caching, then strictly speaking the final
close()
method isn't required - as all connection handling is performed transparently on our behalf; however, it doesn't hurt to have it there as it will return silently in that case.
Named and named ordinal parameters Several of the methods in this class which have a String-based sql query and params in a List
or Object[] support named or named ordinal parameters. These methods are useful for queries with large numbers of parameters - though the GString variations are often preferred in such cases too.
Named parameter queries use placeholder values in the query String. Two forms are supported ':propname1' and '?.propname2'. For these variations, a single model object is supplied in the parameter list. The propname refers to a property of that model object. The model object could be a map, Expando or domain class instance. Here are some examples: println sql.rows('select * from PROJECT where name=:foo', [foo:'Gradle']) println sql.rows('select * from PROJECT where name=:foo and id=?.bar', [foo:'Gradle', bar:40]) class MyDomainClass { def baz = 'Griffon' } println sql.rows('select * from PROJECT where name=?.baz', new MyDomainClass()) Named ordinal parameter queries have multiple model objects with the index number (starting at 1) also supplied in the placeholder. Only the question mark variation of placeholder is supported. Here is an example: println sql.rows("select * from PROJECT where name=?1.baz and id=?2.num", new MyDomainClass(), [num:30]) More details See the method and constructor JavaDoc for more details.
For advanced usage, the class provides numerous extension points for overriding the facade behavior associated with the various aspects of managing the interaction with the underlying database.
@author Chris Stevenson
@author James Strachan
@author Paul King
@author Marc DeXeT
@author John Bito
@author John Hurst
@author David Durham
@author Daniel Henrique Alves Lima