forked from pingcap/dumpling
/
ir.go
107 lines (93 loc) · 2.42 KB
/
ir.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
// Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0.
package export
import (
"bytes"
"database/sql"
"strings"
"github.com/pingcap/errors"
tcontext "github.com/dbakit/dumpling/v5/context"
)
// TableDataIR is table data intermediate representation.
// A table may be split into multiple TableDataIRs.
type TableDataIR interface {
Start(*tcontext.Context, *sql.Conn) error
Rows() SQLRowIter
Close() error
RawRows() *sql.Rows
}
// TableMeta contains the meta information of a table.
type TableMeta interface {
DatabaseName() string
TableName() string
ColumnCount() uint
ColumnTypes() []string
ColumnNames() []string
SelectedField() string
SelectedLen() int
SpecialComments() StringIter
ShowCreateTable() string
ShowCreateView() string
AvgRowLength() uint64
HasImplicitRowID() bool
}
// SQLRowIter is the iterator on a collection of sql.Row.
type SQLRowIter interface {
Decode(RowReceiver) error
Next()
Error() error
HasNext() bool
// release SQLRowIter
Close() error
}
// RowReceiverStringer is a combined interface of RowReceiver and Stringer
type RowReceiverStringer interface {
RowReceiver
Stringer
}
// Stringer is an interface which represents sql types that support writing to buffer in sql/csv type
type Stringer interface {
WriteToBuffer(*bytes.Buffer, bool)
WriteToBufferInCsv(*bytes.Buffer, bool, *csvOption)
}
// RowReceiver is an interface which represents sql types that support bind address for *sql.Rows
type RowReceiver interface {
BindAddress([]interface{})
}
func decodeFromRows(rows *sql.Rows, args []interface{}, row RowReceiver) error {
row.BindAddress(args)
if err := rows.Scan(args...); err != nil {
rows.Close()
return errors.Trace(err)
}
return nil
}
// StringIter is the iterator on a collection of strings.
type StringIter interface {
Next() string
HasNext() bool
}
// MetaIR is the interface that wraps database/table/view's metadata
type MetaIR interface {
SpecialComments() StringIter
TargetName() string
MetaSQL() string
}
func setTableMetaFromRows(rows *sql.Rows) (TableMeta, error) {
tps, err := rows.ColumnTypes()
if err != nil {
return nil, errors.Trace(err)
}
nms, err := rows.Columns()
if err != nil {
return nil, errors.Trace(err)
}
for i := range nms {
nms[i] = wrapBackTicks(nms[i])
}
return &tableMeta{
colTypes: tps,
selectedField: strings.Join(nms, ","),
selectedLen: len(nms),
specCmts: []string{"/*!40101 SET NAMES binary*/;"},
}, nil
}