-
Notifications
You must be signed in to change notification settings - Fork 0
/
saver.go
176 lines (156 loc) · 4.73 KB
/
saver.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
//go:generate mockgen -source $GOFILE -destination ./mocks/mock_$GOPACKAGE/$GOFILE -package mock_$GOPACKAGE
package exql
import (
"context"
"database/sql"
"errors"
"reflect"
q "github.com/loilo-inc/exql/v2/query"
)
type Saver interface {
Insert(structPtr Model) (sql.Result, error)
InsertContext(ctx context.Context, structPtr Model) (sql.Result, error)
Update(table string, set map[string]any, where q.Condition) (sql.Result, error)
UpdateModel(updaterStructPtr ModelUpdate, where q.Condition) (sql.Result, error)
UpdateContext(ctx context.Context, table string, set map[string]any, where q.Condition) (sql.Result, error)
UpdateModelContext(ctx context.Context, updaterStructPtr ModelUpdate, where q.Condition) (sql.Result, error)
Delete(table string, where q.Condition) (sql.Result, error)
DeleteContext(ctx context.Context, table string, where q.Condition) (sql.Result, error)
Exec(query q.Query) (sql.Result, error)
ExecContext(ctx context.Context, query q.Query) (sql.Result, error)
Query(query q.Query) (*sql.Rows, error)
QueryContext(ctx context.Context, query q.Query) (*sql.Rows, error)
QueryRow(query q.Query) (*sql.Row, error)
QueryRowContext(ctx context.Context, query q.Query) (*sql.Row, error)
}
type saver struct {
ex Executor
}
func NewSaver(ex Executor) Saver {
return &saver{ex: ex}
}
func newSaver(ex Executor) *saver {
return &saver{ex: ex}
}
func (s *saver) Insert(modelPtr Model) (sql.Result, error) {
return s.InsertContext(context.Background(), modelPtr)
}
func (s *saver) InsertContext(ctx context.Context, modelPtr Model) (sql.Result, error) {
q, autoIncrField, err := QueryForInsert(modelPtr)
if err != nil {
return nil, err
}
result, err := s.ExecContext(ctx, q)
if err != nil {
return nil, err
}
if autoIncrField != nil {
lid, err := result.LastInsertId()
if err != nil {
return nil, err
}
kind := autoIncrField.Kind()
if kind == reflect.Int64 {
autoIncrField.Set(reflect.ValueOf(lid))
} else if kind == reflect.Uint64 {
autoIncrField.Set(reflect.ValueOf(uint64(lid)))
}
}
return result, nil
}
func (s *saver) Update(
table string,
set map[string]any,
where q.Condition,
) (sql.Result, error) {
return s.UpdateContext(context.Background(), table, set, where)
}
func (s *saver) UpdateContext(
ctx context.Context,
table string,
set map[string]any,
where q.Condition,
) (sql.Result, error) {
if table == "" {
return nil, errors.New("empty table name for update query")
} else if where == nil {
return nil, errors.New("nil condition for update query")
}
b := q.NewBuilder()
b.Sprintf("UPDATE `%s`", table)
b.Query("SET :? WHERE :?", q.Set(set), where)
return s.ExecContext(ctx, b.Build())
}
func (s *saver) Delete(from string, where q.Condition) (sql.Result, error) {
return s.DeleteContext(context.Background(), from, where)
}
func (s *saver) DeleteContext(ctx context.Context, from string, where q.Condition) (sql.Result, error) {
if from == "" {
return nil, errors.New("empty table name for delete query")
} else if where == nil {
return nil, errors.New("nil condition for delete query")
}
b := q.NewBuilder()
b.Sprintf("DELETE FROM `%s`", from)
b.Query("WHERE :?", where)
return s.ExecContext(ctx, b.Build())
}
func (s *saver) UpdateModel(
ptr ModelUpdate,
where q.Condition,
) (sql.Result, error) {
return s.UpdateModelContext(context.Background(), ptr, where)
}
func (s *saver) UpdateModelContext(
ctx context.Context,
ptr ModelUpdate,
where q.Condition,
) (sql.Result, error) {
q, err := QueryForUpdateModel(ptr, where)
if err != nil {
return nil, err
}
return s.ExecContext(ctx, q)
}
func (s *saver) Exec(query q.Query) (sql.Result, error) {
if stmt, args, err := query.Query(); err != nil {
return nil, err
} else {
return s.ex.Exec(stmt, args...)
}
}
func (s *saver) ExecContext(ctx context.Context, query q.Query) (sql.Result, error) {
if stmt, args, err := query.Query(); err != nil {
return nil, err
} else {
return s.ex.ExecContext(ctx, stmt, args...)
}
}
func (s *saver) Query(query q.Query) (*sql.Rows, error) {
if stmt, args, err := query.Query(); err != nil {
return nil, err
} else {
return s.ex.Query(stmt, args...)
}
}
func (s *saver) QueryContext(ctx context.Context, query q.Query) (*sql.Rows, error) {
if stmt, args, err := query.Query(); err != nil {
return nil, err
} else {
return s.ex.QueryContext(ctx, stmt, args...)
}
}
func (s *saver) QueryRow(query q.Query) (*sql.Row, error) {
if stmt, args, err := query.Query(); err != nil {
return nil, err
} else {
return s.ex.QueryRow(stmt, args...), nil
}
}
func (s *saver) QueryRowContext(ctx context.Context, query q.Query) (*sql.Row, error) {
if stmt, args, err := query.Query(); err != nil {
return nil, err
} else {
return s.ex.QueryRowContext(ctx, stmt, args...), nil
}
}