diff options
author | Christopher Speller <crspeller@gmail.com> | 2017-02-02 09:32:00 -0500 |
---|---|---|
committer | Harrison Healey <harrisonmhealey@gmail.com> | 2017-02-02 09:32:00 -0500 |
commit | 701d1ab638b23c24877fc41824add66232446676 (patch) | |
tree | ec120c88d38ac9d38d9eabdd3270b52bb6ac9d96 /vendor/github.com/lib/pq/conn_go18.go | |
parent | ca3211bc04f6dea34e8168217182637d1419f998 (diff) | |
download | chat-701d1ab638b23c24877fc41824add66232446676.tar.gz chat-701d1ab638b23c24877fc41824add66232446676.tar.bz2 chat-701d1ab638b23c24877fc41824add66232446676.zip |
Updating server dependancies (#5249)
Diffstat (limited to 'vendor/github.com/lib/pq/conn_go18.go')
-rw-r--r-- | vendor/github.com/lib/pq/conn_go18.go | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/vendor/github.com/lib/pq/conn_go18.go b/vendor/github.com/lib/pq/conn_go18.go new file mode 100644 index 000000000..0aca1d002 --- /dev/null +++ b/vendor/github.com/lib/pq/conn_go18.go @@ -0,0 +1,92 @@ +// +build go1.8 + +package pq + +import ( + "context" + "database/sql/driver" + "errors" +) + +// Implement the "QueryerContext" interface +func (cn *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) { + list := make([]driver.Value, len(args)) + for i, nv := range args { + list[i] = nv.Value + } + var closed chan<- struct{} + if ctx.Done() != nil { + closed = watchCancel(ctx, cn.cancel) + } + r, err := cn.query(query, list) + if err != nil { + return nil, err + } + r.closed = closed + return r, nil +} + +// Implement the "ExecerContext" interface +func (cn *conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) { + list := make([]driver.Value, len(args)) + for i, nv := range args { + list[i] = nv.Value + } + + if ctx.Done() != nil { + closed := watchCancel(ctx, cn.cancel) + defer close(closed) + } + + return cn.Exec(query, list) +} + +// Implement the "ConnBeginTx" interface +func (cn *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) { + if opts.Isolation != 0 { + return nil, errors.New("isolation levels not supported") + } + if opts.ReadOnly { + return nil, errors.New("read-only transactions not supported") + } + tx, err := cn.Begin() + if err != nil { + return nil, err + } + if ctx.Done() != nil { + cn.txnClosed = watchCancel(ctx, cn.cancel) + } + return tx, nil +} + +func watchCancel(ctx context.Context, cancel func()) chan<- struct{} { + closed := make(chan struct{}) + go func() { + select { + case <-ctx.Done(): + cancel() + case <-closed: + } + }() + return closed +} + +func (cn *conn) cancel() { + var err error + can := &conn{} + can.c, err = dial(cn.dialer, cn.opts) + if err != nil { + return + } + can.ssl(cn.opts) + + defer can.errRecover(&err) + + w := can.writeBuf(0) + w.int32(80877102) // cancel request code + w.int32(cn.processID) + w.int32(cn.secretKey) + + can.sendStartupPacket(w) + _ = can.c.Close() +} |