diff --git a/sqlx_context.go b/sqlx_context.go index 32621d56d..f2e4f8f03 100644 --- a/sqlx_context.go +++ b/sqlx_context.go @@ -359,6 +359,12 @@ func (tx *Tx) NamedExecContext(ctx context.Context, query string, arg interface{ return NamedExecContext(ctx, tx, query, arg) } +// NamedQueryContext using this Tx. +// Any named placeholder parameters are replaced with fields from arg. +func (tx *Tx) NamedQueryContext(ctx context.Context, query string, arg interface{}) (*Rows, error) { + return NamedQueryContext(ctx, tx, query, arg) +} + // SelectContext using the prepared statement. // Any placeholder parameters are replaced with supplied args. func (s *Stmt) SelectContext(ctx context.Context, dest interface{}, args ...interface{}) error { diff --git a/sqlx_context_test.go b/sqlx_context_test.go index 91c5cba1d..fedc664b0 100644 --- a/sqlx_context_test.go +++ b/sqlx_context_test.go @@ -1425,3 +1425,32 @@ func TestConn(t *testing.T) { } }) } + +func TestTxNamedQueryContextMethod(t *testing.T) { + // Compile-time check that *Tx exposes NamedQueryContext (issue #447). + var _ interface { + NamedQueryContext(ctx context.Context, query string, arg interface{}) (*Rows, error) + } = (*Tx)(nil) + + // Runtime path with in-memory schema when a driver is available. + RunWithSchemaContext(context.Background(), defaultSchema, t, func(ctx context.Context, db *DB, t *testing.T) { + loadDefaultFixture(db, t) + tx, err := db.BeginTxx(ctx, nil) + if err != nil { + t.Fatal(err) + } + defer tx.Rollback() + + type p struct { + FirstName string `db:"first_name"` + } + rows, err := tx.NamedQueryContext(ctx, "SELECT first_name FROM person WHERE first_name=:first_name", p{FirstName: "Jason"}) + if err != nil { + t.Fatal(err) + } + defer rows.Close() + if !rows.Next() { + t.Fatal("expected a row") + } + }) +}