-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathmodel_executor.go
More file actions
158 lines (134 loc) · 3.27 KB
/
model_executor.go
File metadata and controls
158 lines (134 loc) · 3.27 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
148
149
150
151
152
153
154
155
156
157
158
package xsql
import (
"context"
"database/sql"
"fmt"
"reflect"
"slices"
"strings"
"time"
)
type ModelExecutor struct {
Executor
Options *sqlOptions
TableName string
Error error
RowsAffected int64
}
func (t *ModelExecutor) Update(ctx context.Context, data map[string]interface{}, expr string, args ...interface{}) *ModelExecutor {
return t.getExecResult(t.update(ctx, data, expr, args...))
}
func (t *ModelExecutor) getExecResult(r sql.Result, err error) *ModelExecutor {
if err != nil {
t.Error = err
return t
} else {
t.Error = nil
}
rowsAffected, err := r.RowsAffected()
if err != nil {
t.Error = err
return t
} else {
t.Error = nil
}
t.RowsAffected = rowsAffected
return t
}
func (t *ModelExecutor) update(ctx context.Context, data map[string]interface{}, expr string, args ...interface{}) (sql.Result, error) {
set := make([]string, 0)
bindArgs := make([]interface{}, 0)
table := t.TableName
opts := t.Options
n := 0
for key, val := range data {
value := reflect.ValueOf(val)
vTyp := value.Type().String()
isTime := isTime(vTyp)
var v string
if opts.Placeholder == "?" {
v = opts.Placeholder
} else {
v = fmt.Sprintf(opts.Placeholder, n)
n++
}
if isTime {
v = opts.TimeFunc(v)
}
var a interface{}
if isTime {
a = formatTime(vTyp, value.Interface(), opts)
} else {
// 非标量用JSON序列化处理
if slices.Contains([]reflect.Kind{reflect.Ptr, reflect.Struct, reflect.Slice, reflect.Array, reflect.Map}, value.Kind()) {
b, e := marshal(value.Interface())
if e != nil {
return nil, fmt.Errorf("json unmarshal error %s for field %s", e, key)
}
a = string(b)
} else {
a = value.Interface()
}
}
set = append(set, fmt.Sprintf("`%s` = %s", key, v))
bindArgs = append(bindArgs, a)
}
where := ""
if expr != "" {
where = fmt.Sprintf(` WHERE %s`, expr)
bindArgs = append(bindArgs, args...)
}
SQL := fmt.Sprintf(`UPDATE %s SET %s%s`, table, strings.Join(set, ", "), where)
startTime := time.Now()
res, err := t.Executor.Exec(SQL, bindArgs...)
var rowsAffected int64
if res != nil {
rowsAffected, _ = res.RowsAffected()
}
l := &Log{
Context: ctx,
Duration: time.Now().Sub(startTime),
SQL: SQL,
Bindings: bindArgs,
RowsAffected: rowsAffected,
Error: err,
}
opts.doDebug(l)
if err != nil {
return nil, err
}
return res, nil
}
func (t *ModelExecutor) Delete(ctx context.Context, expr string, args ...interface{}) *ModelExecutor {
return t.getExecResult(t.delete(ctx, expr, args...))
}
func (t *ModelExecutor) delete(ctx context.Context, expr string, args ...interface{}) (sql.Result, error) {
bindArgs := make([]interface{}, 0)
table := t.TableName
opts := t.Options
where := ""
if expr != "" {
where = fmt.Sprintf(` WHERE %s`, expr)
bindArgs = append(bindArgs, args...)
}
SQL := fmt.Sprintf(`DELETE FROM %s%s`, table, where)
startTime := time.Now()
res, err := t.Executor.Exec(SQL, bindArgs...)
var rowsAffected int64
if res != nil {
rowsAffected, _ = res.RowsAffected()
}
l := &Log{
Context: ctx,
Duration: time.Now().Sub(startTime),
SQL: SQL,
Bindings: bindArgs,
RowsAffected: rowsAffected,
Error: err,
}
opts.doDebug(l)
if err != nil {
return nil, err
}
return res, nil
}