This repository has been archived by the owner on Feb 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stmt_test.go
191 lines (170 loc) · 4.98 KB
/
stmt_test.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package orm_test
import (
"context"
"database/sql"
"io/ioutil"
"os"
"testing"
"github.com/deweppro/go-orm"
"github.com/deweppro/go-orm/schema/sqlite"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestUnit_Stmt(t *testing.T) {
file, err := ioutil.TempFile("/tmp", "prefix")
require.NoError(t, err)
defer os.Remove(file.Name()) //nolint: errcheck
conn := sqlite.New(&sqlite.Config{Pool: []sqlite.Item{{Name: "main", File: file.Name()}}})
require.NoError(t, conn.Reconnect())
defer conn.Close() //nolint: errcheck
pool := orm.NewDB(conn, orm.Plugins{}).Pool("main")
err = pool.CallContext("init", context.Background(), func(ctx context.Context, db *sql.DB) error {
sqls := []string{
`create table users (
id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
name TEXT
);`,
"insert into `users` (`id`, `name`) values (1, 'aaaa');",
"insert into `users` (`id`, `name`) values (2, 'bbbb');",
}
for _, item := range sqls {
if _, err = db.ExecContext(ctx, item); err != nil {
return err
}
}
return nil
})
require.NoError(t, err)
err = pool.QueryContext("", context.Background(), func(q orm.Querier) {
q.SQL("select `name` from `users` where `id` = ?", 1)
q.Bind(func(bind orm.Scanner) error {
name := ""
assert.NoError(t, bind.Scan(&name))
assert.Equal(t, "aaaa", name)
return nil
})
})
assert.NoError(t, err)
var result []string
err = pool.QueryContext("", context.Background(), func(q orm.Querier) {
q.SQL("select `name` from `users`")
q.Bind(func(bind orm.Scanner) error {
name := ""
assert.NoError(t, bind.Scan(&name))
result = append(result, name)
return nil
})
})
assert.NoError(t, err)
assert.Equal(t, []string{"aaaa", "bbbb"}, result)
err = pool.ExecContext("", context.Background(), func(e orm.Executor) {
e.SQL("insert into `users` (`id`, `name`) values (?, ?);")
e.Params(3, "cccc")
e.Params(4, "dddd")
e.Bind(func(result orm.Result) error {
assert.Equal(t, int64(2), result.RowsAffected)
assert.Equal(t, int64(4), result.LastInsertId)
return nil
})
})
assert.NoError(t, err)
var result2 []string
err = pool.QueryContext("", context.Background(), func(q orm.Querier) {
q.SQL("select `name` from `users`")
q.Bind(func(bind orm.Scanner) error {
name := ""
err = bind.Scan(&name)
result2 = append(result2, name)
return err
})
})
assert.NoError(t, err)
assert.Equal(t, []string{"aaaa", "bbbb", "cccc", "dddd"}, result2)
var result3 []string
err = pool.TransactionContext("", context.Background(), func(v orm.Tx) {
v.Exec(func(e orm.Executor) {
e.SQL("insert into `users` (`id`, `name`) values (?, ?);")
e.Params(10, "abcd")
e.Params(11, "efgh")
e.Bind(func(result orm.Result) error {
assert.Equal(t, int64(2), result.RowsAffected)
assert.Equal(t, int64(11), result.LastInsertId)
return nil
})
})
v.Query(func(q orm.Querier) {
q.SQL("select `name` from `users`")
q.Bind(func(bind orm.Scanner) error {
name := ""
err = bind.Scan(&name)
result3 = append(result3, name)
return err
})
})
})
assert.NoError(t, err)
assert.Equal(t, []string{"aaaa", "bbbb", "cccc", "dddd", "abcd", "efgh"}, result3)
var result4 []string
err = pool.QueryContext("", context.Background(), func(q orm.Querier) {
q.SQL("select `name` from `users`")
q.Bind(func(bind orm.Scanner) error {
name := ""
err = bind.Scan(&name)
result4 = append(result4, name)
return err
})
})
assert.NoError(t, err)
assert.Equal(t, []string{"aaaa", "bbbb", "cccc", "dddd", "abcd", "efgh"}, result4)
}
func BenchmarkStmt(b *testing.B) {
file, err := ioutil.TempFile("/tmp", "prefix")
require.NoError(b, err)
defer os.Remove(file.Name()) //nolint: errcheck
conn := sqlite.New(&sqlite.Config{Pool: []sqlite.Item{{Name: "main", File: file.Name()}}})
require.NoError(b, conn.Reconnect())
defer conn.Close() //nolint: errcheck
pool := orm.NewDB(conn, orm.Plugins{}).Pool("main")
err = pool.CallContext("init", context.Background(), func(ctx context.Context, db *sql.DB) error {
sqls := []string{
`create table users (
id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
name TEXT
);`,
}
for _, item := range sqls {
if _, err = db.ExecContext(ctx, item); err != nil {
return err
}
}
return nil
})
require.NoError(b, err)
b.Run("insert", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 1; i < b.N; i++ {
err = pool.ExecContext("", context.Background(), func(e orm.Executor) {
i := i
e.SQL("insert or ignore into `users` (`id`, `name`) values (?, ?);")
e.Params(i, "cccc")
})
assert.NoError(b, err)
}
})
var name string
b.Run("select", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 1; i < b.N; i++ {
err = pool.QueryContext("", context.Background(), func(q orm.Querier) {
i := i
q.SQL("select `name` from `users` where `id` = ?", i)
q.Bind(func(bind orm.Scanner) error {
return bind.Scan(&name)
})
})
assert.NoError(b, err)
}
})
}