This repository was archived by the owner on Dec 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuser_privilege.go
905 lines (732 loc) · 26.3 KB
/
user_privilege.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
// Code generated by SQLBoiler 3.5.0 (https://github.com/volatiletech/sqlboiler). DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package models
import (
"context"
"database/sql"
"fmt"
"reflect"
"strconv"
"strings"
"sync"
"time"
"github.com/pkg/errors"
"github.com/volatiletech/sqlboiler/boil"
"github.com/volatiletech/sqlboiler/queries"
"github.com/volatiletech/sqlboiler/queries/qm"
"github.com/volatiletech/sqlboiler/queries/qmhelper"
"github.com/volatiletech/sqlboiler/strmangle"
)
// UserPrivilege is an object representing the database table.
type UserPrivilege struct {
UserID int `boil:"user_id" json:"user_id" toml:"user_id" yaml:"user_id"`
Privilege string `boil:"privilege" json:"privilege" toml:"privilege" yaml:"privilege"`
R *userPrivilegeR `boil:"-" json:"-" toml:"-" yaml:"-"`
L userPrivilegeL `boil:"-" json:"-" toml:"-" yaml:"-"`
}
var UserPrivilegeColumns = struct {
UserID string
Privilege string
}{
UserID: "user_id",
Privilege: "privilege",
}
// Generated where
var UserPrivilegeWhere = struct {
UserID whereHelperint
Privilege whereHelperstring
}{
UserID: whereHelperint{field: "`user_privilege`.`user_id`"},
Privilege: whereHelperstring{field: "`user_privilege`.`privilege`"},
}
// UserPrivilegeRels is where relationship names are stored.
var UserPrivilegeRels = struct {
}{}
// userPrivilegeR is where relationships are stored.
type userPrivilegeR struct {
}
// NewStruct creates a new relationship struct
func (*userPrivilegeR) NewStruct() *userPrivilegeR {
return &userPrivilegeR{}
}
// userPrivilegeL is where Load methods for each relationship are stored.
type userPrivilegeL struct{}
var (
userPrivilegeAllColumns = []string{"user_id", "privilege"}
userPrivilegeColumnsWithoutDefault = []string{"user_id", "privilege"}
userPrivilegeColumnsWithDefault = []string{}
userPrivilegePrimaryKeyColumns = []string{"user_id", "privilege"}
)
type (
// UserPrivilegeSlice is an alias for a slice of pointers to UserPrivilege.
// This should generally be used opposed to []UserPrivilege.
UserPrivilegeSlice []*UserPrivilege
// UserPrivilegeHook is the signature for custom UserPrivilege hook methods
UserPrivilegeHook func(context.Context, boil.ContextExecutor, *UserPrivilege) error
userPrivilegeQuery struct {
*queries.Query
}
)
// Cache for insert, update and upsert
var (
userPrivilegeType = reflect.TypeOf(&UserPrivilege{})
userPrivilegeMapping = queries.MakeStructMapping(userPrivilegeType)
userPrivilegePrimaryKeyMapping, _ = queries.BindMapping(userPrivilegeType, userPrivilegeMapping, userPrivilegePrimaryKeyColumns)
userPrivilegeInsertCacheMut sync.RWMutex
userPrivilegeInsertCache = make(map[string]insertCache)
userPrivilegeUpdateCacheMut sync.RWMutex
userPrivilegeUpdateCache = make(map[string]updateCache)
userPrivilegeUpsertCacheMut sync.RWMutex
userPrivilegeUpsertCache = make(map[string]insertCache)
)
var (
// Force time package dependency for automated UpdatedAt/CreatedAt.
_ = time.Second
// Force qmhelper dependency for where clause generation (which doesn't
// always happen)
_ = qmhelper.Where
)
var userPrivilegeBeforeInsertHooks []UserPrivilegeHook
var userPrivilegeBeforeUpdateHooks []UserPrivilegeHook
var userPrivilegeBeforeDeleteHooks []UserPrivilegeHook
var userPrivilegeBeforeUpsertHooks []UserPrivilegeHook
var userPrivilegeAfterInsertHooks []UserPrivilegeHook
var userPrivilegeAfterSelectHooks []UserPrivilegeHook
var userPrivilegeAfterUpdateHooks []UserPrivilegeHook
var userPrivilegeAfterDeleteHooks []UserPrivilegeHook
var userPrivilegeAfterUpsertHooks []UserPrivilegeHook
// doBeforeInsertHooks executes all "before insert" hooks.
func (o *UserPrivilege) doBeforeInsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
if boil.HooksAreSkipped(ctx) {
return nil
}
for _, hook := range userPrivilegeBeforeInsertHooks {
if err := hook(ctx, exec, o); err != nil {
return err
}
}
return nil
}
// doBeforeUpdateHooks executes all "before Update" hooks.
func (o *UserPrivilege) doBeforeUpdateHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
if boil.HooksAreSkipped(ctx) {
return nil
}
for _, hook := range userPrivilegeBeforeUpdateHooks {
if err := hook(ctx, exec, o); err != nil {
return err
}
}
return nil
}
// doBeforeDeleteHooks executes all "before Delete" hooks.
func (o *UserPrivilege) doBeforeDeleteHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
if boil.HooksAreSkipped(ctx) {
return nil
}
for _, hook := range userPrivilegeBeforeDeleteHooks {
if err := hook(ctx, exec, o); err != nil {
return err
}
}
return nil
}
// doBeforeUpsertHooks executes all "before Upsert" hooks.
func (o *UserPrivilege) doBeforeUpsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
if boil.HooksAreSkipped(ctx) {
return nil
}
for _, hook := range userPrivilegeBeforeUpsertHooks {
if err := hook(ctx, exec, o); err != nil {
return err
}
}
return nil
}
// doAfterInsertHooks executes all "after Insert" hooks.
func (o *UserPrivilege) doAfterInsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
if boil.HooksAreSkipped(ctx) {
return nil
}
for _, hook := range userPrivilegeAfterInsertHooks {
if err := hook(ctx, exec, o); err != nil {
return err
}
}
return nil
}
// doAfterSelectHooks executes all "after Select" hooks.
func (o *UserPrivilege) doAfterSelectHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
if boil.HooksAreSkipped(ctx) {
return nil
}
for _, hook := range userPrivilegeAfterSelectHooks {
if err := hook(ctx, exec, o); err != nil {
return err
}
}
return nil
}
// doAfterUpdateHooks executes all "after Update" hooks.
func (o *UserPrivilege) doAfterUpdateHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
if boil.HooksAreSkipped(ctx) {
return nil
}
for _, hook := range userPrivilegeAfterUpdateHooks {
if err := hook(ctx, exec, o); err != nil {
return err
}
}
return nil
}
// doAfterDeleteHooks executes all "after Delete" hooks.
func (o *UserPrivilege) doAfterDeleteHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
if boil.HooksAreSkipped(ctx) {
return nil
}
for _, hook := range userPrivilegeAfterDeleteHooks {
if err := hook(ctx, exec, o); err != nil {
return err
}
}
return nil
}
// doAfterUpsertHooks executes all "after Upsert" hooks.
func (o *UserPrivilege) doAfterUpsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
if boil.HooksAreSkipped(ctx) {
return nil
}
for _, hook := range userPrivilegeAfterUpsertHooks {
if err := hook(ctx, exec, o); err != nil {
return err
}
}
return nil
}
// AddUserPrivilegeHook registers your hook function for all future operations.
func AddUserPrivilegeHook(hookPoint boil.HookPoint, userPrivilegeHook UserPrivilegeHook) {
switch hookPoint {
case boil.BeforeInsertHook:
userPrivilegeBeforeInsertHooks = append(userPrivilegeBeforeInsertHooks, userPrivilegeHook)
case boil.BeforeUpdateHook:
userPrivilegeBeforeUpdateHooks = append(userPrivilegeBeforeUpdateHooks, userPrivilegeHook)
case boil.BeforeDeleteHook:
userPrivilegeBeforeDeleteHooks = append(userPrivilegeBeforeDeleteHooks, userPrivilegeHook)
case boil.BeforeUpsertHook:
userPrivilegeBeforeUpsertHooks = append(userPrivilegeBeforeUpsertHooks, userPrivilegeHook)
case boil.AfterInsertHook:
userPrivilegeAfterInsertHooks = append(userPrivilegeAfterInsertHooks, userPrivilegeHook)
case boil.AfterSelectHook:
userPrivilegeAfterSelectHooks = append(userPrivilegeAfterSelectHooks, userPrivilegeHook)
case boil.AfterUpdateHook:
userPrivilegeAfterUpdateHooks = append(userPrivilegeAfterUpdateHooks, userPrivilegeHook)
case boil.AfterDeleteHook:
userPrivilegeAfterDeleteHooks = append(userPrivilegeAfterDeleteHooks, userPrivilegeHook)
case boil.AfterUpsertHook:
userPrivilegeAfterUpsertHooks = append(userPrivilegeAfterUpsertHooks, userPrivilegeHook)
}
}
// One returns a single userPrivilege record from the query.
func (q userPrivilegeQuery) One(ctx context.Context, exec boil.ContextExecutor) (*UserPrivilege, error) {
o := &UserPrivilege{}
queries.SetLimit(q.Query, 1)
err := q.Bind(ctx, exec, o)
if err != nil {
if errors.Cause(err) == sql.ErrNoRows {
return nil, sql.ErrNoRows
}
return nil, errors.Wrap(err, "models: failed to execute a one query for user_privilege")
}
if err := o.doAfterSelectHooks(ctx, exec); err != nil {
return o, err
}
return o, nil
}
// All returns all UserPrivilege records from the query.
func (q userPrivilegeQuery) All(ctx context.Context, exec boil.ContextExecutor) (UserPrivilegeSlice, error) {
var o []*UserPrivilege
err := q.Bind(ctx, exec, &o)
if err != nil {
return nil, errors.Wrap(err, "models: failed to assign all query results to UserPrivilege slice")
}
if len(userPrivilegeAfterSelectHooks) != 0 {
for _, obj := range o {
if err := obj.doAfterSelectHooks(ctx, exec); err != nil {
return o, err
}
}
}
return o, nil
}
// Count returns the count of all UserPrivilege records in the query.
func (q userPrivilegeQuery) Count(ctx context.Context, exec boil.ContextExecutor) (int64, error) {
var count int64
queries.SetSelect(q.Query, nil)
queries.SetCount(q.Query)
err := q.Query.QueryRowContext(ctx, exec).Scan(&count)
if err != nil {
return 0, errors.Wrap(err, "models: failed to count user_privilege rows")
}
return count, nil
}
// Exists checks if the row exists in the table.
func (q userPrivilegeQuery) Exists(ctx context.Context, exec boil.ContextExecutor) (bool, error) {
var count int64
queries.SetSelect(q.Query, nil)
queries.SetCount(q.Query)
queries.SetLimit(q.Query, 1)
err := q.Query.QueryRowContext(ctx, exec).Scan(&count)
if err != nil {
return false, errors.Wrap(err, "models: failed to check if user_privilege exists")
}
return count > 0, nil
}
// UserPrivileges retrieves all the records using an executor.
func UserPrivileges(mods ...qm.QueryMod) userPrivilegeQuery {
mods = append(mods, qm.From("`user_privilege`"))
return userPrivilegeQuery{NewQuery(mods...)}
}
// FindUserPrivilege retrieves a single record by ID with an executor.
// If selectCols is empty Find will return all columns.
func FindUserPrivilege(ctx context.Context, exec boil.ContextExecutor, userID int, privilege string, selectCols ...string) (*UserPrivilege, error) {
userPrivilegeObj := &UserPrivilege{}
sel := "*"
if len(selectCols) > 0 {
sel = strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, selectCols), ",")
}
query := fmt.Sprintf(
"select %s from `user_privilege` where `user_id`=? AND `privilege`=?", sel,
)
q := queries.Raw(query, userID, privilege)
err := q.Bind(ctx, exec, userPrivilegeObj)
if err != nil {
if errors.Cause(err) == sql.ErrNoRows {
return nil, sql.ErrNoRows
}
return nil, errors.Wrap(err, "models: unable to select from user_privilege")
}
return userPrivilegeObj, nil
}
// Insert a single record using an executor.
// See boil.Columns.InsertColumnSet documentation to understand column list inference for inserts.
func (o *UserPrivilege) Insert(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) error {
if o == nil {
return errors.New("models: no user_privilege provided for insertion")
}
var err error
if err := o.doBeforeInsertHooks(ctx, exec); err != nil {
return err
}
nzDefaults := queries.NonZeroDefaultSet(userPrivilegeColumnsWithDefault, o)
key := makeCacheKey(columns, nzDefaults)
userPrivilegeInsertCacheMut.RLock()
cache, cached := userPrivilegeInsertCache[key]
userPrivilegeInsertCacheMut.RUnlock()
if !cached {
wl, returnColumns := columns.InsertColumnSet(
userPrivilegeAllColumns,
userPrivilegeColumnsWithDefault,
userPrivilegeColumnsWithoutDefault,
nzDefaults,
)
cache.valueMapping, err = queries.BindMapping(userPrivilegeType, userPrivilegeMapping, wl)
if err != nil {
return err
}
cache.retMapping, err = queries.BindMapping(userPrivilegeType, userPrivilegeMapping, returnColumns)
if err != nil {
return err
}
if len(wl) != 0 {
cache.query = fmt.Sprintf("INSERT INTO `user_privilege` (`%s`) %%sVALUES (%s)%%s", strings.Join(wl, "`,`"), strmangle.Placeholders(dialect.UseIndexPlaceholders, len(wl), 1, 1))
} else {
cache.query = "INSERT INTO `user_privilege` () VALUES ()%s%s"
}
var queryOutput, queryReturning string
if len(cache.retMapping) != 0 {
cache.retQuery = fmt.Sprintf("SELECT `%s` FROM `user_privilege` WHERE %s", strings.Join(returnColumns, "`,`"), strmangle.WhereClause("`", "`", 0, userPrivilegePrimaryKeyColumns))
}
cache.query = fmt.Sprintf(cache.query, queryOutput, queryReturning)
}
value := reflect.Indirect(reflect.ValueOf(o))
vals := queries.ValuesFromMapping(value, cache.valueMapping)
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, cache.query)
fmt.Fprintln(boil.DebugWriter, vals)
}
_, err = exec.ExecContext(ctx, cache.query, vals...)
if err != nil {
return errors.Wrap(err, "models: unable to insert into user_privilege")
}
var identifierCols []interface{}
if len(cache.retMapping) == 0 {
goto CacheNoHooks
}
identifierCols = []interface{}{
o.UserID,
o.Privilege,
}
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, cache.retQuery)
fmt.Fprintln(boil.DebugWriter, identifierCols...)
}
err = exec.QueryRowContext(ctx, cache.retQuery, identifierCols...).Scan(queries.PtrsFromMapping(value, cache.retMapping)...)
if err != nil {
return errors.Wrap(err, "models: unable to populate default values for user_privilege")
}
CacheNoHooks:
if !cached {
userPrivilegeInsertCacheMut.Lock()
userPrivilegeInsertCache[key] = cache
userPrivilegeInsertCacheMut.Unlock()
}
return o.doAfterInsertHooks(ctx, exec)
}
// Update uses an executor to update the UserPrivilege.
// See boil.Columns.UpdateColumnSet documentation to understand column list inference for updates.
// Update does not automatically update the record in case of default values. Use .Reload() to refresh the records.
func (o *UserPrivilege) Update(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) (int64, error) {
var err error
if err = o.doBeforeUpdateHooks(ctx, exec); err != nil {
return 0, err
}
key := makeCacheKey(columns, nil)
userPrivilegeUpdateCacheMut.RLock()
cache, cached := userPrivilegeUpdateCache[key]
userPrivilegeUpdateCacheMut.RUnlock()
if !cached {
wl := columns.UpdateColumnSet(
userPrivilegeAllColumns,
userPrivilegePrimaryKeyColumns,
)
if !columns.IsWhitelist() {
wl = strmangle.SetComplement(wl, []string{"created_at"})
}
if len(wl) == 0 {
return 0, errors.New("models: unable to update user_privilege, could not build whitelist")
}
cache.query = fmt.Sprintf("UPDATE `user_privilege` SET %s WHERE %s",
strmangle.SetParamNames("`", "`", 0, wl),
strmangle.WhereClause("`", "`", 0, userPrivilegePrimaryKeyColumns),
)
cache.valueMapping, err = queries.BindMapping(userPrivilegeType, userPrivilegeMapping, append(wl, userPrivilegePrimaryKeyColumns...))
if err != nil {
return 0, err
}
}
values := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), cache.valueMapping)
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, cache.query)
fmt.Fprintln(boil.DebugWriter, values)
}
var result sql.Result
result, err = exec.ExecContext(ctx, cache.query, values...)
if err != nil {
return 0, errors.Wrap(err, "models: unable to update user_privilege row")
}
rowsAff, err := result.RowsAffected()
if err != nil {
return 0, errors.Wrap(err, "models: failed to get rows affected by update for user_privilege")
}
if !cached {
userPrivilegeUpdateCacheMut.Lock()
userPrivilegeUpdateCache[key] = cache
userPrivilegeUpdateCacheMut.Unlock()
}
return rowsAff, o.doAfterUpdateHooks(ctx, exec)
}
// UpdateAll updates all rows with the specified column values.
func (q userPrivilegeQuery) UpdateAll(ctx context.Context, exec boil.ContextExecutor, cols M) (int64, error) {
queries.SetUpdate(q.Query, cols)
result, err := q.Query.ExecContext(ctx, exec)
if err != nil {
return 0, errors.Wrap(err, "models: unable to update all for user_privilege")
}
rowsAff, err := result.RowsAffected()
if err != nil {
return 0, errors.Wrap(err, "models: unable to retrieve rows affected for user_privilege")
}
return rowsAff, nil
}
// UpdateAll updates all rows with the specified column values, using an executor.
func (o UserPrivilegeSlice) UpdateAll(ctx context.Context, exec boil.ContextExecutor, cols M) (int64, error) {
ln := int64(len(o))
if ln == 0 {
return 0, nil
}
if len(cols) == 0 {
return 0, errors.New("models: update all requires at least one column argument")
}
colNames := make([]string, len(cols))
args := make([]interface{}, len(cols))
i := 0
for name, value := range cols {
colNames[i] = name
args[i] = value
i++
}
// Append all of the primary key values for each column
for _, obj := range o {
pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), userPrivilegePrimaryKeyMapping)
args = append(args, pkeyArgs...)
}
sql := fmt.Sprintf("UPDATE `user_privilege` SET %s WHERE %s",
strmangle.SetParamNames("`", "`", 0, colNames),
strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 0, userPrivilegePrimaryKeyColumns, len(o)))
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, sql)
fmt.Fprintln(boil.DebugWriter, args...)
}
result, err := exec.ExecContext(ctx, sql, args...)
if err != nil {
return 0, errors.Wrap(err, "models: unable to update all in userPrivilege slice")
}
rowsAff, err := result.RowsAffected()
if err != nil {
return 0, errors.Wrap(err, "models: unable to retrieve rows affected all in update all userPrivilege")
}
return rowsAff, nil
}
var mySQLUserPrivilegeUniqueColumns = []string{}
// Upsert attempts an insert using an executor, and does an update or ignore on conflict.
// See boil.Columns documentation for how to properly use updateColumns and insertColumns.
func (o *UserPrivilege) Upsert(ctx context.Context, exec boil.ContextExecutor, updateColumns, insertColumns boil.Columns) error {
if o == nil {
return errors.New("models: no user_privilege provided for upsert")
}
if err := o.doBeforeUpsertHooks(ctx, exec); err != nil {
return err
}
nzDefaults := queries.NonZeroDefaultSet(userPrivilegeColumnsWithDefault, o)
nzUniques := queries.NonZeroDefaultSet(mySQLUserPrivilegeUniqueColumns, o)
if len(nzUniques) == 0 {
return errors.New("cannot upsert with a table that cannot conflict on a unique column")
}
// Build cache key in-line uglily - mysql vs psql problems
buf := strmangle.GetBuffer()
buf.WriteString(strconv.Itoa(updateColumns.Kind))
for _, c := range updateColumns.Cols {
buf.WriteString(c)
}
buf.WriteByte('.')
buf.WriteString(strconv.Itoa(insertColumns.Kind))
for _, c := range insertColumns.Cols {
buf.WriteString(c)
}
buf.WriteByte('.')
for _, c := range nzDefaults {
buf.WriteString(c)
}
buf.WriteByte('.')
for _, c := range nzUniques {
buf.WriteString(c)
}
key := buf.String()
strmangle.PutBuffer(buf)
userPrivilegeUpsertCacheMut.RLock()
cache, cached := userPrivilegeUpsertCache[key]
userPrivilegeUpsertCacheMut.RUnlock()
var err error
if !cached {
insert, ret := insertColumns.InsertColumnSet(
userPrivilegeAllColumns,
userPrivilegeColumnsWithDefault,
userPrivilegeColumnsWithoutDefault,
nzDefaults,
)
update := updateColumns.UpdateColumnSet(
userPrivilegeAllColumns,
userPrivilegePrimaryKeyColumns,
)
if len(update) == 0 {
return errors.New("models: unable to upsert user_privilege, could not build update column list")
}
ret = strmangle.SetComplement(ret, nzUniques)
cache.query = buildUpsertQueryMySQL(dialect, "user_privilege", update, insert)
cache.retQuery = fmt.Sprintf(
"SELECT %s FROM `user_privilege` WHERE %s",
strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, ret), ","),
strmangle.WhereClause("`", "`", 0, nzUniques),
)
cache.valueMapping, err = queries.BindMapping(userPrivilegeType, userPrivilegeMapping, insert)
if err != nil {
return err
}
if len(ret) != 0 {
cache.retMapping, err = queries.BindMapping(userPrivilegeType, userPrivilegeMapping, ret)
if err != nil {
return err
}
}
}
value := reflect.Indirect(reflect.ValueOf(o))
vals := queries.ValuesFromMapping(value, cache.valueMapping)
var returns []interface{}
if len(cache.retMapping) != 0 {
returns = queries.PtrsFromMapping(value, cache.retMapping)
}
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, cache.query)
fmt.Fprintln(boil.DebugWriter, vals)
}
_, err = exec.ExecContext(ctx, cache.query, vals...)
if err != nil {
return errors.Wrap(err, "models: unable to upsert for user_privilege")
}
var uniqueMap []uint64
var nzUniqueCols []interface{}
if len(cache.retMapping) == 0 {
goto CacheNoHooks
}
uniqueMap, err = queries.BindMapping(userPrivilegeType, userPrivilegeMapping, nzUniques)
if err != nil {
return errors.Wrap(err, "models: unable to retrieve unique values for user_privilege")
}
nzUniqueCols = queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), uniqueMap)
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, cache.retQuery)
fmt.Fprintln(boil.DebugWriter, nzUniqueCols...)
}
err = exec.QueryRowContext(ctx, cache.retQuery, nzUniqueCols...).Scan(returns...)
if err != nil {
return errors.Wrap(err, "models: unable to populate default values for user_privilege")
}
CacheNoHooks:
if !cached {
userPrivilegeUpsertCacheMut.Lock()
userPrivilegeUpsertCache[key] = cache
userPrivilegeUpsertCacheMut.Unlock()
}
return o.doAfterUpsertHooks(ctx, exec)
}
// Delete deletes a single UserPrivilege record with an executor.
// Delete will match against the primary key column to find the record to delete.
func (o *UserPrivilege) Delete(ctx context.Context, exec boil.ContextExecutor) (int64, error) {
if o == nil {
return 0, errors.New("models: no UserPrivilege provided for delete")
}
if err := o.doBeforeDeleteHooks(ctx, exec); err != nil {
return 0, err
}
args := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), userPrivilegePrimaryKeyMapping)
sql := "DELETE FROM `user_privilege` WHERE `user_id`=? AND `privilege`=?"
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, sql)
fmt.Fprintln(boil.DebugWriter, args...)
}
result, err := exec.ExecContext(ctx, sql, args...)
if err != nil {
return 0, errors.Wrap(err, "models: unable to delete from user_privilege")
}
rowsAff, err := result.RowsAffected()
if err != nil {
return 0, errors.Wrap(err, "models: failed to get rows affected by delete for user_privilege")
}
if err := o.doAfterDeleteHooks(ctx, exec); err != nil {
return 0, err
}
return rowsAff, nil
}
// DeleteAll deletes all matching rows.
func (q userPrivilegeQuery) DeleteAll(ctx context.Context, exec boil.ContextExecutor) (int64, error) {
if q.Query == nil {
return 0, errors.New("models: no userPrivilegeQuery provided for delete all")
}
queries.SetDelete(q.Query)
result, err := q.Query.ExecContext(ctx, exec)
if err != nil {
return 0, errors.Wrap(err, "models: unable to delete all from user_privilege")
}
rowsAff, err := result.RowsAffected()
if err != nil {
return 0, errors.Wrap(err, "models: failed to get rows affected by deleteall for user_privilege")
}
return rowsAff, nil
}
// DeleteAll deletes all rows in the slice, using an executor.
func (o UserPrivilegeSlice) DeleteAll(ctx context.Context, exec boil.ContextExecutor) (int64, error) {
if len(o) == 0 {
return 0, nil
}
if len(userPrivilegeBeforeDeleteHooks) != 0 {
for _, obj := range o {
if err := obj.doBeforeDeleteHooks(ctx, exec); err != nil {
return 0, err
}
}
}
var args []interface{}
for _, obj := range o {
pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), userPrivilegePrimaryKeyMapping)
args = append(args, pkeyArgs...)
}
sql := "DELETE FROM `user_privilege` WHERE " +
strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 0, userPrivilegePrimaryKeyColumns, len(o))
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, sql)
fmt.Fprintln(boil.DebugWriter, args)
}
result, err := exec.ExecContext(ctx, sql, args...)
if err != nil {
return 0, errors.Wrap(err, "models: unable to delete all from userPrivilege slice")
}
rowsAff, err := result.RowsAffected()
if err != nil {
return 0, errors.Wrap(err, "models: failed to get rows affected by deleteall for user_privilege")
}
if len(userPrivilegeAfterDeleteHooks) != 0 {
for _, obj := range o {
if err := obj.doAfterDeleteHooks(ctx, exec); err != nil {
return 0, err
}
}
}
return rowsAff, nil
}
// Reload refetches the object from the database
// using the primary keys with an executor.
func (o *UserPrivilege) Reload(ctx context.Context, exec boil.ContextExecutor) error {
ret, err := FindUserPrivilege(ctx, exec, o.UserID, o.Privilege)
if err != nil {
return err
}
*o = *ret
return nil
}
// ReloadAll refetches every row with matching primary key column values
// and overwrites the original object slice with the newly updated slice.
func (o *UserPrivilegeSlice) ReloadAll(ctx context.Context, exec boil.ContextExecutor) error {
if o == nil || len(*o) == 0 {
return nil
}
slice := UserPrivilegeSlice{}
var args []interface{}
for _, obj := range *o {
pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), userPrivilegePrimaryKeyMapping)
args = append(args, pkeyArgs...)
}
sql := "SELECT `user_privilege`.* FROM `user_privilege` WHERE " +
strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 0, userPrivilegePrimaryKeyColumns, len(*o))
q := queries.Raw(sql, args...)
err := q.Bind(ctx, exec, &slice)
if err != nil {
return errors.Wrap(err, "models: unable to reload all in UserPrivilegeSlice")
}
*o = slice
return nil
}
// UserPrivilegeExists checks if the UserPrivilege row exists.
func UserPrivilegeExists(ctx context.Context, exec boil.ContextExecutor, userID int, privilege string) (bool, error) {
var exists bool
sql := "select exists(select 1 from `user_privilege` where `user_id`=? AND `privilege`=? limit 1)"
if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, sql)
fmt.Fprintln(boil.DebugWriter, userID, privilege)
}
row := exec.QueryRowContext(ctx, sql, userID, privilege)
err := row.Scan(&exists)
if err != nil {
return false, errors.Wrap(err, "models: unable to check if user_privilege exists")
}
return exists, nil
}