2
0

Replace QueryFunc with ForEachScannedRow

This commit is contained in:
Jack Christensen
2022-07-07 20:29:04 -05:00
parent a86f4f3db9
commit 76946fb5a3
16 changed files with 301 additions and 469 deletions
-99
View File
@@ -1898,102 +1898,3 @@ func TestQueryWithQueryRewriter(t *testing.T) {
require.NoError(t, rows.Err())
})
}
func TestConnQueryFunc(t *testing.T) {
t.Parallel()
pgxtest.RunWithQueryExecModes(context.Background(), t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
var actualResults []any
var a, b int
ct, err := conn.QueryFunc(
context.Background(),
"select n, n * 2 from generate_series(1, $1) n",
[]any{3},
[]any{&a, &b},
func(pgx.QueryFuncRow) error {
actualResults = append(actualResults, []any{a, b})
return nil
},
)
require.NoError(t, err)
expectedResults := []any{
[]any{1, 2},
[]any{2, 4},
[]any{3, 6},
}
require.Equal(t, expectedResults, actualResults)
require.EqualValues(t, 3, ct.RowsAffected())
})
}
func TestConnQueryFuncScanError(t *testing.T) {
t.Parallel()
pgxtest.RunWithQueryExecModes(context.Background(), t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
var actualResults []any
var a, b int
ct, err := conn.QueryFunc(
context.Background(),
"select 'foo', 'bar' from generate_series(1, $1) n",
[]any{3},
[]any{&a, &b},
func(pgx.QueryFuncRow) error {
actualResults = append(actualResults, []any{a, b})
return nil
},
)
require.EqualError(t, err, "can't scan into dest[0]: cannot scan OID 25 in text format into *int")
require.Equal(t, pgconn.CommandTag{}, ct)
})
}
func TestConnQueryFuncAbort(t *testing.T) {
t.Parallel()
pgxtest.RunWithQueryExecModes(context.Background(), t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
var a, b int
ct, err := conn.QueryFunc(
context.Background(),
"select n, n * 2 from generate_series(1, $1) n",
[]any{3},
[]any{&a, &b},
func(pgx.QueryFuncRow) error {
return errors.New("abort")
},
)
require.EqualError(t, err, "abort")
require.Equal(t, pgconn.CommandTag{}, ct)
})
}
func ExampleConn_QueryFunc() {
conn, err := pgx.Connect(context.Background(), os.Getenv("PGX_TEST_DATABASE"))
if err != nil {
fmt.Printf("Unable to establish connection: %v", err)
return
}
var a, b int
_, err = conn.QueryFunc(
context.Background(),
"select n, n * 2 from generate_series(1, $1) n",
[]any{3},
[]any{&a, &b},
func(pgx.QueryFuncRow) error {
fmt.Printf("%v, %v\n", a, b)
return nil
},
)
if err != nil {
fmt.Printf("QueryFunc error: %v", err)
return
}
// Output:
// 1, 2
// 2, 4
// 3, 6
}