-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperators.go
267 lines (238 loc) · 6.51 KB
/
operators.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
// A quickly mysql access component.
package godbx
import (
"context"
"crypto/md5"
"encoding/json"
"github.com/woaijssss/godbx/utils"
"strconv"
"strings"
"time"
)
const TableIdColumnName = "id"
const maxLogStringLen = 512 * 1024
// ConvertToAnySlice 把泛型的slice转换成 any类型的slice,在应用系统的上层往往是泛型slice,通过强类型校验来防止出错,
// 但在sql driver底层需要 []any进行参数传递,二者不能被编译器自动转换,所以需要该函数来转换
func ConvertToAnySlice[T any](data []T) []any {
l := len(data)
if l == 0 {
return nil
}
target := make([]any, l)
for i, v := range data {
target[i] = v
}
return target
}
// GetTableName 根据meta及上下文中的信息来确定表名称,这应用于分表的场景,记住这需要支持表shard的事务上下文
func GetTableName[T any](ctx context.Context, meta *TableMeta[T]) string {
tableName := meta.Table
if meta.ShardingFunc != nil {
shardingKey := getTableShardingKeyFromCtx(ctx)
tableName = meta.ShardingFunc(tableName, shardingKey)
}
return tableName
}
func buildSelectBase[T any](meta *TableMeta[T], view *View, ctx context.Context) string {
columnsStr := ""
if view == nil || len(view.viewColumns) == 0 {
columnsStr = strings.Join(meta.Columns, ",")
} else {
var includeColumns []string
if view.include {
includeColumns = view.viewColumns
} else {
var excludeMap = make(map[string]int)
for _, c := range view.viewColumns {
excludeMap[c] = 1
}
for _, c := range meta.Columns {
if excludeMap[c] == 0 {
includeColumns = append(includeColumns, c)
}
}
}
columnsStr = strings.Join(includeColumns, ",")
}
return "select " + columnsStr + " from " + GetTableName(ctx, meta)
}
func selectQuery[T any](meta *TableMeta[T], ctx context.Context, matcher Matcher, pager *Pager, orders []*Order, view *View) (string, []any, error) {
base := buildSelectBase(meta, view, ctx)
if matcher == nil {
return base, nil, nil
}
var args []any
condi, args, err := matcher.ToSQL(args)
if err != nil {
return "", nil, err
}
if condi == "" {
return base + buildQuerySuffix(pager, orders), nil, nil
}
return base + " where " + condi + buildQuerySuffix(pager, orders), args, nil
}
func countQuery[T any](meta *TableMeta[T], ctx context.Context, matcher Matcher) (string, []any, error) {
var base string
if meta.AutoColumn == "" {
base = "select count(*) from " + GetTableName(ctx, meta)
} else {
base = "select count(" + meta.AutoColumn + ") from " + GetTableName(ctx, meta)
}
if matcher == nil {
return base, nil, nil
}
var args []any
condi, args, err := matcher.ToSQL(args)
if err != nil {
return "", nil, err
}
if condi == "" {
return base, nil, nil
}
return base + " where " + condi, args, nil
}
func buildQuerySuffix(pager *Pager, orders []*Order) string {
ordStat := ""
last := len(orders) - 1
for i, order := range orders {
if i == 0 {
ordStat = " order by " + order.ColumnName
} else {
ordStat = ordStat + order.ColumnName
}
if order.Desc {
ordStat = ordStat + " desc"
}
if i < last {
ordStat = ordStat + ","
}
}
if pager == nil {
return ordStat
}
limitStat := ""
if pager.PageNumber == 1 {
limitStat = " limit " + strconv.Itoa(pager.PageSize)
} else {
startPos := int64(pager.PageNumber-1) * int64(pager.PageSize)
limitStat = " limit " + strconv.FormatInt(startPos, 10) + "," + strconv.Itoa(pager.PageSize)
}
return ordStat + limitStat
}
func buildUpdateBase[T any](meta *TableMeta[T], ctx context.Context) string {
var upConds []string
for _, v := range meta.Columns {
if v == meta.AutoColumn {
continue
}
upConds = append(upConds, v+" = ?")
}
upCondStmt := strings.Join(upConds, ",")
return "update " + GetTableName(ctx, meta) + " set " + upCondStmt
}
func updateExec[T any](meta *TableMeta[T], ins *T, ctx context.Context, matcher Matcher) (string, []any, error) {
base := buildUpdateBase(meta, ctx)
if matcher == nil {
return base, nil, nil
}
var exclude map[string]int
if meta.AutoColumn != "" {
exclude = map[string]int{
meta.AutoColumn: 1,
}
}
args := meta.ExtractFieldValues(ins, false, exclude)
condi, args, err := matcher.ToSQL(args)
if err != nil {
return "", nil, err
}
if condi == "" {
return base, args, nil
}
return base + " where " + condi, args, nil
}
func buildModifierExec[T any](meta *TableMeta[T], ctx context.Context, modifier Modifier, matcher Matcher) (string, []any, error) {
tableName := GetTableName(ctx, meta)
if BeforeModifyCallback != nil {
pcColumns, pcValues := modifier.getPureChangePairs()
if len(pcColumns) > 0 {
if err := BeforeModifyCallback(tableName, modifier, pcColumns, pcValues); err != nil {
return "", nil, err
}
}
}
base, args := modifier.toSQL(tableName)
if base == "" {
return "", nil, nil
}
if matcher == nil {
return base, args, nil
}
condi, args, err := matcher.ToSQL(args)
if err != nil {
return "", nil, err
}
if condi == "" {
return base, args, nil
}
return base + " where " + condi, args, nil
}
func buildInsInfoOfRow[T any](meta *TableMeta[T], view *View) (*T, []any) {
ins := new(T)
if view == nil || len(view.viewColumns) == 0 {
return ins, meta.ExtractFieldValues(ins, true, nil)
}
if view.include {
return ins, meta.ExtractFieldValuesByColumns(ins, true, view.viewColumns)
} else {
exclude := make(map[string]int)
for _, c := range view.viewColumns {
exclude[c] = 1
}
return ins, meta.ExtractFieldValues(ins, true, exclude)
}
}
func traceLogSQLBefore(ctx context.Context, sql string, args []any) string {
var argJson []byte
md5data := []byte(sql)
argJson, err := json.Marshal(args)
if err != nil {
GLogger.Error(ctx, err)
} else {
md5data = append(md5data, argJson...)
}
sumData := md5.Sum(md5data)
sqlMd5 := utils.ToUpperHexString(sumData[:])
outArgJson := argJson
smallArgs, changed := shortArgs(args)
if changed {
outArgJson, err = json.Marshal(smallArgs)
if err != nil {
GLogger.Error(ctx, err)
outArgJson = argJson
}
}
GLogger.ExecSQLBefore(ctx, sql, outArgJson, sqlMd5)
return sqlMd5
}
func traceLogSQLAfter(ctx context.Context, sqlMd5 string, startTime int64) {
GLogger.ExecSQLAfter(ctx, sqlMd5, time.Now().UnixMilli()-startTime)
}
func shortArgs(args []any) ([]any, bool) {
if len(args) == 0 {
return args, false
}
changed := false
ret := make([]any, len(args))
for i, v := range args {
if s, ok := v.(string); ok {
if len(s) >= maxLogStringLen {
ret[i] = s[:1024] + "..."
changed = true
continue
}
}
ret[i] = v
}
return ret, changed
}