-
-
Notifications
You must be signed in to change notification settings - Fork 233
/
model_map_slice.go
162 lines (130 loc) · 2.6 KB
/
model_map_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
package bun
import (
"context"
"database/sql"
"errors"
"sort"
"github.com/uptrace/bun/dialect/feature"
"github.com/uptrace/bun/schema"
)
type mapSliceModel struct {
mapModel
dest *[]map[string]interface{}
keys []string
}
var _ Model = (*mapSliceModel)(nil)
func newMapSliceModel(db *DB, dest *[]map[string]interface{}) *mapSliceModel {
return &mapSliceModel{
mapModel: mapModel{
db: db,
},
dest: dest,
}
}
func (m *mapSliceModel) Value() interface{} {
return m.dest
}
func (m *mapSliceModel) SetCap(cap int) {
if cap > 100 {
cap = 100
}
if slice := *m.dest; len(slice) < cap {
*m.dest = make([]map[string]interface{}, 0, cap)
}
}
func (m *mapSliceModel) ScanRows(ctx context.Context, rows *sql.Rows) (int, error) {
columns, err := rows.Columns()
if err != nil {
return 0, err
}
m.rows = rows
m.columns = columns
dest := makeDest(m, len(columns))
slice := *m.dest
if len(slice) > 0 {
slice = slice[:0]
}
var n int
for rows.Next() {
m.m = make(map[string]interface{}, len(m.columns))
m.scanIndex = 0
if err := rows.Scan(dest...); err != nil {
return 0, err
}
slice = append(slice, m.m)
n++
}
if err := rows.Err(); err != nil {
return 0, err
}
*m.dest = slice
return n, nil
}
func (m *mapSliceModel) appendColumns(fmter schema.Formatter, b []byte) (_ []byte, err error) {
if err := m.initKeys(); err != nil {
return nil, err
}
for i, k := range m.keys {
if i > 0 {
b = append(b, ", "...)
}
b = fmter.AppendIdent(b, k)
}
return b, nil
}
func (m *mapSliceModel) appendValues(fmter schema.Formatter, b []byte) (_ []byte, err error) {
if err := m.initKeys(); err != nil {
return nil, err
}
slice := *m.dest
b = append(b, "VALUES "...)
if m.db.HasFeature(feature.ValuesRow) {
b = append(b, "ROW("...)
} else {
b = append(b, '(')
}
if fmter.IsNop() {
for i := range m.keys {
if i > 0 {
b = append(b, ", "...)
}
b = append(b, '?')
}
return b, nil
}
for i, el := range slice {
if i > 0 {
b = append(b, "), "...)
if m.db.HasFeature(feature.ValuesRow) {
b = append(b, "ROW("...)
} else {
b = append(b, '(')
}
}
for j, key := range m.keys {
if j > 0 {
b = append(b, ", "...)
}
b = schema.Append(fmter, b, el[key])
}
}
b = append(b, ')')
return b, nil
}
func (m *mapSliceModel) initKeys() error {
if m.keys != nil {
return nil
}
slice := *m.dest
if len(slice) == 0 {
return errors.New("bun: map slice is empty")
}
first := slice[0]
keys := make([]string, 0, len(first))
for k := range first {
keys = append(keys, k)
}
sort.Strings(keys)
m.keys = keys
return nil
}