sql.go
6.0 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
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
package utils
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"github.com/astaxie/beego/orm"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"reflect"
"strings"
)
// 更新指定表的几个列
func UpdateTableByMap(tabeleStruct interface{}, changeMap map[string]interface{}) error {
if reflect.TypeOf(tabeleStruct).Kind() != reflect.Ptr {
err := errors.New("UpdateTableByMap: tableStruct must ptr")
log.Error(err)
return err
}
if len(changeMap) < 1 {
log.Info("changeMap is nil")
return nil
}
o := orm.NewOrm()
changeColumn := make([]string, 0, len(changeMap))
for i, v := range changeMap {
changeColumn = append(changeColumn, i)
if err := SetStructValueByType(tabeleStruct, i, v); err != nil {
log.Error(err, i, v)
return err
}
}
num, err := o.Update(tabeleStruct, changeColumn...)
if err != nil {
log.Error(err)
return err
}
log.Info(fmt.Sprintf("UpdateTableByMap: table:%s effect records:%d column:%v values:%v", GetTableName(tabeleStruct), num, changeColumn, changeMap))
return nil
}
func UpdateTableByMapWithOrmer(o orm.Ormer, tabeleStruct interface{}, changeMap map[string]interface{}) error {
if reflect.TypeOf(tabeleStruct).Kind() != reflect.Ptr {
err := errors.New("UpdateTableByMap: tableStruct must ptr")
log.Error(err)
return err
}
if len(changeMap) < 1 {
log.Info("changeMap is nil")
return nil
}
changeColumn := make([]string, 0, len(changeMap))
for i, v := range changeMap {
changeColumn = append(changeColumn, i)
if err := SetStructValueByType(tabeleStruct, i, v); err != nil {
log.Error(err, i, v)
return err
}
}
num, err := o.Update(tabeleStruct, changeColumn...)
if err != nil {
log.Error(err)
return err
}
log.Info(fmt.Sprintf("UpdateTableByMap: table:%s effect records:%d column:%v", GetTableName(tabeleStruct), num, changeColumn))
return nil
}
// 通过反射调用结构对应的TableName函数,达到返回表名的目的
func GetTableName(tableStruct interface{}) string {
m := reflect.ValueOf(tableStruct).MethodByName("TableName")
if m.IsValid() && m.Kind() == reflect.Func {
re := m.Call(nil)
for _, v := range re {
if v.IsValid() {
return v.String()
}
}
}
return "unknown"
}
// 通用事物提交sql结构体
type SqlData struct {
Sql string
Param []interface{}
}
func ExecuteSqlByRoll(isCheck bool, sqlSlice ...*SqlData) bool {
o := orm.NewOrm()
var someError bool = false
o.Begin()
for i := range sqlSlice {
if sqlSlice[i].Sql == "" {
continue
}
log.Info("execute sql:", sqlSlice[i])
if ret, err := o.Raw(sqlSlice[i].Sql, sqlSlice[i].Param...).Exec(); err != nil {
log.Error(err)
someError = true
} else {
num, _ := ret.RowsAffected()
log.Debug("num:", num)
if isCheck && num < 1 {
someError = true
}
}
}
if someError {
log.Error("出错,回滚事物")
o.Rollback()
return false
} else {
log.Info("成功,提交事物")
o.Commit()
return true
}
return true
}
//PrintLogSql 打印sql语句
func PrintLogSql(sql string, param ...interface{}) {
format := `SQL EXCEUTE:[%s]-%s`
parmformat := `[%v]`
var p strings.Builder
for i := range param {
p.WriteString(fmt.Sprintf(parmformat, param[i]))
}
log.Debug(fmt.Sprintf(format, sql, p.String()))
}
//ExecuteQueryOne 执行原生sql查询单条记录;结果用结构体接收
func ExecuteQueryOne(result interface{}, sqlstr string, param ...interface{}) error {
var err error
o := orm.NewOrm()
err = ExecuteQueryOneWithOrmer(o, result, sqlstr, param)
return err
}
//ExecuteQueryOneWithOrmer 执行原生sql查询单条
func ExecuteQueryOneWithOrmer(o orm.Ormer, result interface{}, sqlstr string, param ...interface{}) error {
PrintLogSql(sqlstr, param...)
var err error
err = o.Raw(sqlstr, param).QueryRow(result)
if err != nil {
return err
}
return nil
}
//ExecuteQuerySql 执行原生sql查询多条记录
func ExecuteQueryAll(result interface{}, sqlstr string, param ...interface{}) error {
//PrintLogSql(sqlstr, param...)
var err error
o := orm.NewOrm()
err = ExecuteQueryAllWithOrmer(o, result, sqlstr, param)
return err
}
//ExecuteQueryOneWithOrmer 执行原生sql查询多条记录
func ExecuteQueryAllWithOrmer(o orm.Ormer, result interface{}, sqlstr string, param ...interface{}) error {
PrintLogSql(sqlstr, param...)
var (
err error
)
_, err = o.Raw(sqlstr, param).QueryRows(result)
if err != nil {
return err
}
return nil
}
func ExecuteSQLWithOrmer(o orm.Ormer, sqlstr string, param ...interface{}) error {
PrintLogSql(sqlstr, param...)
var (
err error
)
r, err := o.Raw(sqlstr, param...).Exec()
if err != nil {
return err
}
num, _ := r.RowsAffected()
log.Debug(fmt.Sprintf("RowsAffected:%d", num))
return nil
}
type SqlExcutor struct {
table string
wherestr []string
orderstr []string
islimit bool
offset int
pagenum int
}
func NewSqlExutor() *SqlExcutor {
return &SqlExcutor{}
}
func (s *SqlExcutor) Table(str string) *SqlExcutor {
s.table = str
return s
}
func (s *SqlExcutor) Where(condition ...string) *SqlExcutor {
if len(condition) <= 0 {
return s
}
s.wherestr = append(s.wherestr, condition...)
return s
}
func (s *SqlExcutor) Order(condition ...string) *SqlExcutor {
if len(condition) <= 0 {
return s
}
s.orderstr = append(s.orderstr, condition...)
return s
}
func (s *SqlExcutor) Limit(page, pagenum int) *SqlExcutor {
offset := 0
if page > 0 {
offset = (page - 1) * pagenum
}
s.islimit = true
s.offset = offset
s.pagenum = pagenum
return s
}
func (s *SqlExcutor) WhereString() string {
sql := bytes.NewBufferString("")
if len(s.wherestr) > 0 {
sql.WriteString(" where ")
for i := range s.wherestr {
if i != 0 {
sql.WriteString(" AND ")
}
sql.WriteString(s.wherestr[i])
}
}
return sql.String()
}
//GobModelTransform 模型转换
func GobModelTransform(dst interface{}, src interface{}) error {
var data bytes.Buffer
enc := gob.NewEncoder(&data)
if err := enc.Encode(src); err != nil {
return err
}
dec := gob.NewDecoder(&data)
if err := dec.Decode(dst); err != nil {
return err
}
return nil
}