forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathir_impl.go
More file actions
404 lines (350 loc) · 8.13 KB
/
ir_impl.go
File metadata and controls
404 lines (350 loc) · 8.13 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
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
// Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0.
package export
import (
"database/sql"
"strings"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/br/pkg/version"
tcontext "github.com/pingcap/tidb/dumpling/context"
"go.uber.org/zap"
)
// rowIter implements the SQLRowIter interface.
// Note: To create a rowIter, please use `newRowIter()` instead of struct literal.
type rowIter struct {
rows *sql.Rows
hasNext bool
args []interface{}
}
func newRowIter(rows *sql.Rows, argLen int) *rowIter {
r := &rowIter{
rows: rows,
hasNext: false,
args: make([]interface{}, argLen),
}
r.hasNext = r.rows.Next()
return r
}
func (iter *rowIter) Close() error {
return iter.rows.Close()
}
func (iter *rowIter) Decode(row RowReceiver) error {
return decodeFromRows(iter.rows, iter.args, row)
}
func (iter *rowIter) Error() error {
return errors.Trace(iter.rows.Err())
}
func (iter *rowIter) Next() {
iter.hasNext = iter.rows.Next()
}
func (iter *rowIter) HasNext() bool {
return iter.hasNext
}
// multiQueriesChunkIter implements the SQLRowIter interface.
// Note: To create a rowIter, please use `newRowIter()` instead of struct literal.
type multiQueriesChunkIter struct {
tctx *tcontext.Context
conn *sql.Conn
rows *sql.Rows
hasNext bool
id int
queries []string
args []interface{}
err error
}
func newMultiQueryChunkIter(tctx *tcontext.Context, conn *sql.Conn, queries []string, argLen int) *multiQueriesChunkIter {
r := &multiQueriesChunkIter{
tctx: tctx,
conn: conn,
queries: queries,
id: 0,
args: make([]interface{}, argLen),
}
r.nextRows()
return r
}
func (iter *multiQueriesChunkIter) nextRows() {
if iter.id >= len(iter.queries) {
iter.hasNext = false
return
}
var err error
defer func() {
if err != nil {
iter.hasNext = false
iter.err = errors.Trace(err)
}
}()
tctx, conn := iter.tctx, iter.conn
// avoid the empty chunk
for iter.id < len(iter.queries) {
rows := iter.rows
if rows != nil {
if err = rows.Close(); err != nil {
return
}
if err = rows.Err(); err != nil {
return
}
}
tctx.L().Debug("try to start nextRows", zap.String("query", iter.queries[iter.id]))
rows, err = conn.QueryContext(tctx, iter.queries[iter.id])
if err != nil {
return
}
if err = rows.Err(); err != nil {
return
}
iter.id++
iter.rows = rows
iter.hasNext = iter.rows.Next()
if iter.hasNext {
return
}
}
}
func (iter *multiQueriesChunkIter) Close() error {
if iter.err != nil {
return iter.err
}
if iter.rows != nil {
return iter.rows.Close()
}
return nil
}
func (iter *multiQueriesChunkIter) Decode(row RowReceiver) error {
if iter.err != nil {
return iter.err
}
if iter.rows == nil {
return errors.Errorf("no valid rows found, id: %d", iter.id)
}
return decodeFromRows(iter.rows, iter.args, row)
}
func (iter *multiQueriesChunkIter) Error() error {
if iter.err != nil {
return iter.err
}
if iter.rows != nil {
return errors.Trace(iter.rows.Err())
}
return nil
}
func (iter *multiQueriesChunkIter) Next() {
if iter.err == nil {
iter.hasNext = iter.rows.Next()
if !iter.hasNext {
iter.nextRows()
}
}
}
func (iter *multiQueriesChunkIter) HasNext() bool {
return iter.hasNext
}
type stringIter struct {
idx int
ss []string
}
func newStringIter(ss ...string) StringIter {
return &stringIter{
idx: 0,
ss: ss,
}
}
func (m *stringIter) Next() string {
if m.idx >= len(m.ss) {
return ""
}
ret := m.ss[m.idx]
m.idx++
return ret
}
func (m *stringIter) HasNext() bool {
return m.idx < len(m.ss)
}
type tableData struct {
query string
rows *sql.Rows
colLen int
needColTypes bool
colTypes []string
SQLRowIter
}
func newTableData(query string, colLength int, needColTypes bool) *tableData {
return &tableData{
query: query,
colLen: colLength,
needColTypes: needColTypes,
}
}
func (td *tableData) Start(tctx *tcontext.Context, conn *sql.Conn) error {
tctx.L().Debug("try to start tableData", zap.String("query", td.query))
rows, err := conn.QueryContext(tctx, td.query)
if err != nil {
return errors.Annotatef(err, "sql: %s", td.query)
}
if err = rows.Err(); err != nil {
return errors.Annotatef(err, "sql: %s", td.query)
}
td.SQLRowIter = nil
td.rows = rows
if td.needColTypes {
ns, err := rows.Columns()
if err != nil {
return errors.Trace(err)
}
td.colLen = len(ns)
td.colTypes = make([]string, 0, td.colLen)
colTps, err := rows.ColumnTypes()
if err != nil {
return errors.Trace(err)
}
for _, c := range colTps {
td.colTypes = append(td.colTypes, c.DatabaseTypeName())
}
}
return nil
}
func (td *tableData) Rows() SQLRowIter {
// should be initialized lazily since it calls rows.Next() which might close the rows when
// there's nothing to read, causes code which relies on rows not closed to fail.
if td.SQLRowIter == nil {
td.SQLRowIter = newRowIter(td.rows, td.colLen)
}
return td.SQLRowIter
}
func (td *tableData) Close() error {
if td.SQLRowIter != nil {
// will close td.rows internally
return td.SQLRowIter.Close()
} else if td.rows != nil {
return td.rows.Close()
}
return nil
}
func (td *tableData) RawRows() *sql.Rows {
return td.rows
}
type tableMeta struct {
database string
table string
colTypes []*sql.ColumnType
selectedField string
selectedLen int
specCmts []string
showCreateTable string
showCreateView string
avgRowLength uint64
hasImplicitRowID bool
}
func (tm *tableMeta) ColumnTypes() []string {
colTypes := make([]string, len(tm.colTypes))
for i, ct := range tm.colTypes {
colTypes[i] = ct.DatabaseTypeName()
}
return colTypes
}
func (tm *tableMeta) ColumnNames() []string {
colNames := make([]string, len(tm.colTypes))
for i, ct := range tm.colTypes {
colNames[i] = ct.Name()
}
return colNames
}
func (tm *tableMeta) DatabaseName() string {
return tm.database
}
func (tm *tableMeta) TableName() string {
return tm.table
}
func (tm *tableMeta) ColumnCount() uint {
return uint(len(tm.colTypes))
}
func (tm *tableMeta) SelectedField() string {
return tm.selectedField
}
func (tm *tableMeta) SelectedLen() int {
return tm.selectedLen
}
func (tm *tableMeta) SpecialComments() StringIter {
return newStringIter(tm.specCmts...)
}
func (tm *tableMeta) ShowCreateTable() string {
return tm.showCreateTable
}
func (tm *tableMeta) ShowCreateView() string {
return tm.showCreateView
}
func (tm *tableMeta) AvgRowLength() uint64 {
return tm.avgRowLength
}
func (tm *tableMeta) HasImplicitRowID() bool {
return tm.hasImplicitRowID
}
type metaData struct {
target string
metaSQL string
specCmts []string
}
func (m *metaData) SpecialComments() StringIter {
return newStringIter(m.specCmts...)
}
func (m *metaData) TargetName() string {
return m.target
}
func (m *metaData) MetaSQL() string {
if !strings.HasSuffix(m.metaSQL, ";\n") {
m.metaSQL += ";\n"
}
return m.metaSQL
}
type multiQueriesChunk struct {
tctx *tcontext.Context
conn *sql.Conn
queries []string
colLen int
SQLRowIter
}
func newMultiQueriesChunk(queries []string, colLength int) *multiQueriesChunk {
return &multiQueriesChunk{
queries: queries,
colLen: colLength,
}
}
func (td *multiQueriesChunk) Start(tctx *tcontext.Context, conn *sql.Conn) error {
td.tctx = tctx
td.conn = conn
td.SQLRowIter = nil
return nil
}
func (td *multiQueriesChunk) Rows() SQLRowIter {
if td.SQLRowIter == nil {
td.SQLRowIter = newMultiQueryChunkIter(td.tctx, td.conn, td.queries, td.colLen)
}
return td.SQLRowIter
}
func (td *multiQueriesChunk) Close() error {
if td.SQLRowIter != nil {
return td.SQLRowIter.Close()
}
return nil
}
func (*multiQueriesChunk) RawRows() *sql.Rows {
return nil
}
var serverSpecialComments = map[version.ServerType][]string{
version.ServerTypeMySQL: {
"/*!40014 SET FOREIGN_KEY_CHECKS=0*/;",
"/*!40101 SET NAMES binary*/;",
},
version.ServerTypeTiDB: {
"/*!40014 SET FOREIGN_KEY_CHECKS=0*/;",
"/*!40101 SET NAMES binary*/;",
},
version.ServerTypeMariaDB: {
"/*!40101 SET NAMES binary*/;",
"SET FOREIGN_KEY_CHECKS=0;",
},
}
func getSpecialComments(serverType version.ServerType) []string {
return serverSpecialComments[serverType]
}