-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlTest.groovy
More file actions
69 lines (57 loc) · 1.79 KB
/
sqlTest.groovy
File metadata and controls
69 lines (57 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* sqlTest.groovy
*
* Demonstrates a bug with 'groovy.sql.Sql', possibly in the protected method 'asSql()'.
*
* Calling 'sql.rows(string)' works. Calling 'sql.rows(gstring)' returns an empty result set.
* It behaves the same in groovy versions 2.4.15, 2.5.2, 2.6.0-alpha-4, and 3.0.0-alpha-3, all
* running under OpenJDK Java 8.0.181-zulu. Run this program by simply calling
* 'groovy sqlTest.groovy'.
*/
@GrabConfig(systemClassLoader=true)
@Grab(group='com.h2database', module='h2', version='1.4.197')
import groovy.sql.Sql
// connect to an in-memory H2 database; turn on INFO level logging
def db = [url:'jdbc:h2:mem:;TRACE_LEVEL_SYSTEM_OUT=2', user:'sa', password:'', driver:'org.h2.Driver']
def sql = Sql.newInstance(db.url, db.user, db.password, db.driver)
// create a table
def create = """
create table employee (
id varchar(11) not null,
name varchar(50),
)
"""
sql.execute(create)
// store some data
def data = [ [id:'1', name:'John Doe'], [id:'2', name:'John Smith'] ]
for (map in data) {
def insert = """
insert into employee (id, name)
values ($map.id, $map.name)
"""
sql.execute(insert)
}
// define ids to lookup
def ids = "'1', '2'"
// look up some data using a String statement
String stringSelect = """
SELECT id, name
FROM employee
WHERE id IN ($ids)
"""
rows = sql.rows(stringSelect)
println ""
println "sql.rows(" + stringSelect.class + ") returned $rows.size rows; expected $data.size rows"
println ""
// lookup some data using a GString statement
def gStringSelect = """
SELECT id, name
FROM employee
WHERE id IN ($ids)
"""
def rows = sql.rows(gStringSelect)
println ""
println "sql.rows(" + gStringSelect.class + ") returned $rows.size rows; expected $data.size rows"
println ""
// clean up
sql.close()