forked from k1LoW/runn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
275 lines (254 loc) · 6.41 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
package runn
import (
"context"
"database/sql"
"fmt"
"reflect"
"strconv"
"strings"
"unsafe"
"github.com/araddon/dateparse"
"github.com/goccy/go-json"
"github.com/golang-sql/sqlexp"
"github.com/golang-sql/sqlexp/nest"
_ "github.com/googleapis/go-sql-spanner"
"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
client TxQuerier
operator *operator
}
type dbQuery struct {
stmt string
}
type DBResponse struct {
LastInsertID int64
RowsAffected int64
Columns []string
Rows []map[string]any
}
func newDBRunner(name, dsn string) (*dbRunner, error) {
var db *sql.DB
var err error
if strings.HasPrefix(dsn, "sp://") || strings.HasPrefix(dsn, "spanner://") {
d := strings.Split(strings.Split(dsn, "://")[1], "/")
db, err = sql.Open("spanner", fmt.Sprintf(`projects/%s/instances/%s/databases/%s`, d[0], d[1], d[2]))
} else {
db, err = dburl.Open(normalizeDSN(dsn))
}
if err != nil {
return nil, err
}
nx, err := nestTx(db)
if err != nil {
return nil, err
}
return &dbRunner{
name: name,
client: nx,
}, nil
}
var dsnRep = strings.NewReplacer("sqlite://", "moderncsqlite://", "sqlite3://", "moderncsqlite://", "sq://", "moderncsqlite://")
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, q *dbQuery) error {
stmts := separateStmt(q.stmt)
out := map[string]any{}
tx, err := rnr.client.BeginTx(ctx, &sql.TxOptions{})
if err != nil {
return err
}
for _, stmt := range stmts {
rnr.operator.capturers.captureDBStatement(rnr.name, stmt)
err := func() error {
if !strings.HasPrefix(strings.ToUpper(stmt), "SELECT") {
// exec
r, err := tx.ExecContext(ctx, stmt)
if err != nil {
return err
}
id, _ := r.LastInsertId()
a, _ := r.RowsAffected()
out = map[string]any{
string(dbStoreLastInsertIDKey): id,
string(dbStoreRowsAffectedKey): a,
}
rnr.operator.capturers.captureDBResponse(rnr.name, &DBResponse{
LastInsertID: id,
RowsAffected: a,
})
return nil
}
// query
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 == "TIME": // MySQL8: ENUM = CHAR
row[c] = s
case t == "DECIMAL" || t == "FLOAT" || t == "DOUBLE": // MySQL: NUMERIC = DECIMAL
num, err := strconv.ParseFloat(s, 64)
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)
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
}
default:
// MySQL8: DATE, TIMESTAMP, DATETIME
row[c] = v
}
}
rows = append(rows, row)
}
if err := r.Err(); err != nil {
return err
}
rnr.operator.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
}
rnr.operator.record(out)
return 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()))
return nest.Wrap(db), nil
default:
return nil, fmt.Errorf("invalid db client: %v", c)
}
}
func separateStmt(stmt string) []string {
if !strings.Contains(stmt, ";") {
return []string{stmt}
}
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"))
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{})
}
}