-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rows.go
152 lines (134 loc) · 3.74 KB
/
rows.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
package pgxpoolgo
import (
"fmt"
"github.com/jackc/pgconn"
"github.com/jackc/pgproto3/v2"
"github.com/jackc/pgx/v4"
"reflect"
)
/*
The codes below is based on `pgxmock` from `github.com/pashagolub/pgxmock`
*/
type MockRows struct {
commandTag pgconn.CommandTag
defs []pgproto3.FieldDescription
rows [][]interface{}
index int
scanErr map[int]error
}
type rows struct {
rows []*MockRows
index int
}
// NewMockRows mocks pgx.Rows.
func NewMockRows(columns []string) *MockRows {
var coldefs []pgproto3.FieldDescription
for _, column := range columns {
coldefs = append(coldefs, pgproto3.FieldDescription{Name: []byte(column)})
}
return &MockRows{
defs: coldefs,
scanErr: make(map[int]error),
}
}
func (mr *MockRows) ScanError(rowIndex int, err error) *MockRows {
mr.scanErr[rowIndex] = err
return mr
}
func (mr *MockRows) AddRow(values ...interface{}) *MockRows {
if len(values) != len(mr.defs) {
panic("expected number of values to match number of columns")
}
newRow := make([]interface{}, len(mr.defs))
copy(newRow, values)
mr.rows = append(mr.rows, newRow)
return mr
}
func (mr *MockRows) AddCommandTag(tag pgconn.CommandTag) *MockRows {
mr.commandTag = tag
return mr
}
func (mr *MockRows) Compose() pgx.Rows {
return &rows{rows: []*MockRows{mr}}
}
func (r *rows) Close() {}
func (r *rows) Err() error {
currentRow := r.rows[r.index]
return currentRow.scanErr[currentRow.index-1]
}
func (r *rows) CommandTag() pgconn.CommandTag {
return r.rows[r.index].commandTag
}
func (r *rows) FieldDescriptions() []pgproto3.FieldDescription {
return r.rows[r.index].defs
}
func (r *rows) Next() bool {
currentRow := r.rows[r.index]
currentRow.index++
return currentRow.index <= len(currentRow.rows)
}
func (r *rows) Scan(dest ...interface{}) error {
currentRow := r.rows[r.index]
if currentRow.scanErr[currentRow.index-1] != nil {
return currentRow.scanErr[currentRow.index-1]
}
if len(dest) != len(currentRow.defs) {
return fmt.Errorf("incorrect argument number %d for columns %d", len(dest), len(currentRow.defs))
}
for i, col := range currentRow.rows[currentRow.index-1] {
if dest[i] == nil {
continue
}
destVal := reflect.ValueOf(dest[i])
if destVal.Kind() != reflect.Ptr {
return fmt.Errorf("destination argument must be a pointer for column %s", currentRow.defs[i].Name)
}
if col == nil {
dest[i] = nil
continue
}
val := reflect.ValueOf(col)
if _, ok := dest[i].(*interface{}); ok || destVal.Elem().Kind() == val.Kind() {
if destElem := destVal.Elem(); destElem.CanSet() {
destElem.Set(val)
} else {
return fmt.Errorf("cannot set destination value for column %s", string(currentRow.defs[i].Name))
}
} else {
scanner, ok := destVal.Interface().(interface{ Scan(interface{}) error })
if !ok {
return fmt.Errorf("destination kind '%v' not supported for value kind '%v' of column '%s'",
destVal.Elem().Kind(), val.Kind(), string(currentRow.defs[i].Name))
}
if err := scanner.Scan(val.Interface()); err != nil {
return fmt.Errorf("scanning value error for column '%s': %w", string(currentRow.defs[i].Name), err)
}
}
}
return nil
}
func (r *rows) Values() ([]interface{}, error) {
currentRow := r.rows[r.index]
return currentRow.rows[currentRow.index-1], currentRow.scanErr[currentRow.index-1]
}
func (r *rows) RawValues() [][]byte {
currentRow := r.rows[r.index]
dest := make([][]byte, len(currentRow.defs))
for i, col := range currentRow.rows[currentRow.index-1] {
if b, ok := rawBytes(col); ok {
dest[i] = b
continue
}
dest[i] = col.([]byte)
}
return dest
}
func rawBytes(col interface{}) (_ []byte, ok bool) {
val, ok := col.([]byte)
if !ok || len(val) == 0 {
return nil, false
}
b := make([]byte, len(val))
copy(b, val)
return b, true
}