-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmysql.go
445 lines (416 loc) · 9.86 KB
/
mysql.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package mcommon
import (
"context"
"database/sql"
"fmt"
"reflect"
"strings"
"time"
"github.com/gin-gonic/gin"
// 导入mysql
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
// DbExeAble 数据库接口
type DbExeAble interface {
Rebind(string) string
Get(dest interface{}, query string, args ...interface{}) error
Exec(query string, args ...interface{}) (sql.Result, error)
Select(dest interface{}, query string, args ...interface{}) error
GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)
QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row
}
// isShowSQL 是否显示执行的sql语句
var isShowSQL bool
// DbCreate 创建数据库链接
func DbCreate(dataSourceName string, showSQL bool) *sqlx.DB {
isShowSQL = showSQL
var err error
var db *sqlx.DB
db, err = sqlx.Connect("mysql", dataSourceName)
if err != nil {
Log.Fatalf("db connect error: %s", err.Error())
return nil
}
//count := runtime.NumCPU()*20 + 1
//db.SetMaxOpenConns(count)
//db.SetMaxIdleConns(count)
//db.SetConnMaxLifetime(1 * time.Hour)
err = db.Ping()
if err != nil {
Log.Fatalf("db ping error: %s", err.Error())
return nil
}
return db
}
// DbSetShowSQL 设置是否显示sql
func DbSetShowSQL(b bool) {
isShowSQL = b
}
// DbExecuteCountManyContent 返回sql语句并返回执行行数
func DbExecuteCountManyContent(ctx context.Context, tx DbExeAble, query string, n int, args ...interface{}) (int64, error) {
var err error
insertArgs := strings.Repeat("(?),", n)
insertArgs = strings.TrimSuffix(insertArgs, ",")
query = fmt.Sprintf(query, insertArgs)
query, args, err = sqlx.In(query, args...)
if err != nil {
return 0, err
}
query = tx.Rebind(query)
sqlLog(query, args)
ret, err := tx.ExecContext(
ctx,
query,
args...,
)
if err != nil {
return 0, err
}
count, err := ret.RowsAffected()
if err != nil {
return 0, err
}
return count, nil
}
// DbExecuteLastIDNamedContent 执行sql语句并返回lastID
func DbExecuteLastIDNamedContent(ctx context.Context, tx DbExeAble, query string, argMap map[string]interface{}) (int64, error) {
query, args, err := sqlx.Named(query, argMap)
if err != nil {
return 0, err
}
query, args, err = sqlx.In(query, args...)
if err != nil {
return 0, err
}
query = tx.Rebind(query)
sqlLog(query, args)
ret, err := tx.ExecContext(
ctx,
query,
args...,
)
if err != nil {
return 0, err
}
lastID, err := ret.LastInsertId()
if err != nil {
return 0, err
}
return lastID, nil
}
// DbExecuteCountNamedContent 执行sql语句返回执行个数
func DbExecuteCountNamedContent(ctx context.Context, tx DbExeAble, query string, argMap map[string]interface{}) (int64, error) {
query, args, err := sqlx.Named(query, argMap)
if err != nil {
return 0, err
}
query, args, err = sqlx.In(query, args...)
if err != nil {
return 0, err
}
query = tx.Rebind(query)
sqlLog(query, args)
ret, err := tx.ExecContext(
ctx,
query,
args...,
)
if err != nil {
return 0, err
}
count, err := ret.RowsAffected()
if err != nil {
return 0, err
}
return count, nil
}
// DbGetNamedContent 执行sql查询并返回当个元素
func DbGetNamedContent(ctx context.Context, tx DbExeAble, dest interface{}, query string, argMap map[string]interface{}) (bool, error) {
query, args, err := sqlx.Named(query, argMap)
if err != nil {
return false, err
}
query, args, err = sqlx.In(query, args...)
if err != nil {
return false, err
}
query = tx.Rebind(query)
sqlLog(query, args)
err = tx.GetContext(
ctx,
dest,
query,
args...,
)
if err == sql.ErrNoRows {
// 没有元素
return false, nil
}
if err != nil {
// 执行错误
return false, err
}
return true, nil
}
// DbSelectNamedContent 执行sql查询并返回多行
func DbSelectNamedContent(ctx context.Context, tx DbExeAble, dest interface{}, query string, argMap map[string]interface{}) error {
query, args, err := sqlx.Named(query, argMap)
if err != nil {
return err
}
query, args, err = sqlx.In(query, args...)
if err != nil {
return err
}
query = tx.Rebind(query)
sqlLog(query, args)
err = tx.SelectContext(
ctx,
dest,
query,
args...,
)
if err == sql.ErrNoRows {
// 没有元素
return nil
}
if err != nil {
// 执行错误
return err
}
return nil
}
// DbNamedRowsContent 执行sql查询并返回多行
func DbNamedRowsContent(ctx context.Context, tx DbExeAble, query string, argMap map[string]interface{}) ([]gin.H, error) {
query, args, err := sqlx.Named(query, argMap)
if err != nil {
return nil, err
}
query, args, err = sqlx.In(query, args...)
if err != nil {
return nil, err
}
query = tx.Rebind(query)
sqlLog(query, args)
rows, err := tx.QueryContext(
ctx,
query,
args...,
)
if err == sql.ErrNoRows {
// 没有元素
return nil, nil
}
if err != nil {
return nil, err
}
defer func() {
_ = rows.Close()
}()
cts, err := rows.ColumnTypes()
if err != nil {
return nil, err
}
l := len(cts)
columns := make([]reflect.Value, l)
columnsPoint := make([]interface{}, l)
for i, ct := range cts {
dbType := ct.DatabaseTypeName()
goType, ok := MysqlTypeToGoMap[dbType]
if !ok {
return nil, fmt.Errorf("no db type: %s", dbType)
}
var tv reflect.Value
switch goType {
case MySqlGoTypeString:
tv = reflect.New(reflect.TypeOf(""))
case MySqlGoTypeInt64:
tv = reflect.New(reflect.TypeOf(int64(0)))
case MySqlGoTypeBytes:
tv = reflect.New(reflect.TypeOf([]byte{}))
case MySqlGoTypeFloat64:
tv = reflect.New(reflect.TypeOf(float64(0)))
case MySqlGoTypeTime:
tv = reflect.New(reflect.TypeOf(time.Time{}))
default:
return nil, fmt.Errorf("no go type: %d", goType)
}
e := tv.Elem()
columns[i] = e
columnsPoint[i] = e.Addr().Interface()
}
var mapRows []gin.H
for rows.Next() {
err := rows.Scan(columnsPoint...)
if err != nil {
return nil, err
}
rowMap := map[string]interface{}{}
for i, v := range columns {
colName := cts[i].Name()
rowMap[colName] = v.Interface()
}
mapRows = append(mapRows, rowMap)
}
return mapRows, nil
}
// DbUpdateKV 更新
func DbUpdateKV(ctx context.Context, tx DbExeAble, table string, updateMap H, keys []string, values []interface{}) (int64, error) {
keysLen := len(keys)
if 0 == keysLen {
return 0, fmt.Errorf("keys len error")
}
if keysLen != len(values) {
return 0, fmt.Errorf("value len error")
}
updateLastIndex := len(updateMap) - 1
argMap := H{}
query := strings.Builder{}
query.WriteString("UPDATE\n")
query.WriteString(table)
query.WriteString("\nSET\n")
var updateIndex int
for k, v := range updateMap {
argK := strings.ReplaceAll(k, ".", "_")
argK = strings.ReplaceAll(argK, "`", "_")
query.WriteString(k)
query.WriteString("=:")
query.WriteString(argK)
if updateIndex == updateLastIndex {
query.WriteString("\n")
} else {
query.WriteString(",\n")
}
updateIndex++
argMap[argK] = v
}
query.WriteString("WHERE\n")
for i, key := range keys {
argK := strings.ReplaceAll(key, ".", "_")
argK = strings.ReplaceAll(argK, "`", "_")
if i != 0 {
query.WriteString("AND ")
}
value := values[i]
query.WriteString(key)
rt := reflect.TypeOf(value)
switch rt.Kind() {
case reflect.Slice:
s := reflect.ValueOf(value)
if s.Len() == 0 {
return 0, nil
}
query.WriteString(" IN (:")
query.WriteString(argK)
query.WriteString(")")
default:
query.WriteString("=:")
query.WriteString(argK)
}
query.WriteString("\n")
argMap[argK] = value
}
count, err := DbExecuteCountNamedContent(
ctx,
tx,
query.String(),
argMap,
)
if err != nil {
return 0, err
}
return count, nil
}
// DbDeleteKV 删除
func DbDeleteKV(ctx context.Context, tx DbExeAble, table string, keys []string, values []interface{}) (int64, error) {
keysLen := len(keys)
if 0 == keysLen {
return 0, fmt.Errorf("keys len error")
}
if keysLen != len(values) {
return 0, fmt.Errorf("value len error")
}
argMap := H{}
query := strings.Builder{}
query.WriteString("DELETE\nFROM\n")
query.WriteString(table)
query.WriteString("\nWHERE\n")
for i, key := range keys {
argK := strings.ReplaceAll(key, ".", "_")
argK = strings.ReplaceAll(argK, "`", "_")
if i != 0 {
query.WriteString("AND ")
}
value := values[i]
query.WriteString(key)
rt := reflect.TypeOf(value)
switch rt.Kind() {
case reflect.Slice:
s := reflect.ValueOf(value)
if s.Len() == 0 {
return 0, nil
}
query.WriteString(" IN (:")
query.WriteString(argK)
query.WriteString(")")
default:
query.WriteString("=:")
query.WriteString(argK)
}
query.WriteString("\n")
argMap[argK] = value
}
count, err := DbExecuteCountNamedContent(
ctx,
tx,
query.String(),
argMap,
)
if err != nil {
return 0, err
}
return count, nil
}
// DbTransaction 执行事物
func DbTransaction(ctx context.Context, db *sqlx.DB, f func(dbTx DbExeAble) error) error {
isComment := false
tx, err := db.BeginTxx(ctx, nil)
if err != nil {
return err
}
defer func() {
if !isComment {
_ = tx.Rollback()
}
}()
err = f(tx)
if err != nil {
return err
}
err = tx.Commit()
if err != nil {
return err
}
isComment = true
return nil
}
func sqlLog(query string, args []interface{}) {
if isShowSQL {
queryStr := query + ";"
for _, arg := range args {
_, ok := arg.(string)
if ok {
queryStr = strings.Replace(queryStr, "?", fmt.Sprintf(`"%s"`, arg), 1)
} else {
queryStr = strings.Replace(queryStr, "?", fmt.Sprintf(`%v`, arg), 1)
}
}
Log.Debugf("exec sql:\n%s;\n%#v", query, args)
}
}