-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute.go
312 lines (254 loc) · 7.01 KB
/
execute.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
package sage
import (
"context"
"database/sql"
"errors"
"reflect"
"strings"
)
var (
// ErrNotFound indicates that a record was not found
ErrNotFound = errors.New("record not found")
// ErrNotAStruct indicates that the provided model is not a struct
ErrNotAStruct = errors.New("model must be a struct")
// ErrNoID indicates that no ID field was found for the model
ErrNoID = errors.New("model does not have an ID field")
)
// Executor is an interface that can execute database operations
type Executor interface {
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}
// Create inserts a new record into the database
func (c *Connection) Create(ctx context.Context, model interface{}) error {
info, err := extractModelInfo(model)
if err != nil {
return err
}
qb := NewQueryBuilder(info.TableName).Insert()
v := reflect.ValueOf(model)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
for _, field := range info.Fields {
// Skip auto-increment primary key fields
if field.IsKey && field.IsAuto {
continue
}
fieldValue := v.FieldByName(field.Name)
qb.Set(field.DBName, fieldValue.Interface())
}
query, args := qb.Build()
result, err := c.DB().ExecContext(ctx, query, args...)
if err != nil {
return err
}
// If the model has an auto-increment primary key, set it
if id, err := result.LastInsertId(); err == nil {
for _, field := range info.Fields {
if field.IsKey && field.IsAuto {
idField := v.FieldByName(field.Name)
if idField.CanSet() {
idField.Set(reflect.ValueOf(id).Convert(idField.Type()))
}
break
}
}
}
return nil
}
// Find finds a record by its primary key
func (c *Connection) Find(ctx context.Context, model interface{}, id interface{}) error {
info, err := extractModelInfo(model)
if err != nil {
return err
}
qb := NewQueryBuilder(info.TableName).Select()
qb.Where(info.PrimaryKey+" = ?", id)
query, args := qb.Build()
return c.scanRow(ctx, model, query, args...)
}
// First finds the first record matching the conditions
func (c *Connection) First(ctx context.Context, model interface{}, conditions string, args ...interface{}) error {
info, err := extractModelInfo(model)
if err != nil {
return err
}
qb := NewQueryBuilder(info.TableName).Select()
if conditions != "" {
qb.Where(conditions, args...)
}
qb.Limit(1)
query, queryArgs := qb.Build()
return c.scanRow(ctx, model, query, queryArgs...)
}
// scanRow scans a single row into the model
func (c *Connection) scanRow(ctx context.Context, model interface{}, query string, args ...interface{}) error {
v := reflect.ValueOf(model)
if v.Kind() != reflect.Ptr || v.IsNil() {
return errors.New("model must be a non-nil pointer")
}
v = v.Elem()
row := c.DB().QueryRowContext(ctx, query, args...)
fields, err := scanFields(v)
if err != nil {
return err
}
err = row.Scan(fields...)
if err == sql.ErrNoRows {
return ErrNotFound
}
return err
}
// scanFields creates a slice of pointers to each field in the struct
func scanFields(v reflect.Value) ([]interface{}, error) {
if v.Kind() != reflect.Struct {
return nil, ErrNotAStruct
}
var fields []interface{}
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
if field.CanSet() {
fields = append(fields, field.Addr().Interface())
}
}
return fields, nil
}
// Update updates a record in the database
func (c *Connection) Update(ctx context.Context, model interface{}) error {
info, err := extractModelInfo(model)
if err != nil {
return err
}
qb := NewQueryBuilder(info.TableName).Update()
v := reflect.ValueOf(model)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
var idValue interface{}
for _, field := range info.Fields {
fieldValue := v.FieldByName(field.Name)
if field.IsKey {
idValue = fieldValue.Interface()
continue
}
qb.Set(field.DBName, fieldValue.Interface())
}
if idValue == nil {
return ErrNoID
}
qb.Where(info.PrimaryKey+" = ?", idValue)
query, args := qb.Build()
result, err := c.DB().ExecContext(ctx, query, args...)
if err != nil {
return err
}
affected, err := result.RowsAffected()
if err != nil {
return err
}
if affected == 0 {
return ErrNotFound
}
return nil
}
// Delete deletes a record from the database
func (c *Connection) Delete(ctx context.Context, model interface{}) error {
info, err := extractModelInfo(model)
if err != nil {
return err
}
v := reflect.ValueOf(model)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
var idValue interface{}
for _, field := range info.Fields {
if field.IsKey {
fieldValue := v.FieldByName(field.Name)
idValue = fieldValue.Interface()
break
}
}
if idValue == nil {
return ErrNoID
}
qb := NewQueryBuilder(info.TableName).Delete()
qb.Where(info.PrimaryKey+" = ?", idValue)
query, args := qb.Build()
result, err := c.DB().ExecContext(ctx, query, args...)
if err != nil {
return err
}
affected, err := result.RowsAffected()
if err != nil {
return err
}
if affected == 0 {
return ErrNotFound
}
return nil
}
// All finds all records matching the conditions
func (c *Connection) All(ctx context.Context, models interface{}, conditions string, args ...interface{}) error {
sliceValue := reflect.ValueOf(models)
if sliceValue.Kind() != reflect.Ptr || sliceValue.Elem().Kind() != reflect.Slice {
return errors.New("models must be a pointer to a slice")
}
sliceValue = sliceValue.Elem()
elemType := sliceValue.Type().Elem()
// Create a new instance of the slice element type
modelInstance := reflect.New(elemType).Interface()
info, err := extractModelInfo(modelInstance)
if err != nil {
return err
}
qb := NewQueryBuilder(info.TableName).Select()
if conditions != "" {
qb.Where(conditions, args...)
}
query, queryArgs := qb.Build()
rows, err := c.DB().QueryContext(ctx, query, queryArgs...)
if err != nil {
return err
}
defer rows.Close()
// Get column names from the query
columns, err := rows.Columns()
if err != nil {
return err
}
for rows.Next() {
// Create a new instance of the model
modelElem := reflect.New(elemType).Elem()
// Create a slice of interface{} to hold the values
values := make([]interface{}, len(columns))
for i := range values {
values[i] = new(interface{})
}
// Scan the row into the values
if err := rows.Scan(values...); err != nil {
return err
}
// Map the values to the model fields
for i, col := range columns {
// Find the corresponding field in the model
for _, field := range info.Fields {
if strings.EqualFold(field.DBName, col) {
fieldValue := modelElem.FieldByName(field.Name)
if fieldValue.CanSet() {
val := reflect.ValueOf(*(values[i].(*interface{})))
if val.Type().ConvertibleTo(fieldValue.Type()) {
fieldValue.Set(val.Convert(fieldValue.Type()))
}
}
break
}
}
}
// Append the model to the slice
sliceValue.Set(reflect.Append(sliceValue, modelElem))
}
return rows.Err()
}