-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_squirrel.go
132 lines (126 loc) · 3.7 KB
/
lib_squirrel.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package sqlhbenchmarks
import (
"database/sql"
"testing"
sq "github.com/Masterminds/squirrel"
"github.com/nofeaturesonlybugs/sqlhbenchmarks/types"
)
// SquirrelInsert performs INSERTs using github.com/Masterminds/squirrel.
func SquirrelInsert(addresses []*types.Address, db *sql.DB) func(b *testing.B) {
fn := func(b *testing.B) {
var err error
for k := 0; k < b.N; k++ {
for _, address := range addresses {
address.PreInsert(b)
//
query := sq.Insert(types.AddressTableName).
Columns("street", "city", "state", "zip").
Values(address.Street, address.City, address.State, address.Zip).
Suffix("RETURNING pk, created_tmz, modified_tmz").
RunWith(db).
PlaceholderFormat(sq.Dollar)
if err = query.QueryRow().Scan(&address.Id, &address.CreatedTime, &address.ModifiedTime); err != nil {
b.Fatalf("squirrel failed with %v", err.Error())
}
//
address.PostInsert(b)
}
}
}
return fn
}
// SquirrelPreparedInsert performs INSERTs using github.com/Masterminds/squirrel.
func SquirrelPreparedInsert(addresses []*types.Address, db *sql.DB) func(b *testing.B) {
fn := func(b *testing.B) {
var dbcache *sq.StmtCache
var tx *sql.Tx
var err error
//
if tx, err = db.Begin(); err != nil {
b.Fatalf("error beginning transaction with %v", err.Error())
}
defer tx.Rollback()
dbcache = sq.NewStmtCache(tx)
defer dbcache.Clear()
//
for k := 0; k < b.N; k++ {
for _, address := range addresses {
query := sq.Insert(types.AddressTableName).
Columns("street", "city", "state", "zip").
Values(address.Street, address.City, address.State, address.Zip).
Suffix("RETURNING pk, created_tmz, modified_tmz").
RunWith(dbcache).
PlaceholderFormat(sq.Dollar)
if err = query.QueryRow().Scan(&address.Id, &address.CreatedTime, &address.ModifiedTime); err != nil {
b.Fatalf("squirrel failed with %v", err.Error())
}
}
}
//
if tx != nil {
if err = tx.Commit(); err != nil {
b.Fatalf("error durring commit with %v", err.Error())
}
}
}
return fn
}
// SquirrelUpdate performs UPDATEs using github.com/Masterminds/squirrel.
func SquirrelUpdate(addresses []*types.Address, tx *sql.Tx) func(b *testing.B) {
fn := func(b *testing.B) {
var err error
for k := 0; k < b.N; k++ {
for _, address := range addresses {
address.PreUpdate(b)
//
query := sq.Update(types.AddressTableName).
Set("street", address.Street).
Set("city", address.City).
Set("state", address.State).
Set("zip", address.Zip).
Where(sq.Eq{"pk": address.Id}).
Suffix("RETURNING modified_tmz").
RunWith(tx).
PlaceholderFormat(sq.Dollar)
if err = query.QueryRow().Scan(&address.ModifiedTime); err != nil {
b.Fatalf("squirrel failed with %v", err.Error())
}
//
address.PostUpdate(b)
}
}
}
return fn
}
// SquirrelPreparedUpdate performs UPDATEs using github.com/Masterminds/squirrel.
func SquirrelPreparedUpdate(addresses []*types.Address, tx *sql.Tx) func(b *testing.B) {
fn := func(b *testing.B) {
var dbcache *sq.StmtCache
var err error
//
dbcache = sq.NewStmtCache(tx)
defer dbcache.Clear()
//
for k := 0; k < b.N; k++ {
for _, address := range addresses {
address.PreUpdate(b)
//
query := sq.Update(types.AddressTableName).
Set("street", address.Street).
Set("city", address.City).
Set("state", address.State).
Set("zip", address.Zip).
Where(sq.Eq{"pk": address.Id}).
Suffix("RETURNING modified_tmz").
RunWith(dbcache).
PlaceholderFormat(sq.Dollar)
if err = query.QueryRow().Scan(&address.ModifiedTime); err != nil {
b.Fatalf("squirrel failed with %v", err.Error())
}
//
address.PostUpdate(b)
}
}
}
return fn
}