-
Notifications
You must be signed in to change notification settings - Fork 5
/
workitemcomment.xo.go
615 lines (527 loc) · 21.9 KB
/
workitemcomment.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
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"
)
// WorkItemComment represents a row from 'public.work_item_comments'.
// 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 WorkItemComment struct {
WorkItemCommentID WorkItemCommentID `json:"workItemCommentID" db:"work_item_comment_id" required:"true" nullable:"false"` // work_item_comment_id
WorkItemID WorkItemID `json:"workItemID" db:"work_item_id" required:"true" nullable:"false"` // work_item_id
UserID UserID `json:"userID" db:"user_id" required:"true" nullable:"false"` // user_id
Message string `json:"message" db:"message" required:"true" nullable:"false"` // message
CreatedAt time.Time `json:"createdAt" db:"created_at" required:"true" nullable:"false"` // created_at
UpdatedAt time.Time `json:"updatedAt" db:"updated_at" required:"true" nullable:"false"` // updated_at
UserJoin *User `json:"-" db:"user_user_id" openapi-go:"ignore"` // O2O users (generated from M2O)
WorkItemJoin *WorkItem `json:"-" db:"work_item_work_item_id" openapi-go:"ignore"` // O2O work_items (generated from M2O)
}
// WorkItemCommentCreateParams represents insert params for 'public.work_item_comments'.
type WorkItemCommentCreateParams struct {
Message string `json:"message" required:"true" nullable:"false"` // message
UserID UserID `json:"userID" required:"true" nullable:"false"` // user_id
WorkItemID WorkItemID `json:"workItemID" required:"true" nullable:"false"` // work_item_id
}
type WorkItemCommentID int
// CreateWorkItemComment creates a new WorkItemComment in the database with the given params.
func CreateWorkItemComment(ctx context.Context, db DB, params *WorkItemCommentCreateParams) (*WorkItemComment, error) {
wic := &WorkItemComment{
Message: params.Message,
UserID: params.UserID,
WorkItemID: params.WorkItemID,
}
return wic.Insert(ctx, db)
}
// WorkItemCommentUpdateParams represents update params for 'public.work_item_comments'.
type WorkItemCommentUpdateParams struct {
Message *string `json:"message" nullable:"false"` // message
UserID *UserID `json:"userID" nullable:"false"` // user_id
WorkItemID *WorkItemID `json:"workItemID" nullable:"false"` // work_item_id
}
// SetUpdateParams updates public.work_item_comments struct fields with the specified params.
func (wic *WorkItemComment) SetUpdateParams(params *WorkItemCommentUpdateParams) {
if params.Message != nil {
wic.Message = *params.Message
}
if params.UserID != nil {
wic.UserID = *params.UserID
}
if params.WorkItemID != nil {
wic.WorkItemID = *params.WorkItemID
}
}
type WorkItemCommentSelectConfig struct {
limit string
orderBy string
joins WorkItemCommentJoins
filters map[string][]any
}
type WorkItemCommentSelectConfigOption func(*WorkItemCommentSelectConfig)
// WithWorkItemCommentLimit limits row selection.
func WithWorkItemCommentLimit(limit int) WorkItemCommentSelectConfigOption {
return func(s *WorkItemCommentSelectConfig) {
if limit > 0 {
s.limit = fmt.Sprintf(" limit %d ", limit)
}
}
}
type WorkItemCommentOrderBy string
const (
WorkItemCommentCreatedAtDescNullsFirst WorkItemCommentOrderBy = " created_at DESC NULLS FIRST "
WorkItemCommentCreatedAtDescNullsLast WorkItemCommentOrderBy = " created_at DESC NULLS LAST "
WorkItemCommentCreatedAtAscNullsFirst WorkItemCommentOrderBy = " created_at ASC NULLS FIRST "
WorkItemCommentCreatedAtAscNullsLast WorkItemCommentOrderBy = " created_at ASC NULLS LAST "
WorkItemCommentUpdatedAtDescNullsFirst WorkItemCommentOrderBy = " updated_at DESC NULLS FIRST "
WorkItemCommentUpdatedAtDescNullsLast WorkItemCommentOrderBy = " updated_at DESC NULLS LAST "
WorkItemCommentUpdatedAtAscNullsFirst WorkItemCommentOrderBy = " updated_at ASC NULLS FIRST "
WorkItemCommentUpdatedAtAscNullsLast WorkItemCommentOrderBy = " updated_at ASC NULLS LAST "
)
// WithWorkItemCommentOrderBy orders results by the given columns.
func WithWorkItemCommentOrderBy(rows ...WorkItemCommentOrderBy) WorkItemCommentSelectConfigOption {
return func(s *WorkItemCommentSelectConfig) {
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 WorkItemCommentJoins struct {
User bool // O2O users
WorkItem bool // O2O work_items
}
// WithWorkItemCommentJoin joins with the given tables.
func WithWorkItemCommentJoin(joins WorkItemCommentJoins) WorkItemCommentSelectConfigOption {
return func(s *WorkItemCommentSelectConfig) {
s.joins = WorkItemCommentJoins{
User: s.joins.User || joins.User,
WorkItem: s.joins.WorkItem || joins.WorkItem,
}
}
}
// WithWorkItemCommentFilters 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 WithWorkItemCommentFilters(filters map[string][]any) WorkItemCommentSelectConfigOption {
return func(s *WorkItemCommentSelectConfig) {
s.filters = filters
}
}
const workItemCommentTableUserJoinSQL = `-- O2O join generated from "work_item_comments_user_id_fkey (Generated from M2O)"
left join users as _work_item_comments_user_id on _work_item_comments_user_id.user_id = work_item_comments.user_id
`
const workItemCommentTableUserSelectSQL = `(case when _work_item_comments_user_id.user_id is not null then row(_work_item_comments_user_id.*) end) as user_user_id`
const workItemCommentTableUserGroupBySQL = `_work_item_comments_user_id.user_id,
_work_item_comments_user_id.user_id,
work_item_comments.work_item_comment_id`
const workItemCommentTableWorkItemJoinSQL = `-- O2O join generated from "work_item_comments_work_item_id_fkey (Generated from M2O)"
left join work_items as _work_item_comments_work_item_id on _work_item_comments_work_item_id.work_item_id = work_item_comments.work_item_id
`
const workItemCommentTableWorkItemSelectSQL = `(case when _work_item_comments_work_item_id.work_item_id is not null then row(_work_item_comments_work_item_id.*) end) as work_item_work_item_id`
const workItemCommentTableWorkItemGroupBySQL = `_work_item_comments_work_item_id.work_item_id,
_work_item_comments_work_item_id.work_item_id,
work_item_comments.work_item_comment_id`
// Insert inserts the WorkItemComment to the database.
func (wic *WorkItemComment) Insert(ctx context.Context, db DB) (*WorkItemComment, error) {
// insert (primary key generated and returned by database)
sqlstr := `INSERT INTO public.work_item_comments (
message, user_id, work_item_id
) VALUES (
$1, $2, $3
) RETURNING * `
// run
logf(sqlstr, wic.Message, wic.UserID, wic.WorkItemID)
rows, err := db.Query(ctx, sqlstr, wic.Message, wic.UserID, wic.WorkItemID)
if err != nil {
return nil, logerror(fmt.Errorf("WorkItemComment/Insert/db.Query: %w", &XoError{Entity: "Work item comment", Err: err}))
}
newwic, err := pgx.CollectOneRow(rows, pgx.RowToStructByNameLax[WorkItemComment])
if err != nil {
return nil, logerror(fmt.Errorf("WorkItemComment/Insert/pgx.CollectOneRow: %w", &XoError{Entity: "Work item comment", Err: err}))
}
*wic = newwic
return wic, nil
}
// Update updates a WorkItemComment in the database.
func (wic *WorkItemComment) Update(ctx context.Context, db DB) (*WorkItemComment, error) {
// update with composite primary key
sqlstr := `UPDATE public.work_item_comments SET
message = $1, user_id = $2, work_item_id = $3
WHERE work_item_comment_id = $4
RETURNING * `
// run
logf(sqlstr, wic.CreatedAt, wic.Message, wic.UpdatedAt, wic.UserID, wic.WorkItemID, wic.WorkItemCommentID)
rows, err := db.Query(ctx, sqlstr, wic.Message, wic.UserID, wic.WorkItemID, wic.WorkItemCommentID)
if err != nil {
return nil, logerror(fmt.Errorf("WorkItemComment/Update/db.Query: %w", &XoError{Entity: "Work item comment", Err: err}))
}
newwic, err := pgx.CollectOneRow(rows, pgx.RowToStructByNameLax[WorkItemComment])
if err != nil {
return nil, logerror(fmt.Errorf("WorkItemComment/Update/pgx.CollectOneRow: %w", &XoError{Entity: "Work item comment", Err: err}))
}
*wic = newwic
return wic, nil
}
// Upsert upserts a WorkItemComment in the database.
// Requires appropriate PK(s) to be set beforehand.
func (wic *WorkItemComment) Upsert(ctx context.Context, db DB, params *WorkItemCommentCreateParams) (*WorkItemComment, error) {
var err error
wic.Message = params.Message
wic.UserID = params.UserID
wic.WorkItemID = params.WorkItemID
wic, err = wic.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: "Work item comment", Err: err})
}
wic, err = wic.Update(ctx, db)
if err != nil {
return nil, fmt.Errorf("UpsertUser/Update: %w", &XoError{Entity: "Work item comment", Err: err})
}
}
}
return wic, err
}
// Delete deletes the WorkItemComment from the database.
func (wic *WorkItemComment) Delete(ctx context.Context, db DB) error {
// delete with single primary key
sqlstr := `DELETE FROM public.work_item_comments
WHERE work_item_comment_id = $1 `
// run
if _, err := db.Exec(ctx, sqlstr, wic.WorkItemCommentID); err != nil {
return logerror(err)
}
return nil
}
// WorkItemCommentPaginatedByWorkItemCommentIDAsc returns a cursor-paginated list of WorkItemComment in Asc order.
func WorkItemCommentPaginatedByWorkItemCommentIDAsc(ctx context.Context, db DB, workItemCommentID WorkItemCommentID, opts ...WorkItemCommentSelectConfigOption) ([]WorkItemComment, error) {
c := &WorkItemCommentSelectConfig{joins: WorkItemCommentJoins{}, 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, workItemCommentTableUserSelectSQL)
joinClauses = append(joinClauses, workItemCommentTableUserJoinSQL)
groupByClauses = append(groupByClauses, workItemCommentTableUserGroupBySQL)
}
if c.joins.WorkItem {
selectClauses = append(selectClauses, workItemCommentTableWorkItemSelectSQL)
joinClauses = append(joinClauses, workItemCommentTableWorkItemJoinSQL)
groupByClauses = append(groupByClauses, workItemCommentTableWorkItemGroupBySQL)
}
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
work_item_comments.created_at,
work_item_comments.message,
work_item_comments.updated_at,
work_item_comments.user_id,
work_item_comments.work_item_comment_id,
work_item_comments.work_item_id %s
FROM public.work_item_comments %s
WHERE work_item_comments.work_item_comment_id > $1
%s %s
ORDER BY
work_item_comment_id Asc`, selects, joins, filters, groupbys)
sqlstr += c.limit
sqlstr = "/* WorkItemCommentPaginatedByWorkItemCommentIDAsc */\n" + sqlstr
// run
rows, err := db.Query(ctx, sqlstr, append([]any{workItemCommentID}, filterParams...)...)
if err != nil {
return nil, logerror(fmt.Errorf("WorkItemComment/Paginated/Asc/db.Query: %w", &XoError{Entity: "Work item comment", Err: err}))
}
res, err := pgx.CollectRows(rows, pgx.RowToStructByNameLax[WorkItemComment])
if err != nil {
return nil, logerror(fmt.Errorf("WorkItemComment/Paginated/Asc/pgx.CollectRows: %w", &XoError{Entity: "Work item comment", Err: err}))
}
return res, nil
}
// WorkItemCommentPaginatedByWorkItemCommentIDDesc returns a cursor-paginated list of WorkItemComment in Desc order.
func WorkItemCommentPaginatedByWorkItemCommentIDDesc(ctx context.Context, db DB, workItemCommentID WorkItemCommentID, opts ...WorkItemCommentSelectConfigOption) ([]WorkItemComment, error) {
c := &WorkItemCommentSelectConfig{joins: WorkItemCommentJoins{}, 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, workItemCommentTableUserSelectSQL)
joinClauses = append(joinClauses, workItemCommentTableUserJoinSQL)
groupByClauses = append(groupByClauses, workItemCommentTableUserGroupBySQL)
}
if c.joins.WorkItem {
selectClauses = append(selectClauses, workItemCommentTableWorkItemSelectSQL)
joinClauses = append(joinClauses, workItemCommentTableWorkItemJoinSQL)
groupByClauses = append(groupByClauses, workItemCommentTableWorkItemGroupBySQL)
}
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
work_item_comments.created_at,
work_item_comments.message,
work_item_comments.updated_at,
work_item_comments.user_id,
work_item_comments.work_item_comment_id,
work_item_comments.work_item_id %s
FROM public.work_item_comments %s
WHERE work_item_comments.work_item_comment_id < $1
%s %s
ORDER BY
work_item_comment_id Desc`, selects, joins, filters, groupbys)
sqlstr += c.limit
sqlstr = "/* WorkItemCommentPaginatedByWorkItemCommentIDDesc */\n" + sqlstr
// run
rows, err := db.Query(ctx, sqlstr, append([]any{workItemCommentID}, filterParams...)...)
if err != nil {
return nil, logerror(fmt.Errorf("WorkItemComment/Paginated/Desc/db.Query: %w", &XoError{Entity: "Work item comment", Err: err}))
}
res, err := pgx.CollectRows(rows, pgx.RowToStructByNameLax[WorkItemComment])
if err != nil {
return nil, logerror(fmt.Errorf("WorkItemComment/Paginated/Desc/pgx.CollectRows: %w", &XoError{Entity: "Work item comment", Err: err}))
}
return res, nil
}
// WorkItemCommentByWorkItemCommentID retrieves a row from 'public.work_item_comments' as a WorkItemComment.
//
// Generated from index 'work_item_comments_pkey'.
func WorkItemCommentByWorkItemCommentID(ctx context.Context, db DB, workItemCommentID WorkItemCommentID, opts ...WorkItemCommentSelectConfigOption) (*WorkItemComment, error) {
c := &WorkItemCommentSelectConfig{joins: WorkItemCommentJoins{}, 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, workItemCommentTableUserSelectSQL)
joinClauses = append(joinClauses, workItemCommentTableUserJoinSQL)
groupByClauses = append(groupByClauses, workItemCommentTableUserGroupBySQL)
}
if c.joins.WorkItem {
selectClauses = append(selectClauses, workItemCommentTableWorkItemSelectSQL)
joinClauses = append(joinClauses, workItemCommentTableWorkItemJoinSQL)
groupByClauses = append(groupByClauses, workItemCommentTableWorkItemGroupBySQL)
}
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
work_item_comments.created_at,
work_item_comments.message,
work_item_comments.updated_at,
work_item_comments.user_id,
work_item_comments.work_item_comment_id,
work_item_comments.work_item_id %s
FROM public.work_item_comments %s
WHERE work_item_comments.work_item_comment_id = $1
%s %s
`, selects, joins, filters, groupbys)
sqlstr += c.orderBy
sqlstr += c.limit
sqlstr = "/* WorkItemCommentByWorkItemCommentID */\n" + sqlstr
// run
// logf(sqlstr, workItemCommentID)
rows, err := db.Query(ctx, sqlstr, append([]any{workItemCommentID}, filterParams...)...)
if err != nil {
return nil, logerror(fmt.Errorf("work_item_comments/WorkItemCommentByWorkItemCommentID/db.Query: %w", &XoError{Entity: "Work item comment", Err: err}))
}
wic, err := pgx.CollectOneRow(rows, pgx.RowToStructByNameLax[WorkItemComment])
if err != nil {
return nil, logerror(fmt.Errorf("work_item_comments/WorkItemCommentByWorkItemCommentID/pgx.CollectOneRow: %w", &XoError{Entity: "Work item comment", Err: err}))
}
return &wic, nil
}
// WorkItemCommentsByWorkItemID retrieves a row from 'public.work_item_comments' as a WorkItemComment.
//
// Generated from index 'work_item_comments_work_item_id_idx'.
func WorkItemCommentsByWorkItemID(ctx context.Context, db DB, workItemID WorkItemID, opts ...WorkItemCommentSelectConfigOption) ([]WorkItemComment, error) {
c := &WorkItemCommentSelectConfig{joins: WorkItemCommentJoins{}, 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, workItemCommentTableUserSelectSQL)
joinClauses = append(joinClauses, workItemCommentTableUserJoinSQL)
groupByClauses = append(groupByClauses, workItemCommentTableUserGroupBySQL)
}
if c.joins.WorkItem {
selectClauses = append(selectClauses, workItemCommentTableWorkItemSelectSQL)
joinClauses = append(joinClauses, workItemCommentTableWorkItemJoinSQL)
groupByClauses = append(groupByClauses, workItemCommentTableWorkItemGroupBySQL)
}
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
work_item_comments.created_at,
work_item_comments.message,
work_item_comments.updated_at,
work_item_comments.user_id,
work_item_comments.work_item_comment_id,
work_item_comments.work_item_id %s
FROM public.work_item_comments %s
WHERE work_item_comments.work_item_id = $1
%s %s
`, selects, joins, filters, groupbys)
sqlstr += c.orderBy
sqlstr += c.limit
sqlstr = "/* WorkItemCommentsByWorkItemID */\n" + sqlstr
// run
// logf(sqlstr, workItemID)
rows, err := db.Query(ctx, sqlstr, append([]any{workItemID}, filterParams...)...)
if err != nil {
return nil, logerror(fmt.Errorf("WorkItemComment/WorkItemCommentsByWorkItemID/Query: %w", &XoError{Entity: "Work item comment", Err: err}))
}
defer rows.Close()
// process
res, err := pgx.CollectRows(rows, pgx.RowToStructByNameLax[WorkItemComment])
if err != nil {
return nil, logerror(fmt.Errorf("WorkItemComment/WorkItemCommentsByWorkItemID/pgx.CollectRows: %w", &XoError{Entity: "Work item comment", Err: err}))
}
return res, nil
}
// FKUser_UserID returns the User associated with the WorkItemComment's (UserID).
//
// Generated from foreign key 'work_item_comments_user_id_fkey'.
func (wic *WorkItemComment) FKUser_UserID(ctx context.Context, db DB) (*User, error) {
return UserByUserID(ctx, db, wic.UserID)
}
// FKWorkItem_WorkItemID returns the WorkItem associated with the WorkItemComment's (WorkItemID).
//
// Generated from foreign key 'work_item_comments_work_item_id_fkey'.
func (wic *WorkItemComment) FKWorkItem_WorkItemID(ctx context.Context, db DB) (*WorkItem, error) {
return WorkItemByWorkItemID(ctx, db, wic.WorkItemID)
}