Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing SQL inserts during Grails application startup

I am in development phase, trying to use in-memory DB and load it with some data during Grails application start up. My question is, is there any way to write/configure SQL insert statements that can be executed during startup.

like image 576
Malla Avatar asked Jan 22 '13 06:01

Malla


1 Answers

You can do this in BootStrap.groovy. If you add a dependency injection for the dataSource bean you can use it with a groovy.sql.Sql instance to do inserts:

import groovy.sql.Sql

class BootStrap {

   def dataSource

   def init = { servletContext ->
      def sql = new Sql(dataSource)
      sql.executeUpdate(
         'insert into some_table(foo, bar) values(?, ?)',
         ['x', 'y'])
   }
}

You would probably be better off using GORM though, assuming these are tables that are managed with domain classes. E.g. run something like new Book(author: 'me', title: 'some title').save()

like image 99
Burt Beckwith Avatar answered Nov 09 '22 17:11

Burt Beckwith