mirror of
https://github.com/documize/community.git
synced 2025-07-21 14:19:43 +02:00
Sync with Community
This commit is contained in:
parent
df8f650319
commit
989b7cd62c
123 changed files with 5054 additions and 2015 deletions
194
vendor/github.com/lib/pq/conn.go
generated
vendored
194
vendor/github.com/lib/pq/conn.go
generated
vendored
|
@ -18,6 +18,7 @@ import (
|
|||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
"unicode"
|
||||
|
||||
|
@ -38,13 +39,18 @@ var (
|
|||
errNoLastInsertID = errors.New("no LastInsertId available after the empty statement")
|
||||
)
|
||||
|
||||
// Compile time validation that our types implement the expected interfaces
|
||||
var (
|
||||
_ driver.Driver = Driver{}
|
||||
)
|
||||
|
||||
// Driver is the Postgres database driver.
|
||||
type Driver struct{}
|
||||
|
||||
// Open opens a new connection to the database. name is a connection string.
|
||||
// Most users should only use it through database/sql package from the standard
|
||||
// library.
|
||||
func (d *Driver) Open(name string) (driver.Conn, error) {
|
||||
func (d Driver) Open(name string) (driver.Conn, error) {
|
||||
return Open(name)
|
||||
}
|
||||
|
||||
|
@ -136,7 +142,7 @@ type conn struct {
|
|||
|
||||
// If true, this connection is bad and all public-facing functions should
|
||||
// return ErrBadConn.
|
||||
bad bool
|
||||
bad *atomic.Value
|
||||
|
||||
// If set, this connection should never use the binary format when
|
||||
// receiving query results from prepared statements. Only provided for
|
||||
|
@ -155,6 +161,9 @@ type conn struct {
|
|||
|
||||
// If not nil, notifications will be synchronously sent here
|
||||
notificationHandler func(*Notification)
|
||||
|
||||
// GSSAPI context
|
||||
gss GSS
|
||||
}
|
||||
|
||||
// Handle driver-side settings in parsed connection string.
|
||||
|
@ -289,11 +298,20 @@ func (c *Connector) open(ctx context.Context) (cn *conn, err error) {
|
|||
// the user.
|
||||
defer errRecoverNoErrBadConn(&err)
|
||||
|
||||
o := c.opts
|
||||
// Create a new values map (copy). This makes it so maps in different
|
||||
// connections do not reference the same underlying data structure, so it
|
||||
// is safe for multiple connections to concurrently write to their opts.
|
||||
o := make(values)
|
||||
for k, v := range c.opts {
|
||||
o[k] = v
|
||||
}
|
||||
|
||||
bad := &atomic.Value{}
|
||||
bad.Store(false)
|
||||
cn = &conn{
|
||||
opts: o,
|
||||
dialer: c.dialer,
|
||||
bad: bad,
|
||||
}
|
||||
err = cn.handleDriverSettings(o)
|
||||
if err != nil {
|
||||
|
@ -335,10 +353,6 @@ func (c *Connector) open(ctx context.Context) (cn *conn, err error) {
|
|||
|
||||
func dial(ctx context.Context, d Dialer, o values) (net.Conn, error) {
|
||||
network, address := network(o)
|
||||
// SSL is not necessary or supported over UNIX domain sockets
|
||||
if network == "unix" {
|
||||
o["sslmode"] = "disable"
|
||||
}
|
||||
|
||||
// Zero or not specified means wait indefinitely.
|
||||
if timeout, ok := o["connect_timeout"]; ok && timeout != "0" {
|
||||
|
@ -502,9 +516,22 @@ func (cn *conn) isInTransaction() bool {
|
|||
cn.txnStatus == txnStatusInFailedTransaction
|
||||
}
|
||||
|
||||
func (cn *conn) setBad() {
|
||||
if cn.bad != nil {
|
||||
cn.bad.Store(true)
|
||||
}
|
||||
}
|
||||
|
||||
func (cn *conn) getBad() bool {
|
||||
if cn.bad != nil {
|
||||
return cn.bad.Load().(bool)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (cn *conn) checkIsInTransaction(intxn bool) {
|
||||
if cn.isInTransaction() != intxn {
|
||||
cn.bad = true
|
||||
cn.setBad()
|
||||
errorf("unexpected transaction status %v", cn.txnStatus)
|
||||
}
|
||||
}
|
||||
|
@ -514,7 +541,7 @@ func (cn *conn) Begin() (_ driver.Tx, err error) {
|
|||
}
|
||||
|
||||
func (cn *conn) begin(mode string) (_ driver.Tx, err error) {
|
||||
if cn.bad {
|
||||
if cn.getBad() {
|
||||
return nil, driver.ErrBadConn
|
||||
}
|
||||
defer cn.errRecover(&err)
|
||||
|
@ -525,11 +552,11 @@ func (cn *conn) begin(mode string) (_ driver.Tx, err error) {
|
|||
return nil, err
|
||||
}
|
||||
if commandTag != "BEGIN" {
|
||||
cn.bad = true
|
||||
cn.setBad()
|
||||
return nil, fmt.Errorf("unexpected command tag %s", commandTag)
|
||||
}
|
||||
if cn.txnStatus != txnStatusIdleInTransaction {
|
||||
cn.bad = true
|
||||
cn.setBad()
|
||||
return nil, fmt.Errorf("unexpected transaction status %v", cn.txnStatus)
|
||||
}
|
||||
return cn, nil
|
||||
|
@ -543,7 +570,7 @@ func (cn *conn) closeTxn() {
|
|||
|
||||
func (cn *conn) Commit() (err error) {
|
||||
defer cn.closeTxn()
|
||||
if cn.bad {
|
||||
if cn.getBad() {
|
||||
return driver.ErrBadConn
|
||||
}
|
||||
defer cn.errRecover(&err)
|
||||
|
@ -565,12 +592,12 @@ func (cn *conn) Commit() (err error) {
|
|||
_, commandTag, err := cn.simpleExec("COMMIT")
|
||||
if err != nil {
|
||||
if cn.isInTransaction() {
|
||||
cn.bad = true
|
||||
cn.setBad()
|
||||
}
|
||||
return err
|
||||
}
|
||||
if commandTag != "COMMIT" {
|
||||
cn.bad = true
|
||||
cn.setBad()
|
||||
return fmt.Errorf("unexpected command tag %s", commandTag)
|
||||
}
|
||||
cn.checkIsInTransaction(false)
|
||||
|
@ -579,7 +606,7 @@ func (cn *conn) Commit() (err error) {
|
|||
|
||||
func (cn *conn) Rollback() (err error) {
|
||||
defer cn.closeTxn()
|
||||
if cn.bad {
|
||||
if cn.getBad() {
|
||||
return driver.ErrBadConn
|
||||
}
|
||||
defer cn.errRecover(&err)
|
||||
|
@ -591,7 +618,7 @@ func (cn *conn) rollback() (err error) {
|
|||
_, commandTag, err := cn.simpleExec("ROLLBACK")
|
||||
if err != nil {
|
||||
if cn.isInTransaction() {
|
||||
cn.bad = true
|
||||
cn.setBad()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
@ -631,7 +658,7 @@ func (cn *conn) simpleExec(q string) (res driver.Result, commandTag string, err
|
|||
case 'T', 'D':
|
||||
// ignore any results
|
||||
default:
|
||||
cn.bad = true
|
||||
cn.setBad()
|
||||
errorf("unknown response for simple query: %q", t)
|
||||
}
|
||||
}
|
||||
|
@ -653,7 +680,7 @@ func (cn *conn) simpleQuery(q string) (res *rows, err error) {
|
|||
// the user can close, though, to avoid connections from being
|
||||
// leaked. A "rows" with done=true works fine for that purpose.
|
||||
if err != nil {
|
||||
cn.bad = true
|
||||
cn.setBad()
|
||||
errorf("unexpected message %q in simple query execution", t)
|
||||
}
|
||||
if res == nil {
|
||||
|
@ -664,8 +691,11 @@ func (cn *conn) simpleQuery(q string) (res *rows, err error) {
|
|||
// Set the result and tag to the last command complete if there wasn't a
|
||||
// query already run. Although queries usually return from here and cede
|
||||
// control to Next, a query with zero results does not.
|
||||
if t == 'C' && res.colNames == nil {
|
||||
if t == 'C' {
|
||||
res.result, res.tag = cn.parseComplete(r.string())
|
||||
if res.colNames != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
res.done = true
|
||||
case 'Z':
|
||||
|
@ -677,7 +707,7 @@ func (cn *conn) simpleQuery(q string) (res *rows, err error) {
|
|||
err = parseError(r)
|
||||
case 'D':
|
||||
if res == nil {
|
||||
cn.bad = true
|
||||
cn.setBad()
|
||||
errorf("unexpected DataRow in simple query execution")
|
||||
}
|
||||
// the query didn't fail; kick off to Next
|
||||
|
@ -692,7 +722,7 @@ func (cn *conn) simpleQuery(q string) (res *rows, err error) {
|
|||
// To work around a bug in QueryRow in Go 1.2 and earlier, wait
|
||||
// until the first DataRow has been received.
|
||||
default:
|
||||
cn.bad = true
|
||||
cn.setBad()
|
||||
errorf("unknown response for simple query: %q", t)
|
||||
}
|
||||
}
|
||||
|
@ -785,7 +815,7 @@ func (cn *conn) prepareTo(q, stmtName string) *stmt {
|
|||
}
|
||||
|
||||
func (cn *conn) Prepare(q string) (_ driver.Stmt, err error) {
|
||||
if cn.bad {
|
||||
if cn.getBad() {
|
||||
return nil, driver.ErrBadConn
|
||||
}
|
||||
defer cn.errRecover(&err)
|
||||
|
@ -824,7 +854,7 @@ func (cn *conn) Query(query string, args []driver.Value) (driver.Rows, error) {
|
|||
}
|
||||
|
||||
func (cn *conn) query(query string, args []driver.Value) (_ *rows, err error) {
|
||||
if cn.bad {
|
||||
if cn.getBad() {
|
||||
return nil, driver.ErrBadConn
|
||||
}
|
||||
if cn.inCopy {
|
||||
|
@ -858,7 +888,7 @@ func (cn *conn) query(query string, args []driver.Value) (_ *rows, err error) {
|
|||
|
||||
// Implement the optional "Execer" interface for one-shot queries
|
||||
func (cn *conn) Exec(query string, args []driver.Value) (res driver.Result, err error) {
|
||||
if cn.bad {
|
||||
if cn.getBad() {
|
||||
return nil, driver.ErrBadConn
|
||||
}
|
||||
defer cn.errRecover(&err)
|
||||
|
@ -892,9 +922,20 @@ func (cn *conn) Exec(query string, args []driver.Value) (res driver.Result, err
|
|||
return r, err
|
||||
}
|
||||
|
||||
type safeRetryError struct {
|
||||
Err error
|
||||
}
|
||||
|
||||
func (se *safeRetryError) Error() string {
|
||||
return se.Err.Error()
|
||||
}
|
||||
|
||||
func (cn *conn) send(m *writeBuf) {
|
||||
_, err := cn.c.Write(m.wrap())
|
||||
n, err := cn.c.Write(m.wrap())
|
||||
if err != nil {
|
||||
if n == 0 {
|
||||
err = &safeRetryError{Err: err}
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
@ -919,7 +960,7 @@ func (cn *conn) sendSimpleMessage(typ byte) (err error) {
|
|||
// the message yourself.
|
||||
func (cn *conn) saveMessage(typ byte, buf *readBuf) {
|
||||
if cn.saveMessageType != 0 {
|
||||
cn.bad = true
|
||||
cn.setBad()
|
||||
errorf("unexpected saveMessageType %d", cn.saveMessageType)
|
||||
}
|
||||
cn.saveMessageType = typ
|
||||
|
@ -1065,7 +1106,7 @@ func isDriverSetting(key string) bool {
|
|||
return true
|
||||
case "password":
|
||||
return true
|
||||
case "sslmode", "sslcert", "sslkey", "sslrootcert":
|
||||
case "sslmode", "sslcert", "sslkey", "sslrootcert", "sslinline":
|
||||
return true
|
||||
case "fallback_application_name":
|
||||
return true
|
||||
|
@ -1075,7 +1116,10 @@ func isDriverSetting(key string) bool {
|
|||
return true
|
||||
case "binary_parameters":
|
||||
return true
|
||||
|
||||
case "krbsrvname":
|
||||
return true
|
||||
case "krbspn":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
|
@ -1155,6 +1199,59 @@ func (cn *conn) auth(r *readBuf, o values) {
|
|||
if r.int32() != 0 {
|
||||
errorf("unexpected authentication response: %q", t)
|
||||
}
|
||||
case 7: // GSSAPI, startup
|
||||
if newGss == nil {
|
||||
errorf("kerberos error: no GSSAPI provider registered (import github.com/lib/pq/auth/kerberos if you need Kerberos support)")
|
||||
}
|
||||
cli, err := newGss()
|
||||
if err != nil {
|
||||
errorf("kerberos error: %s", err.Error())
|
||||
}
|
||||
|
||||
var token []byte
|
||||
|
||||
if spn, ok := o["krbspn"]; ok {
|
||||
// Use the supplied SPN if provided..
|
||||
token, err = cli.GetInitTokenFromSpn(spn)
|
||||
} else {
|
||||
// Allow the kerberos service name to be overridden
|
||||
service := "postgres"
|
||||
if val, ok := o["krbsrvname"]; ok {
|
||||
service = val
|
||||
}
|
||||
|
||||
token, err = cli.GetInitToken(o["host"], service)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
errorf("failed to get Kerberos ticket: %q", err)
|
||||
}
|
||||
|
||||
w := cn.writeBuf('p')
|
||||
w.bytes(token)
|
||||
cn.send(w)
|
||||
|
||||
// Store for GSSAPI continue message
|
||||
cn.gss = cli
|
||||
|
||||
case 8: // GSSAPI continue
|
||||
|
||||
if cn.gss == nil {
|
||||
errorf("GSSAPI protocol error")
|
||||
}
|
||||
|
||||
b := []byte(*r)
|
||||
|
||||
done, tokOut, err := cn.gss.Continue(b)
|
||||
if err == nil && !done {
|
||||
w := cn.writeBuf('p')
|
||||
w.bytes(tokOut)
|
||||
cn.send(w)
|
||||
}
|
||||
|
||||
// Errors fall through and read the more detailed message
|
||||
// from the server..
|
||||
|
||||
case 10:
|
||||
sc := scram.NewClient(sha256.New, o["user"], o["password"])
|
||||
sc.Step(nil)
|
||||
|
@ -1233,7 +1330,7 @@ func (st *stmt) Close() (err error) {
|
|||
if st.closed {
|
||||
return nil
|
||||
}
|
||||
if st.cn.bad {
|
||||
if st.cn.getBad() {
|
||||
return driver.ErrBadConn
|
||||
}
|
||||
defer st.cn.errRecover(&err)
|
||||
|
@ -1247,14 +1344,14 @@ func (st *stmt) Close() (err error) {
|
|||
|
||||
t, _ := st.cn.recv1()
|
||||
if t != '3' {
|
||||
st.cn.bad = true
|
||||
st.cn.setBad()
|
||||
errorf("unexpected close response: %q", t)
|
||||
}
|
||||
st.closed = true
|
||||
|
||||
t, r := st.cn.recv1()
|
||||
if t != 'Z' {
|
||||
st.cn.bad = true
|
||||
st.cn.setBad()
|
||||
errorf("expected ready for query, but got: %q", t)
|
||||
}
|
||||
st.cn.processReadyForQuery(r)
|
||||
|
@ -1263,7 +1360,7 @@ func (st *stmt) Close() (err error) {
|
|||
}
|
||||
|
||||
func (st *stmt) Query(v []driver.Value) (r driver.Rows, err error) {
|
||||
if st.cn.bad {
|
||||
if st.cn.getBad() {
|
||||
return nil, driver.ErrBadConn
|
||||
}
|
||||
defer st.cn.errRecover(&err)
|
||||
|
@ -1276,7 +1373,7 @@ func (st *stmt) Query(v []driver.Value) (r driver.Rows, err error) {
|
|||
}
|
||||
|
||||
func (st *stmt) Exec(v []driver.Value) (res driver.Result, err error) {
|
||||
if st.cn.bad {
|
||||
if st.cn.getBad() {
|
||||
return nil, driver.ErrBadConn
|
||||
}
|
||||
defer st.cn.errRecover(&err)
|
||||
|
@ -1363,7 +1460,7 @@ func (cn *conn) parseComplete(commandTag string) (driver.Result, string) {
|
|||
if affectedRows == nil && strings.HasPrefix(commandTag, "INSERT ") {
|
||||
parts := strings.Split(commandTag, " ")
|
||||
if len(parts) != 3 {
|
||||
cn.bad = true
|
||||
cn.setBad()
|
||||
errorf("unexpected INSERT command tag %s", commandTag)
|
||||
}
|
||||
affectedRows = &parts[len(parts)-1]
|
||||
|
@ -1375,7 +1472,7 @@ func (cn *conn) parseComplete(commandTag string) (driver.Result, string) {
|
|||
}
|
||||
n, err := strconv.ParseInt(*affectedRows, 10, 64)
|
||||
if err != nil {
|
||||
cn.bad = true
|
||||
cn.setBad()
|
||||
errorf("could not parse commandTag: %s", err)
|
||||
}
|
||||
return driver.RowsAffected(n), commandTag
|
||||
|
@ -1442,7 +1539,7 @@ func (rs *rows) Next(dest []driver.Value) (err error) {
|
|||
}
|
||||
|
||||
conn := rs.cn
|
||||
if conn.bad {
|
||||
if conn.getBad() {
|
||||
return driver.ErrBadConn
|
||||
}
|
||||
defer conn.errRecover(&err)
|
||||
|
@ -1467,7 +1564,7 @@ func (rs *rows) Next(dest []driver.Value) (err error) {
|
|||
case 'D':
|
||||
n := rs.rb.int16()
|
||||
if err != nil {
|
||||
conn.bad = true
|
||||
conn.setBad()
|
||||
errorf("unexpected DataRow after error %s", err)
|
||||
}
|
||||
if n < len(dest) {
|
||||
|
@ -1634,10 +1731,9 @@ func (cn *conn) processParameterStatus(r *readBuf) {
|
|||
case "server_version":
|
||||
var major1 int
|
||||
var major2 int
|
||||
var minor int
|
||||
_, err = fmt.Sscanf(r.string(), "%d.%d.%d", &major1, &major2, &minor)
|
||||
_, err = fmt.Sscanf(r.string(), "%d.%d", &major1, &major2)
|
||||
if err == nil {
|
||||
cn.parameterStatus.serverVersion = major1*10000 + major2*100 + minor
|
||||
cn.parameterStatus.serverVersion = major1*10000 + major2*100
|
||||
}
|
||||
|
||||
case "TimeZone":
|
||||
|
@ -1662,7 +1758,7 @@ func (cn *conn) readReadyForQuery() {
|
|||
cn.processReadyForQuery(r)
|
||||
return
|
||||
default:
|
||||
cn.bad = true
|
||||
cn.setBad()
|
||||
errorf("unexpected message %q; expected ReadyForQuery", t)
|
||||
}
|
||||
}
|
||||
|
@ -1682,7 +1778,7 @@ func (cn *conn) readParseResponse() {
|
|||
cn.readReadyForQuery()
|
||||
panic(err)
|
||||
default:
|
||||
cn.bad = true
|
||||
cn.setBad()
|
||||
errorf("unexpected Parse response %q", t)
|
||||
}
|
||||
}
|
||||
|
@ -1707,7 +1803,7 @@ func (cn *conn) readStatementDescribeResponse() (paramTyps []oid.Oid, colNames [
|
|||
cn.readReadyForQuery()
|
||||
panic(err)
|
||||
default:
|
||||
cn.bad = true
|
||||
cn.setBad()
|
||||
errorf("unexpected Describe statement response %q", t)
|
||||
}
|
||||
}
|
||||
|
@ -1725,7 +1821,7 @@ func (cn *conn) readPortalDescribeResponse() rowsHeader {
|
|||
cn.readReadyForQuery()
|
||||
panic(err)
|
||||
default:
|
||||
cn.bad = true
|
||||
cn.setBad()
|
||||
errorf("unexpected Describe response %q", t)
|
||||
}
|
||||
panic("not reached")
|
||||
|
@ -1741,7 +1837,7 @@ func (cn *conn) readBindResponse() {
|
|||
cn.readReadyForQuery()
|
||||
panic(err)
|
||||
default:
|
||||
cn.bad = true
|
||||
cn.setBad()
|
||||
errorf("unexpected Bind response %q", t)
|
||||
}
|
||||
}
|
||||
|
@ -1768,7 +1864,7 @@ func (cn *conn) postExecuteWorkaround() {
|
|||
cn.saveMessage(t, r)
|
||||
return
|
||||
default:
|
||||
cn.bad = true
|
||||
cn.setBad()
|
||||
errorf("unexpected message during extended query execution: %q", t)
|
||||
}
|
||||
}
|
||||
|
@ -1781,7 +1877,7 @@ func (cn *conn) readExecuteResponse(protocolState string) (res driver.Result, co
|
|||
switch t {
|
||||
case 'C':
|
||||
if err != nil {
|
||||
cn.bad = true
|
||||
cn.setBad()
|
||||
errorf("unexpected CommandComplete after error %s", err)
|
||||
}
|
||||
res, commandTag = cn.parseComplete(r.string())
|
||||
|
@ -1795,7 +1891,7 @@ func (cn *conn) readExecuteResponse(protocolState string) (res driver.Result, co
|
|||
err = parseError(r)
|
||||
case 'T', 'D', 'I':
|
||||
if err != nil {
|
||||
cn.bad = true
|
||||
cn.setBad()
|
||||
errorf("unexpected %q after error %s", t, err)
|
||||
}
|
||||
if t == 'I' {
|
||||
|
@ -1803,7 +1899,7 @@ func (cn *conn) readExecuteResponse(protocolState string) (res driver.Result, co
|
|||
}
|
||||
// ignore any results
|
||||
default:
|
||||
cn.bad = true
|
||||
cn.setBad()
|
||||
errorf("unknown %s response: %q", protocolState, t)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue