mirror of https://github.com/jackc/pgx.git
Store runtime parameters
parent
577f9707f7
commit
34e47ed59e
40
conn.go
40
conn.go
|
@ -1,8 +1,6 @@
|
||||||
package pqx
|
package pqx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"bytes"
|
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -14,6 +12,7 @@ type conn struct {
|
||||||
conn net.Conn // the underlying TCP or unix domain socket connection
|
conn net.Conn // the underlying TCP or unix domain socket connection
|
||||||
rowDesc rowDescription // current query rowDescription
|
rowDesc rowDescription // current query rowDescription
|
||||||
buf []byte // work buffer to avoid constant alloc and dealloc
|
buf []byte // work buffer to avoid constant alloc and dealloc
|
||||||
|
runtimeParams map[string]string // parameters that have been reported by the server
|
||||||
}
|
}
|
||||||
|
|
||||||
func Connect(options map[string]string) (c *conn, err error) {
|
func Connect(options map[string]string) (c *conn, err error) {
|
||||||
|
@ -30,6 +29,7 @@ func Connect(options map[string]string) (c *conn, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
c.buf = make([]byte, 1024)
|
c.buf = make([]byte, 1024)
|
||||||
|
c.runtimeParams = make(map[string]string)
|
||||||
|
|
||||||
// conn, err := net.Dial("tcp", "localhost:5432")
|
// conn, err := net.Dial("tcp", "localhost:5432")
|
||||||
|
|
||||||
|
@ -51,6 +51,8 @@ func Connect(options map[string]string) (c *conn, err error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println(c.runtimeParams)
|
||||||
|
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,14 +108,26 @@ func (c *conn) processMsg() (msg interface{}, err error) {
|
||||||
return c.parseMsg(t, body)
|
return c.parseMsg(t, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Processes messages that could potentially occur in multiple contexts
|
||||||
|
func (c *conn) processCommonMsg(t byte, body []byte) (err error) {
|
||||||
|
switch t {
|
||||||
|
case 'S':
|
||||||
|
c.rxParameterStatus(body)
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("Received unknown message type: %c", t)
|
||||||
|
}
|
||||||
|
|
||||||
|
panic("Unreachable")
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func (c *conn) parseMsg(t byte, body []byte) (msg interface{}, err error) {
|
func (c *conn) parseMsg(t byte, body []byte) (msg interface{}, err error) {
|
||||||
switch t {
|
switch t {
|
||||||
case 'K':
|
case 'K':
|
||||||
return c.rxBackendKeyData(body), nil
|
return c.rxBackendKeyData(body), nil
|
||||||
case 'R':
|
case 'R':
|
||||||
return c.rxAuthenticationX(body)
|
return c.rxAuthenticationX(body)
|
||||||
case 'S':
|
|
||||||
return c.rxParameterStatus(body)
|
|
||||||
case 'Z':
|
case 'Z':
|
||||||
return c.rxReadyForQuery(body), nil
|
return c.rxReadyForQuery(body), nil
|
||||||
case 'T':
|
case 'T':
|
||||||
|
@ -123,7 +137,7 @@ func (c *conn) parseMsg(t byte, body []byte) (msg interface{}, err error) {
|
||||||
case 'C':
|
case 'C':
|
||||||
return c.rxCommandComplete(body), nil
|
return c.rxCommandComplete(body), nil
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("Received unknown message type: %c", t)
|
return nil, c.processCommonMsg(t, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
panic("Unreachable")
|
panic("Unreachable")
|
||||||
|
@ -169,17 +183,11 @@ func (c *conn) rxAuthenticationX(buf []byte) (msg interface{}, err error) {
|
||||||
panic("Unreachable")
|
panic("Unreachable")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *conn) rxParameterStatus(buf []byte) (msg *parameterStatus, err error) {
|
func (c *conn) rxParameterStatus(buf []byte) {
|
||||||
msg = new(parameterStatus)
|
r := newMessageReader(buf)
|
||||||
|
key := r.readString()
|
||||||
r := bufio.NewReader(bytes.NewReader(buf))
|
value := r.readString()
|
||||||
msg.name, err = r.ReadString(0)
|
c.runtimeParams[key] = value
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
msg.value, err = r.ReadString(0)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *conn) rxBackendKeyData(buf []byte) (msg *backendKeyData) {
|
func (c *conn) rxBackendKeyData(buf []byte) (msg *backendKeyData) {
|
||||||
|
|
|
@ -38,15 +38,6 @@ func (self *authenticationOk) String() string {
|
||||||
return "AuthenticationOk"
|
return "AuthenticationOk"
|
||||||
}
|
}
|
||||||
|
|
||||||
type parameterStatus struct {
|
|
||||||
name string
|
|
||||||
value string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *parameterStatus) String() string {
|
|
||||||
return fmt.Sprintf("ParameterStatus %s: %s", self.name, self.value)
|
|
||||||
}
|
|
||||||
|
|
||||||
type backendKeyData struct {
|
type backendKeyData struct {
|
||||||
pid int32
|
pid int32
|
||||||
secretKey int32
|
secretKey int32
|
||||||
|
|
Loading…
Reference in New Issue