-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathOverrideFunc.go
227 lines (210 loc) · 8.05 KB
/
OverrideFunc.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package zorm
import (
"context"
"errors"
"reflect"
)
/*
// OverrideFunc 重写ZORM的函数,当你使用这个函数时,你必须知道自己在做什么
//oldInsertFunc 默认的Insert实现
var oldInsertFunc func(ctx context.Context, entity zorm.IEntityStruct) (int, error)
//newInsertFunc 新的Insert实现
var newInsertFunc = func(ctx context.Context, entity zorm.IEntityStruct) (int, error) {
fmt.Println("Insert前")
i, err := oldInsertFunc(ctx, entity)
fmt.Println("Insert后")
return i, err
}
// 在init函数中注册覆盖老的函数
func init() {
ok, oldFunc, err := zorm.OverrideFunc("Insert", newInsertFunc)
if ok && err == nil {
oldInsertFunc = oldFunc.(func(ctx context.Context, entity zorm.IEntityStruct) (int, error))
}
}
*/
// OverrideFunc 重写ZORM的函数,用于风险监控,只要查看这个函数的调用,就知道哪些地方重写了函数,避免项目混乱.当你使用这个函数时,你必须知道自己在做什么
// funcName 是需要重写的方法命,funcObject是对应的函数. 返回值bool是否重写成功,interface{}是重写前的函数
// 一般是在init里调用重写
func OverrideFunc(funcName string, funcObject interface{}) (bool, interface{}, error) {
if funcName == "" {
return false, nil, errors.New("->OverrideFunc-->funcName不能为空")
}
// oldFunc 老的函数
var oldFunc interface{} = nil
switch funcName {
case "Transaction":
newFunc, ok := funcObject.(func(ctx context.Context, doTransaction func(ctx context.Context) (interface{}, error)) (interface{}, error))
if ok {
oldFunc = transaction
transaction = newFunc
}
case "QueryRow":
newFunc, ok := funcObject.(func(ctx context.Context, finder *Finder, entity interface{}) (bool, error))
if ok {
oldFunc = queryRow
queryRow = newFunc
}
case "Query":
newFunc, ok := funcObject.(func(ctx context.Context, finder *Finder, rowsSlicePtr interface{}, page *Page) error)
if ok {
oldFunc = query
query = newFunc
}
case "QueryRowMap":
newFunc, ok := funcObject.(func(ctx context.Context, finder *Finder) (map[string]interface{}, error))
if ok {
oldFunc = queryRowMap
queryRowMap = newFunc
}
case "QueryMap":
newFunc, ok := funcObject.(func(ctx context.Context, finder *Finder, page *Page) ([]map[string]interface{}, error))
if ok {
oldFunc = queryMap
queryMap = newFunc
}
case "UpdateFinder":
newFunc, ok := funcObject.(func(ctx context.Context, finder *Finder) (int, error))
if ok {
oldFunc = updateFinder
updateFinder = newFunc
}
case "Insert":
newFunc, ok := funcObject.(func(ctx context.Context, entity IEntityStruct) (int, error))
if ok {
oldFunc = insert
insert = newFunc
}
case "InsertSlice":
newFunc, ok := funcObject.(func(ctx context.Context, entityStructSlice []IEntityStruct) (int, error))
if ok {
oldFunc = insertSlice
insertSlice = newFunc
}
case "Update":
newFunc, ok := funcObject.(func(ctx context.Context, entity IEntityStruct) (int, error))
if ok {
oldFunc = update
update = newFunc
}
case "UpdateNotZeroValue":
newFunc, ok := funcObject.(func(ctx context.Context, entity IEntityStruct) (int, error))
if ok {
oldFunc = updateNotZeroValue
updateNotZeroValue = newFunc
}
case "Delete":
newFunc, ok := funcObject.(func(ctx context.Context, entity IEntityStruct) (int, error))
if ok {
oldFunc = delete
delete = newFunc
}
case "InsertEntityMap":
newFunc, ok := funcObject.(func(ctx context.Context, entity IEntityMap) (int, error))
if ok {
oldFunc = insertEntityMap
insertEntityMap = newFunc
}
case "InsertEntityMapSlice":
newFunc, ok := funcObject.(func(ctx context.Context, entity []IEntityMap) (int, error))
if ok {
oldFunc = insertEntityMapSlice
insertEntityMapSlice = newFunc
}
case "UpdateEntityMap":
newFunc, ok := funcObject.(func(ctx context.Context, entity IEntityMap) (int, error))
if ok {
oldFunc = updateEntityMap
updateEntityMap = newFunc
}
case "reBuildSQL": //重建语句,用于占位符替换等
newFunc, ok := funcObject.(func(ctx context.Context, config *DataSourceConfig, sqlstr *string, args *[]interface{}) (*string, *[]interface{}, error))
if ok {
oldFunc = reBuildSQL
reBuildSQL = newFunc
}
case "reBuildUpdateSQL": //重建更新语句,处理特殊场景,例如 clickhouse的 UPDATE 和 DELETE 等
newFunc, ok := funcObject.(func(ctx context.Context, config *DataSourceConfig, sqlstr *string) error)
if ok {
oldFunc = reBuildUpdateSQL
reBuildUpdateSQL = newFunc
}
case "wrapPageSQL": //分页SQL
newFunc, ok := funcObject.(func(ctx context.Context, config *DataSourceConfig, sqlstr *string, page *Page) error)
if ok {
oldFunc = wrapPageSQL
wrapPageSQL = newFunc
}
case "wrapInsertSQL": //Insert IEntityStruct SQL
newFunc, ok := funcObject.(func(ctx context.Context, config *DataSourceConfig, typeOf *reflect.Type, entity IEntityStruct, columns *[]reflect.StructField, values *[]interface{}) (*string, int, string, error))
if ok {
oldFunc = wrapInsertSQL
wrapInsertSQL = newFunc
}
case "wrapAutoIncrementInsertSQL": //Insert IEntityStruct 主键自增值的SQL
newFunc, ok := funcObject.(func(ctx context.Context, config *DataSourceConfig, pkColumnName string, sqlstr *string, values *[]interface{}) (*int64, *int64))
if ok {
oldFunc = wrapAutoIncrementInsertSQL
wrapAutoIncrementInsertSQL = newFunc
}
case "wrapInsertSliceSQL": //批量插入 IEntityStruct 的SQL
newFunc, ok := funcObject.(func(ctx context.Context, config *DataSourceConfig, typeOf *reflect.Type, entityStructSlice []IEntityStruct, columns *[]reflect.StructField, values *[]interface{}) (*string, int, error))
if ok {
oldFunc = wrapInsertSliceSQL
wrapInsertSliceSQL = newFunc
}
case "wrapInsertEntityMapSQL": //插入 IEntityMap 的SQL
newFunc, ok := funcObject.(func(ctx context.Context, config *DataSourceConfig, entity IEntityMap) (string, *[]interface{}, bool, error))
if ok {
oldFunc = wrapInsertEntityMapSQL
wrapInsertEntityMapSQL = newFunc
}
case "wrapInsertEntityMapSliceSQL": //批量插入 IEntityMap 的SQL
newFunc, ok := funcObject.(func(ctx context.Context, config *DataSourceConfig, entityMapSlice []IEntityMap) (*string, *[]interface{}, error))
if ok {
oldFunc = wrapInsertEntityMapSliceSQL
wrapInsertEntityMapSliceSQL = newFunc
}
case "wrapDeleteSQL": //删除 IEntityStruct 的SQL
newFunc, ok := funcObject.(func(ctx context.Context, entity IEntityStruct) (string, error))
if ok {
oldFunc = wrapDeleteSQL
wrapDeleteSQL = newFunc
}
case "wrapUpdateSQL": //更新 IEntityStruct 的SQL
newFunc, ok := funcObject.(func(ctx context.Context, typeOf *reflect.Type, entity IEntityStruct, columns *[]reflect.StructField, values *[]interface{}) (string, error))
if ok {
oldFunc = wrapUpdateSQL
wrapUpdateSQL = newFunc
}
case "wrapUpdateEntityMapSQL": //更新 IEntityMap 的SQL
newFunc, ok := funcObject.(func(ctx context.Context, entity IEntityMap) (*string, *[]interface{}, error))
if ok {
oldFunc = wrapUpdateEntityMapSQL
wrapUpdateEntityMapSQL = newFunc
}
default:
return false, oldFunc, errors.New("->OverrideFunc-->函数" + funcName + "暂不支持重写或不存在")
}
if oldFunc == nil {
return false, oldFunc, errors.New("->OverrideFunc-->请检查传入的" + funcName + "函数实现,断言转换失败.")
}
return true, oldFunc, nil
}