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 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
    • Number of attachments :
      0

      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

          Hide
          Paul King added a comment -

          Issue cloned and split out into "fix current error message/doco" and "do the enhancement".

          Show
          Paul King added a comment - Issue cloned and split out into "fix current error message/doco" and "do the enhancement".
          Hide
          Paul King added a comment -

          Currently, DataSet produces invalid Sql and silently continues on resulting in the user getting a strange error message at some point in the future. If we currently don't support particular expressions which cause such behaviour, we should produce a suitable error message at the point where the unsupported expression is discovered.

          Show
          Paul King added a comment - Currently, DataSet produces invalid Sql and silently continues on resulting in the user getting a strange error message at some point in the future. If we currently don't support particular expressions which cause such behaviour, we should produce a suitable error message at the point where the unsupported expression is discovered.
          Hide
          Paul King added a comment -

          Error message added and doco tweaked. Please continue discussions about the desired DataSet enhancement on GROOVY-5373. Thanks.

          Show
          Paul King added a comment - Error message added and doco tweaked. Please continue discussions about the desired DataSet enhancement on GROOVY-5373 . Thanks.
          Hide
          Russel Winder added a comment -

          The error message works for me.

          Show
          Russel Winder added a comment - The error message works for me.
          Hide
          Russel Winder added a comment -

          Seems to work as anticipated.

          Show
          Russel Winder added a comment - Seems to work as anticipated.

            People

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

              Dates

              • Created:
                Updated:
                Resolved: