-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrows.go
188 lines (168 loc) · 4.63 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
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
package pgxscan
import (
"errors"
sqlmaper "github.com/randallmlough/pgxscan/internal/sqlmapper"
"reflect"
"strings"
"github.com/jackc/pgx/v4"
)
type rows struct {
rows pgx.Rows
columns []string
cfg *Config
}
// Next prepares the next row for Scanning. See sql.Rows#Next for more
// information.
func (r *rows) Next() bool {
return r.rows.Next()
}
// Err returns the error, if any that was encountered during iteration. See
// sql.Rows#Err for more information.
func (r *rows) Err() error {
return r.rows.Err()
}
var ErrColumnNotateSyntax = errors.New("column notate syntax is invalid: expecting \"notate:name\"")
var QueryColumnNotatePrefix = "notate:"
// This function returns a list of column names from SQL columns.
//
// It renames SQL notated columns, when needed, for column names who start
// with 'notate:' prefix (you can override prefix with QueryColumnNotatePrefix
// variable)
//
// From columns:
//
// ["a", "b", "notate:whatever", "a", "b"]
//
// Will return:
//
// ["a", "b", "notate:whatever", "whatever.a", "whatever.b"]
//
// Background info:
//
// In order to allow complex queries and prevent having to expand on all column
// names for complex mappings, delimiter columns are used to notate results
// from postgres.
//
// Imagine we have `SELECT a.*, b.* FROM ...` if both _a_ and _b_ tables have
// a field name _id_ there would be no way for us to map it to a struct with
// a couple of nested structs, however, if we use column notation we can
// rewrite the query as:
//
// SELECT 0 as "notate:a",
// a.*,
// 0 as "notate:b",
// b.*
// FROM ...
//
// This way, everything that comes after column "notate:a" will be treated as if
// we would have defined an alias for each column named "a.<col>", and so on
//
// These notations allow zero (using "notate:" with nothing after colon),
// one level (like the example above) or many levels of notations (just do
// "notate:a.sub1.sub2")
//
// This helps map values to struct with simple queries without having to list
// all columns in the SQL.
func GetColumnNames(rows *pgx.Rows) ([]string, error) {
cols := make([]string, 0, len((*rows).FieldDescriptions()))
notatePrefix := ""
for _, field := range (*rows).FieldDescriptions() {
colName := string(field.Name)
// if starts by 'notate:' use what comes after that as the prefix for
// all column definitions moving forward
if strings.HasPrefix(colName, QueryColumnNotatePrefix) {
// "notate: a.b.c" -> ["notate:", " a.b.c"]
splitted := strings.Split(colName, QueryColumnNotatePrefix)
if len(splitted) != 2 {
return nil, ErrColumnNotateSyntax
}
// "a.b.c" or ""
trimmed := strings.TrimSpace(splitted[1])
if len(trimmed) == 0 {
notatePrefix = ""
} else {
notatePrefix = strings.TrimRight(trimmed, ".") + "."
}
} else {
// notatePrefix can be empty, and thus, have no effect
colName = notatePrefix + colName
}
cols = append(cols, colName)
}
return cols, nil
}
// ScanStruct will scan the current row into i.
func (r *rows) Scan(i ...interface{}) (err error) {
if i == nil {
return nil
} else if isVariadic(i...) {
return r.ScanVal(i...)
} else if ii, ok := i[0].([]interface{}); ok {
return r.ScanVal(ii...)
}
val, valErr := validate(i[0])
if valErr != nil {
err = valErr
return
}
var rowCount int64
defer func() {
r.Close()
if r.cfg.ReturnErrNoRowsForRows && err == nil && rowCount == 0 {
err = pgx.ErrNoRows
}
}()
switch val.Kind() {
case reflect.Slice:
sliceOf := sqlmaper.GetSliceElementType(val)
for r.Next() {
sliceVal := reflect.New(sliceOf)
cols, colErr := GetColumnNames(&r.rows)
if colErr != nil {
err = colErr
return
}
if ssErr := ScanStruct(r.rows.Scan, sliceVal.Interface(), cols, r.cfg.MatchAllColumnsToStruct); ssErr != nil {
err = ssErr
return
}
sqlmaper.AppendSliceElement(val, sliceVal)
rowCount++
}
case reflect.Struct:
for r.Next() {
if val.CanAddr() {
cols, colErr := GetColumnNames(&r.rows)
if colErr != nil {
err = colErr
return
}
if ssErr := ScanStruct(r.rows.Scan, val.Addr().Interface(), cols, r.cfg.MatchAllColumnsToStruct); ssErr != nil {
err = ssErr
return
}
}
rowCount++
}
}
return r.Err()
}
// ScanVal will scan the current row and column into i.
func (r *rows) ScanVal(v ...interface{}) error {
defer r.Close()
for r.Next() {
if err := r.rows.Scan(v...); err != nil {
return err
}
}
return r.rows.Err()
}
// Close closes the Rows, preventing further enumeration. See sql.Rows#Close
// for more info.
func (r *rows) Close() {
r.rows.Close()
}
func (r *rows) SetCols(cols ...string) Scanner {
r.columns = cols
return r
}