Skip to content

Commit 8db160b

Browse files
committed
session增加exec方法,允许执行CRUD之外的sql语句,同时不再强制检测action是否匹配
1 parent ee030b4 commit 8db160b

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

sqlrunner.go

+35-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ type DeleteRunner struct {
8181
BaseRunner
8282
}
8383

84+
type ExecRunner struct {
85+
BaseRunner
86+
}
87+
8488
//使用一个session操作数据库
8589
func (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+
149157
func (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+
234256
func (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+
304338
func (this *Session) findSqlParser(sqlId string) sqlparser.SqlParser {
305339
ret, ok := FindDynamicSqlParser(sqlId)
306340
if !ok {

test/postgresql/runner_test.go

+33-3
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ func connect() factory.Factory {
4545

4646
func initTest(t *testing.T) (err error) {
4747
sql_table := "CREATE TABLE IF NOT EXISTS test_table (" +
48-
"id serial NOT NULL,"+
48+
"id serial NOT NULL," +
4949
"username varchar(255) DEFAULT NULL," +
5050
"password varchar(255) DEFAULT NULL," +
51-
"createTime timestamp DEFAULT NULL,"+
51+
"createTime timestamp DEFAULT NULL," +
5252
"PRIMARY KEY (id)" +
53-
")"
53+
")"
5454

5555
db, err := sql.Open("postgres", "host=localhost port=5432 user=test password=test dbname=testdb sslmode=disable")
5656
if err != nil {
@@ -283,3 +283,33 @@ func TestSession(t *testing.T) {
283283
t.Log(ret)
284284
})
285285
}
286+
287+
func TestExec(t *testing.T) {
288+
t.Run("default", func(t *testing.T) {
289+
mgr := gobatis.NewSessionManager(connect())
290+
sql_table := "CREATE TABLE IF NOT EXISTS exec_test_tbl (" +
291+
"id serial NOT NULL," +
292+
"username varchar(255) DEFAULT NULL," +
293+
"password varchar(255) DEFAULT NULL," +
294+
"createTime timestamp DEFAULT NULL," +
295+
"PRIMARY KEY (id)" +
296+
")"
297+
var ret []TestTable
298+
err := mgr.NewSession().Exec(sql_table).Param().Result(&ret)
299+
if err != nil {
300+
t.Fatal(err)
301+
}
302+
t.Log(ret)
303+
})
304+
305+
t.Run("default", func(t *testing.T) {
306+
mgr := gobatis.NewSessionManager(connect())
307+
sql_table := "DROP TABLE exec_test_tbl"
308+
var ret []TestTable
309+
err := mgr.NewSession().Exec(sql_table).Param().Result(&ret)
310+
if err != nil {
311+
t.Fatal(err)
312+
}
313+
t.Log(ret)
314+
})
315+
}

0 commit comments

Comments
 (0)