Skip to content

Commit abb7283

Browse files
authored
Add Tx.Tx accessor for the embedded *sql.Tx (#93)
1 parent 53ced01 commit abb7283

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

‎sequel.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,11 @@ func (t *Tx) rebindModel(query string) string {
511511
return query
512512
}
513513

514+
// Tx returns the embedded *sql.Tx.
515+
func (t *Tx) Tx() *sql.Tx {
516+
return t.tx.Tx
517+
}
518+
514519
// Commit commits the transaction.
515520
func (t *Tx) Commit() error {
516521
err := t.tx.Commit()

‎sequel_test.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,22 @@ func TestTxQueries(t *testing.T) {
579579
assert.NoError(t, tx.Commit())
580580
})
581581

582+
t.Run("sql tx", func(t *testing.T) {
583+
tx, err := db.Begin(ctx)
584+
require.NoError(t, err)
585+
defer func() {
586+
assert.NoError(t, tx.Rollback())
587+
}()
588+
589+
// A write through the *sql.Tx is visible inside the same transaction.
590+
_, err = tx.Tx().ExecContext(ctx, "UPDATE person_test SET name = $1 WHERE id = $2", "Foo Bar", p1.GetID())
591+
require.NoError(t, err)
592+
593+
var name string
594+
require.NoError(t, tx.QueryRow("SELECT name FROM person_test WHERE id = $1", p1.GetID()).Scan(&name))
595+
assert.Equal(t, "Foo Bar", name)
596+
})
597+
582598
t.Run("insert error", func(t *testing.T) {
583599
tx, err := db.Begin(ctx)
584600
require.NoError(t, err)

0 commit comments

Comments
 (0)