Details
-
New Feature
-
Status: Open
-
Major
-
Resolution: Unresolved
-
2.0-beta-3, 2.1.4, 2.2.0-beta-1
-
None
-
None
Description
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.
@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('')
Attachments
Issue Links
- is related to
-
GROOVY-5371 Sql DataSet fails to work with non-literals in queries (fix error message/doco)
- Closed