-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathctx.go
More file actions
147 lines (128 loc) · 3.37 KB
/
ctx.go
File metadata and controls
147 lines (128 loc) · 3.37 KB
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
package QueryHelper
import (
"context"
"errors"
"fmt"
"strings"
)
var (
ErrTableNotInCtx = errors.New("table is missing from context")
ErrDBNotInCtx = errors.New("db is missing from context")
)
type tableCtxName string
type DBCtxName string
const DBContext = "db-base-context"
func AddTableCtx[T any](ctx context.Context, db DB, dataset string, queryType QueryType, suffix ...string) (context.Context, error) {
table, err := NewTable[T](dataset, queryType)
if err != nil {
return ctx, err
}
if _, err := GetDBContext(ctx, ""); err != nil {
AddDBContext(ctx, DBContext, db)
}
err = table.InitializeTable(ctx, db, suffix...)
if err != nil {
return nil, err
}
ctx = context.WithValue(ctx, tableCtxName(table.Name), table)
return ctx, nil
}
func AddDBContext(ctx context.Context, name string, db DB) context.Context {
if name == "" {
name = DBContext
}
ctx = context.WithValue(ctx, DBCtxName(name), db)
return ctx
}
func GetDBContext(ctx context.Context, name string) (DB, error) {
if name == "" {
name = DBContext
}
value := ctx.Value(DBCtxName(name))
if value == nil {
return nil, ErrDBNotInCtx
}
return value.(DB), nil
}
func GetTableCtx[T any](ctx context.Context, suffix ...string) (*Table[T], error) {
var s T
name := ToSnakeCase(getType(s))
value := ctx.Value(tableCtxName(strings.Join(append([]string{name}, suffix...), "_")))
if value == nil {
return nil, ErrTableNotInCtx
}
return value.(*Table[T]), nil
}
func WithTableContext(baseCtx context.Context, tableCtx context.Context, names ...string) (context.Context, error) {
for _, name := range names {
value := tableCtx.Value(tableCtxName(name))
if value == nil {
return nil, ErrTableNotInCtx
}
baseCtx = context.WithValue(baseCtx, tableCtxName(name), value)
}
return baseCtx, nil
}
func InsertCtx[T any](ctx context.Context, data *T, suffix ...string) (string, error) {
table, err := GetTableCtx[T](ctx, suffix...)
if err != nil {
return "", err
}
if data == nil {
return "", fmt.Errorf("no data provided")
}
id, err := table.Insert(ctx, nil, *data)
if err != nil {
return "", err
}
return id, nil
}
func DeleteAllCtx[T any](ctx context.Context, data []*T, suffix ...string) error {
table, err := GetTableCtx[T](ctx, suffix...)
if err != nil {
return err
}
if data == nil {
return fmt.Errorf("no data provided")
}
for _, d := range data {
err = table.Delete(ctx, nil, *d)
if err != nil {
return err
}
}
return nil
}
func DeleteCtx[T any](ctx context.Context, data *T, suffix ...string) error {
table, err := GetTableCtx[T](ctx, suffix...)
if err != nil {
return err
}
if data == nil {
return fmt.Errorf("no data provided")
}
return table.Delete(ctx, nil, *data)
}
func UpdateCtx[T any](ctx context.Context, data *T, suffix ...string) error {
table, err := GetTableCtx[T](ctx, suffix...)
if err != nil {
return err
}
if data == nil {
return fmt.Errorf("no data provided")
}
return table.Update(ctx, nil, *data)
}
func ListCtx[T any](ctx context.Context, stmt ...*WhereStmt) ([]*T, error) {
q := GetQuery[T](ctx)
q.WhereStmts = append(q.WhereStmts, stmt...)
return q.Run(ctx, nil)
}
func GetIDCtx[T any](ctx context.Context, id string) (*T, error) {
q := GetQuery[T](ctx)
q.Where(q.Column("id"), "=", "AND", 0, id)
return q.RunSingle(ctx, nil)
}
func GetColumn[T any](ctx context.Context, name string) Column {
return GetQuery[T](ctx).Column(name)
}