Add ctx to PrepareEx
Remove PrepareExContext
This commit is contained in:
@@ -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
|
// name and sql arguments. This allows a code path to Prepare and Query/Exec without
|
||||||
// concern for if the statement has already been prepared.
|
// concern for if the statement has already been prepared.
|
||||||
func (c *Conn) Prepare(name, sql string) (ps *PreparedStatement, err error) {
|
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
|
// 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
|
// 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
|
// name and sql arguments. This allows a code path to PrepareEx and Query/Exec without
|
||||||
// concern for if the statement has already been prepared.
|
// concern for if the statement has already been prepared.
|
||||||
func (c *Conn) PrepareEx(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) {
|
||||||
return c.PrepareExContext(context.Background(), name, sql, opts)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Conn) PrepareExContext(ctx context.Context, name, sql string, opts *PrepareExOptions) (ps *PreparedStatement, err error) {
|
|
||||||
err = c.waitForPreviousCancelQuery(ctx)
|
err = c.waitForPreviousCancelQuery(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -1455,7 +1451,7 @@ func (c *Conn) ExecEx(ctx context.Context, sql string, options *QueryExOptions,
|
|||||||
ps, ok := c.preparedStatements[sql]
|
ps, ok := c.preparedStatements[sql]
|
||||||
if !ok {
|
if !ok {
|
||||||
var err error
|
var err error
|
||||||
ps, err = c.PrepareExContext(ctx, "", sql, nil)
|
ps, err = c.PrepareEx(ctx, "", sql, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-4
@@ -425,7 +425,7 @@ func (p *ConnPool) Begin() (*Tx, error) {
|
|||||||
// the same name and sql arguments. This allows a code path to Prepare and
|
// 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.
|
// Query/Exec/PrepareEx without concern for if the statement has already been prepared.
|
||||||
func (p *ConnPool) Prepare(name, sql string) (*PreparedStatement, error) {
|
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
|
// 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
|
// 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
|
// 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.
|
// 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()
|
p.cond.L.Lock()
|
||||||
defer p.cond.L.Unlock()
|
defer p.cond.L.Unlock()
|
||||||
|
|
||||||
@@ -461,13 +461,13 @@ func (p *ConnPool) PrepareEx(name, sql string, opts *PrepareExOptions) (*Prepare
|
|||||||
return ps, nil
|
return ps, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ps, err := c.PrepareEx(name, sql, opts)
|
ps, err := c.PrepareEx(ctx, name, sql, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range p.availableConnections {
|
for _, c := range p.availableConnections {
|
||||||
_, err := c.PrepareEx(name, sql, opts)
|
_, err := c.PrepareEx(ctx, name, sql, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -1326,7 +1326,7 @@ func TestPrepareEx(t *testing.T) {
|
|||||||
conn := mustConnect(t, *defaultConnConfig)
|
conn := mustConnect(t, *defaultConnConfig)
|
||||||
defer closeConn(t, conn)
|
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 {
|
if err != nil {
|
||||||
t.Errorf("Unable to prepare statement: %v", err)
|
t.Errorf("Unable to prepare statement: %v", err)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -386,7 +386,7 @@ func (c *Conn) QueryEx(ctx context.Context, sql string, options *QueryExOptions,
|
|||||||
ps, ok := c.preparedStatements[sql]
|
ps, ok := c.preparedStatements[sql]
|
||||||
if !ok {
|
if !ok {
|
||||||
var err error
|
var err error
|
||||||
ps, err = c.PrepareExContext(ctx, "", sql, nil)
|
ps, err = c.PrepareEx(ctx, "", sql, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rows.fatal(err)
|
rows.fatal(err)
|
||||||
return rows, rows.err
|
return rows, rows.err
|
||||||
|
|||||||
+2
-2
@@ -220,7 +220,7 @@ func (c *Conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, e
|
|||||||
name := fmt.Sprintf("pgx_%d", c.psCount)
|
name := fmt.Sprintf("pgx_%d", c.psCount)
|
||||||
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -311,7 +311,7 @@ func (c *Conn) QueryContext(ctx context.Context, query string, argsV []driver.Na
|
|||||||
return nil, driver.ErrBadConn
|
return nil, driver.ErrBadConn
|
||||||
}
|
}
|
||||||
|
|
||||||
ps, err := c.conn.PrepareExContext(ctx, "", query, nil)
|
ps, err := c.conn.PrepareEx(ctx, "", query, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -174,16 +174,16 @@ func (tx *Tx) Exec(sql string, arguments ...interface{}) (commandTag CommandTag,
|
|||||||
|
|
||||||
// Prepare delegates to the underlying *Conn
|
// Prepare delegates to the underlying *Conn
|
||||||
func (tx *Tx) Prepare(name, sql string) (*PreparedStatement, error) {
|
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
|
// 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 {
|
if tx.status != TxStatusInProgress {
|
||||||
return nil, ErrTxClosed
|
return nil, ErrTxClosed
|
||||||
}
|
}
|
||||||
|
|
||||||
return tx.conn.PrepareEx(name, sql, opts)
|
return tx.conn.PrepareEx(ctx, name, sql, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Query delegates to the underlying *Conn
|
// Query delegates to the underlying *Conn
|
||||||
|
|||||||
Reference in New Issue
Block a user