|
@@ -27,12 +27,12 @@ var (
|
|
|
ErrNotSupported = errors.New("pq: Unsupported command")
|
|
ErrNotSupported = errors.New("pq: Unsupported command")
|
|
|
ErrInFailedTransaction = errors.New("pq: Could not complete operation in a failed transaction")
|
|
ErrInFailedTransaction = errors.New("pq: Could not complete operation in a failed transaction")
|
|
|
ErrSSLNotSupported = errors.New("pq: SSL is not enabled on the server")
|
|
ErrSSLNotSupported = errors.New("pq: SSL is not enabled on the server")
|
|
|
- ErrSSLKeyHasWorldPermissions = errors.New("pq: Private key file has group or world access. Permissions should be u=rw (0600) or less.")
|
|
|
|
|
- ErrCouldNotDetectUsername = errors.New("pq: Could not detect default username. Please provide one explicitly.")
|
|
|
|
|
|
|
+ ErrSSLKeyHasWorldPermissions = errors.New("pq: Private key file has group or world access. Permissions should be u=rw (0600) or less")
|
|
|
|
|
+ ErrCouldNotDetectUsername = errors.New("pq: Could not detect default username. Please provide one explicitly")
|
|
|
|
|
|
|
|
errUnexpectedReady = errors.New("unexpected ReadyForQuery")
|
|
errUnexpectedReady = errors.New("unexpected ReadyForQuery")
|
|
|
errNoRowsAffected = errors.New("no RowsAffected available after the empty statement")
|
|
errNoRowsAffected = errors.New("no RowsAffected available after the empty statement")
|
|
|
- errNoLastInsertId = errors.New("no LastInsertId available after the empty statement")
|
|
|
|
|
|
|
+ errNoLastInsertID = errors.New("no LastInsertId available after the empty statement")
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
type Driver struct{}
|
|
type Driver struct{}
|
|
@@ -131,7 +131,7 @@ type conn struct {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Handle driver-side settings in parsed connection string.
|
|
// Handle driver-side settings in parsed connection string.
|
|
|
-func (c *conn) handleDriverSettings(o values) (err error) {
|
|
|
|
|
|
|
+func (cn *conn) handleDriverSettings(o values) (err error) {
|
|
|
boolSetting := func(key string, val *bool) error {
|
|
boolSetting := func(key string, val *bool) error {
|
|
|
if value, ok := o[key]; ok {
|
|
if value, ok := o[key]; ok {
|
|
|
if value == "yes" {
|
|
if value == "yes" {
|
|
@@ -145,18 +145,18 @@ func (c *conn) handleDriverSettings(o values) (err error) {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- err = boolSetting("disable_prepared_binary_result", &c.disablePreparedBinaryResult)
|
|
|
|
|
|
|
+ err = boolSetting("disable_prepared_binary_result", &cn.disablePreparedBinaryResult)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
- err = boolSetting("binary_parameters", &c.binaryParameters)
|
|
|
|
|
|
|
+ err = boolSetting("binary_parameters", &cn.binaryParameters)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (c *conn) handlePgpass(o values) {
|
|
|
|
|
|
|
+func (cn *conn) handlePgpass(o values) {
|
|
|
// if a password was supplied, do not process .pgpass
|
|
// if a password was supplied, do not process .pgpass
|
|
|
if _, ok := o["password"]; ok {
|
|
if _, ok := o["password"]; ok {
|
|
|
return
|
|
return
|
|
@@ -229,10 +229,10 @@ func (c *conn) handlePgpass(o values) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (c *conn) writeBuf(b byte) *writeBuf {
|
|
|
|
|
- c.scratch[0] = b
|
|
|
|
|
|
|
+func (cn *conn) writeBuf(b byte) *writeBuf {
|
|
|
|
|
+ cn.scratch[0] = b
|
|
|
return &writeBuf{
|
|
return &writeBuf{
|
|
|
- buf: c.scratch[:5],
|
|
|
|
|
|
|
+ buf: cn.scratch[:5],
|
|
|
pos: 1,
|
|
pos: 1,
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -310,9 +310,8 @@ func DialOpen(d Dialer, name string) (_ driver.Conn, err error) {
|
|
|
u, err := userCurrent()
|
|
u, err := userCurrent()
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return nil, err
|
|
return nil, err
|
|
|
- } else {
|
|
|
|
|
- o["user"] = u
|
|
|
|
|
}
|
|
}
|
|
|
|
|
+ o["user"] = u
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
cn := &conn{
|
|
cn := &conn{
|
|
@@ -698,7 +697,7 @@ var emptyRows noRows
|
|
|
var _ driver.Result = noRows{}
|
|
var _ driver.Result = noRows{}
|
|
|
|
|
|
|
|
func (noRows) LastInsertId() (int64, error) {
|
|
func (noRows) LastInsertId() (int64, error) {
|
|
|
- return 0, errNoLastInsertId
|
|
|
|
|
|
|
+ return 0, errNoLastInsertID
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (noRows) RowsAffected() (int64, error) {
|
|
func (noRows) RowsAffected() (int64, error) {
|
|
@@ -707,7 +706,7 @@ func (noRows) RowsAffected() (int64, error) {
|
|
|
|
|
|
|
|
// Decides which column formats to use for a prepared statement. The input is
|
|
// Decides which column formats to use for a prepared statement. The input is
|
|
|
// an array of type oids, one element per result column.
|
|
// an array of type oids, one element per result column.
|
|
|
-func decideColumnFormats(colTyps []oid.Oid, forceText bool) (colFmts []format, colFmtData []byte) {
|
|
|
|
|
|
|
+func decideColumnFormats(colTyps []fieldDesc, forceText bool) (colFmts []format, colFmtData []byte) {
|
|
|
if len(colTyps) == 0 {
|
|
if len(colTyps) == 0 {
|
|
|
return nil, colFmtDataAllText
|
|
return nil, colFmtDataAllText
|
|
|
}
|
|
}
|
|
@@ -719,8 +718,8 @@ func decideColumnFormats(colTyps []oid.Oid, forceText bool) (colFmts []format, c
|
|
|
|
|
|
|
|
allBinary := true
|
|
allBinary := true
|
|
|
allText := true
|
|
allText := true
|
|
|
- for i, o := range colTyps {
|
|
|
|
|
- switch o {
|
|
|
|
|
|
|
+ for i, t := range colTyps {
|
|
|
|
|
+ switch t.OID {
|
|
|
// This is the list of types to use binary mode for when receiving them
|
|
// This is the list of types to use binary mode for when receiving them
|
|
|
// through a prepared statement. If a type appears in this list, it
|
|
// through a prepared statement. If a type appears in this list, it
|
|
|
// must also be implemented in binaryDecode in encode.go.
|
|
// must also be implemented in binaryDecode in encode.go.
|
|
@@ -840,16 +839,15 @@ func (cn *conn) query(query string, args []driver.Value) (_ *rows, err error) {
|
|
|
rows.colNames, rows.colFmts, rows.colTyps = cn.readPortalDescribeResponse()
|
|
rows.colNames, rows.colFmts, rows.colTyps = cn.readPortalDescribeResponse()
|
|
|
cn.postExecuteWorkaround()
|
|
cn.postExecuteWorkaround()
|
|
|
return rows, nil
|
|
return rows, nil
|
|
|
- } else {
|
|
|
|
|
- st := cn.prepareTo(query, "")
|
|
|
|
|
- st.exec(args)
|
|
|
|
|
- return &rows{
|
|
|
|
|
- cn: cn,
|
|
|
|
|
- colNames: st.colNames,
|
|
|
|
|
- colTyps: st.colTyps,
|
|
|
|
|
- colFmts: st.colFmts,
|
|
|
|
|
- }, nil
|
|
|
|
|
}
|
|
}
|
|
|
|
|
+ st := cn.prepareTo(query, "")
|
|
|
|
|
+ st.exec(args)
|
|
|
|
|
+ return &rows{
|
|
|
|
|
+ cn: cn,
|
|
|
|
|
+ colNames: st.colNames,
|
|
|
|
|
+ colTyps: st.colTyps,
|
|
|
|
|
+ colFmts: st.colFmts,
|
|
|
|
|
+ }, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Implement the optional "Execer" interface for one-shot queries
|
|
// Implement the optional "Execer" interface for one-shot queries
|
|
@@ -876,17 +874,16 @@ func (cn *conn) Exec(query string, args []driver.Value) (res driver.Result, err
|
|
|
cn.postExecuteWorkaround()
|
|
cn.postExecuteWorkaround()
|
|
|
res, _, err = cn.readExecuteResponse("Execute")
|
|
res, _, err = cn.readExecuteResponse("Execute")
|
|
|
return res, err
|
|
return res, err
|
|
|
- } else {
|
|
|
|
|
- // Use the unnamed statement to defer planning until bind
|
|
|
|
|
- // time, or else value-based selectivity estimates cannot be
|
|
|
|
|
- // used.
|
|
|
|
|
- st := cn.prepareTo(query, "")
|
|
|
|
|
- r, err := st.Exec(args)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- panic(err)
|
|
|
|
|
- }
|
|
|
|
|
- return r, err
|
|
|
|
|
}
|
|
}
|
|
|
|
|
+ // Use the unnamed statement to defer planning until bind
|
|
|
|
|
+ // time, or else value-based selectivity estimates cannot be
|
|
|
|
|
+ // used.
|
|
|
|
|
+ st := cn.prepareTo(query, "")
|
|
|
|
|
+ r, err := st.Exec(args)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ panic(err)
|
|
|
|
|
+ }
|
|
|
|
|
+ return r, err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (cn *conn) send(m *writeBuf) {
|
|
func (cn *conn) send(m *writeBuf) {
|
|
@@ -1147,10 +1144,10 @@ const formatText format = 0
|
|
|
const formatBinary format = 1
|
|
const formatBinary format = 1
|
|
|
|
|
|
|
|
// One result-column format code with the value 1 (i.e. all binary).
|
|
// One result-column format code with the value 1 (i.e. all binary).
|
|
|
-var colFmtDataAllBinary []byte = []byte{0, 1, 0, 1}
|
|
|
|
|
|
|
+var colFmtDataAllBinary = []byte{0, 1, 0, 1}
|
|
|
|
|
|
|
|
// No result-column format codes (i.e. all text).
|
|
// No result-column format codes (i.e. all text).
|
|
|
-var colFmtDataAllText []byte = []byte{0, 0}
|
|
|
|
|
|
|
+var colFmtDataAllText = []byte{0, 0}
|
|
|
|
|
|
|
|
type stmt struct {
|
|
type stmt struct {
|
|
|
cn *conn
|
|
cn *conn
|
|
@@ -1158,7 +1155,7 @@ type stmt struct {
|
|
|
colNames []string
|
|
colNames []string
|
|
|
colFmts []format
|
|
colFmts []format
|
|
|
colFmtData []byte
|
|
colFmtData []byte
|
|
|
- colTyps []oid.Oid
|
|
|
|
|
|
|
+ colTyps []fieldDesc
|
|
|
paramTyps []oid.Oid
|
|
paramTyps []oid.Oid
|
|
|
closed bool
|
|
closed bool
|
|
|
}
|
|
}
|
|
@@ -1321,7 +1318,7 @@ type rows struct {
|
|
|
cn *conn
|
|
cn *conn
|
|
|
finish func()
|
|
finish func()
|
|
|
colNames []string
|
|
colNames []string
|
|
|
- colTyps []oid.Oid
|
|
|
|
|
|
|
+ colTyps []fieldDesc
|
|
|
colFmts []format
|
|
colFmts []format
|
|
|
done bool
|
|
done bool
|
|
|
rb readBuf
|
|
rb readBuf
|
|
@@ -1409,7 +1406,7 @@ func (rs *rows) Next(dest []driver.Value) (err error) {
|
|
|
dest[i] = nil
|
|
dest[i] = nil
|
|
|
continue
|
|
continue
|
|
|
}
|
|
}
|
|
|
- dest[i] = decode(&conn.parameterStatus, rs.rb.next(l), rs.colTyps[i], rs.colFmts[i])
|
|
|
|
|
|
|
+ dest[i] = decode(&conn.parameterStatus, rs.rb.next(l), rs.colTyps[i].OID, rs.colFmts[i])
|
|
|
}
|
|
}
|
|
|
return
|
|
return
|
|
|
case 'T':
|
|
case 'T':
|
|
@@ -1515,7 +1512,7 @@ func (cn *conn) sendBinaryModeQuery(query string, args []driver.Value) {
|
|
|
cn.send(b)
|
|
cn.send(b)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (c *conn) processParameterStatus(r *readBuf) {
|
|
|
|
|
|
|
+func (cn *conn) processParameterStatus(r *readBuf) {
|
|
|
var err error
|
|
var err error
|
|
|
|
|
|
|
|
param := r.string()
|
|
param := r.string()
|
|
@@ -1526,13 +1523,13 @@ func (c *conn) processParameterStatus(r *readBuf) {
|
|
|
var minor int
|
|
var minor int
|
|
|
_, err = fmt.Sscanf(r.string(), "%d.%d.%d", &major1, &major2, &minor)
|
|
_, err = fmt.Sscanf(r.string(), "%d.%d.%d", &major1, &major2, &minor)
|
|
|
if err == nil {
|
|
if err == nil {
|
|
|
- c.parameterStatus.serverVersion = major1*10000 + major2*100 + minor
|
|
|
|
|
|
|
+ cn.parameterStatus.serverVersion = major1*10000 + major2*100 + minor
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
case "TimeZone":
|
|
case "TimeZone":
|
|
|
- c.parameterStatus.currentLocation, err = time.LoadLocation(r.string())
|
|
|
|
|
|
|
+ cn.parameterStatus.currentLocation, err = time.LoadLocation(r.string())
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- c.parameterStatus.currentLocation = nil
|
|
|
|
|
|
|
+ cn.parameterStatus.currentLocation = nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
default:
|
|
@@ -1540,8 +1537,8 @@ func (c *conn) processParameterStatus(r *readBuf) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (c *conn) processReadyForQuery(r *readBuf) {
|
|
|
|
|
- c.txnStatus = transactionStatus(r.byte())
|
|
|
|
|
|
|
+func (cn *conn) processReadyForQuery(r *readBuf) {
|
|
|
|
|
+ cn.txnStatus = transactionStatus(r.byte())
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (cn *conn) readReadyForQuery() {
|
|
func (cn *conn) readReadyForQuery() {
|
|
@@ -1556,9 +1553,9 @@ func (cn *conn) readReadyForQuery() {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (c *conn) processBackendKeyData(r *readBuf) {
|
|
|
|
|
- c.processID = r.int32()
|
|
|
|
|
- c.secretKey = r.int32()
|
|
|
|
|
|
|
+func (cn *conn) processBackendKeyData(r *readBuf) {
|
|
|
|
|
+ cn.processID = r.int32()
|
|
|
|
|
+ cn.secretKey = r.int32()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (cn *conn) readParseResponse() {
|
|
func (cn *conn) readParseResponse() {
|
|
@@ -1576,7 +1573,7 @@ func (cn *conn) readParseResponse() {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (cn *conn) readStatementDescribeResponse() (paramTyps []oid.Oid, colNames []string, colTyps []oid.Oid) {
|
|
|
|
|
|
|
+func (cn *conn) readStatementDescribeResponse() (paramTyps []oid.Oid, colNames []string, colTyps []fieldDesc) {
|
|
|
for {
|
|
for {
|
|
|
t, r := cn.recv1()
|
|
t, r := cn.recv1()
|
|
|
switch t {
|
|
switch t {
|
|
@@ -1602,7 +1599,7 @@ func (cn *conn) readStatementDescribeResponse() (paramTyps []oid.Oid, colNames [
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (cn *conn) readPortalDescribeResponse() (colNames []string, colFmts []format, colTyps []oid.Oid) {
|
|
|
|
|
|
|
+func (cn *conn) readPortalDescribeResponse() (colNames []string, colFmts []format, colTyps []fieldDesc) {
|
|
|
t, r := cn.recv1()
|
|
t, r := cn.recv1()
|
|
|
switch t {
|
|
switch t {
|
|
|
case 'T':
|
|
case 'T':
|
|
@@ -1698,31 +1695,33 @@ func (cn *conn) readExecuteResponse(protocolState string) (res driver.Result, co
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func parseStatementRowDescribe(r *readBuf) (colNames []string, colTyps []oid.Oid) {
|
|
|
|
|
|
|
+func parseStatementRowDescribe(r *readBuf) (colNames []string, colTyps []fieldDesc) {
|
|
|
n := r.int16()
|
|
n := r.int16()
|
|
|
colNames = make([]string, n)
|
|
colNames = make([]string, n)
|
|
|
- colTyps = make([]oid.Oid, n)
|
|
|
|
|
|
|
+ colTyps = make([]fieldDesc, n)
|
|
|
for i := range colNames {
|
|
for i := range colNames {
|
|
|
colNames[i] = r.string()
|
|
colNames[i] = r.string()
|
|
|
r.next(6)
|
|
r.next(6)
|
|
|
- colTyps[i] = r.oid()
|
|
|
|
|
- r.next(6)
|
|
|
|
|
|
|
+ colTyps[i].OID = r.oid()
|
|
|
|
|
+ colTyps[i].Len = r.int16()
|
|
|
|
|
+ colTyps[i].Mod = r.int32()
|
|
|
// format code not known when describing a statement; always 0
|
|
// format code not known when describing a statement; always 0
|
|
|
r.next(2)
|
|
r.next(2)
|
|
|
}
|
|
}
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func parsePortalRowDescribe(r *readBuf) (colNames []string, colFmts []format, colTyps []oid.Oid) {
|
|
|
|
|
|
|
+func parsePortalRowDescribe(r *readBuf) (colNames []string, colFmts []format, colTyps []fieldDesc) {
|
|
|
n := r.int16()
|
|
n := r.int16()
|
|
|
colNames = make([]string, n)
|
|
colNames = make([]string, n)
|
|
|
colFmts = make([]format, n)
|
|
colFmts = make([]format, n)
|
|
|
- colTyps = make([]oid.Oid, n)
|
|
|
|
|
|
|
+ colTyps = make([]fieldDesc, n)
|
|
|
for i := range colNames {
|
|
for i := range colNames {
|
|
|
colNames[i] = r.string()
|
|
colNames[i] = r.string()
|
|
|
r.next(6)
|
|
r.next(6)
|
|
|
- colTyps[i] = r.oid()
|
|
|
|
|
- r.next(6)
|
|
|
|
|
|
|
+ colTyps[i].OID = r.oid()
|
|
|
|
|
+ colTyps[i].Len = r.int16()
|
|
|
|
|
+ colTyps[i].Mod = r.int32()
|
|
|
colFmts[i] = format(r.int16())
|
|
colFmts[i] = format(r.int16())
|
|
|
}
|
|
}
|
|
|
return
|
|
return
|