forked from go-reform/reform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquerier_selects.go
259 lines (231 loc) · 8.84 KB
/
querier_selects.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package reform
import (
"database/sql"
"fmt"
mysqlDriver "github.com/go-sql-driver/mysql"
"strings"
)
// NextRow scans next result row from rows to str. If str has valid method "AfterFind", it also calls AfterFind().
// It is caller's responsibility to call rows.Close().
//
// If there is no next result row, it returns ErrNoRows. It also may return rows.Next(), rows.Scan()
// and AfterFind() errors.
//
// See SelectRows example for idiomatic usage.
func (q *Querier) NextRow(str Struct, rows *sql.Rows) error {
var err error
next := rows.Next()
if !next {
err = rows.Err()
if err == nil {
err = ErrNoRows
}
return err
}
if err = rows.Scan(str.Pointers()...); err != nil {
return err
}
return q.callStructMethod(str, "AfterFind")
}
// selectQuery returns full SELECT query for given view and tail.
func (q *Querier) selectQuery(view View, tail string, limit1 bool, forceAnotherTable *string, forceFields []string) string {
queryStart := q.startQuery("SELECT")
if limit1 && q.SelectLimitMethod() == SelectTop {
queryStart += " TOP 1"
}
var columnsQuoted []string
if len(forceFields) > 0 {
for _, field := range forceFields {
column := view.ColumnNameByFieldName(field)
if column == "" {
panic("Unknown field:" + field)
}
columnsQuoted = append(columnsQuoted, q.QuoteIdentifier(column))
}
} else {
columnsQuoted = q.QualifiedColumns(view)
}
columnsQuery := strings.Join(columnsQuoted, ", ")
var tableQuery string
if forceAnotherTable == nil {
tableQuery = q.QualifiedView(view)
} else {
tableQuery = *forceAnotherTable
}
return fmt.Sprintf("%s %s FROM %s %s", queryStart, columnsQuery, tableQuery, tail)
}
// FlexSelectOneTo queries str's View with tail, args, forceAnotherTable and forceFields and scans first result to str.
// If str has valid method "AfterFind", it also calls AfterFind().
//
// If there are no rows in result, it returns ErrNoRows. It also may return QueryRow(), Scan()
// and AfterFind() errors.
func (q *Querier) FlexSelectOneTo(str Struct, forceAnotherTable *string, forceFields []string, tail string, args ...interface{}) error {
query := q.selectQuery(str.View(), tail, true, forceAnotherTable, forceFields)
for {
err := q.QueryRow(query, args...).Scan(str.FieldPointersByNames(forceFields)...)
if err == mysqlDriver.ErrInvalidConn {
continue
}
if err != nil {
return err
}
break
}
return q.callStructMethod(str, "AfterFind")
}
// SelectOneTo queries str's View with tail and args and scans first result to str.
// If str has valid method "AfterFind", it also calls AfterFind().
//
// If there are no rows in result, it returns ErrNoRows. It also may return QueryRow(), Scan()
// and AfterFind() errors.
func (q *Querier) SelectOneTo(str Struct, tail string, args ...interface{}) error {
return q.FlexSelectOneTo(str, nil, nil, tail, args...)
}
// SelectOneFrom queries view with tail and args and scans first result to new Struct str.
// If str has valid method "AfterFind", it also calls AfterFind().
//
// If there are no rows in result, it returns nil, ErrNoRows. It also may return QueryRow(), Scan()
// and AfterFind() errors.
func (q *Querier) SelectOneFrom(view View, tail string, args ...interface{}) (Struct, error) {
str := view.NewStruct()
if err := q.SelectOneTo(str, tail, args...); err != nil {
return nil, err
}
return str, nil
}
// FlexSelectRows queries view with tail, args, forceAnotherTable and forceFields and returns rows. They can then be iterated with NextRow().
// It is caller's responsibility to call rows.Close().
//
// In case of error rows will be nil. Error is never ErrNoRows.
//
// See example for idiomatic usage.
func (q *Querier) FlexSelectRows(view View, forceAnotherTable *string, forceFields []string, tail string, args ...interface{}) (*sql.Rows, error) {
query := q.selectQuery(view, tail, false, forceAnotherTable, forceFields)
return q.Query(query, args...)
}
// SelectRows queries view with tail and args and returns rows. They can then be iterated with NextRow().
// It is caller's responsibility to call rows.Close().
//
// In case of error rows will be nil. Error is never ErrNoRows.
//
// See example for idiomatic usage.
func (q *Querier) SelectRows(view View, tail string, args ...interface{}) (*sql.Rows, error) {
query := q.selectQuery(view, tail, false, nil, nil)
return q.Query(query, args...)
}
// SelectAllFrom queries view with tail and args and returns a slice of new Structs.
// If view's Struct has valid method "AfterFind", it also calls AfterFind().
//
// In case of query error slice will be nil. If error is encountered during iteration,
// partial result and error will be returned. Error is never ErrNoRows.
func (q *Querier) SelectAllFrom(view View, tail string, args ...interface{}) (structs []Struct, err error) {
var rows *sql.Rows
rows, err = q.SelectRows(view, tail, args...)
if err != nil {
return
}
defer func() {
e := rows.Close()
if err == nil {
err = e
}
}()
for {
str := view.NewStruct()
if err = q.NextRow(str, rows); err != nil {
break
}
structs = append(structs, str)
}
if err == ErrNoRows {
err = nil
}
return
}
// findTail returns a tail of SELECT query for given view, column and arg.
func (q *Querier) findTail(view string, column string, arg interface{}, limit1 bool) (tail string, needArg bool) {
qi := q.QuoteIdentifier(view) + "." + q.QuoteIdentifier(column)
if arg == nil {
tail = fmt.Sprintf("WHERE %s IS NULL", qi)
} else {
tail = fmt.Sprintf("WHERE %s = %s", qi, q.Placeholder(1))
needArg = true
}
if limit1 && q.SelectLimitMethod() == Limit {
tail += " LIMIT 1"
}
return
}
// FindOneTo queries str's View with column and arg and scans first result to str.
// If str has valid method "AfterFind", it also calls AfterFind().
//
// If there are no rows in result, it returns ErrNoRows. It also may return QueryRow(), Scan()
// and AfterFind() errors.
func (q *Querier) FindOneTo(str Struct, column string, arg interface{}) error {
tail, needArg := q.findTail(str.View().Name(), column, arg, true)
if needArg {
return q.SelectOneTo(str, tail, arg)
}
return q.SelectOneTo(str, tail)
}
// FindOneFrom queries view with column and arg and scans first result to new Struct str.
// If str has valid method "AfterFind", it also calls AfterFind().
//
// If there are no rows in result, it returns nil, ErrNoRows. It also may return QueryRow(), Scan()
// and AfterFind() errors.
func (q *Querier) FindOneFrom(view View, column string, arg interface{}) (Struct, error) {
tail, needArg := q.findTail(view.Name(), column, arg, true)
if needArg {
return q.SelectOneFrom(view, tail, arg)
}
return q.SelectOneFrom(view, tail)
}
// FindRows queries view with column and arg and returns rows. They can then be iterated with NextRow().
// It is caller's responsibility to call rows.Close().
//
// In case of error rows will be nil. Error is never ErrNoRows.
//
// See SelectRows example for idiomatic usage.
func (q *Querier) FindRows(view View, column string, arg interface{}) (*sql.Rows, error) {
tail, needArg := q.findTail(view.Name(), column, arg, false)
if needArg {
return q.SelectRows(view, tail, arg)
}
return q.SelectRows(view, tail)
}
// FindAllFrom queries view with column and args and returns a slice of new Structs.
// If view's Struct has valid method "AfterFind", it also calls AfterFind().
//
// In case of query error slice will be nil. If error is encountered during iteration,
// partial result and error will be returned. Error is never ErrNoRows.
func (q *Querier) FindAllFrom(view View, column string, args ...interface{}) ([]Struct, error) {
p := strings.Join(q.Placeholders(1, len(args)), ", ")
qi := q.QualifiedView(view) + "." + q.QuoteIdentifier(column)
tail := fmt.Sprintf("WHERE %s IN (%s)", qi, p)
return q.SelectAllFrom(view, tail, args...)
}
// FindByPrimaryKeyTo queries record's Table with primary key and scans first result to record.
// If record has valid method "AfterFind", it also calls AfterFind().
//
// If there are no rows in result, it returns ErrNoRows. It also may return QueryRow(), Scan()
// and AfterFind() errors.
func (q *Querier) FindByPrimaryKeyTo(record Record, pk interface{}) error {
table := record.Table()
return q.FindOneTo(record, table.Columns()[table.PKColumnIndex()], pk)
}
// FindByPrimaryKeyFrom queries table with primary key and scans first result to new Record.
// If record has valid method "AfterFind", it also calls AfterFind().
//
// If there are no rows in result, it returns nil, ErrNoRows. It also may return QueryRow(), Scan()
// and AfterFind() errors.
func (q *Querier) FindByPrimaryKeyFrom(table Table, pk interface{}) (Record, error) {
record := table.NewRecord()
if err := q.FindOneTo(record, table.Columns()[table.PKColumnIndex()], pk); err != nil {
return nil, err
}
return record, nil
}
// Reload is a shortcut for FindByPrimaryKeyTo for given record.
func (q *Querier) Reload(record Record) error {
return q.FindByPrimaryKeyTo(record, record.PKValue())
}