-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
db.go
445 lines (416 loc) · 10.4 KB
/
db.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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package runn
import (
"context"
"database/sql"
"errors"
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"sync/atomic"
"unsafe"
"github.com/araddon/dateparse"
_ "github.com/go-sql-driver/mysql"
"github.com/goccy/go-json"
"github.com/golang-sql/sqlexp"
"github.com/golang-sql/sqlexp/nest"
_ "github.com/googleapis/go-sql-spanner"
"github.com/k1LoW/donegroup"
_ "github.com/lib/pq"
"github.com/xo/dburl"
"modernc.org/sqlite"
)
const (
dbStoreLastInsertIDKey = "last_insert_id"
dbStoreRowsAffectedKey = "rows_affected"
dbStoreRowsKey = "rows"
)
type Querier interface {
sqlexp.Querier
}
type TxQuerier interface {
nest.Querier
BeginTx(ctx context.Context, opts *nest.TxOptions) (*nest.Tx, error)
}
type dbRunner struct {
name string
dsn string
client TxQuerier
hostRules hostRules
trace *bool
// operatorID - The id of the operator for which the runner is defined.
operatorID string
}
type dbQuery struct {
stmt string
trace *bool
}
type DBResponse struct {
LastInsertID int64
RowsAffected int64
Columns []string
Rows []map[string]any
}
func newDBRunner(name, dsn string) (*dbRunner, error) {
_, err := dburl.Parse(dsn)
if err != nil {
return nil, err
}
return &dbRunner{
name: name,
dsn: dsn,
}, nil
}
var dsnRep = strings.NewReplacer("sqlite://", "moderncsqlite://", "sqlite3://", "moderncsqlite://", "sq://", "moderncsqlite://")
var spannerInvalidatonKeyCounter uint64 = 0
func normalizeDSN(dsn string) string {
if !contains(sql.Drivers(), "sqlite3") { // sqlite3 => github.com/mattn/go-sqlite3
return dsnRep.Replace(dsn)
}
return dsn
}
func (rnr *dbRunner) Run(ctx context.Context, s *step) error {
o := s.parent
e, err := o.expandBeforeRecord(s.dbQuery)
if err != nil {
return err
}
q, ok := e.(map[string]any)
if !ok {
return fmt.Errorf("invalid query: %v", e)
}
query, err := parseDBQuery(q)
if err != nil {
return fmt.Errorf("invalid query: %v %w", q, err)
}
if err := rnr.run(ctx, query, s); err != nil {
return err
}
return nil
}
func (rnr *dbRunner) Close() error {
if rnr.client == nil {
return nil
}
if ndb, ok := rnr.client.(*nest.DB); ok {
if db := ndb.DB(); db != nil {
rnr.client = nil
return db.Close()
}
}
return nil
}
func (rnr *dbRunner) Renew() error {
// If the DSN is in-memory, keep connection ( do not renew )
if rnr.client != nil && rnr.dsn == "" {
return errors.New("DB runners created with the runn.DBRunner option cannot be renewed") //nostyle:errorstrings
}
if err := rnr.Close(); err != nil {
return err
}
return nil
}
func (rnr *dbRunner) run(ctx context.Context, q *dbQuery, s *step) error {
o := s.parent
if rnr.client == nil {
if len(rnr.hostRules) > 0 {
rnr.dsn = rnr.hostRules.replaceDSN(rnr.dsn)
}
nx, err := connectDB(rnr.dsn)
if err != nil {
return err
}
rnr.client = nx
if rnr.dsn != "" {
if err := donegroup.Cleanup(ctx, func() error {
// In the case of Reused runners, leave the cleanup to the main cleanup
if o.id != rnr.operatorID {
return nil
}
return rnr.Renew()
}); err != nil {
return err
}
}
}
stmts := separateStmt(q.stmt)
out := map[string]any{}
tx, err := rnr.client.BeginTx(ctx, &sql.TxOptions{})
if err != nil {
return err
}
// Override trace
switch {
case q.trace == nil && rnr.trace == nil:
q.trace = &o.trace
case q.trace == nil && rnr.trace != nil:
q.trace = rnr.trace
}
tc, err := q.generateTraceStmtComment(s)
if err != nil {
return err
}
for _, stmt := range stmts {
stmt = stmt + tc // add trace comment
o.capturers.captureDBStatement(rnr.name, stmt)
err := func() error {
if !isSELECTStmt(stmt) {
// exec
r, err := tx.ExecContext(ctx, stmt)
if err != nil {
return err
}
if isCommentOnlyStmt(stmt) {
o.capturers.captureDBResponse(rnr.name, &DBResponse{})
return nil
}
id, _ := r.LastInsertId()
a, _ := r.RowsAffected()
out = map[string]any{
string(dbStoreLastInsertIDKey): id,
string(dbStoreRowsAffectedKey): a,
}
o.capturers.captureDBResponse(rnr.name, &DBResponse{
LastInsertID: id,
RowsAffected: a,
})
return nil
}
// query
var rows []map[string]any
r, err := tx.QueryContext(ctx, stmt)
if err != nil {
return err
}
defer r.Close()
columns, err := r.Columns()
if err != nil {
return err
}
types, err := r.ColumnTypes()
if err != nil {
return err
}
for r.Next() {
row := map[string]any{}
vals := make([]any, len(columns))
valsp := make([]any, len(columns))
for i := range columns {
valsp[i] = &vals[i]
}
if err := r.Scan(valsp...); err != nil {
return err
}
for i, c := range columns {
t := strings.ToUpper(types[i].DatabaseTypeName())
switch v := vals[i].(type) {
case []byte:
s := string(v)
switch {
case strings.Contains(t, "TEXT") || strings.Contains(t, "CHAR") || t == "ENUM" || t == "TIME":
row[c] = s
case t == "DECIMAL" || t == "FLOAT" || t == "DOUBLE": // MySQL: NUMERIC = DECIMAL ( FLOAT, DOUBLE maybe not used )
num, err := strconv.ParseFloat(s, 64) //nostyle:repetition
if err != nil {
return fmt.Errorf("invalid column: evaluated %s, but got %s(%v): %w", c, t, s, err)
}
row[c] = num
case t == "DATE" || t == "TIMESTAMP" || t == "DATETIME": // MySQL(SSH port fowarding)
d, err := dateparse.ParseStrict(s)
if err != nil {
return fmt.Errorf("invalid column: evaluated %s, but got %s(%v): %w", c, t, s, err)
}
row[c] = d
case t == "JSONB": // PostgreSQL JSONB
var jsonColumn map[string]any
err = json.Unmarshal(v, &jsonColumn)
if err != nil {
return fmt.Errorf("invalid column: evaluated %s, but got %s(%v): %w", c, t, s, err)
}
row[c] = jsonColumn
default: // MySQL: BOOLEAN = TINYINT
num, err := strconv.Atoi(s) //nostyle:repetition
if err != nil {
return fmt.Errorf("invalid column: evaluated %s, but got %s(%v): %w", c, t, s, err)
}
row[c] = num
}
case string:
switch {
case t == "JSON": // Sqlite JSON
var jsonColumn map[string]any
err = json.Unmarshal([]byte(v), &jsonColumn)
if err != nil {
return fmt.Errorf("invalid column: evaluated %s, but got %s(%v): %w", c, t, v, err)
}
row[c] = jsonColumn
default:
row[c] = v
}
case float32: // MySQL: FLOAT
s := strconv.FormatFloat(float64(v), 'f', -1, 32)
num, err := strconv.ParseFloat(s, 64) //nostyle:repetition
if err != nil {
return fmt.Errorf("invalid column: evaluated %s, but got %s(%v): %w", c, t, v, err)
}
row[c] = num
case float64: // MySQL: DOUBLE
row[c] = v
default:
row[c] = v
}
}
rows = append(rows, row)
}
if err := r.Err(); err != nil {
return err
}
o.capturers.captureDBResponse(rnr.name, &DBResponse{
Columns: columns,
Rows: rows,
})
out = map[string]any{
string(dbStoreRowsKey): rows,
}
return nil
}()
if err != nil {
if err := tx.Rollback(); err != nil {
return err
}
return err
}
}
if err := tx.Commit(); err != nil {
return err
}
o.record(out)
return nil
}
func (q *dbQuery) generateTraceStmtComment(s *step) (string, error) {
if q.trace == nil || !*q.trace {
return "", nil
}
// Generate trace
t := newTrace(s)
// Trace structure to json
tj, err := json.Marshal(t)
if err != nil {
return "", err
}
// Generate trace comment
return fmt.Sprintf(" /* %s */", string(tj)), nil
}
func connectDB(dsn string) (TxQuerier, error) {
var (
db *sql.DB
err error
)
if strings.HasPrefix(dsn, "sp://") || strings.HasPrefix(dsn, "spanner://") {
// NOTE: go-sql-spanner trys to reuse the connection internally when the same DSN is specified.
key := atomic.AddUint64(&spannerInvalidatonKeyCounter, 1)
d := strings.Split(strings.Split(dsn, "://")[1], "/")
db, err = sql.Open("spanner", fmt.Sprintf(`projects/%s/instances/%s/databases/%s;workaroundConnectionInvalidationKey=%d`, d[0], d[1], d[2], key))
} else {
db, err = dburl.Open(normalizeDSN(dsn))
}
if err != nil {
return nil, err
}
nx, err := nestTx(db)
if err != nil {
return nil, err
}
return nx, nil
}
func nestTx(client Querier) (TxQuerier, error) {
switch c := client.(type) {
case *sql.DB:
return nest.Wrap(c), nil
case *sql.Tx:
if c == nil {
return nil, fmt.Errorf("invalid db client: %v", c)
}
var v reflect.Value = reflect.ValueOf(c).Elem()
var psv reflect.Value = v.FieldByName("db").Elem()
db := (*sql.DB)(unsafe.Pointer(psv.UnsafeAddr())) //nolint:gosec
return nest.Wrap(db), nil
default:
return nil, fmt.Errorf("invalid db client: %v", c)
}
}
// reInlineComment - regexp for inline comment.
var reInlineComment = regexp.MustCompile(`\/\*.*?\*\/`)
func isSELECTStmt(stmt string) bool {
stmt = strings.ToUpper(stmt)
if !strings.Contains(stmt, "SELECT") {
return false
}
lines := strings.Split(stmt, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "--") || strings.HasPrefix(line, "#") {
continue
}
line = strings.TrimSpace(reInlineComment.ReplaceAllString(line, ""))
if strings.HasPrefix(line, "SELECT") {
return true
}
}
return false
}
func isCommentOnlyStmt(stmt string) bool {
stmt = strings.ToUpper(stmt)
lines := strings.Split(stmt, "\n")
for _, line := range lines {
line = strings.TrimSpace(reInlineComment.ReplaceAllString(line, ""))
if line == "" {
continue
}
if strings.HasPrefix(line, "--") || strings.HasPrefix(line, "#") {
continue
}
return false
}
return true
}
func separateStmt(stmt string) []string {
stmt = strings.Trim(stmt, " \n\r")
if !strings.Contains(stmt, ";") {
return []string{stmt}
}
var (
stmts []string
s []rune
)
ins := false
ind := false
for _, c := range stmt {
s = append(s, c)
switch c {
case '\'':
ins = !ins
case '"':
ind = !ind
case ';':
if !ins && !ind {
stmts = append(stmts, strings.Trim(string(s), " \n\r"))
s = []rune{}
}
}
}
if len(s) > 0 {
cutset := " \n\\n\"" // When I receive a multi-line query with `key: |`, I get an unexplained string at the end. Therefore, remove it as a workaround.
l := strings.TrimRight(string(s), cutset)
if len(l) > 0 {
stmts = append(stmts, l)
}
}
return stmts
}
func init() {
if !contains(sql.Drivers(), "moderncsqlite") {
sql.Register("moderncsqlite", &sqlite.Driver{})
}
}