Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-3649

SQL named parameters in Sql.executeUpdate

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • None
    • 1.7.2, 1.8-beta-1
    • SQL processing
    • None
    • Patch

    Description

      I've been looking for ways to enhance Groovy's SQL usability, and one thing I came up with is the use of named parameters which can in turn be mapped to bean properties. This is particularly handy for insert/ update statements, which usually require a long parameter list.

      For example:

      class Person {
        int id
        String firstName
        String lastName
        String address
      }
      
      def sql = new Sql(...)
      sql.executeUpdate( '''update person set 
          first_name= :firstName, last_name= :lastName, addr= :address  
          where id= :id''',  somePerson )
      

      Now, I realize using a GString is a similar alternative to passing the parameters as a list, but AFAIK it cannot be lazily evaluated if the actual SQL string is externalized and passed in at runtime. Correct? One possible improvement to this might be to allow a map as the 'model' and hierarchical property access as GStrings already allow.

      Attached is an example class that extends groovy.sql.Sql, but if this was found to be a worthwhile feature, it could be a nice addition to the base Sql class.

      Attachments

        1. Sql2.groovy
          2 kB
          Tom Nichols
        2. Sql2Test.groovy
          1.0 kB
          Tom Nichols
        3. Sql2.groovy
          3 kB
          Tom Nichols
        4. Sql3.groovy
          2 kB
          Tom Nichols
        5. groovysql-prototypes.tar.gz
          6 kB
          Tom Nichols

        Activity

          People

            paulk Paul King
            tomstrummer Tom Nichols
            Votes:
            1 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: