-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbr.go
95 lines (81 loc) · 2.17 KB
/
dbr.go
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package dbeval
import (
"time"
"github.com/gocraft/dbr"
)
// Notes:
// - trivial to insert multiple records with a single statement
// - inserts tz-ignorant times into tz-aware columns
type DBR struct {
db *dbr.Session
}
func (p *DBR) Connect(ds string, connLifetime time.Duration, idleConns, openConns int) {
if p.db != nil {
p.db.Close()
p.db = nil
}
conn, err := dbr.Open("postgres", ds, nil)
check(err)
conn.SetConnMaxLifetime(connLifetime)
conn.SetMaxIdleConns(idleConns)
conn.SetMaxOpenConns(openConns)
p.db = conn.NewSession(nil)
}
func (p *DBR) CreateDatabase() {
_, err := p.db.Exec(createdb)
check(err)
}
func (p *DBR) DropDatabase() {
_, err := p.db.Exec(dropdb)
check(err)
}
func (p *DBR) CreateSchema() {
_, err := p.db.Exec(schema)
check(err)
}
func (p *DBR) InsertAuthors(as []*Author) {
tx, err := p.db.Begin()
check(err)
defer tx.RollbackUnlessCommitted()
ib := tx.InsertInto("authors").Columns("id", "name")
for _, a := range as {
ib.Record(a)
}
_, err = ib.Exec()
check(err)
check(tx.Commit())
}
func (p *DBR) InsertArticles(as []*Article) {
tx, err := p.db.Begin()
check(err)
defer tx.RollbackUnlessCommitted()
ib := tx.InsertInto("articles").Columns("id", "title", "body", "published_at")
for _, a := range as {
// bug in time encoding, drops tz on insert, so convert to local and insert as UTC. PG will add back local tz,
// hopefully the same one as this program.
t := a.PublishedAt.Local()
a.PublishedAt = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), time.UTC)
ib.Record(a)
}
_, err = ib.Exec()
check(err)
check(tx.Commit())
}
func (p *DBR) FindAuthorByID(id int64) *Author {
a := &Author{}
check(p.db.Select("*").From("authors").Where("id = ? ", id).LoadOne(a))
return a
}
func (p *DBR) FindAuthorsByName(name string) []*Author {
var as []*Author
_, err := p.db.Select("*").From("authors").Where("name = ?", name).Load(&as)
check(err)
return as
}
func (p *DBR) RecentArticles(n int) []*Article {
var as []*Article
_, err := p.db.Select("*").From("articles").OrderDir("published_at", false).Limit(uint64(n)).Load(&as)
check(err)
return as
}
var _ Implementation = &DBR{}