Remove Ex versions of Query and QueryRow
Always require context and prepend options to arguments if necessary.
This commit is contained in:
+4
-39
@@ -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)
|
||||
|
||||
+4
-15
@@ -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}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
+6
-33
@@ -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
|
||||
}
|
||||
|
||||
+1
-43
@@ -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)
|
||||
}()
|
||||
}
|
||||
|
||||
|
||||
+4
-12
@@ -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...)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user