/
sql_helper.go
484 lines (414 loc) · 11 KB
/
sql_helper.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package framework
import (
"context"
"database/sql"
"fmt"
"strings"
"sync/atomic"
"time"
"github.com/jmoiron/sqlx"
"github.com/pingcap/errors"
"github.com/pingcap/log"
"github.com/pingcap/tiflow/pkg/quotes"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"upper.io/db.v3/lib/sqlbuilder"
_ "upper.io/db.v3/mysql" // imported for side effects
)
// SQLHelper provides basic utilities for manipulating data
type SQLHelper struct {
upstream *sql.DB
downstream *sql.DB
ctx context.Context
}
// Table represents the handle of a table in the upstream
type Table struct {
err error
tableName string
uniqueIndex []string
helper *SQLHelper
}
// GetTable returns the handle of the given table
func (h *SQLHelper) GetTable(tableName string) *Table {
db, err := sqlbuilder.New("mysql", h.upstream)
if err != nil {
return &Table{err: errors.AddStack(err)}
}
idxCol, err := getUniqueIndexColumn(h.ctx, db, "testdb", tableName)
if err != nil {
return &Table{err: errors.AddStack(err)}
}
return &Table{tableName: tableName, uniqueIndex: idxCol, helper: h}
}
func (t *Table) makeSQLRequest(requestType sqlRequestType, rowData map[string]interface{}) (*sqlRequest, error) {
if t.err != nil {
return nil, t.err
}
return &sqlRequest{
tableName: t.tableName,
data: rowData,
result: nil,
uniqueIndex: t.uniqueIndex,
helper: t.helper,
requestType: requestType,
}, nil
}
// Insert returns a Sendable object that represents an Insert clause
func (t *Table) Insert(rowData map[string]interface{}) Sendable {
basicReq, err := t.makeSQLRequest(sqlRequestTypeInsert, rowData)
if err != nil {
return &errorSender{err: err}
}
return &syncSQLRequest{*basicReq}
}
// Upsert returns a Sendable object that represents a Replace Into clause
func (t *Table) Upsert(rowData map[string]interface{}) Sendable {
basicReq, err := t.makeSQLRequest(sqlRequestTypeUpsert, rowData)
if err != nil {
return &errorSender{err: err}
}
return &syncSQLRequest{*basicReq}
}
// Delete returns a Sendable object that represents a Delete from clause
func (t *Table) Delete(rowData map[string]interface{}) Sendable {
basicReq, err := t.makeSQLRequest(sqlRequestTypeDelete, rowData)
if err != nil {
return &errorSender{err: err}
}
return &syncSQLRequest{*basicReq}
}
type sqlRowContainer interface {
getData() map[string]interface{}
getComparableKey() string
getTable() *Table
}
type awaitableSQLRowContainer struct {
Awaitable
sqlRowContainer
}
type sqlRequestType int32
const (
sqlRequestTypeInsert sqlRequestType = iota
sqlRequestTypeUpsert
sqlRequestTypeDelete
)
type sqlRequest struct {
tableName string
data map[string]interface{}
result map[string]interface{}
uniqueIndex []string
helper *SQLHelper
requestType sqlRequestType
hasReadBack uint32
}
// MarshalLogObjects helps printing the sqlRequest
func (s *sqlRequest) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddString("upstream", fmt.Sprintf("%#v", s.data))
encoder.AddString("downstream", fmt.Sprintf("%#v", s.result))
return nil
}
func (s *sqlRequest) getPrimaryKeyTuple() string {
return makeColumnTuple(s.uniqueIndex)
}
func (s *sqlRequest) getWhereCondition() []interface{} {
builder := strings.Builder{}
args := make([]interface{}, 1, len(s.uniqueIndex)+1)
builder.WriteString(s.getPrimaryKeyTuple() + " = (")
for i, col := range s.uniqueIndex {
builder.WriteString("?")
if i != len(s.uniqueIndex)-1 {
builder.WriteString(",")
}
args = append(args, s.data[col])
}
builder.WriteString(")")
args[0] = builder.String()
return args
}
func (s *sqlRequest) getComparableKey() string {
if len(s.uniqueIndex) == 1 {
return s.uniqueIndex[0]
}
ret := make(map[string]interface{})
for k, v := range s.data {
for _, col := range s.uniqueIndex {
if k == col {
ret[k] = v
}
}
}
return fmt.Sprintf("%v", ret)
}
func (s *sqlRequest) getData() map[string]interface{} {
return s.data
}
func (s *sqlRequest) getTable() *Table {
return &Table{
err: nil,
tableName: s.tableName,
uniqueIndex: s.uniqueIndex,
helper: s.helper,
}
}
func (s *sqlRequest) getAwaitableSQLRowContainer() *awaitableSQLRowContainer {
return &awaitableSQLRowContainer{
Awaitable: &basicAwaitable{
pollableAndCheckable: s,
timeout: 30 * time.Second,
},
sqlRowContainer: s,
}
}
// Sendable is a sendable request to the upstream
type Sendable interface {
Send() Awaitable
}
type errorSender struct {
err error
}
// Send implements sender
func (s *errorSender) Send() Awaitable {
return &errorCheckableAndAwaitable{s.err}
}
type syncSQLRequest struct {
sqlRequest
}
func (r *syncSQLRequest) Send() Awaitable {
atomic.StoreUint32(&r.hasReadBack, 0)
var err error
switch r.requestType {
case sqlRequestTypeInsert:
err = r.insert(r.helper.ctx)
case sqlRequestTypeUpsert:
err = r.upsert(r.helper.ctx)
case sqlRequestTypeDelete:
err = r.delete(r.helper.ctx)
}
go func() {
db, err := sqlbuilder.New("mysql", r.helper.upstream)
if err != nil {
log.Warn("ReadBack:", zap.Error(err))
return
}
cond := r.getWhereCondition()
rows, err := db.SelectFrom(r.tableName).Where(cond).QueryContext(r.helper.ctx)
if err != nil {
log.Warn("ReadBack:", zap.Error(err))
return
}
defer rows.Close()
if !rows.Next() {
// Upstream does not have the row
if r.requestType != sqlRequestTypeDelete {
log.Warn("ReadBack: no row, likely to be bug")
}
} else {
r.data, err = rowsToMap(rows)
if err != nil {
log.Warn("ReadBack", zap.Error(err))
return
}
}
atomic.StoreUint32(&r.hasReadBack, 1)
}()
if err != nil {
return &errorCheckableAndAwaitable{errors.AddStack(err)}
}
return r.getAwaitableSQLRowContainer()
}
/*
type asyncSQLRequest struct {
sqlRequest
}
*/
func (s *sqlRequest) insert(ctx context.Context) error {
db, err := sqlbuilder.New("mysql", s.helper.upstream)
if err != nil {
return errors.AddStack(err)
}
keys := make([]string, len(s.data))
values := make([]interface{}, len(s.data))
i := 0
for k, v := range s.data {
keys[i] = k
values[i] = v
i++
}
_, err = db.InsertInto(s.tableName).Columns(keys...).Values(values...).ExecContext(ctx)
if err != nil {
return errors.AddStack(err)
}
s.requestType = sqlRequestTypeInsert
return nil
}
func (s *sqlRequest) upsert(ctx context.Context) error {
db := sqlx.NewDb(s.helper.upstream, "mysql")
keys := make([]string, len(s.data))
values := make([]interface{}, len(s.data))
i := 0
for k, v := range s.data {
keys[i] = k
values[i] = v
i++
}
query, args, err := sqlx.In("replace into `"+s.tableName+"` "+makeColumnTuple(keys)+" values (?)", values)
if err != nil {
return errors.AddStack(err)
}
query = db.Rebind(query)
_, err = s.helper.upstream.ExecContext(ctx, query, args...)
if err != nil {
return errors.AddStack(err)
}
s.requestType = sqlRequestTypeUpsert
return nil
}
func (s *sqlRequest) delete(ctx context.Context) error {
db, err := sqlbuilder.New("mysql", s.helper.upstream)
if err != nil {
return errors.AddStack(err)
}
_, err = db.DeleteFrom(s.tableName).Where(s.getWhereCondition()).ExecContext(ctx)
if err != nil {
return errors.AddStack(err)
}
s.requestType = sqlRequestTypeDelete
return nil
}
func (s *sqlRequest) read(ctx context.Context) (map[string]interface{}, error) {
db, err := sqlbuilder.New("mysql", s.helper.downstream)
if err != nil {
return nil, errors.AddStack(err)
}
rows, err := db.SelectFrom(s.tableName).Where(s.getWhereCondition()).QueryContext(ctx)
if err != nil {
return nil, errors.AddStack(err)
}
defer rows.Close()
if !rows.Next() {
return nil, nil
}
return rowsToMap(rows)
}
//nolint:unused
func (s *sqlRequest) getBasicAwaitable() basicAwaitable {
return basicAwaitable{
pollableAndCheckable: s,
timeout: 0,
}
}
func (s *sqlRequest) poll(ctx context.Context) (bool, error) {
if atomic.LoadUint32(&s.hasReadBack) == 0 {
return false, nil
}
res, err := s.read(ctx)
if err != nil {
if strings.Contains(err.Error(), "Error 1146") {
return false, nil
}
return false, errors.AddStack(err)
}
s.result = res
switch s.requestType {
case sqlRequestTypeInsert:
if res == nil {
return false, nil
}
return true, nil
case sqlRequestTypeUpsert:
if res == nil {
return false, nil
}
if compareMaps(s.data, res) {
return true, nil
}
log.Debug("Upserted row does not match the expected")
return false, nil
case sqlRequestTypeDelete:
if res == nil {
return true, nil
}
log.Debug("Delete not successful yet", zap.Reflect("where", s.getWhereCondition()))
return false, nil
}
return true, nil
}
func (s *sqlRequest) Check() error {
if s.requestType == sqlRequestTypeUpsert || s.requestType == sqlRequestTypeDelete {
return nil
}
// TODO better comparator
if s.result == nil {
return errors.New("Check: nil result")
}
if compareMaps(s.data, s.result) {
return nil
}
log.Warn("Check failed", zap.Object("request", s))
return errors.New("Check failed")
}
func rowsToMap(rows *sql.Rows) (map[string]interface{}, error) {
colNames, err := rows.Columns()
if err != nil {
return nil, errors.AddStack(err)
}
colData := make([]interface{}, len(colNames))
colDataPtrs := make([]interface{}, len(colNames))
for i := range colData {
colDataPtrs[i] = &colData[i]
}
err = rows.Scan(colDataPtrs...)
if err != nil {
return nil, errors.AddStack(err)
}
ret := make(map[string]interface{}, len(colNames))
for i := 0; i < len(colNames); i++ {
ret[colNames[i]] = colData[i]
}
return ret, nil
}
func getUniqueIndexColumn(ctx context.Context, db sqlbuilder.Database, dbName string, tableName string) ([]string, error) {
row, err := db.QueryRowContext(ctx, `
SELECT GROUP_CONCAT(COLUMN_NAME SEPARATOR ' ') FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?
GROUP BY INDEX_NAME
ORDER BY FIELD(INDEX_NAME,'PRIMARY') DESC
`, dbName, tableName)
if err != nil {
return nil, errors.AddStack(err)
}
colName := ""
err = row.Scan(&colName)
if err != nil {
return nil, errors.AddStack(err)
}
return strings.Split(colName, " "), nil
}
func compareMaps(m1 map[string]interface{}, m2 map[string]interface{}) bool {
// TODO better comparator
if m2 == nil {
return false
}
str1 := fmt.Sprintf("%v", m1)
str2 := fmt.Sprintf("%v", m2)
return str1 == str2
}
func makeColumnTuple(colNames []string) string {
colNamesQuoted := make([]string, len(colNames))
for i := range colNames {
colNamesQuoted[i] = quotes.QuoteName(colNames[i])
}
return "(" + strings.Join(colNamesQuoted, ",") + ")"
}