From 8a7165dd988dcc83f3ce76babdec23c59e44e502 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sat, 20 May 2017 18:03:59 -0500 Subject: [PATCH] Add ctx to PrepareEx Remove PrepareExContext --- conn.go | 10 +++------- conn_pool.go | 8 ++++---- conn_test.go | 2 +- query.go | 2 +- stdlib/sql.go | 4 ++-- tx.go | 6 +++--- v3.md | 2 ++ 7 files changed, 16 insertions(+), 18 deletions(-) diff --git a/conn.go b/conn.go index 20844e57..04299de7 100644 --- a/conn.go +++ b/conn.go @@ -710,7 +710,7 @@ func configSSL(sslmode string, cc *ConnConfig) error { // name and sql arguments. This allows a code path to Prepare and Query/Exec without // concern for if the statement has already been prepared. func (c *Conn) Prepare(name, sql string) (ps *PreparedStatement, err error) { - return c.PrepareEx(name, sql, nil) + return c.PrepareEx(context.Background(), name, sql, nil) } // PrepareEx creates a prepared statement with name and sql. sql can contain placeholders @@ -720,11 +720,7 @@ func (c *Conn) Prepare(name, sql string) (ps *PreparedStatement, err error) { // PrepareEx is idempotent; i.e. it is safe to call PrepareEx multiple times with the same // name and sql arguments. This allows a code path to PrepareEx and Query/Exec without // concern for if the statement has already been prepared. -func (c *Conn) PrepareEx(name, sql string, opts *PrepareExOptions) (ps *PreparedStatement, err error) { - return c.PrepareExContext(context.Background(), name, sql, opts) -} - -func (c *Conn) PrepareExContext(ctx context.Context, name, sql string, opts *PrepareExOptions) (ps *PreparedStatement, err error) { +func (c *Conn) PrepareEx(ctx context.Context, name, sql string, opts *PrepareExOptions) (ps *PreparedStatement, err error) { err = c.waitForPreviousCancelQuery(ctx) if err != nil { return nil, err @@ -1455,7 +1451,7 @@ func (c *Conn) ExecEx(ctx context.Context, sql string, options *QueryExOptions, ps, ok := c.preparedStatements[sql] if !ok { var err error - ps, err = c.PrepareExContext(ctx, "", sql, nil) + ps, err = c.PrepareEx(ctx, "", sql, nil) if err != nil { return "", err } diff --git a/conn_pool.go b/conn_pool.go index 632692de..42200b85 100644 --- a/conn_pool.go +++ b/conn_pool.go @@ -425,7 +425,7 @@ func (p *ConnPool) Begin() (*Tx, error) { // the same name and sql arguments. This allows a code path to Prepare and // Query/Exec/PrepareEx without concern for if the statement has already been prepared. func (p *ConnPool) Prepare(name, sql string) (*PreparedStatement, error) { - return p.PrepareEx(name, sql, nil) + return p.PrepareEx(context.Background(), name, sql, nil) } // PrepareEx creates a prepared statement on a connection in the pool to test the @@ -439,7 +439,7 @@ func (p *ConnPool) Prepare(name, sql string) (*PreparedStatement, error) { // PrepareEx is idempotent; i.e. it is safe to call PrepareEx multiple times with the same // name and sql arguments. This allows a code path to PrepareEx and Query/Exec/Prepare without // concern for if the statement has already been prepared. -func (p *ConnPool) PrepareEx(name, sql string, opts *PrepareExOptions) (*PreparedStatement, error) { +func (p *ConnPool) PrepareEx(ctx context.Context, name, sql string, opts *PrepareExOptions) (*PreparedStatement, error) { p.cond.L.Lock() defer p.cond.L.Unlock() @@ -461,13 +461,13 @@ func (p *ConnPool) PrepareEx(name, sql string, opts *PrepareExOptions) (*Prepare return ps, nil } - ps, err := c.PrepareEx(name, sql, opts) + ps, err := c.PrepareEx(ctx, name, sql, opts) if err != nil { return nil, err } for _, c := range p.availableConnections { - _, err := c.PrepareEx(name, sql, opts) + _, err := c.PrepareEx(ctx, name, sql, opts) if err != nil { return nil, err } diff --git a/conn_test.go b/conn_test.go index f887e030..acee1b49 100644 --- a/conn_test.go +++ b/conn_test.go @@ -1326,7 +1326,7 @@ func TestPrepareEx(t *testing.T) { conn := mustConnect(t, *defaultConnConfig) defer closeConn(t, conn) - _, err := conn.PrepareEx("test", "select $1", &pgx.PrepareExOptions{ParameterOids: []pgtype.Oid{pgtype.TextOid}}) + _, err := conn.PrepareEx(context.Background(), "test", "select $1", &pgx.PrepareExOptions{ParameterOids: []pgtype.Oid{pgtype.TextOid}}) if err != nil { t.Errorf("Unable to prepare statement: %v", err) return diff --git a/query.go b/query.go index 44bf004a..10eda1bc 100644 --- a/query.go +++ b/query.go @@ -386,7 +386,7 @@ func (c *Conn) QueryEx(ctx context.Context, sql string, options *QueryExOptions, ps, ok := c.preparedStatements[sql] if !ok { var err error - ps, err = c.PrepareExContext(ctx, "", sql, nil) + ps, err = c.PrepareEx(ctx, "", sql, nil) if err != nil { rows.fatal(err) return rows, rows.err diff --git a/stdlib/sql.go b/stdlib/sql.go index a0aa6975..aa45dd40 100644 --- a/stdlib/sql.go +++ b/stdlib/sql.go @@ -220,7 +220,7 @@ func (c *Conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, e name := fmt.Sprintf("pgx_%d", c.psCount) c.psCount++ - ps, err := c.conn.PrepareExContext(ctx, name, query, nil) + ps, err := c.conn.PrepareEx(ctx, name, query, nil) if err != nil { return nil, err } @@ -311,7 +311,7 @@ func (c *Conn) QueryContext(ctx context.Context, query string, argsV []driver.Na return nil, driver.ErrBadConn } - ps, err := c.conn.PrepareExContext(ctx, "", query, nil) + ps, err := c.conn.PrepareEx(ctx, "", query, nil) if err != nil { return nil, err } diff --git a/tx.go b/tx.go index f5309468..07cae4ba 100644 --- a/tx.go +++ b/tx.go @@ -174,16 +174,16 @@ func (tx *Tx) Exec(sql string, arguments ...interface{}) (commandTag CommandTag, // Prepare delegates to the underlying *Conn func (tx *Tx) Prepare(name, sql string) (*PreparedStatement, error) { - return tx.PrepareEx(name, sql, nil) + return tx.PrepareEx(context.Background(), name, sql, nil) } // PrepareEx delegates to the underlying *Conn -func (tx *Tx) PrepareEx(name, sql string, opts *PrepareExOptions) (*PreparedStatement, error) { +func (tx *Tx) PrepareEx(ctx context.Context, name, sql string, opts *PrepareExOptions) (*PreparedStatement, error) { if tx.status != TxStatusInProgress { return nil, ErrTxClosed } - return tx.conn.PrepareEx(name, sql, opts) + return tx.conn.PrepareEx(ctx, name, sql, opts) } // Query delegates to the underlying *Conn diff --git a/v3.md b/v3.md index 624c25eb..33a27d2d 100644 --- a/v3.md +++ b/v3.md @@ -48,6 +48,8 @@ Removed Tx.AfterClose() Removed Tx.Conn() +Added ctx parameter to (Conn/Tx/ConnPool).PrepareEx + ## TODO / Possible / Investigate Organize errors better