-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathrunner_execer.go
155 lines (137 loc) · 3.71 KB
/
runner_execer.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
package dbx
import (
"context"
"database/sql"
"errors"
"fmt"
"reflect"
"strings"
"time"
)
func (r *Runner) Exec(sql string, params ...interface{}) (rs sql.Result, err error) {
return r.ExecContext(context.Background(), sql, params...)
}
func (r *Runner) Execute(sql string, params ...interface{}) (lastInsertId, rowsAffected int64, err error) {
rs, err := r.Exec(sql, params...)
if err != nil {
return 0, 0, err
}
lastInsertId, err = rs.LastInsertId()
if err != nil {
return 0, 0, err
}
rowsAffected, err = rs.RowsAffected()
return lastInsertId, rowsAffected, err
}
func (r *Runner) ExecContext(ctx context.Context, sql string, params ...interface{}) (rs sql.Result, err error) {
if r.LoggingEnabled() {
defer func(start time.Time) {
r.Logger().Log(&QueryStatus{
Query: sql,
Args: params,
Err: err,
Start: start,
End: time.Now(),
Context: ctx,
})
}(time.Now())
}
return r.sqlExecutor.ExecContext(ctx, sql, params...)
//stmt, err := r.Prepare(sql)
//if err != nil {
// return nil, err
//}
//defer stmt.Close()
//rs, err = stmt.ExecContext(ctx, params...)
//return rs, err
}
func (r *Runner) Insert(model interface{}) (rs sql.Result, err error) {
return r.InsertContext(context.Background(), model)
}
func (r *Runner) InsertContext(ctx context.Context, model interface{}) (rs sql.Result, err error) {
entity, ind := r.GetEntity(model)
names, placeholders := "", ""
params := make([]interface{}, 0)
for _, fd := range entity.Columns {
if fd.Field.Anonymous || fd.Embedded {
continue
}
lastv := ind.FieldByIndex(fd.Index)
if lastv.Kind() == reflect.Ptr && lastv.IsNil() {
continue
}
if !lastv.IsValid() {
continue
}
if fd.ColumnName == "" || fd.Omitempty {
continue
}
if fd.IsPk {
continue
}
params = append(params, lastv.Interface())
names += "`" + fd.ColumnName + "`,"
placeholders += "?,"
}
sql := fmt.Sprintf("insert into `%s`(%s) values(%s)", entity.TableName, names[0:len(names)-1], placeholders[0:len(placeholders)-1])
return r.ExecContext(ctx, sql, params...)
}
func (r *Runner) Update(model interface{}) (rs sql.Result, err error) {
return r.UpdateContext(context.Background(), model)
}
func (r *Runner) UpdateContext(ctx context.Context, model interface{}) (rs sql.Result, err error) {
entity, ind := r.GetEntity(model)
var sql string
//fmt.Printf("%+v \n", model)
names := ""
params, whereArgs := make([]interface{}, 0), make([]interface{}, 0)
wheres := make([]string, 0)
for _, fd := range entity.Columns {
if fd.Field.Anonymous || fd.Embedded {
continue
}
lastv := ind.FieldByIndex(fd.Index)
if lastv.Kind() == reflect.Ptr && lastv.IsNil() {
continue
}
if !lastv.IsValid() {
continue
}
if fd.ColumnName == "" || fd.Omitempty {
continue
}
if lastv.Interface() == fd.Zero.Interface() {
continue
}
params = append(params, lastv.Interface())
names += "`" + fd.ColumnName + "`=?,"
if fd.IsUnique {
wheres = append(wheres, "`"+fd.ColumnName+"`=? ")
whereArgs = append(whereArgs, lastv.Interface())
}
if fd.IsPk {
wheres = append(wheres, "`"+fd.ColumnName+"`=? ")
whereArgs = append(whereArgs, lastv.Interface())
}
}
if r.LoggingEnabled() {
defer func(start time.Time) {
r.Logger().Log(&QueryStatus{
Query: sql,
Args: params,
Err: err,
Start: start,
End: time.Now(),
Context: ctx,
})
}(time.Now())
}
if len(wheres) == 0 {
err = errors.New("no unique column config for db tag.")
return nil, err
}
where := strings.Join(wheres, " and ")
params = append(params, whereArgs...)
sql = fmt.Sprintf("update `%s` set %s where %s", entity.TableName, names[0:len(names)-1], where)
return r.ExecContext(ctx, sql, params...)
}