Skip to content

Commit

Permalink
add Model.WithTx and Table.WithTx
Browse files Browse the repository at this point in the history
  • Loading branch information
fifsky committed Apr 26, 2019
1 parent 0b42729 commit bcd8def
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
6 changes: 6 additions & 0 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ func (b *Builder) initModel() {
}
}

//WithTx model use tx
func (b *Builder) WithTx(tx *sqlx.Tx) *Builder {
b.wrapper.tx = tx
return b
}

//Hint is set TDDL "/*+TDDL:slave()*/"
func (b *Builder) Hint(hint string) *Builder {
b.hint = hint
Expand Down
6 changes: 6 additions & 0 deletions mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ func (m *Mapper) Count() (num int64, err error) {
return num, err
}

// WithTx Table use tx
func (m *Mapper) WithTx(tx *sqlx.Tx) *Mapper {
m.wrapper.tx = tx
return m
}

// Table select table name
func Table(t string, tx ...*sqlx.Tx) *Mapper {
var txx *sqlx.Tx
Expand Down
25 changes: 16 additions & 9 deletions wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"context"
"database/sql"
"log"
"os"
"reflect"
"strings"
Expand All @@ -29,9 +30,9 @@ var (
type BuilderChainFunc func(b *Builder)

type Wrapper struct {
database string
tx *sqlx.Tx
logging bool
database string
tx *sqlx.Tx
logging bool
RelationMap map[string]BuilderChainFunc
}

Expand Down Expand Up @@ -146,7 +147,7 @@ func (w *Wrapper) Get(dest interface{}, query string, args ...interface{}) (err

if reflect.Indirect(refVal).Kind() == reflect.Struct {
// relation data fill
err = RelationOne(dest , w.RelationMap)
err = RelationOne(dest, w.RelationMap)
}

if err != nil {
Expand Down Expand Up @@ -194,7 +195,7 @@ func (w *Wrapper) Select(dest interface{}, query string, args ...interface{}) (e
if t.Kind() == reflect.Slice {
if indirectType(t.Elem()).Kind() == reflect.Struct {
// relation data fill
err = RelationAll(dest , w.RelationMap)
err = RelationAll(dest, w.RelationMap)
}
}

Expand All @@ -215,7 +216,10 @@ func (w *Wrapper) Txx(ctx context.Context, fn func(ctx context.Context, tx *sqlx
}
defer func() {
if err != nil {
tx.Rollback()
err := tx.Rollback()
if err != nil {
log.Printf("gosql rollback error:%s", err)
}
}
}()

Expand All @@ -236,7 +240,10 @@ func (w *Wrapper) Tx(fn func(tx *sqlx.Tx) error) (err error) {
}
defer func() {
if err != nil {
tx.Rollback()
err := tx.Rollback()
if err != nil {
log.Printf("gosql rollback error:%s", err)
}
}
}()

Expand Down Expand Up @@ -296,7 +303,7 @@ func (w *Wrapper) Import(f string) ([]sql.Result, error) {
}

// Relation association table builder handle
func (w *Wrapper) Relation (name string , fn BuilderChainFunc) *Wrapper {
func (w *Wrapper) Relation(name string, fn BuilderChainFunc) *Wrapper {
if w.RelationMap == nil {
w.RelationMap = make(map[string]BuilderChainFunc)
}
Expand Down Expand Up @@ -360,4 +367,4 @@ func Relation(name string, fn BuilderChainFunc) *Wrapper {
w.RelationMap = make(map[string]BuilderChainFunc)
w.RelationMap[name] = fn
return w
}
}

0 comments on commit bcd8def

Please sign in to comment.