-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.go
79 lines (63 loc) · 1.81 KB
/
view.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
package azamat
import (
"fmt"
sq "github.com/Masterminds/squirrel"
)
// View can be used when an entity doesn't map precisely to a specific table. This is
// not to be confused with "SQL views," but it is similar in concept. A View is
// associated with a custom query. This can be useful when you have an entity where
// some of the fields come from joining multiple tables.
type View[T any] struct {
// When we GetByID, we may need to know what table ID comes from. This is
// optional and only needed if there is an ambiguity in the custom query where
// multiple referenced tables have an "id" column
IDFrom fmt.Stringer
// Query is the custom query that will be used to fetch the entity
Query func() sq.SelectBuilder
}
func (v View[T]) GetAll(runner Runner) ([]T, error) {
sql, args, err := v.Query().ToSql()
if err != nil {
return nil, err
}
var rows []T
err = runner.Select(&rows, sql, args...)
return rows, err
}
func (v View[T]) GetByID(runner Runner, id int) (T, error) {
var row T
idColumn := "id"
idFrom := v.IDFrom.String()
if idFrom != "" {
idColumn = fmt.Sprintf("%s.id", idFrom)
}
sql, args, err := v.Query().Where(sq.Eq{idColumn: id}).ToSql()
if err != nil {
return row, err
}
var rows []T
if err := runner.Select(&rows, sql, args...); err != nil {
return row, err
}
if len(rows) == 0 {
return row, fmt.Errorf("none found")
}
if len(rows) != 1 {
return row, fmt.Errorf("expected to only get row")
}
return rows[0], nil
}
func (v View[T]) GetByIDs(runner Runner, ids ...int) ([]T, error) {
idColumn := "id"
idFrom := v.IDFrom.String()
if idFrom != "" {
idColumn = fmt.Sprintf("%s.id", idFrom)
}
sql, args, err := v.Query().Where(sq.Eq{idColumn: ids}).ToSql()
if err != nil {
return nil, err
}
var rows []T
err = runner.Select(&rows, sql, args...)
return rows, err
}