Groovy
  1. Groovy
  2. GROOVY-5371

Sql DataSet fails to work with non-literals in queries (fix error message/doco)

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Critical Critical
    • Resolution: Fixed
    • Affects Version/s: 2.0-beta-3
    • Fix Version/s: 2.0-beta-3, 1.8.7
    • Component/s: None
    • Labels:
      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.

      import groovy.sql.DataSet
      import groovy.sql.Sql
      
      @Grapes ( [    
                  @Grab ( 'org.xerial:sqlite-jdbc:3.7.2' ),
                  @GrabConfig ( systemClassLoader = true )
                ] )
      def database
      final words = [ ]
      try {
        database = Sql.newInstance ( 'jdbc:sqlite:database.db' , 'org.sqlite.JDBC')
        final wordsTable = new DataSet ( database , 'words' )
        ( 0 ..< 4 ).each { i ->
          final query = { item -> item.id == i }
          query.delegate = { i : i }
          query.resolveStrategy = Closure.DELEGATE_FIRST 
          words << wordsTable.findAll ( query ).firstRow ( ).word
        }
      }
      finally {
        database?.close ( )
      }
      println words.join ( '' )
      

        Issue Links

          Activity

          Russel Winder created issue -
          Russel Winder made changes -
          Field Original Value New Value
          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.
          {code}
          import groovy.sql.DataSet
          import groovy.sql.Sql

          @Grapes ( [
                      @Grab ( 'org.xerial:sqlite-jdbc:3.7.2' ),
                      @GrabConfig ( systemClassLoader = true )
                    ] )
          def database
          final words = [ ]
          try {
            database = Sql.newInstance ( 'jdbc:sqlite:database.db' , 'org.sqlite.JDBC')
            final wordsTable = new DataSet ( database , 'words' )
            ( 0 ..< 4 ).each { i ->
              final query = { item -> item.id == i }
              query.delegate = { i : i }
              query.resolveStrategy = Closure.DELEGATE_FIRST
              words << wordsTable.findAll ( query ).firstRow ( ).word
            }
          }
          finally {
            database?.close ( )
          }
          println words.join ( '' )
          {code}
          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}
          import groovy.sql.DataSet
          import groovy.sql.Sql

          @Grapes ( [
                      @Grab ( 'org.xerial:sqlite-jdbc:3.7.2' ),
                      @GrabConfig ( systemClassLoader = true )
                    ] )
          def database
          final words = [ ]
          try {
            database = Sql.newInstance ( 'jdbc:sqlite:database.db' , 'org.sqlite.JDBC')
            final wordsTable = new DataSet ( database , 'words' )
            ( 0 ..< 4 ).each { i ->
              final query = { item -> item.id == i }
              query.delegate = { i : i }
              query.resolveStrategy = Closure.DELEGATE_FIRST
              words << wordsTable.findAll ( query ).firstRow ( ).word
            }
          }
          finally {
            database?.close ( )
          }
          println words.join ( '' )
          {code}
          Paul King made changes -
          Link This issue relates to GROOVY-5373 [ GROOVY-5373 ]
          Paul King made changes -
          Summary Sql DataSet fails to work with non-literals in queries Sql DataSet fails to work with non-literals in queries (fix error message/doco)
          Paul King made changes -
          Resolution Fixed [ 1 ]
          Fix Version/s 2.0-beta-3 [ 18244 ]
          Status Open [ 1 ] Resolved [ 5 ]
          Fix Version/s 1.8.7 [ 18317 ]
          Assignee Paul King [ paulk ]
          Russel Winder made changes -
          Status Resolved [ 5 ] Closed [ 6 ]
          Mark Thomas made changes -
          Project Import Sun Apr 05 13:32:57 UTC 2015 [ 1428240777691 ]
          Mark Thomas made changes -
          Workflow jira [ 12734294 ] Default workflow, editable Closed status [ 12746080 ]
          Mark Thomas made changes -
          Project Import Mon Apr 06 02:11:23 UTC 2015 [ 1428286283443 ]
          Mark Thomas made changes -
          Workflow jira [ 12973770 ] Default workflow, editable Closed status [ 12980924 ]

            People

            • Assignee:
              Paul King
              Reporter:
              Russel Winder
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development