Added some documentation

Made preparedStatement invisible outside package
This commit is contained in:
Jack Christensen 2013-07-14 16:52:58 -05:00
parent 00e3ec32ca
commit aeeb8a724a

View File

@ -1,3 +1,6 @@
// Package pgx is a PostgreSQL database driver.
//
// It does not implement the standard database/sql interface.
package pgx package pgx
import ( import (
@ -11,15 +14,19 @@ import (
"net" "net"
) )
// ConnectionParameters contains all the options used to establish a connection.
type ConnectionParameters struct { type ConnectionParameters struct {
Socket string // path to unix domain socket (e.g. /private/tmp/.s.PGSQL.5432) Socket string // path to unix domain socket (e.g. /private/tmp/.s.PGSQL.5432)
Host string Host string // url (e.g. localhost)
Port uint16 // default: 5432 Port uint16 // default: 5432
Database string Database string
User string User string
Password string Password string
} }
// Connection is a PostgreSQL connection handle. It is not safe for concurrent usage.
// Use ConnectionPool to manage access to multiple database connections from multiple
// goroutines.
type Connection struct { type Connection struct {
conn net.Conn // the underlying TCP or unix domain socket connection conn net.Conn // the underlying TCP or unix domain socket connection
buf *bytes.Buffer // work buffer to avoid constant alloc and dealloc buf *bytes.Buffer // work buffer to avoid constant alloc and dealloc
@ -28,10 +35,10 @@ type Connection struct {
runtimeParams map[string]string // parameters that have been reported by the server runtimeParams map[string]string // parameters that have been reported by the server
parameters ConnectionParameters // parameters used when establishing this connection parameters ConnectionParameters // parameters used when establishing this connection
txStatus byte txStatus byte
preparedStatements map[string]*PreparedStatement preparedStatements map[string]*preparedStatement
} }
type PreparedStatement struct { type preparedStatement struct {
Name string Name string
FieldDescriptions []FieldDescription FieldDescriptions []FieldDescription
ParameterOids []oid ParameterOids []oid
@ -78,7 +85,7 @@ func Connect(parameters ConnectionParameters) (c *Connection, err error) {
c.buf = bytes.NewBuffer(make([]byte, 0, sharedBufferSize)) c.buf = bytes.NewBuffer(make([]byte, 0, sharedBufferSize))
c.runtimeParams = make(map[string]string) c.runtimeParams = make(map[string]string)
c.preparedStatements = make(map[string]*PreparedStatement) c.preparedStatements = make(map[string]*preparedStatement)
msg := newStartupMessage() msg := newStartupMessage()
msg.options["user"] = c.parameters.User msg.options["user"] = c.parameters.User
@ -253,7 +260,7 @@ func (c *Connection) Prepare(name, sql string) (err error) {
return err return err
} }
ps := PreparedStatement{Name: name} ps := preparedStatement{Name: name}
for { for {
if t, r, rxErr := c.rxMsg(); rxErr == nil { if t, r, rxErr := c.rxMsg(); rxErr == nil {
@ -317,7 +324,7 @@ func (c *Connection) sendSimpleQuery(sql string, arguments ...interface{}) (err
return c.txMsg('Q', buf) return c.txMsg('Q', buf)
} }
func (c *Connection) sendPreparedQuery(ps *PreparedStatement, arguments ...interface{}) (err error) { func (c *Connection) sendPreparedQuery(ps *preparedStatement, arguments ...interface{}) (err error) {
if len(ps.ParameterOids) != len(arguments) { if len(ps.ParameterOids) != len(arguments) {
return fmt.Errorf("Prepared statement \"%v\" requires %d parameters, but %d were provided", ps.Name, len(ps.ParameterOids), len(arguments)) return fmt.Errorf("Prepared statement \"%v\" requires %d parameters, but %d were provided", ps.Name, len(ps.ParameterOids), len(arguments))
} }