Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails query not using GORM

What is the best way to query for something without using GORM in grails?

I have query that doesn't seem to fit in the GORM model, the query has a subquery and a computed field. I posted on stackoverflow already with no response so I decided to take a different approach. I want to query for something not using GORM within a grails application. Is there an easy way to get the connection and go through the result set?

like image 392
Tihom Avatar asked Jan 05 '11 04:01

Tihom


1 Answers

In a service or controller, you can add a dependency injection for the dataSource bean and use groovy.sql.Sql or JDBC directly if you're a masochist.

import groovy.sql.Sql

class DataService {

   def dataSource

   void runQuery(...) {
      def sql = new Sql(dataSource)
      sql.eachRow('select * from foo') { row ->
         ...
      }
   }
}
like image 129
Burt Beckwith Avatar answered Oct 26 '22 03:10

Burt Beckwith