-
Notifications
You must be signed in to change notification settings - Fork 1
/
JdbcPersonDAO.groovy
65 lines (55 loc) · 2.06 KB
/
JdbcPersonDAO.groovy
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
import groovy.sql.Sql
@Singleton
class JdbcPersonDAO implements PersonDAO {
static Sql sql = Sql.newInstance(url:'jdbc:h2:db', driver:'org.h2.Driver')
static {
sql.execute 'drop table if exists people'
sql.execute '''
create table people(
id int auto_increment,
first varchar(255) not null,
last varchar(255) not null,
primary key(id)
);
'''
sql.execute """
insert into people values (null, 'Jean-Luc', 'Picard'),
(null, 'Johnathan', 'Archer'), (null, 'James', 'Kirk'),
(null, 'Benjamin', 'Sisko'), (null, 'Kathryn', 'Janeway');
"""
}
List<Person> findAll() {
String txt = 'select * from people'
sql.rows(txt).collect { row -> new Person(id:row.id, first:row.first, last:row.last) }
}
Person findById(long id) {
String txt = 'select * from people where id=?'
def row = sql.firstRow(txt, [id])
new Person(id:row.id, first:row.first, last:row.last)
}
List<Person> findByLastName(String name) {
String txt = 'select * from people where last like ?'
List<Person> customers = []
customers += sql.rows(txt, "%$name%".toLowerCase()).collect { row ->
new Person(id:row.id, first:row.first, last:row.last) }
customers += sql.rows(txt, "%$name%".toUpperCase()).collect { row ->
new Person(id:row.id, first:row.first, last:row.last) }
customers
}
Person create(Person p) {
String txt = 'insert into people(id, first, last) values(?, ?, ?)'
def keys = sql.executeInsert txt, [null, p.first, p.last]
p.id = keys[0][0]
return p
}
Person update(Person p) {
String txt = 'update people set first=?, last=? where id=?'
sql.execute txt, [p.first, p.last, p.id]
return p
}
boolean delete(long id) {
String txt = 'delete from people where id=?'
int uc = sql.executeUpdate txt, [id]
return uc == 1
}
}