sqlTool.go
2.9 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
package utils
import (
"fmt"
"oppmg/common/log"
"oppmg/protocol"
"strings"
"github.com/astaxie/beego/orm"
)
//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(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 {
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("RowsAffected:%d", num)
return nil
}
type QueryDataByPage struct {
CountSql string
DataSql string
Param []interface{}
offset int
num int
}
func NewQueryDataByPage(countsql, datasql string) *QueryDataByPage {
return &QueryDataByPage{
CountSql: countsql,
DataSql: datasql,
}
}
//AddParam 添加条件参数
func (q *QueryDataByPage) AddParam(param ...interface{}) {
q.Param = param
}
func (q *QueryDataByPage) LimitPage(offset, num int) {
q.offset = offset
q.num = num
}
//Query 执行分页查询
func (q *QueryDataByPage) Query(result interface{}) (pageinfo protocol.ResponsePageInfo, err error) {
pagebegin := (q.offset - 1) * q.num
if pagebegin < 0 {
pagebegin = 0
}
var (
total int
)
o := orm.NewOrm()
err = ExecuteQueryOneWithOrmer(o, &total, q.CountSql, q.Param...)
if err != nil {
return
}
if total == 0 {
return protocol.ResponsePageInfo{CurrentPage: q.offset, TotalPage: total}, nil
}
q.DataSql = fmt.Sprintf("%s limit %d,%d", q.DataSql, pagebegin, q.num)
err = ExecuteQueryAllWithOrmer(o, result, q.DataSql, q.Param...)
if err != nil {
return
}
return protocol.ResponsePageInfo{CurrentPage: q.offset, TotalPage: total}, nil
}