Documentation ¶
Overview ¶
Package pgx is a PostgreSQL database driver.
pgx provides lower level access to PostgreSQL than the standard database/sql It remains as similar to the database/sql interface as possible while providing better speed and access to PostgreSQL specific features. Import github.com/jack/pgx/stdlib to use pgx as a database/sql compatible driver.
Index ¶
- Constants
- Variables
- type CommandTag
- type Conn
- func (c *Conn) Begin() (*Tx, error)
- func (c *Conn) BeginIso(isoLevel string) (*Tx, error)
- func (c *Conn) CauseOfDeath() error
- func (c *Conn) Close() (err error)
- func (c *Conn) Deallocate(name string) (err error)
- func (c *Conn) Exec(sql string, arguments ...interface{}) (commandTag CommandTag, err error)
- func (c *Conn) IsAlive() bool
- func (c *Conn) Listen(channel string) (err error)
- func (c *Conn) Prepare(name, sql string) (ps *PreparedStatement, err error)
- func (c *Conn) Query(sql string, args ...interface{}) (*Rows, error)
- func (c *Conn) QueryRow(sql string, args ...interface{}) *Row
- func (c *Conn) WaitForNotification(timeout time.Duration) (*Notification, error)
- type ConnConfig
- type ConnPool
- func (p *ConnPool) Acquire() (c *Conn, err error)
- func (p *ConnPool) Begin() (*Tx, error)
- func (p *ConnPool) BeginIso(iso string) (*Tx, error)
- func (p *ConnPool) Close()
- func (p *ConnPool) Exec(sql string, arguments ...interface{}) (commandTag CommandTag, err error)
- func (p *ConnPool) Query(sql string, args ...interface{}) (*Rows, error)
- func (p *ConnPool) QueryRow(sql string, args ...interface{}) *Row
- func (p *ConnPool) Release(conn *Conn)
- func (p *ConnPool) Stat() (s ConnPoolStat)
- type ConnPoolConfig
- type ConnPoolStat
- type Encoder
- type FieldDescription
- type Logger
- type Notification
- type NullBool
- type NullFloat32
- type NullFloat64
- type NullInt16
- type NullInt32
- type NullInt64
- type NullString
- type NullTime
- type Oid
- type PgError
- type PreparedStatement
- type ProtocolError
- type QueryArgs
- type Row
- type Rows
- type Scanner
- type SerializationError
- type Tx
- type ValueReader
- func (r *ValueReader) Err() error
- func (r *ValueReader) Fatal(err error)
- func (r *ValueReader) Len() int32
- func (r *ValueReader) ReadByte() byte
- func (r *ValueReader) ReadBytes(count int32) []byte
- func (r *ValueReader) ReadInt16() int16
- func (r *ValueReader) ReadInt32() int32
- func (r *ValueReader) ReadInt64() int64
- func (r *ValueReader) ReadOid() Oid
- func (r *ValueReader) ReadString(count int32) string
- func (r *ValueReader) Type() *FieldDescription
- type WriteBuf
Constants ¶
const ( Serializable = "serializable" RepeatableRead = "repeatable read" ReadCommitted = "read committed" ReadUncommitted = "read uncommitted" )
Transaction isolation levels
const ( BoolOid = 16 ByteaOid = 17 Int8Oid = 20 Int2Oid = 21 Int4Oid = 23 TextOid = 25 OidOid = 26 Float4Oid = 700 Float8Oid = 701 Int2ArrayOid = 1005 Int4ArrayOid = 1007 TextArrayOid = 1009 VarcharArrayOid = 1015 Int8ArrayOid = 1016 Float4ArrayOid = 1021 Float8ArrayOid = 1022 VarcharOid = 1043 DateOid = 1082 TimestampOid = 1114 TimestampTzOid = 1184 )
PostgreSQL oids for common types
const ( TextFormatCode = 0 BinaryFormatCode = 1 )
PostgreSQL format codes
Variables ¶
var DefaultOidFormats map[Oid]int16
var ErrDeadConn = errors.New("conn is dead")
var ErrNoRows = errors.New("no rows in result set")
var ErrNotificationTimeout = errors.New("notification timeout")
var ErrTxClosed = errors.New("tx is closed")
Functions ¶
This section is empty.
Types ¶
type CommandTag ¶
type CommandTag string
func (CommandTag) RowsAffected ¶
func (ct CommandTag) RowsAffected() int64
RowsAffected returns the number of rows affected. If the CommandTag was not for a row affecting command (such as "CREATE TABLE") then it returns 0
type Conn ¶
type Conn struct { Pid int32 // backend pid SecretKey int32 // key to use to send a cancel query message to the server RuntimeParams map[string]string // parameters that have been reported by the server TxStatus byte // contains filtered or unexported fields }
Conn is a PostgreSQL connection handle. It is not safe for concurrent usage. Use ConnPool to manage access to multiple database connections from multiple goroutines.
func Connect ¶
func Connect(config ConnConfig) (c *Conn, err error)
Connect establishes a connection with a PostgreSQL server using config. config.Host must be specified. config.User will default to the OS user name. Other config fields are optional.
func (*Conn) Begin ¶
Begin starts a transaction with the default isolation level for the current connection. To use a specific isolation level see BeginIso.
func (*Conn) BeginIso ¶
BeginIso starts a transaction with isoLevel as the transaction isolation level.
Valid isolation levels (and their constants) are:
serializable (pgx.Serializable) repeatable read (pgx.RepeatableRead) read committed (pgx.ReadCommitted) read uncommitted (pgx.ReadUncommitted)
func (*Conn) CauseOfDeath ¶
func (*Conn) Close ¶
Close closes a connection. It is safe to call Close on a already closed connection.
func (*Conn) Deallocate ¶
Deallocate released a prepared statement
func (*Conn) Exec ¶
func (c *Conn) Exec(sql string, arguments ...interface{}) (commandTag CommandTag, err error)
Exec executes sql. sql can be either a prepared statement name or an SQL string. arguments will be sanitized before being interpolated into sql strings. arguments should be referenced positionally from the sql string as $1, $2, etc.
func (*Conn) Prepare ¶
func (c *Conn) Prepare(name, sql string) (ps *PreparedStatement, err error)
Prepare creates a prepared statement with name and sql. sql can contain placeholders for bound parameters. These placeholders are referenced positional as $1, $2, etc.
func (*Conn) Query ¶
Query executes sql with args. If there is an error the returned *Rows will be returned in an error state. So it is allowed to ignore the error returned from Query and handle it in *Rows.
func (*Conn) QueryRow ¶
QueryRow is a convenience wrapper over Query. Any error that occurs while querying is deferred until calling Scan on the returned *Row. That *Row will error with ErrNoRows if no rows are returned.
func (*Conn) WaitForNotification ¶
func (c *Conn) WaitForNotification(timeout time.Duration) (*Notification, error)
WaitForNotification waits for a PostgreSQL notification for up to timeout. If the timeout occurs it returns pgx.ErrNotificationTimeout
type ConnConfig ¶
type ConnConfig struct { Host string // host (e.g. localhost) or path to unix domain socket directory (e.g. /private/tmp) Port uint16 // default: 5432 Database string User string // default: OS user name Password string TLSConfig *tls.Config // config for TLS connection -- nil disables TLS Logger Logger }
ConnConfig contains all the options used to establish a connection.
func ParseURI ¶
func ParseURI(uri string) (ConnConfig, error)
ParseURI parses a database URI into ConnConfig
type ConnPool ¶
type ConnPool struct {
// contains filtered or unexported fields
}
func NewConnPool ¶
func NewConnPool(config ConnPoolConfig) (p *ConnPool, err error)
NewConnPool creates a new ConnPool. config.ConnConfig is passed through to Connect directly.
func (*ConnPool) Begin ¶
Begin acquires a connection and begins a transaction on it. When the transaction is closed the connection will be automatically released.
func (*ConnPool) BeginIso ¶
BeginIso acquires a connection and begins a transaction in isolation mode iso on it. When the transaction is closed the connection will be automatically released.
func (*ConnPool) Close ¶
func (p *ConnPool) Close()
Close ends the use of a connection pool by closing all underlying connections.
func (*ConnPool) Exec ¶
func (p *ConnPool) Exec(sql string, arguments ...interface{}) (commandTag CommandTag, err error)
Exec acquires a connection, delegates the call to that connection, and releases the connection
func (*ConnPool) Query ¶
Query acquires a connection and delegates the call to that connection. When *Rows are closed, the connection is released automatically.
func (*ConnPool) QueryRow ¶
QueryRow acquires a connection and delegates the call to that connection. The connection is released automatically after Scan is called on the returned *Row.
func (*ConnPool) Stat ¶
func (p *ConnPool) Stat() (s ConnPoolStat)
Stat returns connection pool statistics
type ConnPoolConfig ¶
type ConnPoolConfig struct { ConnConfig MaxConnections int // max simultaneous connections to use, default 5, must be at least 2 AfterConnect func(*Conn) error // function to call on every new connection }
type ConnPoolStat ¶
type Encoder ¶
type Encoder interface { // Encode writes the value to w. // // If the value is NULL an int32(-1) should be written. // // Encode MUST check oid to see if the parameter data type is compatible. If // this is not done, the PostgreSQL server may detect the error if the // expected data size or format of the encoded data does not match. But if // the encoded data is a valid representation of the data type PostgreSQL // expects such as date and int4, incorrect data may be stored. Encode(w *WriteBuf, oid Oid) error // FormatCode returns the format that the encoder writes the value. It must be // either pgx.TextFormatCode or pgx.BinaryFormatCode. FormatCode() int16 }
Encoder is an interface used to encode values for transmission to the PostgreSQL server.
type FieldDescription ¶
type Logger ¶
type Logger interface { // Log a message at the given level with context key/value pairs Debug(msg string, ctx ...interface{}) Info(msg string, ctx ...interface{}) Warn(msg string, ctx ...interface{}) Error(msg string, ctx ...interface{}) }
Logger is the interface used to get logging from pgx internals. https://github.com/inconshreveable/log15 is the recommended logging package. This logging interface was extracted from there. However, it should be simple to adapt any logger to this interface.
type Notification ¶
type NullBool ¶
NullBool represents an bool that may be null. NullBool implements the Scanner, TextEncoder, and BinaryEncoder interfaces so it may be used both as an argument to Query[Row] and a destination for Scan for prepared and unprepared queries.
If Valid is false then the value is NULL.
func (NullBool) FormatCode ¶
func (*NullBool) Scan ¶
func (n *NullBool) Scan(vr *ValueReader) error
type NullFloat32 ¶
NullFloat32 represents an float4 that may be null. NullFloat32 implements the Scanner, TextEncoder, and BinaryEncoder interfaces so it may be used both as an argument to Query[Row] and a destination for Scan for prepared and unprepared queries.
If Valid is false then the value is NULL.
func (NullFloat32) FormatCode ¶
func (n NullFloat32) FormatCode() int16
func (*NullFloat32) Scan ¶
func (n *NullFloat32) Scan(vr *ValueReader) error
type NullFloat64 ¶
NullFloat64 represents an float8 that may be null. NullFloat64 implements the Scanner, TextEncoder, and BinaryEncoder interfaces so it may be used both as an argument to Query[Row] and a destination for Scan for prepared and unprepared queries.
If Valid is false then the value is NULL.
func (NullFloat64) FormatCode ¶
func (n NullFloat64) FormatCode() int16
func (*NullFloat64) Scan ¶
func (n *NullFloat64) Scan(vr *ValueReader) error
type NullInt16 ¶
NullInt16 represents an smallint that may be null. NullInt16 implements the Scanner, TextEncoder, and BinaryEncoder interfaces so it may be used both as an argument to Query[Row] and a destination for Scan for prepared and unprepared queries.
If Valid is false then the value is NULL.
func (NullInt16) FormatCode ¶
func (*NullInt16) Scan ¶
func (n *NullInt16) Scan(vr *ValueReader) error
type NullInt32 ¶
NullInt32 represents an integer that may be null. NullInt32 implements the Scanner, TextEncoder, and BinaryEncoder interfaces so it may be used both as an argument to Query[Row] and a destination for Scan for prepared and unprepared queries.
If Valid is false then the value is NULL.
func (NullInt32) FormatCode ¶
func (*NullInt32) Scan ¶
func (n *NullInt32) Scan(vr *ValueReader) error
type NullInt64 ¶
NullInt64 represents an bigint that may be null. NullInt64 implements the Scanner, TextEncoder, and BinaryEncoder interfaces so it may be used both as an argument to Query[Row] and a destination for Scan for prepared and unprepared queries.
If Valid is false then the value is NULL.
func (NullInt64) FormatCode ¶
func (*NullInt64) Scan ¶
func (n *NullInt64) Scan(vr *ValueReader) error
type NullString ¶
NullString represents an string that may be null. NullString implements the Scanner and TextEncoder interfaces so it may be used both as an argument to Query[Row] and a destination for Scan for prepared and unprepared queries.
If Valid is false then the value is NULL.
func (NullString) FormatCode ¶
func (n NullString) FormatCode() int16
func (*NullString) Scan ¶
func (s *NullString) Scan(vr *ValueReader) error
type NullTime ¶
NullTime represents an bigint that may be null. NullTime implements the Scanner, TextEncoder, and BinaryEncoder interfaces so it may be used both as an argument to Query[Row] and a destination for Scan for prepared and unprepared queries.
If Valid is false then the value is NULL.
func (NullTime) FormatCode ¶
func (*NullTime) Scan ¶
func (n *NullTime) Scan(vr *ValueReader) error
type PreparedStatement ¶
type PreparedStatement struct { Name string FieldDescriptions []FieldDescription ParameterOids []Oid }
type ProtocolError ¶
type ProtocolError string
func (ProtocolError) Error ¶
func (e ProtocolError) Error() string
type QueryArgs ¶
type QueryArgs []interface{}
QueryArgs is a container for arguments to an SQL query. It is helpful when building SQL statements where the number of arguments is variable.
type Row ¶
type Row Rows
Row is a convenience wrapper over Rows that is returned by QueryRow.
type Rows ¶
type Rows struct {
// contains filtered or unexported fields
}
Rows is the result set returned from *Conn.Query. Rows must be closed before the *Conn can be used again. Rows are closed by explicitly calling Close(), calling Next() until it returns false, or when a fatal error occurs.
func (*Rows) Close ¶
func (rows *Rows) Close()
Close closes the rows, making the connection ready for use again. It is not usually necessary to call Close explicitly because reading all returned rows with Next automatically closes Rows. It is safe to call Close after rows is already closed.
func (*Rows) Fatal ¶
Fatal signals an error occurred after the query was sent to the server. It closes the rows automatically.
func (*Rows) FieldDescriptions ¶
func (rows *Rows) FieldDescriptions() []FieldDescription
func (*Rows) Next ¶
Next prepares the next row for reading. It returns true if there is another row and false if no more rows are available. It automatically closes rows when all rows are read.
type Scanner ¶
type Scanner interface { // Scan MUST check r.Type().DataType and r.Type().FormatCode before decoding. // It should not assume that it was called on a data type or format that it // understands. Scan(r *ValueReader) error }
Scanner is an interface used to decode values from the PostgreSQL server.
type SerializationError ¶
type SerializationError string
func (SerializationError) Error ¶
func (e SerializationError) Error() string
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
Tx represents a database transaction.
All Tx methods return ErrTxClosed if Commit or Rollback has already been called on the Tx.
func (*Tx) Exec ¶
func (tx *Tx) Exec(sql string, arguments ...interface{}) (commandTag CommandTag, err error)
Exec delegates to the underlying *Conn
type ValueReader ¶
type ValueReader struct {
// contains filtered or unexported fields
}
ValueReader is used by the Scanner interface to decode values.
func (*ValueReader) Err ¶
func (r *ValueReader) Err() error
Err returns any error that the ValueReader has experienced
func (*ValueReader) Fatal ¶
func (r *ValueReader) Fatal(err error)
Fatal tells r that a Fatal error has occurred
func (*ValueReader) ReadByte ¶
func (r *ValueReader) ReadByte() byte
func (*ValueReader) ReadBytes ¶
func (r *ValueReader) ReadBytes(count int32) []byte
ReadBytes reads count bytes and returns as []byte
func (*ValueReader) ReadInt16 ¶
func (r *ValueReader) ReadInt16() int16
func (*ValueReader) ReadInt32 ¶
func (r *ValueReader) ReadInt32() int32
func (*ValueReader) ReadInt64 ¶
func (r *ValueReader) ReadInt64() int64
func (*ValueReader) ReadOid ¶
func (r *ValueReader) ReadOid() Oid
func (*ValueReader) ReadString ¶
func (r *ValueReader) ReadString(count int32) string
ReadString reads count bytes and returns as string
func (*ValueReader) Type ¶
func (r *ValueReader) Type() *FieldDescription
Type returns the *FieldDescription of the value
type WriteBuf ¶
type WriteBuf struct {
// contains filtered or unexported fields
}
WrifeBuf is used build messages to send to the PostgreSQL server. It is used by the BinaryEncoder interface when implementing custom encoders.
func (*WriteBuf) WriteBytes ¶
func (*WriteBuf) WriteCString ¶
func (*WriteBuf) WriteInt16 ¶
func (*WriteBuf) WriteInt32 ¶
func (*WriteBuf) WriteInt64 ¶
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
examples
|
|
Package stdlib is the compatibility layer from pgx to database/sql.
|
Package stdlib is the compatibility layer from pgx to database/sql. |