Created
July 16, 2014 12:18
-
-
Save mgdelacroix/d663f654e150832a3b33 to your computer and use it in GitHub Desktop.
Groovy simple sql test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@GrabConfig(systemClassLoader=true) | |
@Grab('com.h2database:h2:1.4.180') | |
import groovy.sql.Sql | |
Sql sql = Sql.newInstance("jdbc:h2:/tmp/devDb", 'sa', '') | |
println "=================== TEST CONTENT" | |
println '>> You can invoke this script with two args to insert into test table (id, name)' | |
println '>> You can invoke this script with the argument CREATE or DESTROY to create or destroy the test table' | |
if(args.size() == 1 && args[0] == 'CREATE') { | |
sql.execute('create table test(id int primary key, name varchar)') | |
sql.execute("insert into test values(1, 'coca'), (2, 'cola')") | |
} else if(args.size() == 1 && args[0] == 'DESTROY') { | |
sql.execute('drop table test') | |
} else if (args.size() == 2) { | |
sql.execute("insert into test (id, name) values(${args[0]}, ${args[1]})") | |
} else { | |
sql.eachRow("select * from test") { | |
println '----' | |
println "ID: $it.id" | |
println "NAME: $it.name" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need the
@GrabConfig(systemClassLoader=true)
to use the sql driver