3

How do you invoke database functions from grails? Such as the CONVERT(decimal, timestamp) Without using groovy SQL like this:

def sql = new Sql(dataSource)

I would like to avoid any SQL statements and take full advantage of the grails magic.

PJT
  • 3,439
  • 5
  • 29
  • 40

2 Answers2

5

My understanding is that grails does not have native support for database functions. You will need to write SQL. Gorm is an abstraction of Hibernate, you may want to refer to:

How can you call custom database functions with Hibernate?

Community
  • 1
  • 1
chrislovecnm
  • 2,549
  • 3
  • 20
  • 36
0

In your Domain class First you must -

import groovy.sql.Sql

Then you can create Sql instance and call a function like below -

    def date = "'21-Feb-19 14:10:10.123000'"
    def timestamp_format = "'DD-Mon-RR HH24:MI:SS.FF'"
    String query = 'select to_timestamp('+date+','+timestamp_format+') from dual'
    def sql = Sql.newInstance('url', 'username', 'password', 'driverClassName')
    def result= sql.rows(query);
    def rows = result.collect{it.values()}
    println rows