-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_words_select.go
49 lines (40 loc) · 1.43 KB
/
key_words_select.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
package linq
import (
"context"
"github.com/STRockefeller/go-linq"
)
// SelectRaw is a method defined in the DB struct. It takes a string slice parameter selectedColumns and returns the modified DB object.
func (container DB[T]) SelectRaw(selectedColumns []string) DB[T] {
container.db = container.db.Select(selectedColumns)
return container
}
func (container DB[T]) Find(ctx context.Context) (result linq.Linq[T], err error) {
items := []T{}
err = container.db.WithContext(ctx).Find(&items).Error
result = linq.New(items)
return
}
// panics if something went wrong
func (container DB[T]) FindWithoutError(ctx context.Context) (result linq.Linq[T]) {
items := []T{}
err := container.db.WithContext(ctx).Find(&items).Error
if err != nil {
panic(err.Error())
}
result = linq.New(items)
return
}
func (container DB[T]) Take(ctx context.Context) (result T, err error) {
err = container.db.WithContext(ctx).Take(&result).Error
return
}
func (container DB[T]) FindForUpdate(ctx context.Context, opts ...forUpdateOption) (result linq.Linq[T], err error) {
return container.ForUpdate(opts...).Find(ctx)
}
func (container DB[T]) TakeForUpdate(ctx context.Context, opts ...forUpdateOption) (result T, err error) {
return container.ForUpdate(opts...).Take(ctx)
}
func (container DB[T]) Distinct(columns []string) DB[T] {
container.db = container.db.Distinct(linq.Select(columns, func(c string) any { return c }).ToSlice()...)
return container
}