-
Notifications
You must be signed in to change notification settings - Fork 5
/
userapikey.xo.go
634 lines (543 loc) · 19.7 KB
/
userapikey.xo.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
package db
// Code generated by xo. DO NOT EDIT.
import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/jackc/pgconn"
"github.com/jackc/pgerrcode"
"github.com/jackc/pgx/v5"
)
// UserAPIKey represents a row from 'public.user_api_keys'.
// Change properties via SQL column comments, joined with " && ":
// - "properties":<p1>,<p2>,...
// - private to exclude a field from JSON.
// - not-required to make a schema field not required.
// - "type":<pkg.type> to override the type annotation. An openapi schema named <type> must exist.
// - "cardinality":<O2O|M2O|M2M> to generate/override joins explicitly. Only O2O is inferred.
// - "tags":<tags> to append literal struct tag strings.
type UserAPIKey struct {
UserAPIKeyID UserAPIKeyID `json:"-" db:"user_api_key_id" nullable:"false"` // user_api_key_id
APIKey string `json:"apiKey" db:"api_key" required:"true" nullable:"false"` // api_key
ExpiresOn time.Time `json:"expiresOn" db:"expires_on" required:"true" nullable:"false"` // expires_on
UserID UserID `json:"userID" db:"user_id" required:"true" nullable:"false"` // user_id
UserJoin *User `json:"-" db:"user_user_id" openapi-go:"ignore"` // O2O users (inferred)
}
// UserAPIKeyCreateParams represents insert params for 'public.user_api_keys'.
type UserAPIKeyCreateParams struct {
APIKey string `json:"apiKey" required:"true" nullable:"false"` // api_key
ExpiresOn time.Time `json:"expiresOn" required:"true" nullable:"false"` // expires_on
UserID UserID `json:"userID" required:"true" nullable:"false"` // user_id
}
type UserAPIKeyID int
// CreateUserAPIKey creates a new UserAPIKey in the database with the given params.
func CreateUserAPIKey(ctx context.Context, db DB, params *UserAPIKeyCreateParams) (*UserAPIKey, error) {
uak := &UserAPIKey{
APIKey: params.APIKey,
ExpiresOn: params.ExpiresOn,
UserID: params.UserID,
}
return uak.Insert(ctx, db)
}
// UserAPIKeyUpdateParams represents update params for 'public.user_api_keys'.
type UserAPIKeyUpdateParams struct {
APIKey *string `json:"apiKey" nullable:"false"` // api_key
ExpiresOn *time.Time `json:"expiresOn" nullable:"false"` // expires_on
UserID *UserID `json:"userID" nullable:"false"` // user_id
}
// SetUpdateParams updates public.user_api_keys struct fields with the specified params.
func (uak *UserAPIKey) SetUpdateParams(params *UserAPIKeyUpdateParams) {
if params.APIKey != nil {
uak.APIKey = *params.APIKey
}
if params.ExpiresOn != nil {
uak.ExpiresOn = *params.ExpiresOn
}
if params.UserID != nil {
uak.UserID = *params.UserID
}
}
type UserAPIKeySelectConfig struct {
limit string
orderBy string
joins UserAPIKeyJoins
filters map[string][]any
}
type UserAPIKeySelectConfigOption func(*UserAPIKeySelectConfig)
// WithUserAPIKeyLimit limits row selection.
func WithUserAPIKeyLimit(limit int) UserAPIKeySelectConfigOption {
return func(s *UserAPIKeySelectConfig) {
if limit > 0 {
s.limit = fmt.Sprintf(" limit %d ", limit)
}
}
}
type UserAPIKeyOrderBy string
const (
UserAPIKeyExpiresOnDescNullsFirst UserAPIKeyOrderBy = " expires_on DESC NULLS FIRST "
UserAPIKeyExpiresOnDescNullsLast UserAPIKeyOrderBy = " expires_on DESC NULLS LAST "
UserAPIKeyExpiresOnAscNullsFirst UserAPIKeyOrderBy = " expires_on ASC NULLS FIRST "
UserAPIKeyExpiresOnAscNullsLast UserAPIKeyOrderBy = " expires_on ASC NULLS LAST "
)
// WithUserAPIKeyOrderBy orders results by the given columns.
func WithUserAPIKeyOrderBy(rows ...UserAPIKeyOrderBy) UserAPIKeySelectConfigOption {
return func(s *UserAPIKeySelectConfig) {
if len(rows) > 0 {
orderStrings := make([]string, len(rows))
for i, row := range rows {
orderStrings[i] = string(row)
}
s.orderBy = " order by "
s.orderBy += strings.Join(orderStrings, ", ")
}
}
}
type UserAPIKeyJoins struct {
User bool // O2O users
}
// WithUserAPIKeyJoin joins with the given tables.
func WithUserAPIKeyJoin(joins UserAPIKeyJoins) UserAPIKeySelectConfigOption {
return func(s *UserAPIKeySelectConfig) {
s.joins = UserAPIKeyJoins{
User: s.joins.User || joins.User,
}
}
}
// WithUserAPIKeyFilters adds the given filters, which can be dynamically parameterized
// with $i to prevent SQL injection.
// Example:
//
// filters := map[string][]any{
// "NOT (col.name = any ($i))": {[]string{"excl_name_1", "excl_name_2"}},
// `(col.created_at > $i OR
// col.is_closed = $i)`: {time.Now().Add(-24 * time.Hour), true},
// }
func WithUserAPIKeyFilters(filters map[string][]any) UserAPIKeySelectConfigOption {
return func(s *UserAPIKeySelectConfig) {
s.filters = filters
}
}
const userAPIKeyTableUserJoinSQL = `-- O2O join generated from "user_api_keys_user_id_fkey (inferred)"
left join users as _user_api_keys_user_id on _user_api_keys_user_id.user_id = user_api_keys.user_id
`
const userAPIKeyTableUserSelectSQL = `(case when _user_api_keys_user_id.user_id is not null then row(_user_api_keys_user_id.*) end) as user_user_id`
const userAPIKeyTableUserGroupBySQL = `_user_api_keys_user_id.user_id,
_user_api_keys_user_id.user_id,
user_api_keys.user_api_key_id`
// Insert inserts the UserAPIKey to the database.
func (uak *UserAPIKey) Insert(ctx context.Context, db DB) (*UserAPIKey, error) {
// insert (primary key generated and returned by database)
sqlstr := `INSERT INTO public.user_api_keys (
api_key, expires_on, user_id
) VALUES (
$1, $2, $3
) RETURNING * `
// run
logf(sqlstr, uak.APIKey, uak.ExpiresOn, uak.UserID)
rows, err := db.Query(ctx, sqlstr, uak.APIKey, uak.ExpiresOn, uak.UserID)
if err != nil {
return nil, logerror(fmt.Errorf("UserAPIKey/Insert/db.Query: %w", &XoError{Entity: "User api key", Err: err}))
}
newuak, err := pgx.CollectOneRow(rows, pgx.RowToStructByNameLax[UserAPIKey])
if err != nil {
return nil, logerror(fmt.Errorf("UserAPIKey/Insert/pgx.CollectOneRow: %w", &XoError{Entity: "User api key", Err: err}))
}
*uak = newuak
return uak, nil
}
// Update updates a UserAPIKey in the database.
func (uak *UserAPIKey) Update(ctx context.Context, db DB) (*UserAPIKey, error) {
// update with composite primary key
sqlstr := `UPDATE public.user_api_keys SET
api_key = $1, expires_on = $2, user_id = $3
WHERE user_api_key_id = $4
RETURNING * `
// run
logf(sqlstr, uak.APIKey, uak.ExpiresOn, uak.UserID, uak.UserAPIKeyID)
rows, err := db.Query(ctx, sqlstr, uak.APIKey, uak.ExpiresOn, uak.UserID, uak.UserAPIKeyID)
if err != nil {
return nil, logerror(fmt.Errorf("UserAPIKey/Update/db.Query: %w", &XoError{Entity: "User api key", Err: err}))
}
newuak, err := pgx.CollectOneRow(rows, pgx.RowToStructByNameLax[UserAPIKey])
if err != nil {
return nil, logerror(fmt.Errorf("UserAPIKey/Update/pgx.CollectOneRow: %w", &XoError{Entity: "User api key", Err: err}))
}
*uak = newuak
return uak, nil
}
// Upsert upserts a UserAPIKey in the database.
// Requires appropriate PK(s) to be set beforehand.
func (uak *UserAPIKey) Upsert(ctx context.Context, db DB, params *UserAPIKeyCreateParams) (*UserAPIKey, error) {
var err error
uak.APIKey = params.APIKey
uak.ExpiresOn = params.ExpiresOn
uak.UserID = params.UserID
uak, err = uak.Insert(ctx, db)
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
if pgErr.Code != pgerrcode.UniqueViolation {
return nil, fmt.Errorf("UpsertUser/Insert: %w", &XoError{Entity: "User api key", Err: err})
}
uak, err = uak.Update(ctx, db)
if err != nil {
return nil, fmt.Errorf("UpsertUser/Update: %w", &XoError{Entity: "User api key", Err: err})
}
}
}
return uak, err
}
// Delete deletes the UserAPIKey from the database.
func (uak *UserAPIKey) Delete(ctx context.Context, db DB) error {
// delete with single primary key
sqlstr := `DELETE FROM public.user_api_keys
WHERE user_api_key_id = $1 `
// run
if _, err := db.Exec(ctx, sqlstr, uak.UserAPIKeyID); err != nil {
return logerror(err)
}
return nil
}
// UserAPIKeyPaginatedByUserAPIKeyIDAsc returns a cursor-paginated list of UserAPIKey in Asc order.
func UserAPIKeyPaginatedByUserAPIKeyIDAsc(ctx context.Context, db DB, userAPIKeyID UserAPIKeyID, opts ...UserAPIKeySelectConfigOption) ([]UserAPIKey, error) {
c := &UserAPIKeySelectConfig{joins: UserAPIKeyJoins{}, filters: make(map[string][]any)}
for _, o := range opts {
o(c)
}
paramStart := 1
nth := func() string {
paramStart++
return strconv.Itoa(paramStart)
}
var filterClauses []string
var filterParams []any
for filterTmpl, params := range c.filters {
filter := filterTmpl
for strings.Contains(filter, "$i") {
filter = strings.Replace(filter, "$i", "$"+nth(), 1)
}
filterClauses = append(filterClauses, filter)
filterParams = append(filterParams, params...)
}
filters := ""
if len(filterClauses) > 0 {
filters = " AND " + strings.Join(filterClauses, " AND ") + " "
}
var selectClauses []string
var joinClauses []string
var groupByClauses []string
if c.joins.User {
selectClauses = append(selectClauses, userAPIKeyTableUserSelectSQL)
joinClauses = append(joinClauses, userAPIKeyTableUserJoinSQL)
groupByClauses = append(groupByClauses, userAPIKeyTableUserGroupBySQL)
}
selects := ""
if len(selectClauses) > 0 {
selects = ", " + strings.Join(selectClauses, " ,\n ") + " "
}
joins := strings.Join(joinClauses, " \n ") + " "
groupbys := ""
if len(groupByClauses) > 0 {
groupbys = "GROUP BY " + strings.Join(groupByClauses, " ,\n ") + " "
}
sqlstr := fmt.Sprintf(`SELECT
user_api_keys.api_key,
user_api_keys.expires_on,
user_api_keys.user_api_key_id,
user_api_keys.user_id %s
FROM public.user_api_keys %s
WHERE user_api_keys.user_api_key_id > $1
%s %s
ORDER BY
user_api_key_id Asc`, selects, joins, filters, groupbys)
sqlstr += c.limit
sqlstr = "/* UserAPIKeyPaginatedByUserAPIKeyIDAsc */\n" + sqlstr
// run
rows, err := db.Query(ctx, sqlstr, append([]any{userAPIKeyID}, filterParams...)...)
if err != nil {
return nil, logerror(fmt.Errorf("UserAPIKey/Paginated/Asc/db.Query: %w", &XoError{Entity: "User api key", Err: err}))
}
res, err := pgx.CollectRows(rows, pgx.RowToStructByNameLax[UserAPIKey])
if err != nil {
return nil, logerror(fmt.Errorf("UserAPIKey/Paginated/Asc/pgx.CollectRows: %w", &XoError{Entity: "User api key", Err: err}))
}
return res, nil
}
// UserAPIKeyPaginatedByUserAPIKeyIDDesc returns a cursor-paginated list of UserAPIKey in Desc order.
func UserAPIKeyPaginatedByUserAPIKeyIDDesc(ctx context.Context, db DB, userAPIKeyID UserAPIKeyID, opts ...UserAPIKeySelectConfigOption) ([]UserAPIKey, error) {
c := &UserAPIKeySelectConfig{joins: UserAPIKeyJoins{}, filters: make(map[string][]any)}
for _, o := range opts {
o(c)
}
paramStart := 1
nth := func() string {
paramStart++
return strconv.Itoa(paramStart)
}
var filterClauses []string
var filterParams []any
for filterTmpl, params := range c.filters {
filter := filterTmpl
for strings.Contains(filter, "$i") {
filter = strings.Replace(filter, "$i", "$"+nth(), 1)
}
filterClauses = append(filterClauses, filter)
filterParams = append(filterParams, params...)
}
filters := ""
if len(filterClauses) > 0 {
filters = " AND " + strings.Join(filterClauses, " AND ") + " "
}
var selectClauses []string
var joinClauses []string
var groupByClauses []string
if c.joins.User {
selectClauses = append(selectClauses, userAPIKeyTableUserSelectSQL)
joinClauses = append(joinClauses, userAPIKeyTableUserJoinSQL)
groupByClauses = append(groupByClauses, userAPIKeyTableUserGroupBySQL)
}
selects := ""
if len(selectClauses) > 0 {
selects = ", " + strings.Join(selectClauses, " ,\n ") + " "
}
joins := strings.Join(joinClauses, " \n ") + " "
groupbys := ""
if len(groupByClauses) > 0 {
groupbys = "GROUP BY " + strings.Join(groupByClauses, " ,\n ") + " "
}
sqlstr := fmt.Sprintf(`SELECT
user_api_keys.api_key,
user_api_keys.expires_on,
user_api_keys.user_api_key_id,
user_api_keys.user_id %s
FROM public.user_api_keys %s
WHERE user_api_keys.user_api_key_id < $1
%s %s
ORDER BY
user_api_key_id Desc`, selects, joins, filters, groupbys)
sqlstr += c.limit
sqlstr = "/* UserAPIKeyPaginatedByUserAPIKeyIDDesc */\n" + sqlstr
// run
rows, err := db.Query(ctx, sqlstr, append([]any{userAPIKeyID}, filterParams...)...)
if err != nil {
return nil, logerror(fmt.Errorf("UserAPIKey/Paginated/Desc/db.Query: %w", &XoError{Entity: "User api key", Err: err}))
}
res, err := pgx.CollectRows(rows, pgx.RowToStructByNameLax[UserAPIKey])
if err != nil {
return nil, logerror(fmt.Errorf("UserAPIKey/Paginated/Desc/pgx.CollectRows: %w", &XoError{Entity: "User api key", Err: err}))
}
return res, nil
}
// UserAPIKeyByAPIKey retrieves a row from 'public.user_api_keys' as a UserAPIKey.
//
// Generated from index 'user_api_keys_api_key_key'.
func UserAPIKeyByAPIKey(ctx context.Context, db DB, apiKey string, opts ...UserAPIKeySelectConfigOption) (*UserAPIKey, error) {
c := &UserAPIKeySelectConfig{joins: UserAPIKeyJoins{}, filters: make(map[string][]any)}
for _, o := range opts {
o(c)
}
paramStart := 1
nth := func() string {
paramStart++
return strconv.Itoa(paramStart)
}
var filterClauses []string
var filterParams []any
for filterTmpl, params := range c.filters {
filter := filterTmpl
for strings.Contains(filter, "$i") {
filter = strings.Replace(filter, "$i", "$"+nth(), 1)
}
filterClauses = append(filterClauses, filter)
filterParams = append(filterParams, params...)
}
filters := ""
if len(filterClauses) > 0 {
filters = " AND " + strings.Join(filterClauses, " AND ") + " "
}
var selectClauses []string
var joinClauses []string
var groupByClauses []string
if c.joins.User {
selectClauses = append(selectClauses, userAPIKeyTableUserSelectSQL)
joinClauses = append(joinClauses, userAPIKeyTableUserJoinSQL)
groupByClauses = append(groupByClauses, userAPIKeyTableUserGroupBySQL)
}
selects := ""
if len(selectClauses) > 0 {
selects = ", " + strings.Join(selectClauses, " ,\n ") + " "
}
joins := strings.Join(joinClauses, " \n ") + " "
groupbys := ""
if len(groupByClauses) > 0 {
groupbys = "GROUP BY " + strings.Join(groupByClauses, " ,\n ") + " "
}
sqlstr := fmt.Sprintf(`SELECT
user_api_keys.api_key,
user_api_keys.expires_on,
user_api_keys.user_api_key_id,
user_api_keys.user_id %s
FROM public.user_api_keys %s
WHERE user_api_keys.api_key = $1
%s %s
`, selects, joins, filters, groupbys)
sqlstr += c.orderBy
sqlstr += c.limit
sqlstr = "/* UserAPIKeyByAPIKey */\n" + sqlstr
// run
// logf(sqlstr, apiKey)
rows, err := db.Query(ctx, sqlstr, append([]any{apiKey}, filterParams...)...)
if err != nil {
return nil, logerror(fmt.Errorf("user_api_keys/UserAPIKeyByAPIKey/db.Query: %w", &XoError{Entity: "User api key", Err: err}))
}
uak, err := pgx.CollectOneRow(rows, pgx.RowToStructByNameLax[UserAPIKey])
if err != nil {
return nil, logerror(fmt.Errorf("user_api_keys/UserAPIKeyByAPIKey/pgx.CollectOneRow: %w", &XoError{Entity: "User api key", Err: err}))
}
return &uak, nil
}
// UserAPIKeyByUserAPIKeyID retrieves a row from 'public.user_api_keys' as a UserAPIKey.
//
// Generated from index 'user_api_keys_pkey'.
func UserAPIKeyByUserAPIKeyID(ctx context.Context, db DB, userAPIKeyID UserAPIKeyID, opts ...UserAPIKeySelectConfigOption) (*UserAPIKey, error) {
c := &UserAPIKeySelectConfig{joins: UserAPIKeyJoins{}, filters: make(map[string][]any)}
for _, o := range opts {
o(c)
}
paramStart := 1
nth := func() string {
paramStart++
return strconv.Itoa(paramStart)
}
var filterClauses []string
var filterParams []any
for filterTmpl, params := range c.filters {
filter := filterTmpl
for strings.Contains(filter, "$i") {
filter = strings.Replace(filter, "$i", "$"+nth(), 1)
}
filterClauses = append(filterClauses, filter)
filterParams = append(filterParams, params...)
}
filters := ""
if len(filterClauses) > 0 {
filters = " AND " + strings.Join(filterClauses, " AND ") + " "
}
var selectClauses []string
var joinClauses []string
var groupByClauses []string
if c.joins.User {
selectClauses = append(selectClauses, userAPIKeyTableUserSelectSQL)
joinClauses = append(joinClauses, userAPIKeyTableUserJoinSQL)
groupByClauses = append(groupByClauses, userAPIKeyTableUserGroupBySQL)
}
selects := ""
if len(selectClauses) > 0 {
selects = ", " + strings.Join(selectClauses, " ,\n ") + " "
}
joins := strings.Join(joinClauses, " \n ") + " "
groupbys := ""
if len(groupByClauses) > 0 {
groupbys = "GROUP BY " + strings.Join(groupByClauses, " ,\n ") + " "
}
sqlstr := fmt.Sprintf(`SELECT
user_api_keys.api_key,
user_api_keys.expires_on,
user_api_keys.user_api_key_id,
user_api_keys.user_id %s
FROM public.user_api_keys %s
WHERE user_api_keys.user_api_key_id = $1
%s %s
`, selects, joins, filters, groupbys)
sqlstr += c.orderBy
sqlstr += c.limit
sqlstr = "/* UserAPIKeyByUserAPIKeyID */\n" + sqlstr
// run
// logf(sqlstr, userAPIKeyID)
rows, err := db.Query(ctx, sqlstr, append([]any{userAPIKeyID}, filterParams...)...)
if err != nil {
return nil, logerror(fmt.Errorf("user_api_keys/UserAPIKeyByUserAPIKeyID/db.Query: %w", &XoError{Entity: "User api key", Err: err}))
}
uak, err := pgx.CollectOneRow(rows, pgx.RowToStructByNameLax[UserAPIKey])
if err != nil {
return nil, logerror(fmt.Errorf("user_api_keys/UserAPIKeyByUserAPIKeyID/pgx.CollectOneRow: %w", &XoError{Entity: "User api key", Err: err}))
}
return &uak, nil
}
// UserAPIKeyByUserID retrieves a row from 'public.user_api_keys' as a UserAPIKey.
//
// Generated from index 'user_api_keys_user_id_key'.
func UserAPIKeyByUserID(ctx context.Context, db DB, userID UserID, opts ...UserAPIKeySelectConfigOption) (*UserAPIKey, error) {
c := &UserAPIKeySelectConfig{joins: UserAPIKeyJoins{}, filters: make(map[string][]any)}
for _, o := range opts {
o(c)
}
paramStart := 1
nth := func() string {
paramStart++
return strconv.Itoa(paramStart)
}
var filterClauses []string
var filterParams []any
for filterTmpl, params := range c.filters {
filter := filterTmpl
for strings.Contains(filter, "$i") {
filter = strings.Replace(filter, "$i", "$"+nth(), 1)
}
filterClauses = append(filterClauses, filter)
filterParams = append(filterParams, params...)
}
filters := ""
if len(filterClauses) > 0 {
filters = " AND " + strings.Join(filterClauses, " AND ") + " "
}
var selectClauses []string
var joinClauses []string
var groupByClauses []string
if c.joins.User {
selectClauses = append(selectClauses, userAPIKeyTableUserSelectSQL)
joinClauses = append(joinClauses, userAPIKeyTableUserJoinSQL)
groupByClauses = append(groupByClauses, userAPIKeyTableUserGroupBySQL)
}
selects := ""
if len(selectClauses) > 0 {
selects = ", " + strings.Join(selectClauses, " ,\n ") + " "
}
joins := strings.Join(joinClauses, " \n ") + " "
groupbys := ""
if len(groupByClauses) > 0 {
groupbys = "GROUP BY " + strings.Join(groupByClauses, " ,\n ") + " "
}
sqlstr := fmt.Sprintf(`SELECT
user_api_keys.api_key,
user_api_keys.expires_on,
user_api_keys.user_api_key_id,
user_api_keys.user_id %s
FROM public.user_api_keys %s
WHERE user_api_keys.user_id = $1
%s %s
`, selects, joins, filters, groupbys)
sqlstr += c.orderBy
sqlstr += c.limit
sqlstr = "/* UserAPIKeyByUserID */\n" + sqlstr
// run
// logf(sqlstr, userID)
rows, err := db.Query(ctx, sqlstr, append([]any{userID}, filterParams...)...)
if err != nil {
return nil, logerror(fmt.Errorf("user_api_keys/UserAPIKeyByUserID/db.Query: %w", &XoError{Entity: "User api key", Err: err}))
}
uak, err := pgx.CollectOneRow(rows, pgx.RowToStructByNameLax[UserAPIKey])
if err != nil {
return nil, logerror(fmt.Errorf("user_api_keys/UserAPIKeyByUserID/pgx.CollectOneRow: %w", &XoError{Entity: "User api key", Err: err}))
}
return &uak, nil
}
// FKUser_UserID returns the User associated with the UserAPIKey's (UserID).
//
// Generated from foreign key 'user_api_keys_user_id_fkey'.
func (uak *UserAPIKey) FKUser_UserID(ctx context.Context, db DB) (*User, error) {
return UserByUserID(ctx, db, uak.UserID)
}