-
Notifications
You must be signed in to change notification settings - Fork 0
/
dml.go
210 lines (197 loc) · 5 KB
/
dml.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
package skt
import (
"database/sql"
"reflect"
"strings"
"time"
)
// DMLStruct is DML struct
type DMLStruct struct {
StructName string
InsertString string
DeleteString string
UpdateString string
RowID int64
InsertData []interface{}
UpdateData []interface{}
}
// GetDMLI is get DML string and data
func GetDMLI(i interface{}) DMLStruct {
var ds DMLStruct
rv := reflect.ValueOf(i).Elem()
rt := rv.Type()
ds.StructName = strings.ToLower(rt.Name())
var its, itx []string
for n := 0; n < rv.NumField(); n++ {
// 判断值是否为空
if !IsBlank(rv.Field(n)) {
// 判断是否为结构体
if rv.Field(n).Kind() == reflect.Struct {
rs := reflect.Indirect(rv.Field(n))
if !IsBlank(rs.FieldByName("ID")) {
ds.RowID = rs.FieldByName("ID").Int()
}
continue
}
fn := strings.ToLower(rt.Field(n).Name)
if fn == "id" {
ds.RowID = rv.Field(n).Int()
} else {
its = append(its, fn)
itx = append(itx, "?")
ds.InsertData = append(ds.InsertData, rv.Field(n).Interface())
}
}
}
// 自动加创建时间
if _, b := rt.FieldByName("Created"); b {
its = append(its, "created")
itx = append(itx, "?")
ds.InsertData = append(ds.InsertData, time.Now())
}
// 自动加更新时间
if _, b := rt.FieldByName("Updated"); b {
its = append(its, "updated")
itx = append(itx, "?")
ds.InsertData = append(ds.InsertData, time.Now())
}
itstr := strings.Join(its, ",")
itxstr := strings.Join(itx, ",")
ds.InsertString = "INSERT INTO " + ds.StructName + "(" + itstr + ") VALUES(" + itxstr + ")"
ds.DeleteString = "DELETE FROM " + ds.StructName + " WHERE id=?"
return ds
}
// GetDMLU is get DML string and data
func GetDMLU(i interface{}, ss ...string) DMLStruct {
var ds DMLStruct
rv := reflect.ValueOf(i).Elem()
rt := rv.Type()
ds.StructName = strings.ToLower(rt.Name())
var ius []string
// 设置必须更新为空的字段
for _, s := range ss {
ius = append(ius, strings.ToLower(s)+"=?")
ds.UpdateData = append(ds.UpdateData, reflect.Zero(rv.FieldByName(s).Type()).Interface())
}
for n := 0; n < rv.NumField(); n++ {
// 判断值是否为空
if !IsBlank(rv.Field(n)) {
// 判断是否为结构体
if rv.Field(n).Kind() == reflect.Struct {
rs := reflect.Indirect(rv.Field(n))
if !IsBlank(rs.FieldByName("ID")) {
ds.RowID = rs.FieldByName("ID").Int()
}
continue
}
fn := strings.ToLower(rt.Field(n).Name)
if fn == "id" {
ds.RowID = rv.Field(n).Int()
} else {
ius = append(ius, fn+"=?")
ds.UpdateData = append(ds.UpdateData, rv.Field(n).Interface())
}
}
}
// 自动加更新时间
if _, b := rt.FieldByName("Updated"); b {
ius = append(ius, "updated=?")
ds.UpdateData = append(ds.UpdateData, time.Now())
}
iustr := strings.Join(ius, ",")
ds.UpdateData = append(ds.UpdateData, ds.RowID)
ds.UpdateString = "UPDATE " + ds.StructName + " SET " + iustr + " WHERE id=?"
return ds
}
// GetDMLD is get DML string and data
func GetDMLD(i interface{}) DMLStruct {
var ds DMLStruct
rv := reflect.ValueOf(i).Elem()
rt := rv.Type()
ds.StructName = strings.ToLower(rt.Name())
for n := 0; n < rv.NumField(); n++ {
// 判断值是否为空
if !IsBlank(rv.Field(n)) {
// 判断是否为结构体
if rv.Field(n).Kind() == reflect.Struct {
rs := reflect.Indirect(rv.Field(n))
if !IsBlank(rs.FieldByName("ID")) {
ds.RowID = rs.FieldByName("ID").Int()
}
continue
}
fn := strings.ToLower(rt.Field(n).Name)
if fn == "id" {
ds.RowID = rv.Field(n).Int()
}
}
}
ds.DeleteString = "DELETE FROM " + ds.StructName + " WHERE id=?"
return ds
}
// Insert insert data
func (db *DB) Insert(i interface{}) (sql.Result, error) {
gm := GetDMLI(i)
stmt, err := db.Prepare(gm.InsertString)
if err != nil {
return nil, err
}
res, err := stmt.Exec(gm.InsertData...)
if err != nil {
return nil, err
}
return res, nil
}
// Delete data
func (db *DB) Delete(i interface{}) (sql.Result, error) {
gm := GetDMLD(i)
stmt, err := db.Prepare(gm.DeleteString)
if err != nil {
return nil, err
}
res, err := stmt.Exec(gm.RowID)
if err != nil {
return nil, err
}
return res, nil
}
// Update data
func (db *DB) Update(i interface{}, ss ...string) (sql.Result, error) {
gm := GetDMLU(i, ss...)
stmt, err := db.Prepare(gm.UpdateString)
if err != nil {
return nil, err
}
res, err := stmt.Exec(gm.UpdateData...)
if err != nil {
return nil, err
}
return res, nil
}
// Insert tx insert data
func (tx *Tx) Insert(i interface{}) (sql.Result, error) {
gm := GetDMLI(i)
res, err := tx.Exec(gm.InsertString, gm.InsertData...)
if err != nil {
return nil, err
}
return res, nil
}
// Update tx update data
func (tx *Tx) Update(i interface{}, ss ...string) (sql.Result, error) {
gm := GetDMLU(i, ss...)
res, err := tx.Exec(gm.UpdateString, gm.UpdateData...)
if err != nil {
return nil, err
}
return res, nil
}
// Delete data
func (tx *Tx) Delete(i interface{}) (sql.Result, error) {
gm := GetDMLD(i)
res, err := tx.Exec(gm.DeleteString, gm.RowID)
if err != nil {
return nil, err
}
return res, nil
}