Skip to content
Closed
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
7 changes: 7 additions & 0 deletions sqlhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ func (conn *ExecerContext) Exec(query string, args []driver.Value) (driver.Resul
return nil, errors.New("Exec was called when ExecContext was implemented")
}

func (conn *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
if ciCtx, is := conn.Conn.(driver.ConnBeginTx); is {
return ciCtx.BeginTx(ctx, opts)
}
return nil, errors.New("driver does not implement driver.ConnBeginTx")
}

// QueryerContext implements a database/sql.driver.QueryerContext
type QueryerContext struct {
*Conn
Expand Down
10 changes: 10 additions & 0 deletions sqlhooks_postgres_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sqlhooks

import (
"context"
"database/sql"
"os"
"testing"
Expand Down Expand Up @@ -53,5 +54,14 @@ func TestPostgres(t *testing.T) {
s.db.QueryRow("SELECT COUNT(*) FROM users").Scan(&count),
)
assert.Equal(t, 5, count)

{ // Should execute the query successfully when a transaction with non default isolation level is used.
var count int
tx, err := s.db.BeginTx(context.Background(), &sql.TxOptions{Isolation: sql.LevelSerializable})
if assert.NoError(t, err) {
require.NoError(t, tx.QueryRow("SELECT COUNT(*) FROM users").Scan(&count))
assert.Equal(t, 5, count)
}
}
})
}