-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
159 lines (142 loc) · 6.58 KB
/
context.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
package sqlxp
import (
"database/sql"
"fmt"
"github.com/jmoiron/sqlx"
"github.com/jtao539/sqlxp/model"
. "github.com/jtao539/sqlxp/sqlFactory"
)
// finished by JiangTao in 2022.07
type SqlxP struct {
DB *sqlx.DB
}
// PageInfo Paging common input parameter structure(use in select)
type PageInfo struct {
Page int `json:"page" form:"page"` // 页码
PageSize int `json:"page_size" form:"page_size"` // 每页大小
model.SelectParams
}
// Select 列表查找, dest为要查找的数据类型数组, request为查询条件结构体, table 为表名称, tags为需要跳过的字段
func (s *SqlxP) Select(dest interface{}, request interface{}, table string, tags ...string) (err error, total int) {
str, params, countStr := SafeSelect(request, table, tags...)
err = s.DB.Select(dest, str, params...)
s.DB.Get(&total, countStr, params...)
return
}
// SelectMT 多表查询 列表查找, dest为要查找的数据类型数组, request为查询条件结构体, table 为表名称, otherFiledMap为表字段与sql映射, factors 为sql条件, tags为需要跳过的字段
func (s *SqlxP) SelectMT(dest interface{}, request interface{}, table string, otherFiledMap map[string]string, factors []string, tags ...string) (err error, total int) {
str, params, countStr := SafeSelectMT(request, table, otherFiledMap, factors, true, tags...)
err = s.DB.Select(dest, str, params...)
s.DB.Get(&total, countStr, params...)
return
}
// SelectMTP 多表查询 列表查找, dest为要查找的数据类型数组, request为查询条件结构体, table 为表名称, otherFiledMap为表字段与sql映射, factors 为条件和参数的map, tags为需要跳过的字段
func (s *SqlxP) SelectMTP(dest interface{}, request interface{}, table string, otherFiledMap map[string]string, factorsMap map[string][]interface{}, tags ...string) (err error, total int) {
str, params, countStr := SafeSelectMTP(request, table, otherFiledMap, factorsMap, true, tags...)
err = s.DB.Select(dest, str, params...)
s.DB.Get(&total, countStr, params...)
return
}
// SelectP 多表查询 列表查找, dest为要查找的数据类型数组, request为查询条件结构体, table 为表名称, otherFiledMap为表字段与sql映射, factors 为条件和参数的map, tags为需要跳过的字段
func (s *SqlxP) SelectP(dest interface{}, request interface{}, table string, tags ...string) (err error, total int) {
str, params, countStr := SafeSelectP(request, table, tags...)
err = s.DB.Select(dest, str, params...)
s.DB.Get(&total, countStr, params...)
return
}
// SelectMTOrder 多表查询 列表查找, dest为要查找的数据类型数组, request为查询条件结构体, table 为表名称, otherFiledMap为表字段与sql映射, factors 为sql条件, tags为需要跳过的字段
func (s *SqlxP) SelectMTOrder(dest interface{}, request interface{}, table string, otherFiledMap map[string]string, factors []string, tags ...string) (err error, total int) {
str, params, countStr := SafeSelectMT(request, table, otherFiledMap, factors, false, tags...)
err = s.DB.Select(dest, str, params...)
s.DB.Get(&total, countStr, params...)
return
}
// SelectWF 可手动介入查询条件的列表查找, dest为要查找的数据类型数组, request为查询条件结构体, table 为表名称, factors 为sql条件, tags为需要跳过的字段
func (s *SqlxP) SelectWF(dest interface{}, request interface{}, table string, factors []string, tags ...string) (err error, total int) {
str, params, countStr := SafeSelectWithFactor(request, table, factors, tags...)
err = s.DB.Select(dest, str, params...)
s.DB.Get(&total, countStr, params...)
return
}
// SelectOrder 列表查找, dest为要查找的数据类型数组, request为查询条件结构体, table 为表名称, tags为需要跳过的字段
func (s *SqlxP) SelectOrder(dest interface{}, request interface{}, desc bool, orderField string, table string, tags ...string) (err error, total int) {
str, params, countStr := SafeSelectOrder(request, table, desc, orderField, tags...)
err = s.DB.Select(dest, str, params...)
s.DB.Get(&total, countStr, params...)
return
}
// SelectWFOrder 可手动介入查询条件的列表查找, dest为要查找的数据类型数组, request为查询条件结构体, table 为表名称, factors 为sql条件, tags为需要跳过的字段
func (s *SqlxP) SelectWFOrder(dest interface{}, request interface{}, desc bool, orderField string, table string, factors []string, tags ...string) (err error, total int) {
str, params, countStr := SafeSelectWithFactorOrder(request, table, desc, orderField, factors, tags...)
err = s.DB.Select(dest, str, params...)
s.DB.Get(&total, countStr, params...)
return
}
// Update 数据更新, request为新数据的结构体, entity为SQLNULL实体 table 为表名称, 通过对比request和entity获取跳过的字段, tx 为事务支持
func (s *SqlxP) Update(request interface{}, entity interface{}, table string, tx ...*sqlx.Tx) error {
var err error
str, params := SafeUpdate(request, entity, table)
if len(tx) > 0 {
_, err = tx[0].Exec(str, params...)
} else {
_, err = s.DB.Exec(str, params...)
}
if err != nil {
return err
}
return err
}
// UpdateP 数据更新, request为新数据的结构体, entity为SQLNULL实体 table 为表名称, fields为需要跳过更新的字段, 通过对比request和entity获取跳过的字段, tx 为事务支持
func (s *SqlxP) UpdateP(request interface{}, entity interface{}, table string, fields []string, tx ...*sqlx.Tx) error {
var err error
str, params := SafeUpdateP(request, entity, table, fields...)
if len(tx) > 0 {
_, err = tx[0].Exec(str, params...)
} else {
_, err = s.DB.Exec(str, params...)
}
if err != nil {
return err
}
return err
}
func (s *SqlxP) GetOneById(one interface{}, table string, id int) error {
str := fmt.Sprintf("select * from %s where id=?", table)
return s.DB.Get(one, str, id)
}
func (s *SqlxP) InsertOne(one interface{}, table string, tx ...*sqlx.Tx) error {
var err error
var rows sql.Result
str := SafeInsert(one, table)
if len(tx) > 0 {
rows, err = tx[0].NamedExec(str, one)
} else {
rows, err = s.DB.NamedExec(str, one)
}
if err != nil {
return err
}
AffectedNum, _ := rows.RowsAffected()
if AffectedNum == 0 {
return InsertError
}
return err
}
func (s *SqlxP) DeleteOneById(table string, id int, tx ...*sqlx.Tx) error {
var err error
var rows sql.Result
str := fmt.Sprintf("delete from %s where id = ?", table)
if len(tx) > 0 {
rows, err = tx[0].Exec(str, id)
} else {
rows, err = s.DB.Exec(str, id)
}
if err != nil {
return err
}
AffectedNum, _ := rows.RowsAffected()
if AffectedNum == 0 {
return DeleteError
}
return err
}