-
Notifications
You must be signed in to change notification settings - Fork 9
/
dsc.go
148 lines (133 loc) · 3.87 KB
/
dsc.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
package dsunit
import (
"github.com/pkg/errors"
"github.com/viant/dsc"
"github.com/viant/toolbox"
"strings"
"time"
)
type datasetDmlProvider struct {
*dsc.DmlBuilder
}
func (p *datasetDmlProvider) record(instance interface{}) *map[string]interface{} {
switch result := instance.(type) {
case map[string]interface{}:
return &result
case *map[string]interface{}:
return result
}
return nil
}
func (p *datasetDmlProvider) Key(instance interface{}) []interface{} {
record := instance.(map[string]interface{})
var result = make([]interface{}, 0)
for _, column := range p.TableDescriptor.PkColumns {
var value = record[column]
if toolbox.IsFloat(value) {
value = toolbox.AsInt(value)
}
result = append(result, value)
}
return result
}
func (p *datasetDmlProvider) SetKey(instance interface{}, seq int64) {
record := p.record(instance)
key := p.TableDescriptor.PkColumns[0]
(*record)[key] = seq
}
func (p *datasetDmlProvider) Get(sqlType int, instance interface{}) *dsc.ParametrizedSQL {
record := p.record(instance)
return p.GetParametrizedSQL(sqlType, func(column string) interface{} {
return (*record)[column]
})
}
func newDatasetDmlProvider(dmlBuilder *dsc.DmlBuilder) *datasetDmlProvider {
return &datasetDmlProvider{dmlBuilder}
}
type datasetRowMapper struct {
dsc.Scanner
valueProviders []func(slice []interface{}, index int)
columns []string
columnToIndexMap map[string]int
}
func (m *datasetRowMapper) Map(scanner dsc.Scanner) (interface{}, error) {
m.Scanner = scanner
columnValues, columns, err := dsc.ScanRow(m)
if err != nil {
return nil, err
}
var values = make(map[string]interface{})
for i, item := range columnValues {
values[columns[i]] = item
}
return &values, nil
}
func (m *datasetRowMapper) buildProviders(types map[string]dsc.Column) []func(slice []interface{}, index int) {
valueProvider := []func(slice []interface{}, index int){}
if len(types) == 0 || len(m.columns) == 0 {
return valueProvider
}
for _, column := range m.columns {
info, ok := types[column]
if !ok {
info = dsc.NewSimpleColumn(column, "string")
}
dbTypeName := info.DatabaseTypeName()
switch strings.ToUpper(dbTypeName) {
case "VARCHAR", "VARCHAR2", "CHAR", "STRING", "TEXT", "S":
valueProvider = append(valueProvider, func(slice []interface{}, index int) {
var value *string
slice[index] = &value
})
case "DATE", "DATETIME", "TIMESTAMP":
valueProvider = append(valueProvider, func(slice []interface{}, index int) {
var value *time.Time
slice[index] = &value
})
case "INT", "INTEGER", "BIGINT", "TINYINT", "INT64", "N":
valueProvider = append(valueProvider, func(slice []interface{}, index int) {
var value *int
slice[index] = &value
})
case "FLOAT", "FLOAT64", "DECIMAL", "NUMERIC":
valueProvider = append(valueProvider, func(slice []interface{}, index int) {
var value *float64
slice[index] = &value
})
default:
valueProvider = append(valueProvider, func(slice []interface{}, index int) {
var value interface{}
slice[index] = &value
})
}
}
return valueProvider
}
func (m *datasetRowMapper) ColumnValues() ([]interface{}, error) {
if len(m.valueProviders) == 0 {
return nil, errors.New("column values not supported")
}
var result = make([]interface{}, len(m.columns))
for i, provider := range m.valueProviders {
provider(result, i)
}
return result, nil
}
func newDatasetRowMapper(columns []string, types []dsc.Column) dsc.RecordMapper {
var columnToIndexMap = make(map[string]int)
for index, column := range columns {
columnToIndexMap[column] = index
}
var result = &datasetRowMapper{
columns: columns,
columnToIndexMap: columnToIndexMap,
}
if len(types) > 0 {
indexTypes := make(map[string]dsc.Column)
for _, column := range types {
indexTypes[column.Name()] = column
}
result.valueProviders = result.buildProviders(indexTypes)
}
return result
}