@@ -81,6 +81,10 @@ type DeleteRunner struct {
8181 BaseRunner
8282}
8383
84+ type ExecRunner struct {
85+ BaseRunner
86+ }
87+
8488//使用一个session操作数据库
8589func (this * SessionManager ) NewSession () * Session {
8690 return & Session {
@@ -146,6 +150,10 @@ func (this *Session) Insert(sql string) Runner {
146150 return this .createInsert (this .findSqlParser (sql ))
147151}
148152
153+ func (this * Session ) Exec (sql string ) Runner {
154+ return this .createExec (this .findSqlParser (sql ))
155+ }
156+
149157func (this * BaseRunner ) Param (params ... interface {}) Runner {
150158 //TODO: 使用缓存加速,避免每次都生成动态sql
151159 //测试发现性能提升非常有限,故取消
@@ -167,10 +175,12 @@ func (this *BaseRunner) Param(params ...interface{}) Runner {
167175 md , err := this .sqlParser .ParseMetadata (this .driver , params ... )
168176
169177 if err == nil {
170- if this .action == md .Action {
178+ if this .action == "" || this . action == md .Action {
171179 this .metadata = md
172180 } else {
181+ //allow different action
173182 this .log (logging .WARN , "sql action not match expect %s get %s" , this .action , md .Action )
183+ this .metadata = md
174184 }
175185 } else {
176186 this .log (logging .WARN , err .Error ())
@@ -231,6 +241,18 @@ func (this *UpdateRunner) Result(bean interface{}) error {
231241 return err
232242}
233243
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+
234256func (this * DeleteRunner ) Result (bean interface {}) error {
235257 if this .metadata == nil {
236258 this .log (logging .WARN , "Sql Matadata is nil" )
@@ -301,6 +323,18 @@ func (this *Session) createInsert(parser sqlparser.SqlParser) Runner {
301323 return ret
302324}
303325
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+
304338func (this * Session ) findSqlParser (sqlId string ) sqlparser.SqlParser {
305339 ret , ok := FindDynamicSqlParser (sqlId )
306340 if ! ok {
0 commit comments