Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions dbmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,12 @@ func (m *DbMap) Begin() (*Transaction, error) {
return &Transaction{m, tx}, nil
}

// Wraps an already existing sqlx Transaction granting the ability to use
// modl query methods within the same transaction
func (m *DbMap) WrapTx(tx *sqlx.Tx) *Transaction {
return &Transaction{m, tx}
}

// FIXME: This is a poor interface. Checking for nils is un-go-like, and this
// function should be TableFor(i interface{}) (*TableMap, error)
// FIXME: rewrite this in terms of sqlx's reflect helpers
Expand Down
39 changes: 39 additions & 0 deletions modl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,45 @@ func TestTransaction(t *testing.T) {
}
}

// Ensures that calling transactin methods with an outside
// *sqlx.Tx works as expected
func TestWrapTransaction(t *testing.T) {
dbmap := initDbMap()
defer dbmap.Cleanup()

inv1 := &Invoice{0, 100, 200, "t1", 0, true}
inv2 := &Invoice{0, 100, 200, "t2", 0, false}

tx, err := dbmap.Dbx.Beginx()
if err != nil {
panic(err)
}

trans := dbmap.WrapTx(tx)
trans.Insert(inv1, inv2)

// Commit on our standard *sqlx.Tx struct
if err = tx.Commit(); err != nil {
panic(err)
}

obj := &Invoice{}
err = dbmap.Get(obj, inv1.ID)
if err != nil {
panic(err)
}
if !reflect.DeepEqual(inv1, obj) {
t.Errorf("%v != %v", inv1, obj)
}
err = dbmap.Get(obj, inv2.ID)
if err != nil {
panic(err)
}
if !reflect.DeepEqual(inv2, obj) {
t.Errorf("%v != %v", inv2, obj)
}
}

func TestMultiple(t *testing.T) {
dbmap := initDbMap()
defer dbmap.Cleanup()
Expand Down