-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
input_slice.go
224 lines (206 loc) · 5.2 KB
/
input_slice.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
package trdsql
import (
"fmt"
"io"
"reflect"
)
// SliceReader is a structure for reading tabular data in memory.
// It can be used as the trdsql reader interface.
type SliceReader struct {
tableName string
names []string
types []string
data [][]any
}
// NewSliceReader takes a tableName and tabular data in memory
// and returns SliceReader.
// The tabular data that can be received is
// a one-dimensional array,
// a two-dimensional array,
// a map,
// and an array of structures.
func NewSliceReader(tableName string, args any) *SliceReader {
val := reflect.ValueOf(args)
if val.Kind() == reflect.Ptr {
val = reflect.Indirect(val)
}
// One-dimensional
switch val.Kind() {
case reflect.Map:
return mapReader(tableName, val)
case reflect.Struct:
return structReader(tableName, val)
case reflect.Slice:
return sliceReader(tableName, val)
default:
single := val.Interface()
data := [][]any{
{single},
}
names := []string{"c1"}
types := []string{typeToDBType(val.Kind())}
return &SliceReader{
tableName: tableName,
names: names,
types: types,
data: data,
}
}
}
func mapReader(tableName string, val reflect.Value) *SliceReader {
val = reflect.Indirect(val)
names := []string{"c1", "c2"}
keyType := val.MapKeys()[0].Kind()
valType := val.MapIndex(val.MapKeys()[0]).Kind()
types := []string{typeToDBType(keyType), typeToDBType(valType)}
data := make([][]any, 0)
for _, e := range val.MapKeys() {
data = append(data, []any{e.Interface(), val.MapIndex(e).Interface()})
}
return &SliceReader{
tableName: tableName,
names: names,
types: types,
data: data,
}
}
func structReader(tableName string, val reflect.Value) *SliceReader {
t := val.Type()
columnNum := t.NumField()
names := make([]string, columnNum)
types := make([]string, columnNum)
for i := 0; i < columnNum; i++ {
f := t.Field(i)
names[i] = f.Name
types[i] = typeToDBType(f.Type.Kind())
}
single := make([]any, t.NumField())
for j := 0; j < t.NumField(); j++ {
single[j] = fmt.Sprintf("%v", val.Field(j))
}
data := [][]any{
single,
}
return &SliceReader{
tableName: tableName,
names: names,
types: types,
data: data,
}
}
func sliceReader(tableName string, val reflect.Value) *SliceReader {
if val.Len() == 0 {
return &SliceReader{
tableName: tableName,
names: []string{"c1"},
types: []string{"text"},
data: nil,
}
}
switch val.Index(0).Kind() {
case reflect.Struct:
// {{ id: 1, name: "test"},{ id: 2, name: "test2"}}
return structSliceReader(tableName, val)
case reflect.Slice:
// {{1, "test"},{2, "test2"}}
return sliceSliceReader(tableName, val)
default:
// {{"a", "b", "c"}}
return interfaceSliceReader(tableName, val)
}
}
func structSliceReader(tableName string, val reflect.Value) *SliceReader {
length := val.Len()
t := val.Index(0).Type()
columnNum := t.NumField()
names := make([]string, columnNum)
types := make([]string, columnNum)
for i := 0; i < columnNum; i++ {
f := t.Field(i)
names[i] = f.Name
types[i] = typeToDBType(f.Type.Kind())
}
data := make([][]any, 0)
for i := 0; i < length; i++ {
rows := val.Index(i)
r := make([]any, rows.NumField())
for j := 0; j < rows.NumField(); j++ {
r[j] = fmt.Sprintf("%v", rows.Field(j))
}
data = append(data, r)
}
return &SliceReader{
tableName: tableName,
names: names,
types: types,
data: data,
}
}
func sliceSliceReader(tableName string, val reflect.Value) *SliceReader {
length := val.Len()
col := val.Index(0)
columnNum := col.Len()
names := make([]string, columnNum)
types := make([]string, columnNum)
for i := 0; i < columnNum; i++ {
names[i] = fmt.Sprintf("c%d", i+1)
colType := reflect.ValueOf(col.Index(i).Interface()).Kind()
types[i] = typeToDBType(colType)
}
data := make([][]any, 0)
for i := 0; i < length; i++ {
data = append(data, val.Index(i).Interface().([]any))
}
return &SliceReader{
tableName: tableName,
names: names,
types: types,
data: data,
}
}
func interfaceSliceReader(tableName string, val reflect.Value) *SliceReader {
v := val.Index(0).Interface()
length := val.Len()
t := reflect.ValueOf(v)
names := []string{"c1"}
types := []string{typeToDBType(t.Kind())}
data := make([][]any, length)
for i := 0; i < length; i++ {
data[i] = []any{val.Index(i).Interface()}
}
return &SliceReader{
tableName: tableName,
names: names,
types: types,
data: data,
}
}
// In sliceReader, only int type is passed to the database as int type.
func typeToDBType(t reflect.Kind) string {
switch t {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return "int"
default:
return DefaultDBType
}
}
// TableName returns Table name.
func (r *SliceReader) TableName() (string, error) {
return r.tableName, nil
}
// Names returns column names.
func (r *SliceReader) Names() ([]string, error) {
return r.names, nil
}
// Types returns column types.
func (r *SliceReader) Types() ([]string, error) {
return r.types, nil
}
// PreReadRow is returns entity of the data.
func (r *SliceReader) PreReadRow() [][]any {
return r.data
}
// ReadRow only returns EOF.
func (r *SliceReader) ReadRow(row []any) ([]any, error) {
return nil, io.EOF
}