Revision: 9999
                            
                                                            
                                    
                                        
Initial Code
                                    
                                    
                                                            
                                    
                                        
Initial URL
                                    
                                    
                                
                                                            
                                    
                                        
Initial Description
                                    
                                    
                                                            
                                    
                                        
Initial Title
                                    
                                    
                                                            
                                    
                                        
Initial Tags
                                    
                                    
                                                            
                                    
                                        
Initial Language
                                    
                                    
                                                    
                        at December 3, 2008 05:02 by kouphax
                            
                            Initial Code
groovy.sql.GroovyRowResult.metaClass.coerce << {Class clazz ->
  def resultant = clazz.newInstance()
  def targetProps = [:]
  (resultant.properties.keySet() - ["metaClass","class"]).each({
      targetProps."${it.toLowerCase()}" = it
  })
  delegate.keySet().each {
      def property = targetProps."${it.toLowerCase().replaceAll('_','')}"
      if(property){
        resultant."$property" = delegate."$it"
      }
  }
 
  return resultant
}
groovy.sql.GroovyRowResult.metaClass.transmogrify << {Class clazz ->
  delegate.coerce(clazz)
}
 
groovy.sql.DataSet.metaClass.transmogrify << {Class clazz ->
    delegate.rows().collect {row -> 
        row.coerce(clazz)
    }    
}
 
groovy.sql.Sql.metaClass.transmogrify << {String sql, Class clazz ->
    delegate.rows(sql).collect {row -> 
        row.coerce(clazz)
    }   
}
                                Initial URL
Initial Description
Using Groovy you can extend the Sql, DataSet and GroovyResultSet objects present in the language to do simple object mapping similar to iBatis et al.  An example of using the code below
    /* POGO used in the mapping */
    class BranchFax {
      def faxCodeId, nsc, faxNumber, branchName, address1
    }
    /* Connect to the DB */
    def db = Sql.newInstance(
        "jdbc:oracle:thin:@//10.7.1.79/DATABSE",
        "user1", "test1",
        "oracle.jdbc.driver.OracleDriver"
    )
    /* create a dataset */
    def bf = db.dataSet("BRANCH_FAX")
    /* now work some magic */
    def result01 = bf.firstRow().coerce(BranchFax)
    def result02 = bf.firstRow().transmogrify(BranchFax)
    def result03 = bf.transmogrify(BranchFax)
    def result04 = db.transmogrify("SELECT * FROM BRANCH_FAX", BranchFax)
                                Initial Title
Simple ORM in Groovy
Initial Tags
groovy
Initial Language
Groovy