-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathops.go
333 lines (282 loc) · 10.6 KB
/
ops.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package pgc
import (
"fmt"
"reflect"
"strings"
"github.com/cliqueinc/pgc/pgcq"
"github.com/jackc/pgx"
)
// defaultAdapter allows to perform pgc operations without creating adapter instance.
var defaultAdapter *Adapter
func getDefault() *Adapter {
if defaultAdapter == nil {
defaultAdapter = NewAdapter()
}
return defaultAdapter
}
// NewAdapter creates new adapter instance.
func NewAdapter() *Adapter {
return &Adapter{&mustAdapter{&crudAdapter{con: getConn()}}}
}
// Begin begins new transaction.
func Begin() (*TxAdapter, error) {
return getDefault().Begin()
}
// MustInsert ensures struct will be inserted without errors, panics othervise.
// Limit of items to insert at once is 1000 items.
func MustInsert(structPtrs ...interface{}) {
getDefault().MustInsert(structPtrs...)
}
// Insert inserts struct into db. If no options specied, struct will be updated by primary key.
// Limit of items to insert at once is 1000 items.
func Insert(structPtrs ...interface{}) error {
return getDefault().Insert(structPtrs...)
}
// MustUpdate ensures struct will be updated without errors, panics othervise.
func MustUpdate(structPtr interface{}) {
getDefault().MustUpdate(structPtr)
}
// Update updates struct by primary key.
func Update(structPtr interface{}) error {
return getDefault().Update(structPtr)
}
// MustUpdateRows ensures rows are updated without errors, panics othervise. Returns number of affected rows.
// In case when you really need to update all rows (e.g. migration script), you need to pass pgc.QueryAll() option.
// It is done to avoid unintentional update of all rows.
func MustUpdateRows(structPtr interface{}, colsMap Map, opts ...pgcq.Option) int64 {
return getDefault().MustUpdateRows(structPtr, colsMap, opts...)
}
// UpdateRows updates rows with specified map data by query, returns number of affected rows.
// In case when you really need to update all rows (e.g. migration script), you need to pass pgc.QueryAll() option.
// It is done to avoid unintentional update of all rows.
func UpdateRows(structPtr interface{}, colsMap Map, opts ...pgcq.Option) (int64, error) {
return getDefault().UpdateRows(structPtr, colsMap, opts...)
}
// MustSelect ensures select will not produce any error, panics othervise.
func MustSelect(destSlicePtr interface{}, opts ...pgcq.Option) {
getDefault().MustSelect(destSlicePtr, opts...)
}
// Select performs select using query options. If not options specified, all rows will be returned.
// destSlicePtr parameter expects pointer to a slice
func Select(destSlicePtr interface{}, opts ...pgcq.Option) error {
return getDefault().Select(destSlicePtr, opts...)
}
// MustSelectCustomData ensures select will not produce any error, panics othervise.
func MustSelectCustomData(model interface{}, destSlicePtr interface{}, opts ...pgcq.Option) {
getDefault().MustSelectCustomData(model, destSlicePtr, opts...)
}
// SelectCustomData selects custom columns like aggregated one or any specified by struct form destSlicePtr.
// Use pgc_name tage to specify custom column name, e.g. `COUNT(*) as count`. If no options specified, all rows will be returned.
// destSlicePtr parameter expects pointer to a slice
func SelectCustomData(model interface{}, destSlicePtr interface{}, opts ...pgcq.Option) error {
return getDefault().SelectCustomData(model, destSlicePtr, opts...)
}
// MustGet returns whether or not it found the item and panic on errors.
func MustGet(structPtr interface{}, opts ...pgcq.Option) bool {
return getDefault().MustGet(structPtr, opts...)
}
// Get gets struct by primary key or by specified options.
func Get(structPtr interface{}, opts ...pgcq.Option) (found bool, err error) {
return getDefault().Get(structPtr, opts...)
}
// MustDelete ensures struct will be deleted without errors, panics othervise.
func MustDelete(structPtr interface{}) {
getDefault().MustDelete(structPtr)
}
// Delete deletes struct by primary key or by specified options.
func Delete(structPtr interface{}) error {
return getDefault().Delete(structPtr)
}
// MustDeleteRows ensures rows are deleted without errors, panics othervise. Returns number of affected rows.
func MustDeleteRows(structPtr interface{}, opts ...pgcq.Option) int64 {
return getDefault().MustDeleteRows(structPtr, opts...)
}
// DeleteRows deletes rows by specified options. Returns number of affected rows.
func DeleteRows(structPtr interface{}, opts ...pgcq.Option) (int64, error) {
return getDefault().DeleteRows(structPtr, opts...)
}
// MustCount gets rows count, panics in case of an error.
func MustCount(model interface{}, opts ...pgcq.Option) int {
return getDefault().MustCount(model, opts...)
}
// Count gets rows count by query.
func Count(model interface{}, opts ...pgcq.Option) (int, error) {
return getDefault().Count(model, opts...)
}
// MustCreateTable ensures table is created from struct.
func MustCreateTable(structPtr interface{}) {
a := &MigrationAdapter{crudAdapter: getDefault().crudAdapter}
a.MustCreateTable(structPtr)
}
// CreateTable creates table from struct.
func CreateTable(structPtr interface{}) error {
a := &MigrationAdapter{crudAdapter: getDefault().crudAdapter}
return a.CreateTable(structPtr)
}
// IsUniqueViolationError checks whether an error is unique constraint violation error.
func IsUniqueViolationError(err error) bool {
if err == nil {
return false
}
return strings.Contains(err.Error(), "SQLSTATE "+PGECUniqueViolation)
}
// IsTableExistsError checks whether an error is table already exists error.
func IsTableExistsError(err error) bool {
if err == nil {
return false
}
return strings.Contains(err.Error(), PGECTableExists)
}
// SelectAllWhere performs raw select and panics in case of errors.
func SelectAllWhere(destSlicePtr interface{}, sqlWhereStmt string, args ...interface{}) {
if sqlWhereStmt != "" && strings.HasPrefix(strings.ToLower(sqlWhereStmt), "select") {
panic("pgc: If provided, SelectAllWhere.sqlWhereStmt must not start with select")
}
mod, sliceValElement, sliceTypeElement, err := parseDestSlice(destSlicePtr)
if err != nil {
panic(err.Error())
}
finalSQL := renderTemplate(Map{"mod": mod, "fields": mod.Fields, "joins": nil}, selectBaseTemplate) + " " + sqlWhereStmt + ";"
if cfg.LogQueries {
fmt.Println(finalSQL)
}
err = rawSelect(finalSQL, nil, nil, nil, true, sliceValElement, sliceTypeElement, getDefault().con, args...)
if err != nil {
panic(err.Error())
}
}
func Query(stmt string, args ...interface{}) (*pgx.Rows, error) {
con := getDefault().con
rows, err := con.Query(stmt, args...)
return rows, err
}
func rawSelect(sqlStmt string, columns []string, joinMods []*model, joinFields [][]*field, requirePK bool, sliceValElement reflect.Value,
sliceTypeElement reflect.Type, con connection, args ...interface{}) error {
if cfg.LogQueries {
fmt.Println(sqlStmt, args)
}
rows, err := con.Query(sqlStmt, args...)
if err != nil {
return err
}
var prevRow struct {
modPK string
model reflect.Value
parsedJoinModels map[string][]string
}
var (
mod *model
modFields []*field
modPK string
valAddrs []interface{}
rowModel reflect.Value
rowJoins []reflect.Value
)
defer rows.Close()
for rows.Next() {
if mod == nil {
mod = parseModel(reflect.New(sliceTypeElement).Interface(), requirePK)
modFields = mod.getFields(columns)
valAddrs = make([]interface{}, 0, len(modFields))
} else {
valAddrs = valAddrs[:0]
rowJoins = rowJoins[:0]
}
rowModel = reflect.New(mod.ReflectType.Elem())
for i := range joinMods {
rowJoins = append(rowJoins, reflect.New(joinMods[i].ReflectType.Elem()))
}
for i := range modFields {
valAddrs = append(valAddrs, rowModel.Elem().Field(modFields[i].FieldPos).Addr().Interface())
}
for i := range joinMods {
for ind := range joinFields[i] {
valAddrs = append(valAddrs, rowJoins[i].Elem().Field(joinFields[i][ind].FieldPos).Addr().Interface())
}
}
err = rows.Scan(valAddrs...)
if err != nil {
panic(err)
}
if mod.PKPos != -1 {
modPK = mod.getPK(rowModel)
}
// rowIsTheSame is used for checks whether the next row represents the same
// model, and if yes it means that the difference is in the different join model,
// which happens in one to many relation.
rowIsTheSame := modPK != "" && modPK == prevRow.modPK
if len(joinMods) != 0 {
for i := range joinMods {
joinName := joinMods[i].ReflectType.Elem().Name()
joinPos, ok := mod.Joins[joinName]
if !ok {
panic(fmt.Sprintf("unknown join %s", joinMods[i].ReflectType.Elem().Name()))
}
modJoin := rowModel.Elem().Field(joinPos)
var joinPKVal string
if joinMods[i].PKPos != -1 {
joinPKVal = joinMods[i].getPK(rowJoins[i])
}
// during join select we replace possible joined null values with default values,
// as pgx don't want to parse null into string), so we just check whether
// joined primary key is empty, which means that this row don't have anything joined.
if joinPKVal == "" {
continue
}
// if case its one to one join
if modJoin.Kind() != reflect.Slice {
if modJoin.Kind() == reflect.Ptr {
modJoin.Set(rowJoins[i])
} else {
modJoin.Set(rowJoins[i].Elem())
}
continue
}
// in case one-to-many join we want to ensure that we haven't already added this
// join to our model, thats why we keep added models in prevRow.parsedJoinModels map.
var modelAlreadySet bool
if prevRow.parsedJoinModels != nil {
if parsedModels, ok := prevRow.parsedJoinModels[joinName]; ok {
for _, mID := range parsedModels {
if mID == joinPKVal {
modelAlreadySet = true
break
}
}
}
} else {
prevRow.parsedJoinModels = make(map[string][]string)
}
if modelAlreadySet {
continue
}
prevRow.parsedJoinModels[joinName] = append(prevRow.parsedJoinModels[joinName], joinPKVal)
// set current join model to our real model.
if rowIsTheSame {
prevVal := prevRow.model.Elem().Field(joinPos)
prevVal.Set(reflect.Append(prevVal, rowJoins[i].Elem()))
} else {
slice := reflect.MakeSlice(reflect.SliceOf(joinMods[i].ReflectType.Elem()), 0, 1)
rowModel.Elem().Field(joinPos).Set(reflect.Append(slice, rowJoins[i].Elem()))
}
}
}
if !rowIsTheSame {
prevRow.model = rowModel
prevRow.modPK = modPK
}
if !rowIsTheSame {
// if our model is scanned first time, just append it to other models
sliceValElement.Set(reflect.Append(sliceValElement, rowModel.Elem()))
} else {
// if this model was already scanned, but some joins were added to it,
// we just want to update the latest slice element with newest changes.
sliceValElement.Index(sliceValElement.Len() - 1).Set(prevRow.model.Elem())
}
}
err = rows.Err()
if err != nil {
return err
}
return nil
}