Saturday, May 1, 2010

Getting Groovy gStrings to work with sql queries

If you are building dynamic SQL queries with gstrings, you may have had issues embedding SQL date strings in them, the way I solved this was to explicitly define the query as a string:

date="{ts '2010-05-01 00:00:00'}"
String query=""" 
  SELECT
    order_number
    order_amount
  FROM
    orders
  where
   order_date >${date}
"""
sql.eachLine(query),{println it })