-
-
Notifications
You must be signed in to change notification settings - Fork 233
/
model_map.go
186 lines (150 loc) · 3.17 KB
/
model_map.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
package bun
import (
"bytes"
"context"
"database/sql"
"reflect"
"sort"
"github.com/uptrace/bun/schema"
)
type mapModel struct {
db *DB
dest *map[string]interface{}
m map[string]interface{}
rows *sql.Rows
columns []string
_columnTypes []*sql.ColumnType
scanIndex int
}
var _ Model = (*mapModel)(nil)
func newMapModel(db *DB, dest *map[string]interface{}) *mapModel {
m := &mapModel{
db: db,
dest: dest,
}
if dest != nil {
m.m = *dest
}
return m
}
func (m *mapModel) Value() interface{} {
return m.dest
}
func (m *mapModel) ScanRows(ctx context.Context, rows *sql.Rows) (int, error) {
if !rows.Next() {
return 0, rows.Err()
}
columns, err := rows.Columns()
if err != nil {
return 0, err
}
m.rows = rows
m.columns = columns
dest := makeDest(m, len(columns))
if m.m == nil {
m.m = make(map[string]interface{}, len(m.columns))
}
m.scanIndex = 0
if err := rows.Scan(dest...); err != nil {
return 0, err
}
*m.dest = m.m
return 1, nil
}
func (m *mapModel) Scan(src interface{}) error {
if _, ok := src.([]byte); !ok {
return m.scanRaw(src)
}
columnTypes, err := m.columnTypes()
if err != nil {
return err
}
scanType := columnTypes[m.scanIndex].ScanType()
switch scanType.Kind() {
case reflect.Interface:
return m.scanRaw(src)
case reflect.Slice:
if scanType.Elem().Kind() == reflect.Uint8 {
// Reference types such as []byte are only valid until the next call to Scan.
src := bytes.Clone(src.([]byte))
return m.scanRaw(src)
}
}
dest := reflect.New(scanType).Elem()
if err := schema.Scanner(scanType)(dest, src); err != nil {
return err
}
return m.scanRaw(dest.Interface())
}
func (m *mapModel) columnTypes() ([]*sql.ColumnType, error) {
if m._columnTypes == nil {
columnTypes, err := m.rows.ColumnTypes()
if err != nil {
return nil, err
}
m._columnTypes = columnTypes
}
return m._columnTypes, nil
}
func (m *mapModel) scanRaw(src interface{}) error {
columnName := m.columns[m.scanIndex]
m.scanIndex++
m.m[columnName] = src
return nil
}
func (m *mapModel) appendColumnsValues(fmter schema.Formatter, b []byte) []byte {
keys := make([]string, 0, len(m.m))
for k := range m.m {
keys = append(keys, k)
}
sort.Strings(keys)
b = append(b, " ("...)
for i, k := range keys {
if i > 0 {
b = append(b, ", "...)
}
b = fmter.AppendIdent(b, k)
}
b = append(b, ") VALUES ("...)
isTemplate := fmter.IsNop()
for i, k := range keys {
if i > 0 {
b = append(b, ", "...)
}
if isTemplate {
b = append(b, '?')
} else {
b = schema.Append(fmter, b, m.m[k])
}
}
b = append(b, ")"...)
return b
}
func (m *mapModel) appendSet(fmter schema.Formatter, b []byte) []byte {
keys := make([]string, 0, len(m.m))
for k := range m.m {
keys = append(keys, k)
}
sort.Strings(keys)
isTemplate := fmter.IsNop()
for i, k := range keys {
if i > 0 {
b = append(b, ", "...)
}
b = fmter.AppendIdent(b, k)
b = append(b, " = "...)
if isTemplate {
b = append(b, '?')
} else {
b = schema.Append(fmter, b, m.m[k])
}
}
return b
}
func makeDest(v interface{}, n int) []interface{} {
dest := make([]interface{}, n)
for i := range dest {
dest[i] = v
}
return dest
}