forked from rocketlaunchr/dataframe-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.go
362 lines (312 loc) · 10.4 KB
/
sql.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
// Copyright 2019-20 PJ Engineering and Business Solutions Pty. Ltd. All rights reserved.
package imports
import (
"context"
"database/sql"
"errors"
"fmt"
"strconv"
"time"
"github.com/padchin/dataframe-go"
rlSql "github.com/rocketlaunchr/mysql-go"
)
// Database is used to set the Database.
// Different databases have different syntax for placeholders etc.
type Database int
const (
// PostgreSQL database
PostgreSQL Database = 0
// MySQL database
MySQL Database = 1
)
type queryContexter1 interface {
QueryContext(ctx context.Context, args ...interface{}) (*sql.Rows, error)
}
type queryContexter2 interface {
QueryContext(ctx context.Context, args ...interface{}) (*rlSql.Rows, error)
}
type queryContexter3 interface {
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
}
type queryContexter4 interface {
QueryContext(ctx context.Context, query string, args ...interface{}) (*rlSql.Rows, error)
}
type rows interface {
Close() error
ColumnTypes() ([]*sql.ColumnType, error)
Columns() ([]string, error)
Err() error
Next() bool
NextResultSet() bool
Scan(dest ...interface{}) error
}
// SQLLoadOptions is likely to change.
type SQLLoadOptions struct {
// KnownRowCount is used to set the capacity of the underlying slices of the Dataframe.
// The maximum number of rows supported (on a 64-bit machine) is 9,223,372,036,854,775,807 (half of 64 bit range).
// Preallocating memory can provide speed improvements. Benchmarks should be performed for your use-case.
//
// WARNING: Some databases may allow tables to contain more rows than the maximum supported.
KnownRowCount *int
// DictateDataType is used to inform LoadFromSQL what the true underlying data type is for a given column name.
// The key must be the case-sensitive column name.
// The value for a given key must be of the data type of the data.
// eg. For a string use "". For a int64 use int64(0). What is relevant is the data type and not the value itself.
//
// NOTE: A custom Series must implement NewSerieser interface and be able to interpret strings to work.
DictateDataType map[string]interface{}
// Database is used to set the Database.
Database Database
// Query can be set to the sql stmt if a *sql.DB, *sql.TX, *sql.Conn or the equivalent from the mysql-go package is provided.
//
// See: https://godoc.org/github.com/rocketlaunchr/mysql-go
Query string
}
// LoadFromSQL will load data from a sql database.
// stmt must be a *sql.Stmt or the equivalent from the mysql-go package.
//
// See: https://godoc.org/github.com/rocketlaunchr/mysql-go#Stmt
func LoadFromSQL(ctx context.Context, stmt interface{}, options *SQLLoadOptions, args ...interface{}) (*dataframe.DataFrame, error) {
var (
init *dataframe.SeriesInit
database Database
row int
df *dataframe.DataFrame
)
if options != nil {
if options.KnownRowCount != nil {
init = &dataframe.SeriesInit{
Size: *options.KnownRowCount,
}
}
database = options.Database
if database != PostgreSQL && database != MySQL {
return nil, errors.New("invalid database")
}
}
var (
rows rows
err error
)
switch stmt := stmt.(type) {
case queryContexter1:
rows, err = stmt.QueryContext(ctx, args...)
case queryContexter2:
rows, err = stmt.QueryContext(ctx, args...)
case queryContexter3:
query := ""
if options != nil {
query = (*options).Query
}
rows, err = stmt.QueryContext(ctx, query, args...)
case queryContexter4:
query := ""
if options != nil {
query = (*options).Query
}
rows, err = stmt.QueryContext(ctx, query, args...)
default:
panic(fmt.Sprintf("interface conversion: %T is not a valid Stmt", stmt))
}
if err != nil {
return nil, err
}
defer rows.Close()
cols, _ := rows.ColumnTypes()
totalColumns := len(cols)
if totalColumns <= 0 {
return nil, errors.New("no series found")
}
// Create the dataframe
seriess := []dataframe.Series{}
for _, ct := range cols { // ct is ColumnType
name := ct.Name()
typ := ct.DatabaseTypeName()
// Check if data type is dictated and use if available
if options != nil && len(options.DictateDataType) > 0 {
if dtyp, exists := options.DictateDataType[name]; exists {
switch T := dtyp.(type) {
case float64:
seriess = append(seriess, dataframe.NewSeriesFloat64(name, init))
case int64, bool:
seriess = append(seriess, dataframe.NewSeriesInt64(name, init))
case string:
seriess = append(seriess, dataframe.NewSeriesString(name, init))
case time.Time:
seriess = append(seriess, dataframe.NewSeriesTime(name, init))
case dataframe.NewSerieser:
seriess = append(seriess, T.NewSeries(name, init))
case Converter:
switch T.ConcreteType.(type) {
case time.Time:
seriess = append(seriess, dataframe.NewSeriesTime(name, init))
default:
seriess = append(seriess, dataframe.NewSeriesGeneric(name, T.ConcreteType, init))
}
default:
seriess = append(seriess, dataframe.NewSeriesGeneric(name, typ, init))
}
continue
}
}
// Use typ if info is available
switch typ {
case "VARCHAR", "TEXT", "NVARCHAR", "MEDIUMTEXT", "LONGTEXT":
seriess = append(seriess, dataframe.NewSeriesString(name, init))
case "FLOAT", "FLOAT4", "FLOAT8", "DOUBLE", "DECIMAL", "NUMERIC":
seriess = append(seriess, dataframe.NewSeriesFloat64(name, init))
case "BOOL", "INT", "TINYINT", "INT2", "INT4", "INT8", "MEDIUMINT", "SMALLINT", "BIGINT":
seriess = append(seriess, dataframe.NewSeriesInt64(name, init))
case "DATETIME", "TIMESTAMP", "TIMESTAMPTZ":
seriess = append(seriess, dataframe.NewSeriesTime(name, init))
case "":
// Assume string
seriess = append(seriess, dataframe.NewSeriesString(name, init))
default: // assume string if info is not available
seriess = append(seriess, dataframe.NewSeriesString(name, init))
}
}
df = dataframe.NewDataFrame(seriess...)
for rows.Next() {
row++
rowData := make([]interface{}, totalColumns)
for i := range rowData {
rowData[i] = &[]byte{}
}
if err := rows.Scan(rowData...); err != nil {
return nil, err
}
insertVals := map[string]interface{}{}
for colID, elem := range rowData {
colType := cols[colID].DatabaseTypeName()
fieldName := cols[colID].Name()
var val *string
raw := elem.(*[]byte)
if !(raw == nil || *raw == nil) {
val = &[]string{string(*raw)}[0]
}
if val == nil {
insertVals[fieldName] = nil
continue
}
if options != nil && len(options.DictateDataType) > 0 {
if dtyp, exists := options.DictateDataType[fieldName]; exists {
switch T := dtyp.(type) {
case float64:
f, err := strconv.ParseFloat(*val, 64)
if err != nil {
return nil, fmt.Errorf("can't force string: %s to float64. row: %d field: %s", *val, row-1, fieldName)
}
insertVals[fieldName] = f
case int64:
n, err := strconv.ParseInt(*val, 10, 64)
if err != nil {
return nil, fmt.Errorf("can't force string: %s to Int. row: %d field: %s", *val, row-1, fieldName)
}
insertVals[fieldName] = n
case string:
insertVals[fieldName] = *val
case bool:
if *val == "true" || *val == "TRUE" || *val == "True" || *val == "1" {
insertVals[fieldName] = int64(1)
} else if *val == "false" || *val == "FALSE" || *val == "False" || *val == "0" {
insertVals[fieldName] = int64(0)
} else {
return nil, fmt.Errorf("can't force string: %s to bool. row: %d field: %s", *val, row-1, fieldName)
}
case time.Time:
layout := time.RFC3339 // Default for PostgreSQL
if database == MySQL {
layout = "2006-01-02 15:04:05"
}
t, err := time.Parse(layout, *val)
if err != nil {
// Assume unix timestamp
sec, err := strconv.ParseInt(*val, 10, 64)
if err != nil {
return nil, fmt.Errorf("can't force string: %s to time.Time (%s). row: %d field: %s", *val, layout, row-1, fieldName)
}
t = time.Unix(sec, 0)
}
insertVals[fieldName] = t
case dataframe.NewSerieser:
insertVals[fieldName] = *val
case Converter:
cv, err := T.ConverterFunc(*val)
if err != nil {
return nil, fmt.Errorf("can't force string: %s to generic data type. row: %d field: %s", *val, row-1, fieldName)
}
insertVals[fieldName] = cv
default:
insertVals[fieldName] = *val
}
continue
}
}
switch colType {
case "VARCHAR", "TEXT", "NVARCHAR", "MEDIUMTEXT", "LONGTEXT":
insertVals[fieldName] = *val
case "FLOAT", "DOUBLE", "DECIMAL", "NUMERIC", "FLOAT4", "FLOAT8":
f, err := strconv.ParseFloat(*val, 64)
if err != nil {
return nil, fmt.Errorf("can't force string: %s to float64. row: %d field: %s", *val, row-1, fieldName)
}
insertVals[fieldName] = f
case "INT", "TINYINT", "INT2", "INT4", "INT8", "MEDIUMINT", "SMALLINT", "BIGINT":
n, err := strconv.ParseInt(*val, 10, 64)
if err != nil {
return nil, fmt.Errorf("can't force string: %s to Int. row: %d field: %s", *val, row-1, fieldName)
}
insertVals[fieldName] = n
case "BOOL":
if *val == "true" || *val == "TRUE" || *val == "True" || *val == "1" {
insertVals[fieldName] = int64(1)
} else if *val == "false" || *val == "FALSE" || *val == "False" || *val == "0" {
insertVals[fieldName] = int64(0)
} else {
return nil, fmt.Errorf("can't force string: %s to bool. row: %d field: %s", *val, row-1, fieldName)
}
case "DATETIME", "TIMESTAMP", "TIMESTAMPTZ":
layout := time.RFC3339 // Default for PostgreSQL
if database == MySQL {
layout = "2006-01-02 15:04:05"
}
t, err := time.Parse(layout, *val)
if err != nil {
// Assume unix timestamp
sec, err := strconv.ParseInt(*val, 10, 64)
if err != nil {
return nil, fmt.Errorf("can't force string: %s to time.Time (%s). row: %d field: %s", *val, layout, row-1, fieldName)
}
t = time.Unix(sec, 0)
}
insertVals[fieldName] = t
default:
// Assume string
insertVals[fieldName] = *val
}
}
if init == nil {
df.Append(&dataframe.DontLock, make([]interface{}, len(df.Series))...)
}
df.UpdateRow(row-1, &dataframe.DontLock, insertVals)
}
if err := rows.Err(); err != nil {
return nil, err
}
if df == nil {
return nil, dataframe.ErrNoRows
}
// Remove unused preallocated rows from dataframe
if init != nil {
excess := init.Size - df.NRows()
for {
if excess <= 0 {
break
}
df.Remove(df.NRows() - 1) // remove current last row
excess--
}
}
return df, nil
}