-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathmysql.go
More file actions
219 lines (199 loc) · 5.19 KB
/
mysql.go
File metadata and controls
219 lines (199 loc) · 5.19 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
package sql
import (
"database/sql"
"fmt"
"strconv"
"strings"
"time"
"github.com/go-sql-driver/mysql"
"github.com/influxdata/flux"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/values"
)
type MySQLRowReader struct {
Cursor *sql.Rows
columns []interface{}
columnTypes []flux.ColType
columnNames []string
NextFunc func() bool
CloseFunc func() error
}
// Next prepares MySQLRowReader to return rows
func (m *MySQLRowReader) Next() bool {
if m.NextFunc != nil {
return m.NextFunc()
}
next := m.Cursor.Next()
if next {
columnNames, err := m.Cursor.Columns()
if err != nil {
return false
}
m.columns = make([]interface{}, len(columnNames))
columnPointers := make([]interface{}, len(columnNames))
for i := 0; i < len(columnNames); i++ {
columnPointers[i] = &m.columns[i]
}
if err := m.Cursor.Scan(columnPointers...); err != nil {
return false
}
}
return next
}
func (m *MySQLRowReader) GetNextRow() ([]values.Value, error) {
row := make([]values.Value, len(m.columns))
for i, col := range m.columns {
switch col := col.(type) {
case bool, int64, uint64, float64, string:
row[i] = values.New(col)
case []uint8:
// Hack for MySQL, might need to work with charset?
// Can't do boolean with MySQL - stores BOOLEANs as TINYINTs (0 or 1)
// No way to distinguish if intended int or bool
switch m.columnTypes[i] {
case flux.TInt:
newInt, err := UInt8ToInt64(col)
if err != nil {
return nil, err
}
row[i] = values.NewInt(newInt)
case flux.TFloat:
newFloat, err := UInt8ToFloat(col)
if err != nil {
return nil, err
}
row[i] = values.NewFloat(newFloat)
case flux.TTime:
t, err := time.Parse(layout, string(col))
if err != nil {
fmt.Print(err)
}
row[i] = values.NewTime(values.ConvertTime(t))
default:
row[i] = values.NewString(string(col))
}
case time.Time:
row[i] = values.NewTime(values.ConvertTime(col))
case nil:
row[i] = values.NewNull(flux.SemanticType(m.columnTypes[i]))
default:
execute.PanicUnknownType(flux.TInvalid)
}
}
return row, nil
}
func (m *MySQLRowReader) InitColumnNames(names []string) {
m.columnNames = names
}
func (m *MySQLRowReader) InitColumnTypes(types []*sql.ColumnType) {
stringTypes := make([]flux.ColType, len(types))
for i := 0; i < len(types); i++ {
switch types[i].DatabaseTypeName() {
case "INT", "BIGINT", "SMALLINT", "TINYINT":
stringTypes[i] = flux.TInt
case "FLOAT", "DOUBLE":
stringTypes[i] = flux.TFloat
case "DATETIME":
stringTypes[i] = flux.TTime
default:
stringTypes[i] = flux.TString
}
}
m.columnTypes = stringTypes
}
func (m *MySQLRowReader) ColumnNames() []string {
return m.columnNames
}
func (m *MySQLRowReader) ColumnTypes() []flux.ColType {
return m.columnTypes
}
func (m *MySQLRowReader) SetColumnTypes(types []flux.ColType) {
m.columnTypes = types
}
func (m *MySQLRowReader) SetColumns(i []interface{}) {
m.columns = i
}
func (m *MySQLRowReader) Close() error {
if m.CloseFunc != nil {
return m.CloseFunc()
}
if err := m.Cursor.Err(); err != nil {
return err
}
return m.Cursor.Close()
}
func UInt8ToFloat(a []uint8) (float64, error) {
str := string(a)
s, err := strconv.ParseFloat(str, 64)
if err != nil {
return s, err
}
return s, nil
}
func UInt8ToInt64(a []uint8) (int64, error) {
str := string(a)
s, err := strconv.ParseInt(str, 0, 64)
if err != nil {
return s, err
}
return s, nil
}
func NewMySQLRowReader(r *sql.Rows) (execute.RowReader, error) {
reader := &MySQLRowReader{
Cursor: r,
}
cols, err := r.Columns()
if err != nil {
return nil, err
}
reader.InitColumnNames(cols)
types, err := r.ColumnTypes()
if err != nil {
return nil, err
}
reader.InitColumnTypes(types)
return reader, nil
}
// MysqlTranslateColumn translates flux colTypes into their corresponding MySQL column type
func MysqlColumnTranslateFunc() translationFunc {
c := map[string]string{
flux.TFloat.String(): "FLOAT",
flux.TInt.String(): "BIGINT",
flux.TUInt.String(): "BIGINT",
flux.TString.String(): "TEXT(16383)",
flux.TTime.String(): "DATETIME",
flux.TBool.String(): "BOOL",
// BOOL is a synonym supplied by MySQL for "convenience", and MYSQL turns this into a TINYINT type under the hood
// which means that looking at the schema afterwards shows the columntype as TINYINT, and not bool!
}
return func(f flux.ColType, colName string) (string, error) {
s, found := c[f.String()]
if !found {
return "", errors.Newf(codes.Internal, "MySQL does not support column type %s", f.String())
}
return mysqlQuoteIdent(colName) + " " + s, nil
}
}
func mysqlQuoteIdent(name string) string {
return fmt.Sprintf("`%s`", strings.ReplaceAll(name, "`", "``"))
}
func mysqlOpenFunction(dataSourceName string) openFunc {
return func(deps flux.Dependencies) (*sql.DB, error) {
cfg, err := mysql.ParseDSN(dataSourceName)
if err != nil {
return nil, err
}
dialer, err := deps.Dialer()
if err != nil {
return nil, err
}
cfg.DialFunc = dialer.DialContext
connector, err := mysql.NewConnector(cfg)
if err != nil {
return nil, err
}
return sql.OpenDB(connector), nil
}
}