@@ -81,6 +81,10 @@ type DeleteRunner struct {
81
81
BaseRunner
82
82
}
83
83
84
+ type ExecRunner struct {
85
+ BaseRunner
86
+ }
87
+
84
88
//使用一个session操作数据库
85
89
func (this * SessionManager ) NewSession () * Session {
86
90
return & Session {
@@ -146,6 +150,10 @@ func (this *Session) Insert(sql string) Runner {
146
150
return this .createInsert (this .findSqlParser (sql ))
147
151
}
148
152
153
+ func (this * Session ) Exec (sql string ) Runner {
154
+ return this .createExec (this .findSqlParser (sql ))
155
+ }
156
+
149
157
func (this * BaseRunner ) Param (params ... interface {}) Runner {
150
158
//TODO: 使用缓存加速,避免每次都生成动态sql
151
159
//测试发现性能提升非常有限,故取消
@@ -167,10 +175,12 @@ func (this *BaseRunner) Param(params ...interface{}) Runner {
167
175
md , err := this .sqlParser .ParseMetadata (this .driver , params ... )
168
176
169
177
if err == nil {
170
- if this .action == md .Action {
178
+ if this .action == "" || this . action == md .Action {
171
179
this .metadata = md
172
180
} else {
181
+ //allow different action
173
182
this .log (logging .WARN , "sql action not match expect %s get %s" , this .action , md .Action )
183
+ this .metadata = md
174
184
}
175
185
} else {
176
186
this .log (logging .WARN , err .Error ())
@@ -231,6 +241,18 @@ func (this *UpdateRunner) Result(bean interface{}) error {
231
241
return err
232
242
}
233
243
244
+ func (this * ExecRunner ) Result (bean interface {}) error {
245
+ if this .metadata == nil {
246
+ this .log (logging .WARN , "Sql Matadata is nil" )
247
+ return errors .RUNNER_NOT_READY
248
+ }
249
+ i , err := this .session .Update (this .ctx , this .metadata .PrepareSql , this .metadata .Params ... )
250
+ if reflection .CanSet (bean ) {
251
+ reflection .SetValue (reflection .ReflectValue (bean ), i )
252
+ }
253
+ return err
254
+ }
255
+
234
256
func (this * DeleteRunner ) Result (bean interface {}) error {
235
257
if this .metadata == nil {
236
258
this .log (logging .WARN , "Sql Matadata is nil" )
@@ -301,6 +323,18 @@ func (this *Session) createInsert(parser sqlparser.SqlParser) Runner {
301
323
return ret
302
324
}
303
325
326
+ func (this * Session ) createExec (parser sqlparser.SqlParser ) Runner {
327
+ ret := & ExecRunner {}
328
+ ret .action = ""
329
+ ret .log = this .log
330
+ ret .session = this .session
331
+ ret .sqlParser = parser
332
+ ret .ctx = this .ctx
333
+ ret .driver = this .driver
334
+ ret .this = ret
335
+ return ret
336
+ }
337
+
304
338
func (this * Session ) findSqlParser (sqlId string ) sqlparser.SqlParser {
305
339
ret , ok := FindDynamicSqlParser (sqlId )
306
340
if ! ok {
0 commit comments