Skip to content

Commit

Permalink
add stmt sql
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Feb 15, 2020
1 parent 75be855 commit f0fe189
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 7 deletions.
9 changes: 9 additions & 0 deletions GoMybatisEngine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,18 @@ func (it *TestSession) Query(sqlorArgs string) ([]map[string][]byte, error) {
func (it *TestSession) Exec(sqlorArgs string) (*Result, error) {
return nil, nil
}

func (it *TestSession) QueryPrepare(sqlorArgs string, args ...interface{}) ([]map[string][]byte, error) {
return nil, nil
}
func (it *TestSession) ExecPrepare(sqlorArgs string, args ...interface{}) (*Result, error) {
return nil, nil
}

func (it *TestSession) Rollback() error {
return nil
}

func (it *TestSession) Commit() error {
return nil
}
Expand Down
13 changes: 9 additions & 4 deletions GoMybatisTempleteDecoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ func (it *TempleteSession) Exec(sqlorArgs string) (*Result, error) {
}
return &result, nil
}
func (it *TempleteSession) QueryPrepare(sqlorArgs string, args ...interface{}) ([]map[string][]byte, error) {
return nil, nil
}
func (it *TempleteSession) ExecPrepare(sqlorArgs string, args ...interface{}) (*Result, error) {
return nil, nil
}

func (it *TempleteSession) Rollback() error {
return nil
}
Expand Down Expand Up @@ -232,12 +239,10 @@ func TestGoMybatisTempleteDecoder_Delete(t *testing.T) {
time.Sleep(time.Second)
}

func TestInit(t *testing.T) {
func TestInit(t *testing.T) {
initMapperTest()
}



func initMapperTest() {
bytes := []byte(`<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
Expand Down Expand Up @@ -290,4 +295,4 @@ func initMapperTest() {
}
engine.SetLogEnable(false)
engine.WriteMapperPtr(&exampleActivityMapper, bytes)
}
}
79 changes: 79 additions & 0 deletions LocalSession.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,85 @@ func (it *LocalSession) Exec(sqlorArgs string) (*Result, error) {
}
}

func (it *LocalSession) QueryPrepare(sqlPrepare string, args ...interface{}) ([]map[string][]byte, error) {
if it.isClosed == true {
return nil, utils.NewError("LocalSession", " can not Query() a Closed Session!")
}
if it.newLocalSession != nil {
return it.newLocalSession.Query(sqlPrepare)
}

var rows *sql.Rows
var err error
var t, _ = it.txStack.Last()
if t != nil {
stmt, err := t.Prepare(sqlPrepare)
err = it.dbErrorPack(err)
if err != nil {
return nil, err
}
rows, err = stmt.Query(args)
err = it.dbErrorPack(err)
} else {
stmt, err := it.db.Prepare(sqlPrepare)
err = it.dbErrorPack(err)
if err != nil {
return nil, err
}
rows, err = stmt.Query(args)
err = it.dbErrorPack(err)
}
if rows != nil {
defer rows.Close()
}
if err != nil {
return nil, err
} else {
return rows2maps(rows)
}
return nil, nil
}

func (it *LocalSession) ExecPrepare(sqlPrepare string, args ...interface{}) (*Result, error) {
if it.isClosed == true {
return nil, utils.NewError("LocalSession", " can not Exec() a Closed Session!")
}
if it.newLocalSession != nil {
return it.newLocalSession.Exec(sqlPrepare)
}

var result sql.Result
var err error
var t, _ = it.txStack.Last()
if t != nil {
stmt, err := t.Prepare(sqlPrepare)
err = it.dbErrorPack(err)
if err != nil {
return nil, err
}
result, err = stmt.Exec(args)
err = it.dbErrorPack(err)
} else {
stmt, err := it.db.Prepare(sqlPrepare)
err = it.dbErrorPack(err)
if err != nil {
return nil, err
}
result, err = stmt.Exec(args)
err = it.dbErrorPack(err)
}
if err != nil {
return nil, err
} else {
var LastInsertId, _ = result.LastInsertId()
var RowsAffected, _ = result.RowsAffected()
return &Result{
LastInsertId: LastInsertId,
RowsAffected: RowsAffected,
}, nil
}
}

func (it *LocalSession) dbErrorPack(e error) error {
if e != nil {
var sqlError = errors.New("[GoMybatis][LocalSession]" + e.Error())
Expand Down
18 changes: 16 additions & 2 deletions SessionFactorySession.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ func (it *SessionFactorySession) Exec(sqlorArgs string) (*Result, error) {
}
return it.Session.Exec(sqlorArgs)
}

func (it *SessionFactorySession) QueryPrepare(sqlorArgs string, args ...interface{}) ([]map[string][]byte, error) {
if it.Session == nil {
return nil, utils.NewError("SessionFactorySession", " can not run Id(),it.Session == nil")
}
return it.Session.QueryPrepare(sqlorArgs, args)
}
func (it *SessionFactorySession) ExecPrepare(sqlorArgs string, args ...interface{}) (*Result, error) {
if it.Session == nil {
return nil, utils.NewError("SessionFactorySession", " can not run Exec(),it.Session == nil")
}
return it.Session.ExecPrepare(sqlorArgs, args)
}

func (it *SessionFactorySession) Rollback() error {
if it.Session == nil {
return utils.NewError("SessionFactorySession", " can not run Rollback(),it.Session == nil")
Expand All @@ -48,7 +62,7 @@ func (it *SessionFactorySession) Begin(p *tx.Propagation) error {
}
func (it *SessionFactorySession) Close() {
var id = it.Id()
var s,_ = it.Factory.SessionMap.Load(id)
var s, _ = it.Factory.SessionMap.Load(id)
if s != nil {
if it.Session != nil {
it.Session.Close()
Expand All @@ -57,6 +71,6 @@ func (it *SessionFactorySession) Close() {
}
}

func (it *SessionFactorySession) LastPROPAGATION () *tx.Propagation{
func (it *SessionFactorySession) LastPROPAGATION() *tx.Propagation {
return it.Session.LastPROPAGATION()
}
6 changes: 5 additions & 1 deletion SqlEngine.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ type Session interface {
Id() string
Query(sqlorArgs string) ([]map[string][]byte, error)
Exec(sqlorArgs string) (*Result, error)
//Prepare sql, example sqlPrepare: select * from table where id = ? , args:'1'
QueryPrepare(sqlPrepare string, args ...interface{}) ([]map[string][]byte, error)
//Prepare sql, example sqlPrepare: select * from table where id = ? , args:'1'
ExecPrepare(sqlPrepare string, args ...interface{}) (*Result, error)
Rollback() error
Commit() error
Begin(p *tx.Propagation) error
Close()
LastPROPAGATION () *tx.Propagation
LastPROPAGATION() *tx.Propagation
}

//产生session的引擎
Expand Down

0 comments on commit f0fe189

Please sign in to comment.