-
Notifications
You must be signed in to change notification settings - Fork 5
/
notification.xo.go
709 lines (614 loc) · 27.1 KB
/
notification.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
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
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"
)
// Notification represents a row from 'public.notifications'.
// 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 Notification struct {
NotificationID NotificationID `json:"notificationID" db:"notification_id" required:"true" nullable:"false"` // notification_id
ReceiverRank *int `json:"receiverRank" db:"receiver_rank"` // receiver_rank
Title string `json:"title" db:"title" required:"true" nullable:"false"` // title
Body string `json:"body" db:"body" required:"true" nullable:"false"` // body
Labels []string `json:"labels" db:"labels" required:"true" nullable:"false"` // labels
Link *string `json:"link" db:"link"` // link
CreatedAt time.Time `json:"createdAt" db:"created_at" required:"true" nullable:"false"` // created_at
Sender UserID `json:"sender" db:"sender" required:"true" nullable:"false"` // sender
Receiver *UserID `json:"receiver" db:"receiver"` // receiver
NotificationType NotificationType `json:"notificationType" db:"notification_type" required:"true" nullable:"false" ref:"#/components/schemas/NotificationType"` // notification_type
ReceiverJoin *User `json:"-" db:"user_receiver" openapi-go:"ignore"` // O2O users (generated from M2O)
SenderJoin *User `json:"-" db:"user_sender" openapi-go:"ignore"` // O2O users (generated from M2O)
NotificationUserNotificationsJoin *[]UserNotification `json:"-" db:"user_notifications" openapi-go:"ignore"` // M2O notifications
}
// NotificationCreateParams represents insert params for 'public.notifications'.
type NotificationCreateParams struct {
Body string `json:"body" required:"true" nullable:"false"` // body
Labels []string `json:"labels" required:"true" nullable:"false"` // labels
Link *string `json:"link"` // link
NotificationType NotificationType `json:"notificationType" required:"true" nullable:"false" ref:"#/components/schemas/NotificationType"` // notification_type
Receiver *UserID `json:"receiver"` // receiver
ReceiverRank *int `json:"receiverRank"` // receiver_rank
Sender UserID `json:"sender" required:"true" nullable:"false"` // sender
Title string `json:"title" required:"true" nullable:"false"` // title
}
type NotificationID int
// CreateNotification creates a new Notification in the database with the given params.
func CreateNotification(ctx context.Context, db DB, params *NotificationCreateParams) (*Notification, error) {
n := &Notification{
Body: params.Body,
Labels: params.Labels,
Link: params.Link,
NotificationType: params.NotificationType,
Receiver: params.Receiver,
ReceiverRank: params.ReceiverRank,
Sender: params.Sender,
Title: params.Title,
}
return n.Insert(ctx, db)
}
// NotificationUpdateParams represents update params for 'public.notifications'.
type NotificationUpdateParams struct {
Body *string `json:"body" nullable:"false"` // body
Labels *[]string `json:"labels" nullable:"false"` // labels
Link **string `json:"link"` // link
NotificationType *NotificationType `json:"notificationType" nullable:"false" ref:"#/components/schemas/NotificationType"` // notification_type
Receiver **UserID `json:"receiver"` // receiver
ReceiverRank **int `json:"receiverRank"` // receiver_rank
Sender *UserID `json:"sender" nullable:"false"` // sender
Title *string `json:"title" nullable:"false"` // title
}
// SetUpdateParams updates public.notifications struct fields with the specified params.
func (n *Notification) SetUpdateParams(params *NotificationUpdateParams) {
if params.Body != nil {
n.Body = *params.Body
}
if params.Labels != nil {
n.Labels = *params.Labels
}
if params.Link != nil {
n.Link = *params.Link
}
if params.NotificationType != nil {
n.NotificationType = *params.NotificationType
}
if params.Receiver != nil {
n.Receiver = *params.Receiver
}
if params.ReceiverRank != nil {
n.ReceiverRank = *params.ReceiverRank
}
if params.Sender != nil {
n.Sender = *params.Sender
}
if params.Title != nil {
n.Title = *params.Title
}
}
type NotificationSelectConfig struct {
limit string
orderBy string
joins NotificationJoins
filters map[string][]any
}
type NotificationSelectConfigOption func(*NotificationSelectConfig)
// WithNotificationLimit limits row selection.
func WithNotificationLimit(limit int) NotificationSelectConfigOption {
return func(s *NotificationSelectConfig) {
if limit > 0 {
s.limit = fmt.Sprintf(" limit %d ", limit)
}
}
}
type NotificationOrderBy string
const (
NotificationCreatedAtDescNullsFirst NotificationOrderBy = " created_at DESC NULLS FIRST "
NotificationCreatedAtDescNullsLast NotificationOrderBy = " created_at DESC NULLS LAST "
NotificationCreatedAtAscNullsFirst NotificationOrderBy = " created_at ASC NULLS FIRST "
NotificationCreatedAtAscNullsLast NotificationOrderBy = " created_at ASC NULLS LAST "
)
// WithNotificationOrderBy orders results by the given columns.
func WithNotificationOrderBy(rows ...NotificationOrderBy) NotificationSelectConfigOption {
return func(s *NotificationSelectConfig) {
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 NotificationJoins struct {
UserReceiver bool // O2O users
UserSender bool // O2O users
UserNotifications bool // M2O user_notifications
}
// WithNotificationJoin joins with the given tables.
func WithNotificationJoin(joins NotificationJoins) NotificationSelectConfigOption {
return func(s *NotificationSelectConfig) {
s.joins = NotificationJoins{
UserReceiver: s.joins.UserReceiver || joins.UserReceiver,
UserSender: s.joins.UserSender || joins.UserSender,
UserNotifications: s.joins.UserNotifications || joins.UserNotifications,
}
}
}
// WithNotificationFilters 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 WithNotificationFilters(filters map[string][]any) NotificationSelectConfigOption {
return func(s *NotificationSelectConfig) {
s.filters = filters
}
}
const notificationTableUserReceiverJoinSQL = `-- O2O join generated from "notifications_receiver_fkey (Generated from M2O)"
left join users as _notifications_receiver on _notifications_receiver.user_id = notifications.receiver
`
const notificationTableUserReceiverSelectSQL = `(case when _notifications_receiver.user_id is not null then row(_notifications_receiver.*) end) as user_receiver`
const notificationTableUserReceiverGroupBySQL = `_notifications_receiver.user_id,
_notifications_receiver.user_id,
notifications.notification_id`
const notificationTableUserSenderJoinSQL = `-- O2O join generated from "notifications_sender_fkey (Generated from M2O)"
left join users as _notifications_sender on _notifications_sender.user_id = notifications.sender
`
const notificationTableUserSenderSelectSQL = `(case when _notifications_sender.user_id is not null then row(_notifications_sender.*) end) as user_sender`
const notificationTableUserSenderGroupBySQL = `_notifications_sender.user_id,
_notifications_sender.user_id,
notifications.notification_id`
const notificationTableUserNotificationsJoinSQL = `-- M2O join generated from "user_notifications_notification_id_fkey"
left join (
select
notification_id as user_notifications_notification_id
, array_agg(user_notifications.*) as user_notifications
from
user_notifications
group by
notification_id
) as joined_user_notifications on joined_user_notifications.user_notifications_notification_id = notifications.notification_id
`
const notificationTableUserNotificationsSelectSQL = `COALESCE(joined_user_notifications.user_notifications, '{}') as user_notifications`
const notificationTableUserNotificationsGroupBySQL = `joined_user_notifications.user_notifications, notifications.notification_id`
// Insert inserts the Notification to the database.
func (n *Notification) Insert(ctx context.Context, db DB) (*Notification, error) {
// insert (primary key generated and returned by database)
sqlstr := `INSERT INTO public.notifications (
body, labels, link, notification_type, receiver, receiver_rank, sender, title
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8
) RETURNING * `
// run
logf(sqlstr, n.Body, n.Labels, n.Link, n.NotificationType, n.Receiver, n.ReceiverRank, n.Sender, n.Title)
rows, err := db.Query(ctx, sqlstr, n.Body, n.Labels, n.Link, n.NotificationType, n.Receiver, n.ReceiverRank, n.Sender, n.Title)
if err != nil {
return nil, logerror(fmt.Errorf("Notification/Insert/db.Query: %w", &XoError{Entity: "Notification", Err: err}))
}
newn, err := pgx.CollectOneRow(rows, pgx.RowToStructByNameLax[Notification])
if err != nil {
return nil, logerror(fmt.Errorf("Notification/Insert/pgx.CollectOneRow: %w", &XoError{Entity: "Notification", Err: err}))
}
*n = newn
return n, nil
}
// Update updates a Notification in the database.
func (n *Notification) Update(ctx context.Context, db DB) (*Notification, error) {
// update with composite primary key
sqlstr := `UPDATE public.notifications SET
body = $1, labels = $2, link = $3, notification_type = $4, receiver = $5, receiver_rank = $6, sender = $7, title = $8
WHERE notification_id = $9
RETURNING * `
// run
logf(sqlstr, n.Body, n.CreatedAt, n.Labels, n.Link, n.NotificationType, n.Receiver, n.ReceiverRank, n.Sender, n.Title, n.NotificationID)
rows, err := db.Query(ctx, sqlstr, n.Body, n.Labels, n.Link, n.NotificationType, n.Receiver, n.ReceiverRank, n.Sender, n.Title, n.NotificationID)
if err != nil {
return nil, logerror(fmt.Errorf("Notification/Update/db.Query: %w", &XoError{Entity: "Notification", Err: err}))
}
newn, err := pgx.CollectOneRow(rows, pgx.RowToStructByNameLax[Notification])
if err != nil {
return nil, logerror(fmt.Errorf("Notification/Update/pgx.CollectOneRow: %w", &XoError{Entity: "Notification", Err: err}))
}
*n = newn
return n, nil
}
// Upsert upserts a Notification in the database.
// Requires appropriate PK(s) to be set beforehand.
func (n *Notification) Upsert(ctx context.Context, db DB, params *NotificationCreateParams) (*Notification, error) {
var err error
n.Body = params.Body
n.Labels = params.Labels
n.Link = params.Link
n.NotificationType = params.NotificationType
n.Receiver = params.Receiver
n.ReceiverRank = params.ReceiverRank
n.Sender = params.Sender
n.Title = params.Title
n, err = n.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: "Notification", Err: err})
}
n, err = n.Update(ctx, db)
if err != nil {
return nil, fmt.Errorf("UpsertUser/Update: %w", &XoError{Entity: "Notification", Err: err})
}
}
}
return n, err
}
// Delete deletes the Notification from the database.
func (n *Notification) Delete(ctx context.Context, db DB) error {
// delete with single primary key
sqlstr := `DELETE FROM public.notifications
WHERE notification_id = $1 `
// run
if _, err := db.Exec(ctx, sqlstr, n.NotificationID); err != nil {
return logerror(err)
}
return nil
}
// NotificationPaginatedByNotificationIDAsc returns a cursor-paginated list of Notification in Asc order.
func NotificationPaginatedByNotificationIDAsc(ctx context.Context, db DB, notificationID NotificationID, opts ...NotificationSelectConfigOption) ([]Notification, error) {
c := &NotificationSelectConfig{joins: NotificationJoins{}, 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.UserReceiver {
selectClauses = append(selectClauses, notificationTableUserReceiverSelectSQL)
joinClauses = append(joinClauses, notificationTableUserReceiverJoinSQL)
groupByClauses = append(groupByClauses, notificationTableUserReceiverGroupBySQL)
}
if c.joins.UserSender {
selectClauses = append(selectClauses, notificationTableUserSenderSelectSQL)
joinClauses = append(joinClauses, notificationTableUserSenderJoinSQL)
groupByClauses = append(groupByClauses, notificationTableUserSenderGroupBySQL)
}
if c.joins.UserNotifications {
selectClauses = append(selectClauses, notificationTableUserNotificationsSelectSQL)
joinClauses = append(joinClauses, notificationTableUserNotificationsJoinSQL)
groupByClauses = append(groupByClauses, notificationTableUserNotificationsGroupBySQL)
}
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
notifications.body,
notifications.created_at,
notifications.labels,
notifications.link,
notifications.notification_id,
notifications.notification_type,
notifications.receiver,
notifications.receiver_rank,
notifications.sender,
notifications.title %s
FROM public.notifications %s
WHERE notifications.notification_id > $1
%s %s
ORDER BY
notification_id Asc`, selects, joins, filters, groupbys)
sqlstr += c.limit
sqlstr = "/* NotificationPaginatedByNotificationIDAsc */\n" + sqlstr
// run
rows, err := db.Query(ctx, sqlstr, append([]any{notificationID}, filterParams...)...)
if err != nil {
return nil, logerror(fmt.Errorf("Notification/Paginated/Asc/db.Query: %w", &XoError{Entity: "Notification", Err: err}))
}
res, err := pgx.CollectRows(rows, pgx.RowToStructByNameLax[Notification])
if err != nil {
return nil, logerror(fmt.Errorf("Notification/Paginated/Asc/pgx.CollectRows: %w", &XoError{Entity: "Notification", Err: err}))
}
return res, nil
}
// NotificationPaginatedByNotificationIDDesc returns a cursor-paginated list of Notification in Desc order.
func NotificationPaginatedByNotificationIDDesc(ctx context.Context, db DB, notificationID NotificationID, opts ...NotificationSelectConfigOption) ([]Notification, error) {
c := &NotificationSelectConfig{joins: NotificationJoins{}, 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.UserReceiver {
selectClauses = append(selectClauses, notificationTableUserReceiverSelectSQL)
joinClauses = append(joinClauses, notificationTableUserReceiverJoinSQL)
groupByClauses = append(groupByClauses, notificationTableUserReceiverGroupBySQL)
}
if c.joins.UserSender {
selectClauses = append(selectClauses, notificationTableUserSenderSelectSQL)
joinClauses = append(joinClauses, notificationTableUserSenderJoinSQL)
groupByClauses = append(groupByClauses, notificationTableUserSenderGroupBySQL)
}
if c.joins.UserNotifications {
selectClauses = append(selectClauses, notificationTableUserNotificationsSelectSQL)
joinClauses = append(joinClauses, notificationTableUserNotificationsJoinSQL)
groupByClauses = append(groupByClauses, notificationTableUserNotificationsGroupBySQL)
}
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
notifications.body,
notifications.created_at,
notifications.labels,
notifications.link,
notifications.notification_id,
notifications.notification_type,
notifications.receiver,
notifications.receiver_rank,
notifications.sender,
notifications.title %s
FROM public.notifications %s
WHERE notifications.notification_id < $1
%s %s
ORDER BY
notification_id Desc`, selects, joins, filters, groupbys)
sqlstr += c.limit
sqlstr = "/* NotificationPaginatedByNotificationIDDesc */\n" + sqlstr
// run
rows, err := db.Query(ctx, sqlstr, append([]any{notificationID}, filterParams...)...)
if err != nil {
return nil, logerror(fmt.Errorf("Notification/Paginated/Desc/db.Query: %w", &XoError{Entity: "Notification", Err: err}))
}
res, err := pgx.CollectRows(rows, pgx.RowToStructByNameLax[Notification])
if err != nil {
return nil, logerror(fmt.Errorf("Notification/Paginated/Desc/pgx.CollectRows: %w", &XoError{Entity: "Notification", Err: err}))
}
return res, nil
}
// NotificationByNotificationID retrieves a row from 'public.notifications' as a Notification.
//
// Generated from index 'notifications_pkey'.
func NotificationByNotificationID(ctx context.Context, db DB, notificationID NotificationID, opts ...NotificationSelectConfigOption) (*Notification, error) {
c := &NotificationSelectConfig{joins: NotificationJoins{}, 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.UserReceiver {
selectClauses = append(selectClauses, notificationTableUserReceiverSelectSQL)
joinClauses = append(joinClauses, notificationTableUserReceiverJoinSQL)
groupByClauses = append(groupByClauses, notificationTableUserReceiverGroupBySQL)
}
if c.joins.UserSender {
selectClauses = append(selectClauses, notificationTableUserSenderSelectSQL)
joinClauses = append(joinClauses, notificationTableUserSenderJoinSQL)
groupByClauses = append(groupByClauses, notificationTableUserSenderGroupBySQL)
}
if c.joins.UserNotifications {
selectClauses = append(selectClauses, notificationTableUserNotificationsSelectSQL)
joinClauses = append(joinClauses, notificationTableUserNotificationsJoinSQL)
groupByClauses = append(groupByClauses, notificationTableUserNotificationsGroupBySQL)
}
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
notifications.body,
notifications.created_at,
notifications.labels,
notifications.link,
notifications.notification_id,
notifications.notification_type,
notifications.receiver,
notifications.receiver_rank,
notifications.sender,
notifications.title %s
FROM public.notifications %s
WHERE notifications.notification_id = $1
%s %s
`, selects, joins, filters, groupbys)
sqlstr += c.orderBy
sqlstr += c.limit
sqlstr = "/* NotificationByNotificationID */\n" + sqlstr
// run
// logf(sqlstr, notificationID)
rows, err := db.Query(ctx, sqlstr, append([]any{notificationID}, filterParams...)...)
if err != nil {
return nil, logerror(fmt.Errorf("notifications/NotificationByNotificationID/db.Query: %w", &XoError{Entity: "Notification", Err: err}))
}
n, err := pgx.CollectOneRow(rows, pgx.RowToStructByNameLax[Notification])
if err != nil {
return nil, logerror(fmt.Errorf("notifications/NotificationByNotificationID/pgx.CollectOneRow: %w", &XoError{Entity: "Notification", Err: err}))
}
return &n, nil
}
// NotificationsByReceiverRankNotificationTypeCreatedAt retrieves a row from 'public.notifications' as a Notification.
//
// Generated from index 'notifications_receiver_rank_notification_type_created_at_idx'.
func NotificationsByReceiverRankNotificationTypeCreatedAt(ctx context.Context, db DB, receiverRank *int, notificationType NotificationType, createdAt time.Time, opts ...NotificationSelectConfigOption) ([]Notification, error) {
c := &NotificationSelectConfig{joins: NotificationJoins{}, filters: make(map[string][]any)}
for _, o := range opts {
o(c)
}
paramStart := 3
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.UserReceiver {
selectClauses = append(selectClauses, notificationTableUserReceiverSelectSQL)
joinClauses = append(joinClauses, notificationTableUserReceiverJoinSQL)
groupByClauses = append(groupByClauses, notificationTableUserReceiverGroupBySQL)
}
if c.joins.UserSender {
selectClauses = append(selectClauses, notificationTableUserSenderSelectSQL)
joinClauses = append(joinClauses, notificationTableUserSenderJoinSQL)
groupByClauses = append(groupByClauses, notificationTableUserSenderGroupBySQL)
}
if c.joins.UserNotifications {
selectClauses = append(selectClauses, notificationTableUserNotificationsSelectSQL)
joinClauses = append(joinClauses, notificationTableUserNotificationsJoinSQL)
groupByClauses = append(groupByClauses, notificationTableUserNotificationsGroupBySQL)
}
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
notifications.body,
notifications.created_at,
notifications.labels,
notifications.link,
notifications.notification_id,
notifications.notification_type,
notifications.receiver,
notifications.receiver_rank,
notifications.sender,
notifications.title %s
FROM public.notifications %s
WHERE notifications.receiver_rank = $1 AND notifications.notification_type = $2 AND notifications.created_at = $3
%s %s
`, selects, joins, filters, groupbys)
sqlstr += c.orderBy
sqlstr += c.limit
sqlstr = "/* NotificationsByReceiverRankNotificationTypeCreatedAt */\n" + sqlstr
// run
// logf(sqlstr, receiverRank, notificationType, createdAt)
rows, err := db.Query(ctx, sqlstr, append([]any{receiverRank, notificationType, createdAt}, filterParams...)...)
if err != nil {
return nil, logerror(fmt.Errorf("Notification/NotificationsByReceiverRankNotificationTypeCreatedAt/Query: %w", &XoError{Entity: "Notification", Err: err}))
}
defer rows.Close()
// process
res, err := pgx.CollectRows(rows, pgx.RowToStructByNameLax[Notification])
if err != nil {
return nil, logerror(fmt.Errorf("Notification/NotificationsByReceiverRankNotificationTypeCreatedAt/pgx.CollectRows: %w", &XoError{Entity: "Notification", Err: err}))
}
return res, nil
}
// FKUser_Receiver returns the User associated with the Notification's (Receiver).
//
// Generated from foreign key 'notifications_receiver_fkey'.
func (n *Notification) FKUser_Receiver(ctx context.Context, db DB) (*User, error) {
return UserByUserID(ctx, db, *n.Receiver)
}
// FKUser_Sender returns the User associated with the Notification's (Sender).
//
// Generated from foreign key 'notifications_sender_fkey'.
func (n *Notification) FKUser_Sender(ctx context.Context, db DB) (*User, error) {
return UserByUserID(ctx, db, n.Sender)
}