Skip to content

Commit

Permalink
session增加exec方法,允许执行CRUD之外的sql语句,同时不再强制检测action是否匹配
Browse files Browse the repository at this point in the history
  • Loading branch information
xfali committed Feb 14, 2020
1 parent ee030b4 commit 8db160b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
36 changes: 35 additions & 1 deletion sqlrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ type DeleteRunner struct {
BaseRunner
}

type ExecRunner struct {
BaseRunner
}

//使用一个session操作数据库
func (this *SessionManager) NewSession() *Session {
return &Session{
Expand Down Expand Up @@ -146,6 +150,10 @@ func (this *Session) Insert(sql string) Runner {
return this.createInsert(this.findSqlParser(sql))
}

func (this *Session) Exec(sql string) Runner {
return this.createExec(this.findSqlParser(sql))
}

func (this *BaseRunner) Param(params ...interface{}) Runner {
//TODO: 使用缓存加速,避免每次都生成动态sql
//测试发现性能提升非常有限,故取消
Expand All @@ -167,10 +175,12 @@ func (this *BaseRunner) Param(params ...interface{}) Runner {
md, err := this.sqlParser.ParseMetadata(this.driver, params...)

if err == nil {
if this.action == md.Action {
if this.action == "" || this.action == md.Action {
this.metadata = md
} else {
//allow different action
this.log(logging.WARN, "sql action not match expect %s get %s", this.action, md.Action)
this.metadata = md
}
} else {
this.log(logging.WARN, err.Error())
Expand Down Expand Up @@ -231,6 +241,18 @@ func (this *UpdateRunner) Result(bean interface{}) error {
return err
}

func (this *ExecRunner) Result(bean interface{}) error {
if this.metadata == nil {
this.log(logging.WARN, "Sql Matadata is nil")
return errors.RUNNER_NOT_READY
}
i, err := this.session.Update(this.ctx, this.metadata.PrepareSql, this.metadata.Params...)
if reflection.CanSet(bean) {
reflection.SetValue(reflection.ReflectValue(bean), i)
}
return err
}

func (this *DeleteRunner) Result(bean interface{}) error {
if this.metadata == nil {
this.log(logging.WARN, "Sql Matadata is nil")
Expand Down Expand Up @@ -301,6 +323,18 @@ func (this *Session) createInsert(parser sqlparser.SqlParser) Runner {
return ret
}

func (this *Session) createExec(parser sqlparser.SqlParser) Runner {
ret := &ExecRunner{}
ret.action = ""
ret.log = this.log
ret.session = this.session
ret.sqlParser = parser
ret.ctx = this.ctx
ret.driver = this.driver
ret.this = ret
return ret
}

func (this *Session) findSqlParser(sqlId string) sqlparser.SqlParser {
ret, ok := FindDynamicSqlParser(sqlId)
if !ok {
Expand Down
36 changes: 33 additions & 3 deletions test/postgresql/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ func connect() factory.Factory {

func initTest(t *testing.T) (err error) {
sql_table := "CREATE TABLE IF NOT EXISTS test_table (" +
"id serial NOT NULL,"+
"id serial NOT NULL," +
"username varchar(255) DEFAULT NULL," +
"password varchar(255) DEFAULT NULL," +
"createTime timestamp DEFAULT NULL,"+
"createTime timestamp DEFAULT NULL," +
"PRIMARY KEY (id)" +
")"
")"

db, err := sql.Open("postgres", "host=localhost port=5432 user=test password=test dbname=testdb sslmode=disable")
if err != nil {
Expand Down Expand Up @@ -283,3 +283,33 @@ func TestSession(t *testing.T) {
t.Log(ret)
})
}

func TestExec(t *testing.T) {
t.Run("default", func(t *testing.T) {
mgr := gobatis.NewSessionManager(connect())
sql_table := "CREATE TABLE IF NOT EXISTS exec_test_tbl (" +
"id serial NOT NULL," +
"username varchar(255) DEFAULT NULL," +
"password varchar(255) DEFAULT NULL," +
"createTime timestamp DEFAULT NULL," +
"PRIMARY KEY (id)" +
")"
var ret []TestTable
err := mgr.NewSession().Exec(sql_table).Param().Result(&ret)
if err != nil {
t.Fatal(err)
}
t.Log(ret)
})

t.Run("default", func(t *testing.T) {
mgr := gobatis.NewSessionManager(connect())
sql_table := "DROP TABLE exec_test_tbl"
var ret []TestTable
err := mgr.NewSession().Exec(sql_table).Param().Result(&ret)
if err != nil {
t.Fatal(err)
}
t.Log(ret)
})
}

0 comments on commit 8db160b

Please sign in to comment.