[ 
https://issues.apache.org/jira/browse/GROOVY-5373?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18072823#comment-18072823
 ] 

Paul King commented on GROOVY-5373:
-----------------------------------

The DataSet feature is currently _not_ a high priority area to enhance. There 
is a minor fix we can do for simple cases.

What now works:

{code:groovy}
def cutoff = 10
def name = "Bloggs"
people.findAll { it.size < cutoff && it.lastName == name }.rows()
// → SELECT * FROM person WHERE (size < ?) AND lastName = ?  [10, "Bloggs"]
{code}

What still doesn't work (and not planned):
* Method calls on fields (it.name.toUpperCase())
* Arithmetic expressions (it.age * 2)
* IN lists, IS NULL, LIKE
* Joins, groupby, aggregates

> Sql DataSet fails to work with non-literals in queries (enhancement required)
> -----------------------------------------------------------------------------
>
>                 Key: GROOVY-5373
>                 URL: https://issues.apache.org/jira/browse/GROOVY-5373
>             Project: Groovy
>          Issue Type: New Feature
>          Components: SQL processing
>    Affects Versions: 2.0-beta-3, 2.1.4, 2.2.0-beta-1
>            Reporter: Dr. Russel Winder
>            Assignee: Paul King
>            Priority: Major
>
> All the examples of using _findAll_ in the _Sql_ _DataSet_ class use literals 
> for the search values of queries. Using free variables causes failure as 
> Groovy does not implement lexical closure automatically.  However this can be 
> realized using the Closure delegate field. I therefore believe that the 
> following example fails because the _Sql.SqlWhereVisitor_ fails to lookup 
> variables but assumes that all query values are literals.
> {code}
> @Grab('org.xerial:sqlite-jdbc:3.7.2')
> @GrabConfig(systemClassLoader=true)
> import groovy.sql.DataSet
> import groovy.sql.Sql
> final words = []
> Sql.withInstance('jdbc:sqlite:database.db', 'org.sqlite.JDBC') {database ->
>   final wordsTable = new DataSet(database, 'words')
>   (0 ..< 4).each {i ->
>     // This doesn't work as SqlWhereVisitor doesn't resolve variables it only 
> looks for
>     // literals.  cf.  http://jira.codehaus.org/browse/GROOVY-5373
>     final query = {item -> item.id == i}
>     query.delegate = {i: i}
>     query.resolveStrategy = Closure.DELEGATE_FIRST
>     words << wordsTable.findAll(query).firstRow().word
>   }
> }
> println words.join('')
> {code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to