-
Notifications
You must be signed in to change notification settings - Fork 21
/
load.go
225 lines (212 loc) · 6.44 KB
/
load.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
package carta
import (
"database/sql"
"errors"
"fmt"
"reflect"
"github.com/jackskj/carta/value"
)
func (m *Mapper) loadRows(rows *sql.Rows, colTyps []*sql.ColumnType) (*resolver, error) {
defer rows.Close() // may not need
var err error
row := make([]interface{}, len(colTyps))
colTypNames := make([]string, len(colTyps))
for i := 0; i < len(colTyps); i++ {
colTypNames[i] = colTyps[i].DatabaseTypeName()
}
rsv := newResolver()
for rows.Next() {
for i := 0; i < len(colTyps); i++ {
row[i] = value.NewCell(colTypNames[i])
}
if err = rows.Scan(row...); err != nil {
return nil, err
}
if err = loadRow(m, row, rsv); err != nil {
return nil, err
}
}
return rsv, nil
}
// load row maps a single sql row onto a structure that resembles the users struct
// that mapping is stored in the resolver as a pointer reference to an instance of the struct
//
// if new object is foind, create a new instance of a struct that
// maps onto that struct,
// for example, if a user maps onto:
// type Blog struct {
// BlogId string
// }
// blogs := []Blog:
// carta.Map(rows, &blogs)
// if a new blog_id column value is found, I instantiatiate a new instance of Blog,
// set BlogId, then store the pointer referenct to this instance in the resolver
// nothins is done when the object has been already mapped in previous rows, however,
// the function contunous to recursivelly map rows for all sub mappings inside Blog
// for example, if a blog has many Authors
// rows are actually []*Cell, theu are passed here as interface since sql scan requires []interface{}
func loadRow(m *Mapper, row []interface{}, rsv *resolver) error {
var (
err error
dstField reflect.Value // destination field to be set with
cell *value.Cell
elem *element
found bool
)
uid := getUniqueId(row, m)
if elem, found = rsv.elements[uid]; !found {
// unique row mapping found, new object
loadElem := reflect.New(m.Typ).Elem()
for _, col := range m.PresentColumns {
var (
kind reflect.Kind // kind of destination
dst reflect.Value // destination to set
typ reflect.Type // underlying type of the destination
isDstPtr bool //is the destination a pointer
)
cell = row[col.columnIndex].(*value.Cell)
if m.IsBasic {
dst = loadElem
kind = m.Kind
typ = m.Typ
isDstPtr = m.IsTypePtr
} else {
dstField = loadElem.Field(int(col.i))
if m.Fields[col.i].IsPtr {
dst = reflect.New(m.Fields[col.i].ElemTyp).Elem()
kind = m.Fields[col.i].ElemKind
typ = m.Fields[col.i].ElemTyp
isDstPtr = true
} else {
dst = dstField
kind = m.Fields[col.i].Kind
typ = m.Fields[col.i].Typ
isDstPtr = false
}
}
if cell.IsNull() {
_, nullable := value.NullableTypes[typ]
if !(isDstPtr || nullable) {
return errors.New(fmt.Sprintf("carta: cannot load null value to type %s for column %s", typ, col.name))
}
// no need to set destination if cell is null
} else {
switch kind {
case reflect.Bool:
if d, err := cell.Bool(); err != nil {
return value.ConvertsionError(err, typ)
} else {
dst.SetBool(d)
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if d, err := cell.Uint64(); err != nil {
return value.ConvertsionError(err, typ)
} else {
dst.SetUint(d)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if d, err := cell.Int64(); err != nil {
return value.ConvertsionError(err, typ)
} else {
dst.SetInt(d)
}
case reflect.String:
if d, err := cell.String(); err != nil {
return value.ConvertsionError(err, typ)
} else {
dst.SetString(d)
}
case reflect.Float32, reflect.Float64:
if d, err := cell.Float64(); err != nil {
return value.ConvertsionError(err, typ)
} else {
dst.SetFloat(d)
}
case reflect.Struct:
if strTyp, ok := value.BasicTypes[typ]; ok {
// TODO: Type asserion, prevent from calling ValueOf
// TODO: make these stupid error checks more concise
// this swich statement should be optimized
switch strTyp {
case value.Time:
if d, err := cell.Time(); err != nil {
return value.ConvertsionError(err, typ)
} else {
dst.Set(reflect.ValueOf(d))
}
case value.Timestamp:
if d, err := cell.Timestamp(); err != nil {
return value.ConvertsionError(err, typ)
} else {
dst.Set(reflect.ValueOf(d))
}
case value.NullBool:
if d, err := cell.NullBool(); err != nil {
return value.ConvertsionError(err, typ)
} else {
dst.Set(reflect.ValueOf(d))
}
case value.NullFloat64:
if d, err := cell.NullFloat64(); err != nil {
return value.ConvertsionError(err, typ)
} else {
dst.Set(reflect.ValueOf(d))
}
case value.NullInt32:
if d, err := cell.NullInt32(); err != nil {
return value.ConvertsionError(err, typ)
} else {
dst.Set(reflect.ValueOf(d))
}
case value.NullInt64:
if d, err := cell.NullInt64(); err != nil {
return value.ConvertsionError(err, typ)
} else {
dst.Set(reflect.ValueOf(d))
}
case value.NullString:
if d, err := cell.NullString(); err != nil {
return value.ConvertsionError(err, typ)
} else {
dst.Set(reflect.ValueOf(d))
}
case value.NullTime:
if d, err := cell.NullTime(); err != nil {
return value.ConvertsionError(err, typ)
} else {
dst.Set(reflect.ValueOf(d))
}
}
}
}
if !m.IsBasic && m.Fields[col.i].IsPtr {
dstField.Set(dst.Addr())
}
}
}
elem = &element{v: loadElem}
if len(m.SubMaps) != 0 {
elem.subMaps = map[fieldIndex]*resolver{}
for i, _ := range m.SubMaps {
elem.subMaps[i] = newResolver()
}
}
rsv.elements[uid] = elem
rsv.elementOrder = append(rsv.elementOrder, uid)
}
for i, subMap := range m.SubMaps {
if err = loadRow(subMap, row, elem.subMaps[i]); err != nil {
return err
}
}
return nil
}
// Generates unique id based on the ancestors of the struct as well as currently considered colum values
func getUniqueId(row []interface{}, m *Mapper) uniqueValId {
// TODO: set capacity of the uid slice, using bytes.buffer
uid := ""
for _, i := range m.SortedColumnIndexes {
uid = uid + row[i].(*value.Cell).Uid()
}
return uniqueValId(uid)
}