diff --git a/batch_test.go b/batch_test.go index 186dcfd4..9fcbdf0d 100644 --- a/batch_test.go +++ b/batch_test.go @@ -538,7 +538,7 @@ func TestTxBeginBatch(t *testing.T) { tx.Commit() var count int - conn.QueryRow("select count(1) from ledger1 where id = $1", id).Scan(&count) + conn.QueryRow(context.Background(), "select count(1) from ledger1 where id = $1", id).Scan(&count) if count != 1 { t.Errorf("count => %v, want %v", count, 1) } @@ -583,7 +583,7 @@ func TestTxBeginBatchRollback(t *testing.T) { batch.Close() tx.Rollback() - row := conn.QueryRow("select count(1) from ledger1 where id = $1", id) + row := conn.QueryRow(context.Background(), "select count(1) from ledger1 where id = $1", id) var count int row.Scan(&count) if count != 0 { diff --git a/bench-tmp_test.go b/bench-tmp_test.go index 9e36bedc..787fd162 100644 --- a/bench-tmp_test.go +++ b/bench-tmp_test.go @@ -1,6 +1,7 @@ package pgx_test import ( + "context" "os" "testing" ) @@ -18,7 +19,7 @@ func BenchmarkPgtypeInt4ParseBinary(b *testing.B) { for i := 0; i < b.N; i++ { var n int32 - rows, err := conn.Query("selectBinary") + rows, err := conn.Query(context.Background(), "selectBinary") if err != nil { b.Fatal(err) } @@ -47,7 +48,7 @@ func BenchmarkPgtypeInt4EncodeBinary(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - rows, err := conn.Query("encodeBinary", int32(i), int32(i), int32(i), int32(i), int32(i), int32(i), int32(i)) + rows, err := conn.Query(context.Background(), "encodeBinary", int32(i), int32(i), int32(i), int32(i), int32(i), int32(i), int32(i)) if err != nil { b.Fatal(err) } diff --git a/bench_test.go b/bench_test.go index 2b39cf07..5f64ba7b 100644 --- a/bench_test.go +++ b/bench_test.go @@ -34,7 +34,7 @@ func BenchmarkPointerPointerWithNullValues(b *testing.B) { lastLoginTime *time.Time } - err = conn.QueryRow("selectNulls").Scan( + err = conn.QueryRow(context.Background(), "selectNulls").Scan( &record.id, &record.userName, &record.email, @@ -94,7 +94,7 @@ func BenchmarkPointerPointerWithPresentValues(b *testing.B) { lastLoginTime *time.Time } - err = conn.QueryRow("selectNulls").Scan( + err = conn.QueryRow(context.Background(), "selectNulls").Scan( &record.id, &record.userName, &record.email, @@ -206,7 +206,7 @@ func benchmarkSelectWithLog(b *testing.B, conn *pgx.Conn) { lastLoginTime time.Time } - err = conn.QueryRow("test").Scan( + err = conn.QueryRow(context.Background(), "test").Scan( &record.id, &record.userName, &record.email, @@ -582,7 +582,7 @@ func BenchmarkMultipleQueriesNonBatch(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { for j := 0; j < queryCount; j++ { - rows, err := conn.Query("select n from generate_series(0, 5) n") + rows, err := conn.Query(context.Background(), "select n from generate_series(0, 5) n") if err != nil { b.Fatal(err) } diff --git a/conn.go b/conn.go index 0fd42a8d..808dc99b 100644 --- a/conn.go +++ b/conn.go @@ -229,7 +229,7 @@ where ( )` ) - nameOIDs, err := connInfoFromRows(c.Query(namedOIDQuery)) + nameOIDs, err := connInfoFromRows(c.Query(context.TODO(), namedOIDQuery)) if err != nil { return nil, err } @@ -277,7 +277,7 @@ func (c *Conn) initConnInfo() (err error) { // initConnInfoEnumArray introspects for arrays of enums and registers a data type for them. func (c *Conn) initConnInfoEnumArray(cinfo *pgtype.ConnInfo) error { nameOIDs := make(map[string]pgtype.OID, 16) - rows, err := c.Query(`select t.oid, t.typname + rows, err := c.Query(context.TODO(), `select t.oid, t.typname from pg_type t join pg_type base_type on t.typelem=base_type.oid where t.typtype = 'b' @@ -321,7 +321,7 @@ func (c *Conn) initConnInfoDomains(cinfo *pgtype.ConnInfo) error { domains := make([]*domain, 0, 16) - rows, err := c.Query(`select t.oid, t.typname, t.typbasetype + rows, err := c.Query(context.TODO(), `select t.oid, t.typname, t.typbasetype from pg_type t join pg_type base_type on t.typbasetype=base_type.oid where t.typtype = 'd' @@ -414,7 +414,7 @@ func (c *Conn) crateDBTypesQuery(err error) (*pgtype.ConnInfo, error) { nameOIDs map[string]pgtype.OID ) - if nameOIDs, err = connInfoFromRows(c.Query(`select oid, typname from pg_catalog.pg_type`)); err != nil { + if nameOIDs, err = connInfoFromRows(c.Query(context.TODO(), `select oid, typname from pg_catalog.pg_type`)); err != nil { return nil, err } diff --git a/conn_test.go b/conn_test.go index 00698245..95fe6357 100644 --- a/conn_test.go +++ b/conn_test.go @@ -27,7 +27,7 @@ func TestCrateDBConnect(t *testing.T) { defer closeConn(t, conn) var result int - err = conn.QueryRow("select 1 +1").Scan(&result) + err = conn.QueryRow(context.Background(), "select 1 +1").Scan(&result) if err != nil { t.Fatalf("QueryRow Scan unexpectedly failed: %v", err) } @@ -55,7 +55,7 @@ func TestConnect(t *testing.T) { } var currentDB string - err = conn.QueryRow("select current_database()").Scan(¤tDB) + err = conn.QueryRow(context.Background(), "select current_database()").Scan(¤tDB) if err != nil { t.Fatalf("QueryRow Scan unexpectedly failed: %v", err) } @@ -64,7 +64,7 @@ func TestConnect(t *testing.T) { } var user string - err = conn.QueryRow("select current_user").Scan(&user) + err = conn.QueryRow(context.Background(), "select current_user").Scan(&user) if err != nil { t.Fatalf("QueryRow Scan unexpectedly failed: %v", err) } @@ -91,7 +91,7 @@ func TestConnectWithPreferSimpleProtocol(t *testing.T) { // into a pgtype.Text as the integer will have been encoded in text. var s pgtype.Text - err := conn.QueryRow("select $1::int4", 42).Scan(&s) + err := conn.QueryRow(context.Background(), "select $1::int4", 42).Scan(&s) if err != nil { t.Fatal(err) } @@ -151,7 +151,7 @@ func TestExecFailure(t *testing.T) { t.Error("Expected LastStmtSent to return true") } - rows, _ := conn.Query("select 1") + rows, _ := conn.Query(context.Background(), "select 1") rows.Close() if rows.Err() != nil { t.Fatalf("Exec failure appears to have broken connection: %v", rows.Err()) @@ -212,7 +212,7 @@ func TestExecContextFailureWithoutCancelation(t *testing.T) { t.Error("Expected LastStmtSent to return true") } - rows, _ := conn.Query("select 1") + rows, _ := conn.Query(context.Background(), "select 1") rows.Close() if rows.Err() != nil { t.Fatalf("ExecEx failure appears to have broken connection: %v", rows.Err()) @@ -350,7 +350,7 @@ func TestPrepare(t *testing.T) { } var s string - err = conn.QueryRow("test", "hello").Scan(&s) + err = conn.QueryRow(context.Background(), "test", "hello").Scan(&s) if err != nil { t.Errorf("Executing prepared statement failed: %v", err) } @@ -374,7 +374,7 @@ func TestPrepare(t *testing.T) { } var n int32 - err = conn.QueryRow("test", int32(1)).Scan(&n) + err = conn.QueryRow(context.Background(), "test", int32(1)).Scan(&n) if err != nil { t.Errorf("Executing prepared statement failed: %v", err) } @@ -415,7 +415,7 @@ func TestPrepareIdempotency(t *testing.T) { } var n int32 - err = conn.QueryRow("test").Scan(&n) + err = conn.QueryRow(context.Background(), "test").Scan(&n) if err != nil { t.Errorf("%d. Executing prepared statement failed: %v", i, err) } @@ -445,7 +445,7 @@ func TestPrepareEx(t *testing.T) { } var s string - err = conn.QueryRow("test", "hello").Scan(&s) + err = conn.QueryRow(context.Background(), "test", "hello").Scan(&s) if err != nil { t.Errorf("Executing prepared statement failed: %v", err) } @@ -472,7 +472,7 @@ func TestFatalRxError(t *testing.T) { defer wg.Done() var n int32 var s string - err := conn.QueryRow("select 1::int4, pg_sleep(10)::varchar").Scan(&n, &s) + err := conn.QueryRow(context.Background(), "select 1::int4, pg_sleep(10)::varchar").Scan(&n, &s) if err == pgx.ErrDeadConn { } else if pgErr, ok := err.(*pgconn.PgError); ok && pgErr.Severity == "FATAL" { } else { @@ -511,7 +511,7 @@ func TestFatalTxError(t *testing.T) { t.Fatalf("Unable to kill backend PostgreSQL process: %v", err) } - err = conn.QueryRow("select 1").Scan(nil) + err = conn.QueryRow(context.Background(), "select 1").Scan(nil) if err == nil { t.Fatal("Expected error but none occurred") } @@ -561,13 +561,13 @@ func TestCatchSimultaneousConnectionQueries(t *testing.T) { conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) - rows1, err := conn.Query("select generate_series(1,$1)", 10) + rows1, err := conn.Query(context.Background(), "select generate_series(1,$1)", 10) if err != nil { t.Fatalf("conn.Query failed: %v", err) } defer rows1.Close() - _, err = conn.Query("select generate_series(1,$1)", 10) + _, err = conn.Query(context.Background(), "select generate_series(1,$1)", 10) if err != pgx.ErrConnBusy { t.Fatalf("conn.Query should have failed with pgx.ErrConnBusy, but it was %v", err) } @@ -579,7 +579,7 @@ func TestCatchSimultaneousConnectionQueryAndExec(t *testing.T) { conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) - rows, err := conn.Query("select generate_series(1,$1)", 10) + rows, err := conn.Query(context.Background(), "select generate_series(1,$1)", 10) if err != nil { t.Fatalf("conn.Query failed: %v", err) } @@ -756,7 +756,7 @@ func TestDomainType(t *testing.T) { } var n uint64 - err := conn.QueryRow("select $1::uint64", uint64(42)).Scan(&n) + err := conn.QueryRow(context.Background(), "select $1::uint64", uint64(42)).Scan(&n) if err != nil { t.Fatal(err) } diff --git a/copy_from_test.go b/copy_from_test.go index 8809501f..dc9dc072 100644 --- a/copy_from_test.go +++ b/copy_from_test.go @@ -1,6 +1,7 @@ package pgx_test import ( + "context" "os" "reflect" "testing" @@ -44,7 +45,7 @@ func TestConnCopyFromSmall(t *testing.T) { t.Errorf("Expected CopyFrom to return %d copied rows, but got %d", len(inputRows), copyCount) } - rows, err := conn.Query("select * from foo") + rows, err := conn.Query(context.Background(), "select * from foo") if err != nil { t.Errorf("Unexpected error for Query: %v", err) } @@ -139,7 +140,7 @@ func TestConnCopyFromLarge(t *testing.T) { t.Errorf("Expected CopyFrom to return %d copied rows, but got %d", len(inputRows), copyCount) } - rows, err := conn.Query("select * from foo") + rows, err := conn.Query(context.Background(), "select * from foo") if err != nil { t.Errorf("Unexpected error for Query: %v", err) } @@ -230,7 +231,7 @@ func TestConnCopyFromJSON(t *testing.T) { t.Errorf("Expected CopyFrom to return %d copied rows, but got %d", len(inputRows), copyCount) } - rows, err := conn.Query("select * from foo") + rows, err := conn.Query(context.Background(), "select * from foo") if err != nil { t.Errorf("Unexpected error for Query: %v", err) } @@ -341,7 +342,7 @@ func TestConnCopyFromFailServerSideMidway(t *testing.T) { t.Errorf("Expected CopyFrom to return 0 copied rows, but got %d", copyCount) } - rows, err := conn.Query("select * from foo") + rows, err := conn.Query(context.Background(), "select * from foo") if err != nil { t.Errorf("Unexpected error for Query: %v", err) } @@ -454,7 +455,7 @@ func TestConnCopyFromFailServerSideMidwayAbortsWithoutWaiting(t *testing.T) { t.Errorf("Failing CopyFrom shouldn't have taken so long: %v", copyTime) } - rows, err := conn.Query("select * from foo") + rows, err := conn.Query(context.Background(), "select * from foo") if err != nil { t.Errorf("Unexpected error for Query: %v", err) } @@ -497,7 +498,7 @@ func TestConnCopyFromCopyFromSourceErrorMidway(t *testing.T) { t.Errorf("Expected CopyFrom to return 0 copied rows, but got %d", copyCount) } - rows, err := conn.Query("select * from foo") + rows, err := conn.Query(context.Background(), "select * from foo") if err != nil { t.Errorf("Unexpected error for Query: %v", err) } @@ -557,7 +558,7 @@ func TestConnCopyFromCopyFromSourceErrorEnd(t *testing.T) { t.Errorf("Expected CopyFrom to return 0 copied rows, but got %d", copyCount) } - rows, err := conn.Query("select * from foo") + rows, err := conn.Query(context.Background(), "select * from foo") if err != nil { t.Errorf("Unexpected error for Query: %v", err) } diff --git a/example_custom_type_test.go b/example_custom_type_test.go index 2eb36e6e..594fc9e4 100644 --- a/example_custom_type_test.go +++ b/example_custom_type_test.go @@ -88,14 +88,14 @@ func Example_CustomType() { }) p := &Point{} - err = conn.QueryRow("select null::point").Scan(p) + err = conn.QueryRow(context.Background(), "select null::point").Scan(p) if err != nil { fmt.Println(err) return } fmt.Println(p) - err = conn.QueryRow("select point(1.5,2.5)").Scan(p) + err = conn.QueryRow(context.Background(), "select point(1.5,2.5)").Scan(p) if err != nil { fmt.Println(err) return diff --git a/example_json_test.go b/example_json_test.go index 5b0623ef..4863cb5a 100644 --- a/example_json_test.go +++ b/example_json_test.go @@ -27,7 +27,7 @@ func Example_JSON() { var output person - err = conn.QueryRow("select $1::json", input).Scan(&output) + err = conn.QueryRow(context.Background(), "select $1::json", input).Scan(&output) if err != nil { fmt.Println(err) return diff --git a/go.mod b/go.mod index a6ffb3f8..7dd92c2e 100644 --- a/go.mod +++ b/go.mod @@ -4,12 +4,22 @@ go 1.12 require ( github.com/cockroachdb/apd v1.1.0 + github.com/go-stack/stack v1.8.0 // indirect github.com/jackc/pgconn v0.0.0-20190405170659-7ad3625edd3b github.com/jackc/pgio v1.0.0 github.com/jackc/pgproto3 v1.0.0 github.com/jackc/puddle v0.0.0-20190409004018-0d93e0ec116a + github.com/lib/pq v1.0.0 + github.com/mattn/go-colorable v0.1.1 // indirect + github.com/mattn/go-isatty v0.0.7 // indirect github.com/pkg/errors v0.8.1 + github.com/rs/zerolog v1.13.0 github.com/satori/go.uuid v1.2.0 github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24 + github.com/sirupsen/logrus v1.4.1 github.com/stretchr/testify v1.3.0 + go.uber.org/atomic v1.3.2 // indirect + go.uber.org/multierr v1.1.0 // indirect + go.uber.org/zap v1.9.1 + gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec ) diff --git a/go.sum b/go.sum index 59ad6856..3dda696b 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,10 @@ github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0= github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo= github.com/jackc/pgconn v0.0.0-20190330221323-ed7d91dc9873 h1:M68R77AKFS7dub7R7WgJ9D6yiNuExOYhBuGtazlbr10= @@ -16,14 +20,40 @@ github.com/jackc/pgproto3 v1.0.0 h1:25tUmlES7eyD96oYaUHc1dLOFbgcJtFzCdnOOoqmA1I= github.com/jackc/pgproto3 v1.0.0/go.mod h1:eR5FA3leWg7p9aeAqi37XOTgTIbkABlvcPB3E5rlc78= github.com/jackc/puddle v0.0.0-20190409004018-0d93e0ec116a h1:zx0j45Wa4oRefVk0D3muLxUujnMWN7ZRraF+78DXEwE= github.com/jackc/puddle v0.0.0-20190409004018-0d93e0ec116a/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A= +github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/mattn/go-colorable v0.1.1 h1:G1f5SKeVxmagw/IyvzvtZE4Gybcc4Tr1tf7I8z0XgOg= +github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= +github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc= +github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rs/zerolog v1.13.0 h1:hSNcYHyxDWycfePW7pUI8swuFkcSMPKh3E63Pokg1Hk= +github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU= github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24 h1:pntxY8Ary0t43dCZ5dqY4YTJCObLY1kIXl0uzMv+7DE= github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4= +github.com/sirupsen/logrus v1.4.1 h1:GL2rEmy6nsikmW0r8opw9JIRScdMF5hA8cOYLH7In1k= +github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +go.uber.org/atomic v1.3.2 h1:2Oa65PReHzfn29GpvgsYwloV9AVFHPDk8tYxt2c2tr4= +go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= +go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI= +go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= +go.uber.org/zap v1.9.1 h1:XCJQEf3W6eZaVwhRBof6ImoYGJSITeKWsyeh3HFu/5o= +go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 h1:I6FyU15t786LL7oL/hn43zqTuEGr4PN7F4XJ1p4E3Y8= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec h1:RlWgLqCMMIYYEVcAR5MDsuHlVkaIPDAF+5Dehzg8L5A= +gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s= diff --git a/helper_test.go b/helper_test.go index 1cbfd9f6..fa139cf6 100644 --- a/helper_test.go +++ b/helper_test.go @@ -65,7 +65,7 @@ func mustExec(t testing.TB, conn *pgx.Conn, sql string, arguments ...interface{} func ensureConnValid(t *testing.T, conn *pgx.Conn) { var sum, rowCount int32 - rows, err := conn.Query("select generate_series(1,$1)", 10) + rows, err := conn.Query(context.Background(), "select generate_series(1,$1)", 10) if err != nil { t.Fatalf("conn.Query failed: %v", err) } diff --git a/large_objects.go b/large_objects.go index e109bce2..1083f397 100644 --- a/large_objects.go +++ b/large_objects.go @@ -1,6 +1,7 @@ package pgx import ( + "context" "io" "github.com/jackc/pgx/pgtype" @@ -38,7 +39,7 @@ func (tx *Tx) LargeObjects() (*LargeObjects, error) { tx.conn.fp = newFastpath(tx.conn) } if _, exists := tx.conn.fp.fns["lo_open"]; !exists { - res, err := tx.Query(largeObjectFns) + res, err := tx.Query(context.TODO(), largeObjectFns) if err != nil { return nil, err } diff --git a/large_objects_test.go b/large_objects_test.go index a1842918..2c8651bb 100644 --- a/large_objects_test.go +++ b/large_objects_test.go @@ -167,7 +167,7 @@ func TestLargeObjectsMultipleTransactions(t *testing.T) { // IMPORTANT: Use the same connection for another query query := `select n from generate_series(1,10) n` - rows, err := conn.Query(query) + rows, err := conn.Query(context.Background(), query) if err != nil { t.Fatal(err) } diff --git a/pool/common_test.go b/pool/common_test.go index a8abf71f..1ba9d0ed 100644 --- a/pool/common_test.go +++ b/pool/common_test.go @@ -6,7 +6,6 @@ import ( "time" "github.com/jackc/pgconn" - "github.com/jackc/pgx" "github.com/jackc/pgx/pool" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -38,35 +37,13 @@ func testExec(t *testing.T, db execer) { } type queryer interface { - Query(sql string, args ...interface{}) (*pool.Rows, error) + Query(ctx context.Context, sql string, optionsAndArgs ...interface{}) (*pool.Rows, error) } func testQuery(t *testing.T, db queryer) { var sum, rowCount int32 - rows, err := db.Query("select generate_series(1,$1)", 10) - require.NoError(t, err) - - for rows.Next() { - var n int32 - rows.Scan(&n) - sum += n - rowCount++ - } - - assert.NoError(t, rows.Err()) - assert.Equal(t, int32(10), rowCount) - assert.Equal(t, int32(55), sum) -} - -type queryExer interface { - QueryEx(ctx context.Context, sql string, options *pgx.QueryExOptions, args ...interface{}) (*pool.Rows, error) -} - -func testQueryEx(t *testing.T, db queryExer) { - var sum, rowCount int32 - - rows, err := db.QueryEx(context.Background(), "select generate_series(1,$1)", nil, 10) + rows, err := db.Query(context.Background(), "select generate_series(1,$1)", 10) require.NoError(t, err) for rows.Next() { @@ -82,24 +59,12 @@ func testQueryEx(t *testing.T, db queryExer) { } type queryRower interface { - QueryRow(sql string, args ...interface{}) *pool.Row + QueryRow(ctx context.Context, sql string, optionsAndArgs ...interface{}) *pool.Row } func testQueryRow(t *testing.T, db queryRower) { var what, who string - err := db.QueryRow("select 'hello', $1", "world").Scan(&what, &who) - assert.NoError(t, err) - assert.Equal(t, "hello", what) - assert.Equal(t, "world", who) -} - -type queryRowExer interface { - QueryRowEx(ctx context.Context, sql string, options *pgx.QueryExOptions, args ...interface{}) *pool.Row -} - -func testQueryRowEx(t *testing.T, db queryRowExer) { - var what, who string - err := db.QueryRowEx(context.Background(), "select 'hello', $1", nil, "world").Scan(&what, &who) + err := db.QueryRow(context.Background(), "select 'hello', $1", "world").Scan(&what, &who) assert.NoError(t, err) assert.Equal(t, "hello", what) assert.Equal(t, "world", who) diff --git a/pool/conn.go b/pool/conn.go index 54db253e..81f5a625 100644 --- a/pool/conn.go +++ b/pool/conn.go @@ -54,25 +54,14 @@ func (c *Conn) Exec(ctx context.Context, sql string, arguments ...interface{}) ( return conn.Exec(ctx, sql, arguments...) } -func (c *Conn) Query(sql string, args ...interface{}) (*Rows, error) { - r, err := c.res.Value().(*pgx.Conn).Query(sql, args...) +func (c *Conn) Query(ctx context.Context, sql string, optionsAndArgs ...interface{}) (*Rows, error) { + r, err := c.res.Value().(*pgx.Conn).Query(ctx, sql, optionsAndArgs...) rows := &Rows{r: r, err: err} return rows, err } -func (c *Conn) QueryEx(ctx context.Context, sql string, options *pgx.QueryExOptions, args ...interface{}) (*Rows, error) { - r, err := c.res.Value().(*pgx.Conn).QueryEx(ctx, sql, options, args...) - rows := &Rows{r: r, err: err} - return rows, err -} - -func (c *Conn) QueryRow(sql string, args ...interface{}) *Row { - r := c.res.Value().(*pgx.Conn).QueryRow(sql, args...) - return &Row{r: r} -} - -func (c *Conn) QueryRowEx(ctx context.Context, sql string, options *pgx.QueryExOptions, args ...interface{}) *Row { - r := c.res.Value().(*pgx.Conn).QueryRowEx(ctx, sql, options, args...) +func (c *Conn) QueryRow(ctx context.Context, sql string, optionsAndArgs ...interface{}) *Row { + r := c.res.Value().(*pgx.Conn).QueryRow(ctx, sql, optionsAndArgs...) return &Row{r: r} } diff --git a/pool/conn_test.go b/pool/conn_test.go index e7f39050..b4d6adda 100644 --- a/pool/conn_test.go +++ b/pool/conn_test.go @@ -33,18 +33,6 @@ func TestConnQuery(t *testing.T) { testQuery(t, c) } -func TestConnQueryEx(t *testing.T) { - pool, err := pool.Connect(context.Background(), os.Getenv("PGX_TEST_DATABASE")) - require.NoError(t, err) - defer pool.Close() - - c, err := pool.Acquire(context.Background()) - require.NoError(t, err) - defer c.Release() - - testQueryEx(t, c) -} - func TestConnQueryRow(t *testing.T) { pool, err := pool.Connect(context.Background(), os.Getenv("PGX_TEST_DATABASE")) require.NoError(t, err) @@ -56,15 +44,3 @@ func TestConnQueryRow(t *testing.T) { testQueryRow(t, c) } - -func TestConnlQueryRowEx(t *testing.T) { - pool, err := pool.Connect(context.Background(), os.Getenv("PGX_TEST_DATABASE")) - require.NoError(t, err) - defer pool.Close() - - c, err := pool.Acquire(context.Background()) - require.NoError(t, err) - defer c.Release() - - testQueryRowEx(t, c) -} diff --git a/pool/pool.go b/pool/pool.go index 2d2d9be1..ad6dc458 100644 --- a/pool/pool.go +++ b/pool/pool.go @@ -63,13 +63,13 @@ func (p *Pool) Exec(ctx context.Context, sql string, arguments ...interface{}) ( return c.Exec(ctx, sql, arguments...) } -func (p *Pool) Query(sql string, args ...interface{}) (*Rows, error) { - c, err := p.Acquire(context.Background()) +func (p *Pool) Query(ctx context.Context, sql string, optionsAndArgs ...interface{}) (*Rows, error) { + c, err := p.Acquire(ctx) if err != nil { return &Rows{err: err}, err } - rows, err := c.Query(sql, args...) + rows, err := c.Query(ctx, sql, optionsAndArgs...) if err == nil { rows.c = c } else { @@ -79,40 +79,13 @@ func (p *Pool) Query(sql string, args ...interface{}) (*Rows, error) { return rows, err } -func (p *Pool) QueryEx(ctx context.Context, sql string, options *pgx.QueryExOptions, args ...interface{}) (*Rows, error) { - c, err := p.Acquire(context.Background()) - if err != nil { - return &Rows{err: err}, err - } - - rows, err := c.QueryEx(ctx, sql, options, args...) - if err == nil { - rows.c = c - } else { - c.Release() - } - - return rows, err -} - -func (p *Pool) QueryRow(sql string, args ...interface{}) *Row { - c, err := p.Acquire(context.Background()) +func (p *Pool) QueryRow(ctx context.Context, sql string, optionsAndArgs ...interface{}) *Row { + c, err := p.Acquire(ctx) if err != nil { return &Row{err: err} } - row := c.QueryRow(sql, args...) - row.c = c - return row -} - -func (p *Pool) QueryRowEx(ctx context.Context, sql string, options *pgx.QueryExOptions, args ...interface{}) *Row { - c, err := p.Acquire(context.Background()) - if err != nil { - return &Row{err: err} - } - - row := c.QueryRowEx(ctx, sql, options, args...) + row := c.QueryRow(ctx, sql, optionsAndArgs...) row.c = c return row } diff --git a/pool/pool_test.go b/pool/pool_test.go index a9c36d68..f67795e2 100644 --- a/pool/pool_test.go +++ b/pool/pool_test.go @@ -53,7 +53,7 @@ func TestPoolQuery(t *testing.T) { waitForReleaseToComplete() // Test expected pool behavior - rows, err := pool.Query("select generate_series(1,$1)", 10) + rows, err := pool.Query(context.Background(), "select generate_series(1,$1)", 10) require.NoError(t, err) stats := pool.Stat() @@ -70,33 +70,6 @@ func TestPoolQuery(t *testing.T) { } -func TestPoolQueryEx(t *testing.T) { - pool, err := pool.Connect(context.Background(), os.Getenv("PGX_TEST_DATABASE")) - require.NoError(t, err) - defer pool.Close() - - // Test common usage - testQueryEx(t, pool) - waitForReleaseToComplete() - - // Test expected pool behavior - - rows, err := pool.QueryEx(context.Background(), "select generate_series(1,$1)", nil, 10) - require.NoError(t, err) - - stats := pool.Stat() - assert.Equal(t, 1, stats.AcquiredConns()) - assert.Equal(t, 1, stats.TotalConns()) - - rows.Close() - assert.NoError(t, rows.Err()) - waitForReleaseToComplete() - - stats = pool.Stat() - assert.Equal(t, 0, stats.AcquiredConns()) - assert.Equal(t, 1, stats.TotalConns()) -} - func TestPoolQueryRow(t *testing.T) { pool, err := pool.Connect(context.Background(), os.Getenv("PGX_TEST_DATABASE")) require.NoError(t, err) @@ -110,19 +83,6 @@ func TestPoolQueryRow(t *testing.T) { assert.Equal(t, 1, stats.TotalConns()) } -func TestPoolQueryRowEx(t *testing.T) { - pool, err := pool.Connect(context.Background(), os.Getenv("PGX_TEST_DATABASE")) - require.NoError(t, err) - defer pool.Close() - - testQueryRowEx(t, pool) - waitForReleaseToComplete() - - stats := pool.Stat() - assert.Equal(t, 0, stats.AcquiredConns()) - assert.Equal(t, 1, stats.TotalConns()) -} - func TestConnReleaseRollsBackFailedTransaction(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() @@ -222,9 +182,7 @@ func TestConnPoolQueryConcurrentLoad(t *testing.T) { go func() { defer func() { done <- true }() testQuery(t, pool) - testQueryEx(t, pool) testQueryRow(t, pool) - testQueryRowEx(t, pool) }() } diff --git a/pool/tx.go b/pool/tx.go index cef15ea8..59527080 100644 --- a/pool/tx.go +++ b/pool/tx.go @@ -38,18 +38,10 @@ func (tx *Tx) Exec(ctx context.Context, sql string, arguments ...interface{}) (p return tx.c.Exec(ctx, sql, arguments...) } -func (tx *Tx) Query(sql string, args ...interface{}) (*Rows, error) { - return tx.c.Query(sql, args...) +func (tx *Tx) Query(ctx context.Context, sql string, optionsAndArgs ...interface{}) (*Rows, error) { + return tx.c.Query(ctx, sql, optionsAndArgs...) } -func (tx *Tx) QueryEx(ctx context.Context, sql string, options *pgx.QueryExOptions, args ...interface{}) (*Rows, error) { - return tx.c.QueryEx(ctx, sql, options, args...) -} - -func (tx *Tx) QueryRow(sql string, args ...interface{}) *Row { - return tx.c.QueryRow(sql, args...) -} - -func (tx *Tx) QueryRowEx(ctx context.Context, sql string, options *pgx.QueryExOptions, args ...interface{}) *Row { - return tx.c.QueryRowEx(ctx, sql, options, args...) +func (tx *Tx) QueryRow(ctx context.Context, sql string, optionsAndArgs ...interface{}) *Row { + return tx.c.QueryRow(ctx, sql, optionsAndArgs...) } diff --git a/pool/tx_test.go b/pool/tx_test.go index 518ba196..20a7ec55 100644 --- a/pool/tx_test.go +++ b/pool/tx_test.go @@ -33,18 +33,6 @@ func TestTxQuery(t *testing.T) { testQuery(t, tx) } -func TestTxQueryEx(t *testing.T) { - pool, err := pool.Connect(context.Background(), os.Getenv("PGX_TEST_DATABASE")) - require.NoError(t, err) - defer pool.Close() - - tx, err := pool.Begin() - require.NoError(t, err) - defer tx.Rollback() - - testQueryEx(t, tx) -} - func TestTxQueryRow(t *testing.T) { pool, err := pool.Connect(context.Background(), os.Getenv("PGX_TEST_DATABASE")) require.NoError(t, err) @@ -56,15 +44,3 @@ func TestTxQueryRow(t *testing.T) { testQueryRow(t, tx) } - -func TestTxQueryRowEx(t *testing.T) { - pool, err := pool.Connect(context.Background(), os.Getenv("PGX_TEST_DATABASE")) - require.NoError(t, err) - defer pool.Close() - - tx, err := pool.Begin() - require.NoError(t, err) - defer tx.Rollback() - - testQueryRowEx(t, tx) -} diff --git a/query.go b/query.go index d2df5f77..bf27e16c 100644 --- a/query.go +++ b/query.go @@ -307,13 +307,6 @@ func (e scanArgError) Error() string { return fmt.Sprintf("can't scan into dest[%d]: %v", e.col, e.err) } -// Query executes sql with args. If there is an error the returned *Rows will -// be returned in an error state. So it is allowed to ignore the error returned -// from Query and handle it in *Rows. -func (c *Conn) Query(sql string, args ...interface{}) (*Rows, error) { - return c.QueryEx(context.Background(), sql, nil, args...) -} - func (c *Conn) getRows(sql string, args []interface{}) *Rows { if len(c.preallocatedRows) == 0 { c.preallocatedRows = make([]Rows, 64) @@ -330,14 +323,6 @@ func (c *Conn) getRows(sql string, args []interface{}) *Rows { return r } -// QueryRow is a convenience wrapper over Query. Any error that occurs while -// querying is deferred until calling Scan on the returned *Row. That *Row will -// error with ErrNoRows if no rows are returned. -func (c *Conn) QueryRow(sql string, args ...interface{}) *Row { - rows, _ := c.Query(sql, args...) - return (*Row)(rows) -} - type QueryExOptions struct { // When ParameterOIDs are present and the query is not a prepared statement, // then ParameterOIDs and ResultFormatCodes will be used to avoid an extra @@ -348,10 +333,22 @@ type QueryExOptions struct { SimpleProtocol bool } -func (c *Conn) QueryEx(ctx context.Context, sql string, options *QueryExOptions, args ...interface{}) (rows *Rows, err error) { +// Query executes sql with args. If there is an error the returned *Rows will +// be returned in an error state. So it is allowed to ignore the error returned +// from Query and handle it in *Rows. +func (c *Conn) Query(ctx context.Context, sql string, optionsAndArgs ...interface{}) (rows *Rows, err error) { c.lastStmtSent = false // rows = c.getRows(sql, args) + var options *QueryExOptions + args := optionsAndArgs + if len(optionsAndArgs) > 0 { + if o, ok := optionsAndArgs[0].(*QueryExOptions); ok { + options = o + args = optionsAndArgs[1:] + } + } + rows = &Rows{ conn: c, startTime: time.Now(), @@ -547,7 +544,10 @@ func (c *Conn) sanitizeForSimpleQuery(sql string, args ...interface{}) (string, return sanitize.SanitizeSQL(sql, valueArgs...) } -func (c *Conn) QueryRowEx(ctx context.Context, sql string, options *QueryExOptions, args ...interface{}) *Row { - rows, _ := c.QueryEx(ctx, sql, options, args...) +// QueryRow is a convenience wrapper over Query. Any error that occurs while +// querying is deferred until calling Scan on the returned *Row. That *Row will +// error with ErrNoRows if no rows are returned. +func (c *Conn) QueryRow(ctx context.Context, sql string, optionsAndArgs ...interface{}) *Row { + rows, _ := c.Query(ctx, sql, optionsAndArgs...) return (*Row)(rows) } diff --git a/query_test.go b/query_test.go index 1ddbda57..0f320d8a 100644 --- a/query_test.go +++ b/query_test.go @@ -28,7 +28,7 @@ func TestConnQueryScan(t *testing.T) { var sum, rowCount int32 - rows, err := conn.Query("select generate_series(1,$1)", 10) + rows, err := conn.Query(context.Background(), "select generate_series(1,$1)", 10) if err != nil { t.Fatalf("conn.Query failed: %v", err) } @@ -73,7 +73,7 @@ func TestConnQueryScanWithManyColumns(t *testing.T) { var rowCount int - rows, err := conn.Query(sql) + rows, err := conn.Query(context.Background(), sql) if err != nil { t.Fatalf("conn.Query failed: %v", err) } @@ -113,7 +113,7 @@ func TestConnQueryValues(t *testing.T) { var rowCount int32 - rows, err := conn.Query("select 'foo'::text, 'bar'::varchar, n, null, n from generate_series(1,$1) n", 10) + rows, err := conn.Query(context.Background(), "select 'foo'::text, 'bar'::varchar, n, null, n from generate_series(1,$1) n", 10) if err != nil { t.Fatalf("conn.Query failed: %v", err) } @@ -187,7 +187,7 @@ func TestConnQueryValuesWithMultipleComplexColumnsOfSameType(t *testing.T) { var rowCount int32 - rows, err := conn.Query("select '{1,2,3}'::bigint[], '{4,5,6}'::bigint[] from generate_series(1,$1) n", 10) + rows, err := conn.Query(context.Background(), "select '{1,2,3}'::bigint[], '{4,5,6}'::bigint[] from generate_series(1,$1) n", 10) if err != nil { t.Fatalf("conn.Query failed: %v", err) } @@ -229,7 +229,7 @@ func TestRowsScanDoesNotAllowScanningBinaryFormatValuesIntoString(t *testing.T) var s string - err := conn.QueryRow("select 1").Scan(&s) + err := conn.QueryRow(context.Background(), "select 1").Scan(&s) if err == nil || !(strings.Contains(err.Error(), "cannot decode binary value into string") || strings.Contains(err.Error(), "cannot assign")) { t.Fatalf("Expected Scan to fail to encode binary value into string but: %v", err) } @@ -245,7 +245,7 @@ func TestConnQueryCloseEarly(t *testing.T) { defer closeConn(t, conn) // Immediately close query without reading any rows - rows, err := conn.Query("select generate_series(1,$1)", 10) + rows, err := conn.Query(context.Background(), "select generate_series(1,$1)", 10) if err != nil { t.Fatalf("conn.Query failed: %v", err) } @@ -254,7 +254,7 @@ func TestConnQueryCloseEarly(t *testing.T) { ensureConnValid(t, conn) // Read partial response then close - rows, err = conn.Query("select generate_series(1,$1)", 10) + rows, err = conn.Query(context.Background(), "select generate_series(1,$1)", 10) if err != nil { t.Fatalf("conn.Query failed: %v", err) } @@ -281,7 +281,7 @@ func TestConnQueryCloseEarlyWithErrorOnWire(t *testing.T) { conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) - rows, err := conn.Query("select 1/(10-n) from generate_series(1,10) n") + rows, err := conn.Query(context.Background(), "select 1/(10-n) from generate_series(1,10) n") if err != nil { t.Fatalf("conn.Query failed: %v", err) } @@ -301,7 +301,7 @@ func TestConnQueryReadWrongTypeError(t *testing.T) { defer closeConn(t, conn) // Read a single value incorrectly - rows, err := conn.Query("select generate_series(1,$1)", 10) + rows, err := conn.Query(context.Background(), "select generate_series(1,$1)", 10) if err != nil { t.Fatalf("conn.Query failed: %v", err) } @@ -337,7 +337,7 @@ func TestConnQueryReadTooManyValues(t *testing.T) { defer closeConn(t, conn) // Read too many values - rows, err := conn.Query("select generate_series(1,$1)", 10) + rows, err := conn.Query(context.Background(), "select generate_series(1,$1)", 10) if err != nil { t.Fatalf("conn.Query failed: %v", err) } @@ -367,7 +367,7 @@ func TestConnQueryScanIgnoreColumn(t *testing.T) { conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) - rows, err := conn.Query("select 1::int8, 2::int8, 3::int8") + rows, err := conn.Query(context.Background(), "select 1::int8, 2::int8, 3::int8") if err != nil { t.Fatalf("conn.Query failed: %v", err) } @@ -405,7 +405,7 @@ func TestConnQueryErrorWhileReturningRows(t *testing.T) { func() { sql := `select 42 / (random() * 20)::integer from generate_series(1,100000)` - rows, err := conn.Query(sql) + rows, err := conn.Query(context.Background(), sql) if err != nil { t.Fatal(err) } @@ -432,7 +432,7 @@ func TestQueryEncodeError(t *testing.T) { conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) - rows, err := conn.Query("select $1::integer", "wrong") + rows, err := conn.Query(context.Background(), "select $1::integer", "wrong") if err != nil { t.Errorf("conn.Query failure: %v", err) } @@ -487,7 +487,7 @@ func TestQueryRowCoreTypes(t *testing.T) { for i, tt := range tests { actual = zero - err := conn.QueryRow(tt.sql, tt.queryArgs...).Scan(tt.scanArgs...) + err := conn.QueryRow(context.Background(), tt.sql, tt.queryArgs...).Scan(tt.scanArgs...) if err != nil { t.Errorf("%d. Unexpected failure: %v (sql -> %v, queryArgs -> %v)", i, err, tt.sql, tt.queryArgs) } @@ -499,7 +499,7 @@ func TestQueryRowCoreTypes(t *testing.T) { ensureConnValid(t, conn) // Check that Scan errors when a core type is null - err = conn.QueryRow(tt.sql, nil).Scan(tt.scanArgs...) + err = conn.QueryRow(context.Background(), tt.sql, nil).Scan(tt.scanArgs...) if err == nil { t.Errorf("%d. Expected null to cause error, but it didn't (sql -> %v)", i, tt.sql) } @@ -575,7 +575,7 @@ func TestQueryRowCoreIntegerEncoding(t *testing.T) { for i, tt := range successfulEncodeTests { actual = zero - err := conn.QueryRow(tt.sql, tt.queryArg).Scan(tt.scanArg) + err := conn.QueryRow(context.Background(), tt.sql, tt.queryArg).Scan(tt.scanArg) if err != nil { t.Errorf("%d. Unexpected failure: %v (sql -> %v, queryArg -> %v)", i, err, tt.sql, tt.queryArg) continue @@ -612,7 +612,7 @@ func TestQueryRowCoreIntegerEncoding(t *testing.T) { } for i, tt := range failedEncodeTests { - err := conn.QueryRow(tt.sql, tt.queryArg).Scan(nil) + err := conn.QueryRow(context.Background(), tt.sql, tt.queryArg).Scan(nil) if err == nil { t.Errorf("%d. Expected failure to encode, but unexpectedly succeeded: %v (sql -> %v, queryArg -> %v)", i, err, tt.sql, tt.queryArg) } else if !strings.Contains(err.Error(), "is greater than") { @@ -718,7 +718,7 @@ func TestQueryRowCoreIntegerDecoding(t *testing.T) { for i, tt := range successfulDecodeTests { actual = zero - err := conn.QueryRow(tt.sql).Scan(tt.scanArg) + err := conn.QueryRow(context.Background(), tt.sql).Scan(tt.scanArg) if err != nil { t.Errorf("%d. Unexpected failure: %v (sql -> %v)", i, err, tt.sql) continue @@ -787,7 +787,7 @@ func TestQueryRowCoreIntegerDecoding(t *testing.T) { } for i, tt := range failedDecodeTests { - err := conn.QueryRow(tt.sql).Scan(tt.scanArg) + err := conn.QueryRow(context.Background(), tt.sql).Scan(tt.scanArg) if err == nil { t.Errorf("%d. Expected failure to decode, but unexpectedly succeeded: %v (sql -> %v)", i, err, tt.sql) } else if !strings.Contains(err.Error(), tt.expectedErr) { @@ -818,7 +818,7 @@ func TestQueryRowCoreByteSlice(t *testing.T) { for i, tt := range tests { var actual []byte - err := conn.QueryRow(tt.sql, tt.queryArg).Scan(&actual) + err := conn.QueryRow(context.Background(), tt.sql, tt.queryArg).Scan(&actual) if err != nil { t.Errorf("%d. Unexpected failure: %v (sql -> %v)", i, err, tt.sql) } @@ -854,7 +854,7 @@ func TestQueryRowUnknownType(t *testing.T) { expected := "(1,0)" var actual string - err := conn.QueryRow(sql, expected).Scan(&actual) + err := conn.QueryRow(context.Background(), sql, expected).Scan(&actual) if err != nil { t.Errorf("Unexpected failure: %v (sql -> %v)", err, sql) } @@ -896,7 +896,7 @@ func TestQueryRowErrors(t *testing.T) { for i, tt := range tests { actual = zero - err := conn.QueryRow(tt.sql, tt.queryArgs...).Scan(tt.scanArgs...) + err := conn.QueryRow(context.Background(), tt.sql, tt.queryArgs...).Scan(tt.scanArgs...) if err == nil { t.Errorf("%d. Unexpected success (sql -> %v, queryArgs -> %v)", i, tt.sql, tt.queryArgs) } @@ -921,17 +921,18 @@ func TestQueryRowExErrorsWrongParameterOIDs(t *testing.T) { select some_int from t where some_text = $1` paramOIDs := []pgtype.OID{pgtype.TextArrayOID} queryArgs := []interface{}{"bar"} + queryOptions := &pgx.QueryExOptions{ + ParameterOIDs: paramOIDs, + ResultFormatCodes: []int16{pgx.BinaryFormatCode}, + } + optionsAndArgs := append([]interface{}{queryOptions}, queryArgs...) expectedErr := "operator does not exist: text = text[] (SQLSTATE 42883)" var result int64 - err := conn.QueryRowEx( + err := conn.QueryRow( context.Background(), sql, - &pgx.QueryExOptions{ - ParameterOIDs: paramOIDs, - ResultFormatCodes: []int16{pgx.BinaryFormatCode}, - }, - queryArgs..., + optionsAndArgs..., ).Scan(&result) if err == nil { @@ -952,7 +953,7 @@ func TestQueryRowNoResults(t *testing.T) { defer closeConn(t, conn) var n int32 - err := conn.QueryRow("select 1 where 1=0").Scan(&n) + err := conn.QueryRow(context.Background(), "select 1 where 1=0").Scan(&n) if err != pgx.ErrNoRows { t.Errorf("Expected pgx.ErrNoRows, got %v", err) } @@ -966,7 +967,7 @@ func TestReadingValueAfterEmptyArray(t *testing.T) { var a []string var b int32 - err := conn.QueryRow("select '{}'::text[], 42::integer").Scan(&a, &b) + err := conn.QueryRow(context.Background(), "select '{}'::text[], 42::integer").Scan(&a, &b) if err != nil { t.Fatalf("conn.QueryRow failed: %v", err) } @@ -985,7 +986,7 @@ func TestReadingNullByteArray(t *testing.T) { defer closeConn(t, conn) var a []byte - err := conn.QueryRow("select null::text").Scan(&a) + err := conn.QueryRow(context.Background(), "select null::text").Scan(&a) if err != nil { t.Fatalf("conn.QueryRow failed: %v", err) } @@ -999,7 +1000,7 @@ func TestReadingNullByteArrays(t *testing.T) { conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) - rows, err := conn.Query("select null::text union all select null::text") + rows, err := conn.Query(context.Background(), "select null::text union all select null::text") if err != nil { t.Fatalf("conn.Query failed: %v", err) } @@ -1030,7 +1031,7 @@ func TestConnQueryDatabaseSQLScanner(t *testing.T) { var num decimal.Decimal - err := conn.QueryRow("select '1234.567'::decimal").Scan(&num) + err := conn.QueryRow(context.Background(), "select '1234.567'::decimal").Scan(&num) if err != nil { t.Fatalf("Scan failed: %v", err) } @@ -1061,7 +1062,7 @@ func TestConnQueryDatabaseSQLDriverValuer(t *testing.T) { } var num decimal.Decimal - err = conn.QueryRow("select $1::decimal", &expected).Scan(&num) + err = conn.QueryRow(context.Background(), "select $1::decimal", &expected).Scan(&num) if err != nil { t.Fatalf("Scan failed: %v", err) } @@ -1112,7 +1113,7 @@ func TestConnQueryDatabaseSQLDriverValuerWithBinaryPgTypeThatAcceptsSameType(t * } var u2 uuid.UUID - err = conn.QueryRow("select $1::uuid", expected).Scan(&u2) + err = conn.QueryRow(context.Background(), "select $1::uuid", expected).Scan(&u2) if err != nil { t.Fatalf("Scan failed: %v", err) } @@ -1151,6 +1152,7 @@ func TestConnQueryDatabaseSQLNullX(t *testing.T) { var actual row err := conn.QueryRow( + context.Background(), "select $1::bool, $2::bool, $3::int8, $4::int8, $5::float8, $6::float8, $7::text, $8::text", expected.boolValid, expected.boolNull, @@ -1181,7 +1183,7 @@ func TestConnQueryDatabaseSQLNullX(t *testing.T) { ensureConnValid(t, conn) } -func TestQueryExContextSuccess(t *testing.T) { +func TestQueryContextSuccess(t *testing.T) { t.Parallel() conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) @@ -1190,7 +1192,7 @@ func TestQueryExContextSuccess(t *testing.T) { ctx, cancelFunc := context.WithCancel(context.Background()) defer cancelFunc() - rows, err := conn.QueryEx(ctx, "select 42::integer", nil) + rows, err := conn.Query(ctx, "select 42::integer") if err != nil { t.Fatal(err) } @@ -1221,7 +1223,7 @@ func TestQueryExContextSuccess(t *testing.T) { ensureConnValid(t, conn) } -func TestQueryExContextErrorWhileReceivingRows(t *testing.T) { +func TestQueryContextErrorWhileReceivingRows(t *testing.T) { t.Parallel() conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) @@ -1230,7 +1232,7 @@ func TestQueryExContextErrorWhileReceivingRows(t *testing.T) { ctx, cancelFunc := context.WithCancel(context.Background()) defer cancelFunc() - rows, err := conn.QueryEx(ctx, "select 10/(10-n) from generate_series(1, 100) n", nil) + rows, err := conn.Query(ctx, "select 10/(10-n) from generate_series(1, 100) n") if err != nil { t.Fatal(err) } @@ -1258,7 +1260,7 @@ func TestQueryExContextErrorWhileReceivingRows(t *testing.T) { ensureConnValid(t, conn) } -func TestQueryRowExContextSuccess(t *testing.T) { +func TestQueryRowContextSuccess(t *testing.T) { t.Parallel() conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) @@ -1268,7 +1270,7 @@ func TestQueryRowExContextSuccess(t *testing.T) { defer cancelFunc() var result int - err := conn.QueryRowEx(ctx, "select 42::integer", nil).Scan(&result) + err := conn.QueryRow(ctx, "select 42::integer").Scan(&result) if err != nil { t.Fatal(err) } @@ -1282,7 +1284,7 @@ func TestQueryRowExContextSuccess(t *testing.T) { ensureConnValid(t, conn) } -func TestQueryRowExContextErrorWhileReceivingRow(t *testing.T) { +func TestQueryRowContextErrorWhileReceivingRow(t *testing.T) { t.Parallel() conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) @@ -1292,7 +1294,7 @@ func TestQueryRowExContextErrorWhileReceivingRow(t *testing.T) { defer cancelFunc() var result int - err := conn.QueryRowEx(ctx, "select 10/0", nil).Scan(&result) + err := conn.QueryRow(ctx, "select 10/0").Scan(&result) if err == nil || err.Error() != "ERROR: division by zero (SQLSTATE 22012)" { t.Fatalf("Expected division by zero error, but got %v", err) } @@ -1300,14 +1302,14 @@ func TestQueryRowExContextErrorWhileReceivingRow(t *testing.T) { ensureConnValid(t, conn) } -func TestConnQueryRowExSingleRoundTrip(t *testing.T) { +func TestConnQueryRowSingleRoundTrip(t *testing.T) { t.Parallel() conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) var result int32 - err := conn.QueryRowEx( + err := conn.QueryRow( context.Background(), "select $1 + $2", &pgx.QueryExOptions{ @@ -1337,7 +1339,7 @@ func TestConnSimpleProtocol(t *testing.T) { { expected := int64(42) var actual int64 - err := conn.QueryRowEx( + err := conn.QueryRow( context.Background(), "select $1::int8", &pgx.QueryExOptions{SimpleProtocol: true}, @@ -1357,7 +1359,7 @@ func TestConnSimpleProtocol(t *testing.T) { { expected := float64(1.23) var actual float64 - err := conn.QueryRowEx( + err := conn.QueryRow( context.Background(), "select $1::float8", &pgx.QueryExOptions{SimpleProtocol: true}, @@ -1377,7 +1379,7 @@ func TestConnSimpleProtocol(t *testing.T) { { expected := true var actual bool - err := conn.QueryRowEx( + err := conn.QueryRow( context.Background(), "select $1", &pgx.QueryExOptions{SimpleProtocol: true}, @@ -1397,7 +1399,7 @@ func TestConnSimpleProtocol(t *testing.T) { { expected := []byte{0, 1, 20, 35, 64, 80, 120, 3, 255, 240, 128, 95} var actual []byte - err := conn.QueryRowEx( + err := conn.QueryRow( context.Background(), "select $1::bytea", &pgx.QueryExOptions{SimpleProtocol: true}, @@ -1417,7 +1419,7 @@ func TestConnSimpleProtocol(t *testing.T) { { expected := "test" var actual string - err := conn.QueryRowEx( + err := conn.QueryRow( context.Background(), "select $1::text", &pgx.QueryExOptions{SimpleProtocol: true}, @@ -1439,7 +1441,7 @@ func TestConnSimpleProtocol(t *testing.T) { { expected := pgtype.Circle{P: pgtype.Vec2{1, 2}, R: 1.5, Status: pgtype.Present} actual := expected - err := conn.QueryRowEx( + err := conn.QueryRow( context.Background(), "select $1::circle", &pgx.QueryExOptions{SimpleProtocol: true}, @@ -1469,7 +1471,7 @@ func TestConnSimpleProtocol(t *testing.T) { var actualBool bool var actualBytes []byte var actualString string - err := conn.QueryRowEx( + err := conn.QueryRow( context.Background(), "select $1::int8, $2::float8, $3, $4::bytea, $5::text", &pgx.QueryExOptions{SimpleProtocol: true}, @@ -1503,7 +1505,7 @@ func TestConnSimpleProtocol(t *testing.T) { { expected := "foo';drop table users;" var actual string - err := conn.QueryRowEx( + err := conn.QueryRow( context.Background(), "select $1", &pgx.QueryExOptions{SimpleProtocol: true}, @@ -1532,7 +1534,7 @@ func TestConnSimpleProtocolRefusesNonUTF8ClientEncoding(t *testing.T) { mustExec(t, conn, "set client_encoding to 'SQL_ASCII'") var expected string - err := conn.QueryRowEx( + err := conn.QueryRow( context.Background(), "select $1", &pgx.QueryExOptions{SimpleProtocol: true}, @@ -1554,7 +1556,7 @@ func TestConnSimpleProtocolRefusesNonStandardConformingStrings(t *testing.T) { mustExec(t, conn, "set standard_conforming_strings to off") var expected string - err := conn.QueryRowEx( + err := conn.QueryRow( context.Background(), "select $1", &pgx.QueryExOptions{SimpleProtocol: true}, @@ -1567,13 +1569,13 @@ func TestConnSimpleProtocolRefusesNonStandardConformingStrings(t *testing.T) { ensureConnValid(t, conn) } -func TestQueryExCloseBefore(t *testing.T) { +func TestQueryCloseBefore(t *testing.T) { t.Parallel() conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) closeConn(t, conn) - if _, err := conn.QueryEx(context.Background(), "select 1", nil); err == nil { + if _, err := conn.Query(context.Background(), "select 1"); err == nil { t.Fatal("Expected network error") } if conn.LastStmtSent() { diff --git a/replication_test.go b/replication_test.go index e68dc413..0574e86f 100644 --- a/replication_test.go +++ b/replication_test.go @@ -17,7 +17,7 @@ import ( // This function uses a postgresql 9.6 specific column func getConfirmedFlushLsnFor(t *testing.T, conn *pgx.Conn, slot string) string { // Fetch the restart LSN of the slot, to establish a starting point - rows, err := conn.Query(fmt.Sprintf("select confirmed_flush_lsn from pg_replication_slots where slot_name='%s'", slot)) + rows, err := conn.Query(context.Background(), fmt.Sprintf("select confirmed_flush_lsn from pg_replication_slots where slot_name='%s'", slot)) if err != nil { t.Fatalf("conn.Query failed: %v", err) } diff --git a/tx.go b/tx.go index a697e35d..14d356b1 100644 --- a/tx.go +++ b/tx.go @@ -185,30 +185,19 @@ func (tx *Tx) PrepareEx(ctx context.Context, name, sql string, opts *PrepareExOp } // Query delegates to the underlying *Conn -func (tx *Tx) Query(sql string, args ...interface{}) (*Rows, error) { - return tx.QueryEx(context.Background(), sql, nil, args...) -} - -// QueryEx delegates to the underlying *Conn -func (tx *Tx) QueryEx(ctx context.Context, sql string, options *QueryExOptions, args ...interface{}) (*Rows, error) { +func (tx *Tx) Query(ctx context.Context, sql string, optionsAndArgs ...interface{}) (*Rows, error) { if tx.status != TxStatusInProgress { // Because checking for errors can be deferred to the *Rows, build one with the error err := ErrTxClosed return &Rows{closed: true, err: err}, err } - return tx.conn.QueryEx(ctx, sql, options, args...) + return tx.conn.Query(ctx, sql, optionsAndArgs...) } // QueryRow delegates to the underlying *Conn -func (tx *Tx) QueryRow(sql string, args ...interface{}) *Row { - rows, _ := tx.Query(sql, args...) - return (*Row)(rows) -} - -// QueryRowEx delegates to the underlying *Conn -func (tx *Tx) QueryRowEx(ctx context.Context, sql string, options *QueryExOptions, args ...interface{}) *Row { - rows, _ := tx.QueryEx(ctx, sql, options, args...) +func (tx *Tx) QueryRow(ctx context.Context, sql string, optionsAndArgs ...interface{}) *Row { + rows, _ := tx.Query(ctx, sql, optionsAndArgs...) return (*Row)(rows) } diff --git a/tx_test.go b/tx_test.go index 66016b51..8244649e 100644 --- a/tx_test.go +++ b/tx_test.go @@ -42,7 +42,7 @@ func TestTransactionSuccessfulCommit(t *testing.T) { } var n int64 - err = conn.QueryRow("select count(*) from foo").Scan(&n) + err = conn.QueryRow(context.Background(), "select count(*) from foo").Scan(&n) if err != nil { t.Fatalf("QueryRow Scan failed: %v", err) } @@ -88,7 +88,7 @@ func TestTxCommitWhenTxBroken(t *testing.T) { } var n int64 - err = conn.QueryRow("select count(*) from foo").Scan(&n) + err = conn.QueryRow(context.Background(), "select count(*) from foo").Scan(&n) if err != nil { t.Fatalf("QueryRow Scan failed: %v", err) } @@ -179,7 +179,7 @@ func TestTransactionSuccessfulRollback(t *testing.T) { } var n int64 - err = conn.QueryRow("select count(*) from foo").Scan(&n) + err = conn.QueryRow(context.Background(), "select count(*) from foo").Scan(&n) if err != nil { t.Fatalf("QueryRow Scan failed: %v", err) } @@ -202,7 +202,7 @@ func TestBeginExIsoLevels(t *testing.T) { } var level pgx.TxIsoLevel - conn.QueryRow("select current_setting('transaction_isolation')").Scan(&level) + conn.QueryRow(context.Background(), "select current_setting('transaction_isolation')").Scan(&level) if level != iso { t.Errorf("Expected to be in isolation level %v but was %v", iso, level) } diff --git a/values_test.go b/values_test.go index fba38281..7f3cc359 100644 --- a/values_test.go +++ b/values_test.go @@ -2,6 +2,7 @@ package pgx_test import ( "bytes" + "context" "net" "os" "reflect" @@ -41,7 +42,7 @@ func TestDateTranscode(t *testing.T) { for _, actualDate := range dates { var d time.Time - err := conn.QueryRow("select $1::date", actualDate).Scan(&d) + err := conn.QueryRow(context.Background(), "select $1::date", actualDate).Scan(&d) if err != nil { t.Fatalf("Unexpected failure on QueryRow Scan: %v", err) } @@ -61,7 +62,7 @@ func TestTimestampTzTranscode(t *testing.T) { var outputTime time.Time - err := conn.QueryRow("select $1::timestamptz", inputTime).Scan(&outputTime) + err := conn.QueryRow(context.Background(), "select $1::timestamptz", inputTime).Scan(&outputTime) if err != nil { t.Fatalf("QueryRow Scan failed: %v", err) } @@ -98,7 +99,7 @@ func testJSONString(t *testing.T, conn *pgx.Conn, typename string) { input := `{"key": "value"}` expectedOutput := map[string]string{"key": "value"} var output map[string]string - err := conn.QueryRow("select $1::"+typename, input).Scan(&output) + err := conn.QueryRow(context.Background(), "select $1::"+typename, input).Scan(&output) if err != nil { t.Errorf("%s: QueryRow Scan failed: %v", typename, err) return @@ -114,7 +115,7 @@ func testJSONStringPointer(t *testing.T, conn *pgx.Conn, typename string) { input := `{"key": "value"}` expectedOutput := map[string]string{"key": "value"} var output map[string]string - err := conn.QueryRow("select $1::"+typename, &input).Scan(&output) + err := conn.QueryRow(context.Background(), "select $1::"+typename, &input).Scan(&output) if err != nil { t.Errorf("%s: QueryRow Scan failed: %v", typename, err) return @@ -129,7 +130,7 @@ func testJSONStringPointer(t *testing.T, conn *pgx.Conn, typename string) { func testJSONSingleLevelStringMap(t *testing.T, conn *pgx.Conn, typename string) { input := map[string]string{"key": "value"} var output map[string]string - err := conn.QueryRow("select $1::"+typename, input).Scan(&output) + err := conn.QueryRow(context.Background(), "select $1::"+typename, input).Scan(&output) if err != nil { t.Errorf("%s: QueryRow Scan failed: %v", typename, err) return @@ -148,7 +149,7 @@ func testJSONNestedMap(t *testing.T, conn *pgx.Conn, typename string) { "inventory": []interface{}{"phone", "key"}, } var output map[string]interface{} - err := conn.QueryRow("select $1::"+typename, input).Scan(&output) + err := conn.QueryRow(context.Background(), "select $1::"+typename, input).Scan(&output) if err != nil { t.Errorf("%s: QueryRow Scan failed: %v", typename, err) return @@ -163,7 +164,7 @@ func testJSONNestedMap(t *testing.T, conn *pgx.Conn, typename string) { func testJSONStringArray(t *testing.T, conn *pgx.Conn, typename string) { input := []string{"foo", "bar", "baz"} var output []string - err := conn.QueryRow("select $1::"+typename, input).Scan(&output) + err := conn.QueryRow(context.Background(), "select $1::"+typename, input).Scan(&output) if err != nil { t.Errorf("%s: QueryRow Scan failed: %v", typename, err) } @@ -176,7 +177,7 @@ func testJSONStringArray(t *testing.T, conn *pgx.Conn, typename string) { func testJSONInt64Array(t *testing.T, conn *pgx.Conn, typename string) { input := []int64{1, 2, 234432} var output []int64 - err := conn.QueryRow("select $1::"+typename, input).Scan(&output) + err := conn.QueryRow(context.Background(), "select $1::"+typename, input).Scan(&output) if err != nil { t.Errorf("%s: QueryRow Scan failed: %v", typename, err) } @@ -189,7 +190,7 @@ func testJSONInt64Array(t *testing.T, conn *pgx.Conn, typename string) { func testJSONInt16ArrayFailureDueToOverflow(t *testing.T, conn *pgx.Conn, typename string) { input := []int{1, 2, 234432} var output []int16 - err := conn.QueryRow("select $1::"+typename, input).Scan(&output) + err := conn.QueryRow(context.Background(), "select $1::"+typename, input).Scan(&output) if err == nil || err.Error() != "can't scan into dest[0]: json: cannot unmarshal number 234432 into Go value of type int16" { t.Errorf("%s: Expected *json.UnmarkalTypeError, but got %v", typename, err) } @@ -208,7 +209,7 @@ func testJSONStruct(t *testing.T, conn *pgx.Conn, typename string) { var output person - err := conn.QueryRow("select $1::"+typename, input).Scan(&output) + err := conn.QueryRow(context.Background(), "select $1::"+typename, input).Scan(&output) if err != nil { t.Errorf("%s: QueryRow Scan failed: %v", typename, err) } @@ -236,7 +237,7 @@ func TestStringToNotTextTypeTranscode(t *testing.T) { input := "01086ee0-4963-4e35-9116-30c173a8d0bd" var output string - err := conn.QueryRow("select $1::uuid", input).Scan(&output) + err := conn.QueryRow(context.Background(), "select $1::uuid", input).Scan(&output) if err != nil { t.Fatal(err) } @@ -244,7 +245,7 @@ func TestStringToNotTextTypeTranscode(t *testing.T) { t.Errorf("uuid: Did not transcode string successfully: %s is not %s", input, output) } - err = conn.QueryRow("select $1::uuid", &input).Scan(&output) + err = conn.QueryRow(context.Background(), "select $1::uuid", &input).Scan(&output) if err != nil { t.Fatal(err) } @@ -288,7 +289,7 @@ func TestInetCIDRTranscodeIPNet(t *testing.T) { for i, tt := range tests { var actual net.IPNet - err := conn.QueryRow(tt.sql, tt.value).Scan(&actual) + err := conn.QueryRow(context.Background(), tt.sql, tt.value).Scan(&actual) if err != nil { t.Errorf("%d. Unexpected failure: %v (sql -> %v, value -> %v)", i, err, tt.sql, tt.value) continue @@ -329,7 +330,7 @@ func TestInetCIDRTranscodeIP(t *testing.T) { for i, tt := range tests { var actual net.IP - err := conn.QueryRow(tt.sql, tt.value).Scan(&actual) + err := conn.QueryRow(context.Background(), tt.sql, tt.value).Scan(&actual) if err != nil { t.Errorf("%d. Unexpected failure: %v (sql -> %v, value -> %v)", i, err, tt.sql, tt.value) continue @@ -352,7 +353,7 @@ func TestInetCIDRTranscodeIP(t *testing.T) { for i, tt := range failTests { var actual net.IP - err := conn.QueryRow(tt.sql, tt.value).Scan(&actual) + err := conn.QueryRow(context.Background(), tt.sql, tt.value).Scan(&actual) if err == nil { t.Errorf("%d. Expected failure but got none", i) continue @@ -407,7 +408,7 @@ func TestInetCIDRArrayTranscodeIPNet(t *testing.T) { for i, tt := range tests { var actual []*net.IPNet - err := conn.QueryRow(tt.sql, tt.value).Scan(&actual) + err := conn.QueryRow(context.Background(), tt.sql, tt.value).Scan(&actual) if err != nil { t.Errorf("%d. Unexpected failure: %v (sql -> %v, value -> %v)", i, err, tt.sql, tt.value) continue @@ -456,7 +457,7 @@ func TestInetCIDRArrayTranscodeIP(t *testing.T) { for i, tt := range tests { var actual []net.IP - err := conn.QueryRow(tt.sql, tt.value).Scan(&actual) + err := conn.QueryRow(context.Background(), tt.sql, tt.value).Scan(&actual) if err != nil { t.Errorf("%d. Unexpected failure: %v (sql -> %v, value -> %v)", i, err, tt.sql, tt.value) continue @@ -492,7 +493,7 @@ func TestInetCIDRArrayTranscodeIP(t *testing.T) { for i, tt := range failTests { var actual []net.IP - err := conn.QueryRow(tt.sql, tt.value).Scan(&actual) + err := conn.QueryRow(context.Background(), tt.sql, tt.value).Scan(&actual) if err == nil { t.Errorf("%d. Expected failure but got none", i) continue @@ -530,7 +531,7 @@ func TestInetCIDRTranscodeWithJustIP(t *testing.T) { expected := mustParseCIDR(t, tt.value) var actual net.IPNet - err := conn.QueryRow(tt.sql, expected.IP).Scan(&actual) + err := conn.QueryRow(context.Background(), tt.sql, expected.IP).Scan(&actual) if err != nil { t.Errorf("%d. Unexpected failure: %v (sql -> %v, value -> %v)", i, err, tt.sql, tt.value) continue @@ -648,7 +649,7 @@ func TestArrayDecoding(t *testing.T) { } for i, tt := range tests { - err := conn.QueryRow(tt.sql, tt.query).Scan(tt.scan) + err := conn.QueryRow(context.Background(), tt.sql, tt.query).Scan(tt.scan) if err != nil { t.Errorf(`%d. error reading array: %v`, i, err) continue @@ -666,7 +667,7 @@ func TestEmptyArrayDecoding(t *testing.T) { var val []string - err := conn.QueryRow("select array[]::text[]").Scan(&val) + err := conn.QueryRow(context.Background(), "select array[]::text[]").Scan(&val) if err != nil { t.Errorf(`error reading array: %v`, err) } @@ -676,7 +677,7 @@ func TestEmptyArrayDecoding(t *testing.T) { var n, m int32 - err = conn.QueryRow("select 1::integer, array[]::text[], 42::integer").Scan(&n, &val, &m) + err = conn.QueryRow(context.Background(), "select 1::integer, array[]::text[], 42::integer").Scan(&n, &val, &m) if err != nil { t.Errorf(`error reading array: %v`, err) } @@ -690,7 +691,7 @@ func TestEmptyArrayDecoding(t *testing.T) { t.Errorf("Expected n to be 42, but it was %d", n) } - rows, err := conn.Query("select 1::integer, array['test']::text[] union select 2::integer, array[]::text[] union select 3::integer, array['test']::text[]") + rows, err := conn.Query(context.Background(), "select 1::integer, array['test']::text[] union select 2::integer, array[]::text[] union select 3::integer, array['test']::text[]") if err != nil { t.Errorf(`error retrieving rows with array: %v`, err) } @@ -771,7 +772,7 @@ func TestPointerPointer(t *testing.T) { for i, tt := range tests { actual = zero - err := conn.QueryRow(tt.sql, tt.queryArgs...).Scan(tt.scanArgs...) + err := conn.QueryRow(context.Background(), tt.sql, tt.queryArgs...).Scan(tt.scanArgs...) if err != nil { t.Errorf("%d. Unexpected failure: %v (sql -> %v, queryArgs -> %v)", i, err, tt.sql, tt.queryArgs) } @@ -793,7 +794,7 @@ func TestPointerPointerNonZero(t *testing.T) { f := "foo" dest := &f - err := conn.QueryRow("select $1::text", nil).Scan(&dest) + err := conn.QueryRow(context.Background(), "select $1::text", nil).Scan(&dest) if err != nil { t.Errorf("Unexpected failure scanning: %v", err) } @@ -852,7 +853,7 @@ func TestEncodeTypeRename(t *testing.T) { inString := _string("foo") var outString _string - err := conn.QueryRow("select $1::int, $2::int, $3::int2, $4::int4, $5::int8, $6::int, $7::int, $8::int, $9::int, $10::int, $11::text", + err := conn.QueryRow(context.Background(), "select $1::int, $2::int, $3::int2, $4::int4, $5::int8, $6::int, $7::int, $8::int, $9::int, $10::int, $11::text", inInt, inInt8, inInt16, inInt32, inInt64, inUint, inUint8, inUint16, inUint32, inUint64, inString, ).Scan(&outInt, &outInt8, &outInt16, &outInt32, &outInt64, &outUint, &outUint8, &outUint16, &outUint32, &outUint64, &outString) if err != nil { @@ -936,7 +937,7 @@ func TestRowDecode(t *testing.T) { for i, tt := range tests { var actual []interface{} - err := conn.QueryRow(tt.sql).Scan(&actual) + err := conn.QueryRow(context.Background(), tt.sql).Scan(&actual) if err != nil { t.Errorf("%d. Unexpected failure: %v (sql -> %v)", i, err, tt.sql) continue