-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.go
More file actions
324 lines (272 loc) · 7.2 KB
/
Copy pathdriver.go
File metadata and controls
324 lines (272 loc) · 7.2 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package sqlite
import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/go-goe/goe"
"github.com/go-goe/goe/model"
"modernc.org/sqlite"
)
type Driver struct {
dns string
sql *sql.DB
config
}
func (d *Driver) GetDatabaseConfig() *model.DatabaseConfig {
return &d.config.DatabaseConfig
}
type ExecQuerierContext interface {
driver.ExecerContext
driver.QueryerContext
}
type ConnectionHook func(
conn ExecQuerierContext,
dsn string,
) error
type config struct {
model.DatabaseConfig
MigratePath string
ConnectionHook ConnectionHook
}
type Config struct {
Logger model.Logger
IncludeArguments bool // include all arguments used on query
QueryThreshold time.Duration // query threshold to warning on slow queries
MigratePath string // output sql file, if defined the driver will not auto apply the migration.
ConnectionHook ConnectionHook // ConnectionHook is called after each connection is opened.
}
func NewConfig(c Config) config {
return config{
DatabaseConfig: model.DatabaseConfig{
Logger: c.Logger,
IncludeArguments: c.IncludeArguments,
QueryThreshold: c.QueryThreshold,
},
MigratePath: c.MigratePath,
ConnectionHook: c.ConnectionHook,
}
}
var lock = struct {
sync.Mutex
}{sync.Mutex{}}
// OpenInMemory opens a in memory database.
func OpenInMemory(c config) (driver *Driver) {
return Open("file:goe?mode=memory&cache=shared", c)
}
// Open opens a sqlite connection. By default uses "PRAGMA foreign_keys = ON;" and "PRAGMA busy_timeout = 5000;".
func Open(dns string, c config) (driver *Driver) {
return &Driver{
dns: dns,
config: c,
}
}
func (dr *Driver) Init() error {
dr.DatabaseConfig.SetInitCallback(func() error {
lock.Lock()
defer lock.Unlock()
var err error
dr.setHooks()
dr.sql, err = sql.Open("sqlite", dr.dns)
if err != nil {
// logged by goe
return err
}
return dr.sql.Ping()
})
return nil
}
func (dr *Driver) setHooks() {
sqlite.RegisterConnectionHook(func(conn sqlite.ExecQuerierContext, dsn string) error {
conn.ExecContext(context.Background(), "PRAGMA foreign_keys = ON;", nil)
conn.ExecContext(context.Background(), "PRAGMA busy_timeout = 5000;", nil)
return nil
})
if dr.ConnectionHook != nil {
sqlite.RegisterConnectionHook(func(conn sqlite.ExecQuerierContext, dsn string) error {
return dr.ConnectionHook(conn, dsn)
})
}
schemas := dr.Schemas()
dns, params, _ := strings.Cut(dr.dns, "?")
if len(schemas) != 0 {
rx := regexp.MustCompile(`([^/]+?)(?:\.[a-zA-Z0-9]+)?$`)
currentDb := rx.FindString(dns)
currentDb = strings.TrimPrefix(currentDb, "file:")
var ex string
ix := strings.Index(currentDb, ".")
if ix != -1 {
ex = currentDb[ix:]
}
schemaBuilder := strings.Builder{}
for _, schema := range schemas {
schemaBuilder.WriteString(
fmt.Sprintf("ATTACH DATABASE '%v' AS %v;\n",
strings.Replace(dns, currentDb, schema[1:len(schema)-1]+ex, 1)+func() string {
if params != "" {
return "?" + params
}
return ""
}(),
schema))
}
sqlite.RegisterConnectionHook(func(conn sqlite.ExecQuerierContext, dsn string) error {
conn.ExecContext(context.Background(), schemaBuilder.String(), nil)
return nil
})
}
}
func (dr *Driver) KeywordHandler(s string) string {
return keywordHandler(s)
}
func keywordHandler(s string) string {
return fmt.Sprintf(`"%s"`, s)
}
func (dr *Driver) Name() string {
return "SQLite"
}
func (dr *Driver) Stats() sql.DBStats {
return dr.sql.Stats()
}
func (dr *Driver) Close() error {
return dr.sql.Close()
}
var errMap = map[int][]error{
1555: {goe.ErrBadRequest, goe.ErrUniqueValue},
2067: {goe.ErrBadRequest, goe.ErrUniqueValue},
787: {goe.ErrBadRequest, goe.ErrForeignKey},
}
type wrapErrors struct {
msg string
errs []error
}
func (e *wrapErrors) Error() string {
return "goe: " + e.msg
}
func (e *wrapErrors) Unwrap() []error {
return e.errs
}
func (dr *Driver) ErrorTranslator() func(err error) error {
return func(err error) error {
if sqliteError, ok := err.(*sqlite.Error); ok {
return &wrapErrors{msg: err.Error(), errs: append(errMap[sqliteError.Code()], err)}
}
return err
}
}
func (dr *Driver) NewConnection() model.Connection {
return Connection{sql: dr.sql, config: dr.config, dns: dr.dns}
}
type Connection struct {
dns string
config config
sql *sql.DB
}
func (c Connection) QueryContext(ctx context.Context, query *model.Query) (model.Rows, error) {
buildSql(query)
rows, err := c.sql.QueryContext(ctx, query.RawSql, query.Arguments...)
return Rows{rows: rows}, err
}
func (c Connection) QueryRowContext(ctx context.Context, query *model.Query) model.Row {
buildSql(query)
row := c.sql.QueryRowContext(ctx, query.RawSql, query.Arguments...)
return Row{row: row}
}
func (c Connection) ExecContext(ctx context.Context, query *model.Query) error {
buildSql(query)
_, err := c.sql.ExecContext(ctx, query.RawSql, query.Arguments...)
return err
}
func (dr *Driver) NewTransaction(ctx context.Context, opts *sql.TxOptions) (model.Transaction, error) {
tx, err := dr.sql.BeginTx(ctx, opts)
return Transaction{tx: tx, config: dr.config, dns: dr.dns}, err
}
type Transaction struct {
dns string
config config
tx *sql.Tx
saves int64
}
func (t Transaction) QueryContext(ctx context.Context, query *model.Query) (model.Rows, error) {
buildSql(query)
rows, err := t.tx.QueryContext(ctx, query.RawSql, query.Arguments...)
return Rows{rows: rows}, err
}
func (t Transaction) QueryRowContext(ctx context.Context, query *model.Query) model.Row {
buildSql(query)
return Row{row: t.tx.QueryRowContext(ctx, query.RawSql, query.Arguments...)}
}
func (t Transaction) ExecContext(ctx context.Context, query *model.Query) error {
buildSql(query)
_, err := t.tx.ExecContext(ctx, query.RawSql, query.Arguments...)
return err
}
func (t Transaction) Commit() error {
err := t.tx.Commit()
if err != nil {
// goe can't log
return t.config.ErrorHandler(context.TODO(), err)
}
return nil
}
func (t Transaction) Rollback() error {
err := t.tx.Rollback()
if err != nil {
// goe can't log
return t.config.ErrorHandler(context.TODO(), err)
}
return nil
}
type SavePoint struct {
name string
tx Transaction
}
func (t Transaction) SavePoint() (model.SavePoint, error) {
t.saves++
point := "sp_" + strconv.FormatInt(t.saves, 10)
_, err := t.tx.Exec("SAVEPOINT " + point)
if err != nil {
// goe can't log
return nil, t.config.ErrorHandler(context.TODO(), err)
}
return SavePoint{point, t}, nil
}
func (s SavePoint) Rollback() error {
_, err := s.tx.tx.Exec("ROLLBACK TO SAVEPOINT " + s.name)
if err != nil {
// goe can't log
return s.tx.config.ErrorHandler(context.TODO(), err)
}
return nil
}
func (s SavePoint) Commit() error {
_, err := s.tx.tx.Exec("RELEASE SAVEPOINT " + s.name)
if err != nil {
// goe can't log
return s.tx.config.ErrorHandler(context.TODO(), err)
}
return nil
}
type Rows struct {
rows *sql.Rows
}
func (rs Rows) Close() error {
return rs.rows.Close()
}
func (rs Rows) Next() bool {
return rs.rows.Next()
}
func (rs Rows) Scan(dest ...any) error {
return rs.rows.Scan(dest...)
}
type Row struct {
row *sql.Row
}
func (r Row) Scan(dest ...any) error {
return r.row.Scan(dest...)
}