2
0

Rows and Row are now interfaces

This commit is contained in:
Jack Christensen
2019-04-11 17:53:52 -05:00
parent 5ea8191003
commit 938ee9f434
11 changed files with 117 additions and 87 deletions
+3 -3
View File
@@ -6,7 +6,7 @@ import (
"time"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/pool"
"github.com/jackc/pgx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -37,7 +37,7 @@ func testExec(t *testing.T, db execer) {
}
type queryer interface {
Query(ctx context.Context, sql string, optionsAndArgs ...interface{}) (*pool.Rows, error)
Query(ctx context.Context, sql string, optionsAndArgs ...interface{}) (pgx.Rows, error)
}
func testQuery(t *testing.T, db queryer) {
@@ -59,7 +59,7 @@ func testQuery(t *testing.T, db queryer) {
}
type queryRower interface {
QueryRow(ctx context.Context, sql string, optionsAndArgs ...interface{}) *pool.Row
QueryRow(ctx context.Context, sql string, optionsAndArgs ...interface{}) pgx.Row
}
func testQueryRow(t *testing.T, db queryRower) {
+6 -10
View File
@@ -50,23 +50,19 @@ func (c *Conn) Release() {
}
func (c *Conn) Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error) {
conn := c.res.Value().(*pgx.Conn)
return conn.Exec(ctx, sql, arguments...)
return c.Conn().Exec(ctx, sql, arguments...)
}
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) Query(ctx context.Context, sql string, optionsAndArgs ...interface{}) (pgx.Rows, error) {
return c.Conn().Query(ctx, sql, optionsAndArgs...)
}
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}
func (c *Conn) QueryRow(ctx context.Context, sql string, optionsAndArgs ...interface{}) pgx.Row {
return c.Conn().QueryRow(ctx, sql, optionsAndArgs...)
}
func (c *Conn) Begin() (*pgx.Tx, error) {
return c.res.Value().(*pgx.Conn).Begin()
return c.Conn().Begin()
}
func (c *Conn) Conn() *pgx.Conn {
+8 -10
View File
@@ -68,31 +68,29 @@ func (p *Pool) Exec(ctx context.Context, sql string, arguments ...interface{}) (
return c.Exec(ctx, sql, arguments...)
}
func (p *Pool) Query(ctx context.Context, sql string, optionsAndArgs ...interface{}) (*Rows, error) {
func (p *Pool) Query(ctx context.Context, sql string, optionsAndArgs ...interface{}) (pgx.Rows, error) {
c, err := p.Acquire(ctx)
if err != nil {
return &Rows{err: err}, err
return errRows{err: err}, err
}
rows, err := c.Query(ctx, sql, optionsAndArgs...)
if err == nil {
rows.c = c
} else {
if err != nil {
c.Release()
return errRows{err: err}, err
}
return rows, err
return &poolRows{r: rows, c: c}, nil
}
func (p *Pool) QueryRow(ctx context.Context, sql string, optionsAndArgs ...interface{}) *Row {
func (p *Pool) QueryRow(ctx context.Context, sql string, optionsAndArgs ...interface{}) pgx.Row {
c, err := p.Acquire(ctx)
if err != nil {
return &Row{err: err}
return errRow{err: err}
}
row := c.QueryRow(ctx, sql, optionsAndArgs...)
row.c = c
return row
return &poolRow{r: row, c: c}
}
func (p *Pool) Begin() (*Tx, error) {
+28 -11
View File
@@ -4,13 +4,30 @@ import (
"github.com/jackc/pgx"
)
type Rows struct {
r *pgx.Rows
type errRows struct {
err error
}
func (errRows) Close() {}
func (e errRows) Err() error { return e.err }
func (errRows) FieldDescriptions() []pgx.FieldDescription { return nil }
func (errRows) Next() bool { return false }
func (e errRows) Scan(dest ...interface{}) error { return e.err }
func (e errRows) Values() ([]interface{}, error) { return nil, e.err }
type errRow struct {
err error
}
func (e errRow) Scan(dest ...interface{}) error { return e.err }
type poolRows struct {
r pgx.Rows
c *Conn
err error
}
func (rows *Rows) Close() {
func (rows *poolRows) Close() {
rows.r.Close()
if rows.c != nil {
rows.c.Release()
@@ -18,18 +35,18 @@ func (rows *Rows) Close() {
}
}
func (rows *Rows) Err() error {
func (rows *poolRows) Err() error {
if rows.err != nil {
return rows.err
}
return rows.r.Err()
}
func (rows *Rows) FieldDescriptions() []pgx.FieldDescription {
func (rows *poolRows) FieldDescriptions() []pgx.FieldDescription {
return rows.r.FieldDescriptions()
}
func (rows *Rows) Next() bool {
func (rows *poolRows) Next() bool {
if rows.err != nil {
return false
}
@@ -41,7 +58,7 @@ func (rows *Rows) Next() bool {
return n
}
func (rows *Rows) Scan(dest ...interface{}) error {
func (rows *poolRows) Scan(dest ...interface{}) error {
err := rows.r.Scan(dest...)
if err != nil {
rows.Close()
@@ -49,7 +66,7 @@ func (rows *Rows) Scan(dest ...interface{}) error {
return err
}
func (rows *Rows) Values() ([]interface{}, error) {
func (rows *poolRows) Values() ([]interface{}, error) {
values, err := rows.r.Values()
if err != nil {
rows.Close()
@@ -57,13 +74,13 @@ func (rows *Rows) Values() ([]interface{}, error) {
return values, err
}
type Row struct {
r *pgx.Row
type poolRow struct {
r pgx.Row
c *Conn
err error
}
func (row *Row) Scan(dest ...interface{}) error {
func (row *poolRow) Scan(dest ...interface{}) error {
if row.err != nil {
return row.err
}
+2 -2
View File
@@ -38,10 +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(ctx context.Context, sql string, optionsAndArgs ...interface{}) (*Rows, error) {
func (tx *Tx) Query(ctx context.Context, sql string, optionsAndArgs ...interface{}) (pgx.Rows, error) {
return tx.c.Query(ctx, sql, optionsAndArgs...)
}
func (tx *Tx) QueryRow(ctx context.Context, sql string, optionsAndArgs ...interface{}) *Row {
func (tx *Tx) QueryRow(ctx context.Context, sql string, optionsAndArgs ...interface{}) pgx.Row {
return tx.c.QueryRow(ctx, sql, optionsAndArgs...)
}