Syam
Syam

Reputation: 575

DDL queries using Grails

I'm trying to create a DBA tool on Oracle DB to manage multiple database servers using Grails. My application needs to create database users by taking user (DBA) inputs. Creating a new database user is a DDL statement. Can we write DDL statements in Grails?

Upvotes: 0

Views: 492

Answers (1)

Burt Beckwith
Burt Beckwith

Reputation: 75681

Sure, dependency-inject the dataSource bean into a controller/service/etc. and get a connection from there. Then you can do any JDBC query you want. Your best bet is to use groovy.sql.Sql as it simplifies the workflow for doing JDBC.

You can also run queries from the Grails console (run grails console to launch it). Beans are available using property access notation from the implicit ctx variable which is the Spring ApplicationContext, so you can get the dataSource bean via def dataSource = ctx.dataSource

Here's an example in a Service:

import groovy.sql.Sql

class MyService {

   def dataSource

   def someMethod() {

      def sql = new Sql(dataSource)

      String dml = '''\
         create table foo (
         ...
         )'''

      sql.execute(dml)
   }
}

Upvotes: 4

Related Questions