pgclient

package
v0.0.0-...-2d5dc59 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 21, 2017 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Copyright 2016 The Transicator Authors

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const (
	CopyFormatText    CopyFormat = "text"
	CopyFormatBinary             = "binary"
	CopyFormatCsv                = "csv"
	CopyFormatUnknown            = "unknown"
)

Various types of copy formats

Variables

This section is empty.

Functions

func ParseCommandComplete

func ParseCommandComplete(m *InputMessage) (int64, error)

ParseCommandComplete parses the CommandComplete message and returns the row count that it contains

func ParseDataRow

func ParseDataRow(m *InputMessage) ([][]byte, error)

ParseDataRow turns a single DataRow message to a list of buffers

func ParseError

func ParseError(m *InputMessage) error

ParseError looks at an input message that represents an error and returns a Go error.

func ParseNotice

func ParseNotice(m *InputMessage) (string, error)

ParseNotice looks at an input message that's an error or a "notice" message (both have the same format) and returns the text.

func ParseParameterStatus

func ParseParameterStatus(m *InputMessage) (string, error)

ParseParameterStatus parses the ParameterStatus message

func RegisterDriver

func RegisterDriver() bool

RegisterDriver ensures that the driver has been registered with the runtime. It's OK to call it more than once.

Types

type ColumnInfo

type ColumnInfo struct {
	// Name of the column
	Name string
	// Postgres type OID
	Type PgType
	// Was the column represented in binary form in the result row?
	Binary bool
}

A ColumnInfo describes a single row in a query result.

func ParseRowDescription

func ParseRowDescription(m *InputMessage) ([]ColumnInfo, error)

ParseRowDescription looks at a RowDescription message and parses it

type CopyFormat

type CopyFormat string

CopyFormat is the format of the copy.

func GetCopyFormat

func GetCopyFormat(i int) CopyFormat

GetCopyFormat translates a numeric format code from the server to a format

type CopyResponseInfo

type CopyResponseInfo struct {
	// contains filtered or unexported fields
}

CopyResponseInfo describes the format of the copy response

func ParseCopyOutResponse

func ParseCopyOutResponse(m *InputMessage) (info *CopyResponseInfo, err error)

ParseCopyOutResponse parses the response from the CopyData message

type InputMessage

type InputMessage struct {
	// contains filtered or unexported fields
}

An InputMessage represents a message read from the server. It's understood that we already read the type byte and also the four-byte length, and that we are being given a slice to the data of the appropriate length for the message.

func NewInputMessage

func NewInputMessage(msgType PgInputType, b []byte) *InputMessage

NewInputMessage generates a new input message from the specified byte array, which must be the correct length for the message.

func NewServerInputMessage

func NewServerInputMessage(msgType PgOutputType, b []byte) *InputMessage

NewServerInputMessage generates a new input message from the specified byte array, which must be the correct length for the message.

func ParseCopyData

func ParseCopyData(m *InputMessage) (*InputMessage, error)

ParseCopyData looks at a CopyData message and then parses it again as another message.

func (*InputMessage) ReadByte

func (m *InputMessage) ReadByte() (byte, error)

ReadByte reads a single byte.

func (*InputMessage) ReadBytes

func (m *InputMessage) ReadBytes(n int) ([]byte, error)

ReadBytes reads a count of bytes.

func (*InputMessage) ReadInt16

func (m *InputMessage) ReadInt16() (int16, error)

ReadInt16 reads a single int16 from the message and returns it.

func (*InputMessage) ReadInt32

func (m *InputMessage) ReadInt32() (int32, error)

ReadInt32 reads a single int32 from the message and returns it.

func (*InputMessage) ReadInt64

func (m *InputMessage) ReadInt64() (int64, error)

ReadInt64 reads a single int64 from the message and returns it.

func (*InputMessage) ReadInt8

func (m *InputMessage) ReadInt8() (int8, error)

ReadInt8 reads a single int8 from the message and returns it.

func (*InputMessage) ReadRemaining

func (m *InputMessage) ReadRemaining() []byte

ReadRemaining reads everything that is left in the message.

func (*InputMessage) ReadString

func (m *InputMessage) ReadString() (string, error)

ReadString reads a single null-terminated string form the message and returns it.

func (*InputMessage) ServerType

func (m *InputMessage) ServerType() PgOutputType

ServerType returns the message type byte from the message that was passed in to the "NewInputMessage" function when we're a server.

func (*InputMessage) Type

func (m *InputMessage) Type() PgInputType

Type returns the message type byte from the message that was passed in to the "NewInputMessage" function.

type MockAuth

type MockAuth int

MockAuth specifies what type of authentication the server supports

const (
	MockTrust MockAuth = 0
	MockClear MockAuth = 1
	MockMD5   MockAuth = 2
)

Different auth types

type MockServer

type MockServer struct {
	// contains filtered or unexported fields
}

A MockServer is a server that implements a little bit of the Postgres wire protocol. We can use it for testing of the wire protocol client. In particular, we can use it to test the myriad of password authentication and TLS options without having to start and stop a real Postgres server in the test suite.

func NewMockServer

func NewMockServer() *MockServer

NewMockServer starts a new server in the current process, listening on the specified port.

func (*MockServer) Address

func (m *MockServer) Address() string

Address returns the listen address in host:port format.

func (*MockServer) SetAuthType

func (m *MockServer) SetAuthType(auth MockAuth)

SetAuthType sets what kind of password authentication to require

func (*MockServer) SetForceTLS

func (m *MockServer) SetForceTLS()

SetForceTLS sets up the server to reject any non-TLS clients.

func (*MockServer) SetTLSInfo

func (m *MockServer) SetTLSInfo(certFile, keyFile string) error

SetTLSInfo sets the cert and key file and makes it possible for the server to support TLS.

func (*MockServer) Start

func (m *MockServer) Start(port int) (err error)

Start listening for stuff.

func (*MockServer) Stop

func (m *MockServer) Stop()

Stop stops the server listening for new connections.

type OutputMessage

type OutputMessage struct {
	// contains filtered or unexported fields
}

An OutputMessage represents a single message that is going to be sent to the Postgres server. It will be prepended with a type byte and a four-byte length.

func NewOutputMessage

func NewOutputMessage(msgType PgOutputType) *OutputMessage

NewOutputMessage constructs a new message with the given type byte

func NewServerOutputMessage

func NewServerOutputMessage(msgType PgInputType) *OutputMessage

NewServerOutputMessage constructs a new message with the given type byte

func NewStartupMessage

func NewStartupMessage() *OutputMessage

NewStartupMessage constructs a startup message, which has no type byte

func (*OutputMessage) Encode

func (m *OutputMessage) Encode() []byte

Encode returns a byte slice that represents the entire message, including the header byte and length.

func (*OutputMessage) ServerType

func (m *OutputMessage) ServerType() PgInputType

ServerType returns the message type byte from the message that was passed in to the "NewOutputMessage" function if we're a server

func (*OutputMessage) Type

func (m *OutputMessage) Type() PgOutputType

Type returns the message type byte from the message that was passed in to the "NewOutputMessage" function.

func (*OutputMessage) WriteByte

func (m *OutputMessage) WriteByte(b byte) error

WriteByte writes a single byte to the output.

func (*OutputMessage) WriteBytes

func (m *OutputMessage) WriteBytes(b []byte) error

WriteBytes writes a bunch of bytes to the output.

func (*OutputMessage) WriteInt16

func (m *OutputMessage) WriteInt16(i int16)

WriteInt16 writes a single "int16" to the output.

func (*OutputMessage) WriteInt32

func (m *OutputMessage) WriteInt32(i int32)

WriteInt32 writes a single "int32" to the output.

func (*OutputMessage) WriteInt64

func (m *OutputMessage) WriteInt64(i int64)

WriteInt64 writes a single "int64" to the output.

func (*OutputMessage) WriteString

func (m *OutputMessage) WriteString(s string)

WriteString writes a single "string" to the output.

func (*OutputMessage) WriteUint64

func (m *OutputMessage) WriteUint64(i uint64)

WriteUint64 writes a single "uint64" to the output.

type PgConnection

type PgConnection struct {
	// contains filtered or unexported fields
}

A PgConnection represents a connection to the database.

func Connect

func Connect(connect string) (*PgConnection, error)

Connect to the database. "host" must be a "host:port" pair, "user" and "database" must contain the appropriate user name and database name, and "opts" contains any other keys and values to send to the database.

The connect stringsupports the style of URL that the standard "libpq" supports. https://www.postgresql.org/docs/9.6/static/libpq-connect.html

postgres://[user[:password]@]hostname[:port]/[database]?sslmode=[require|allow|prefer|disable]&param=val&param=val

func (*PgConnection) Close

func (c *PgConnection) Close()

Close does what you think it does.

func (*PgConnection) CopyTo

func (c *PgConnection) CopyTo(wr io.Writer, query string, cf CopyFormat) (io.Writer, error)

CopyTo performs the actual copy on the connection

func (*PgConnection) ReadMessage

func (c *PgConnection) ReadMessage() (*InputMessage, error)

ReadMessage reads a single message from the socket, and decodes its type byte and length.

func (*PgConnection) SimpleExec

func (c *PgConnection) SimpleExec(query string) (int64, error)

SimpleExec executes a query as a string, and returns a row count. It does not do any kind of preparation. The first parameter returned is an array of descriptors for each row. The second is an array of rows.

func (*PgConnection) SimpleQuery

func (c *PgConnection) SimpleQuery(query string) ([]ColumnInfo, [][]string, error)

SimpleQuery executes a query as a string, and returns a list of rows. It does not do any kind of preparation. The first parameter returned is an array of descriptors for each row. The second is an array of rows.

func (*PgConnection) WriteMessage

func (c *PgConnection) WriteMessage(m *OutputMessage) error

WriteMessage sends the specified message to the server, and does not wait to see what comes back.

type PgDriver

type PgDriver struct {
	// contains filtered or unexported fields
}

PgDriver implements the standard driver interface

func (*PgDriver) Open

func (d *PgDriver) Open(url string) (driver.Conn, error)

Open takes a Postgres URL as used elsewhere in this package

func (*PgDriver) SetExtendedColumnNames

func (d *PgDriver) SetExtendedColumnNames(extended bool)

SetExtendedColumnNames enables a mode in which the column names returned from the "Rows" interface will return the format "name:type" where "type" is the integer postgres type ID.

func (*PgDriver) SetIsolationLevel

func (d *PgDriver) SetIsolationLevel(level string)

SetIsolationLevel ensures that all connections opened by this driver have the specified isolation level. It will only affect connections opened after it was called, so callers should call it before executing any transactions

func (*PgDriver) SetReadTimeout

func (d *PgDriver) SetReadTimeout(t time.Duration)

SetReadTimeout enables a timeout on all "read" operations from the database. This effectively bounds the amount of time that the database can spend on any single SQL operation.

type PgDriverConn

type PgDriverConn struct {
	// contains filtered or unexported fields
}

PgDriverConn implements Conn

func (*PgDriverConn) Begin

func (c *PgDriverConn) Begin() (driver.Tx, error)

Begin just runs the SQL "begin" statement

func (*PgDriverConn) Close

func (c *PgDriverConn) Close() error

Close closes the connection

func (*PgDriverConn) Exec

func (c *PgDriverConn) Exec(query string, args []driver.Value) (driver.Result, error)

Exec is the fast path for SQL that the caller did not individually prepare. If there are no parameters, it uses the "simple query protocol" which works with fewer messages to the database. Otherwise, it uses the default (unnamed) prepared statement.

func (*PgDriverConn) Prepare

func (c *PgDriverConn) Prepare(query string) (driver.Stmt, error)

Prepare creates a statement. Right now it just saves the SQL.

func (*PgDriverConn) Query

func (c *PgDriverConn) Query(query string, args []driver.Value) (driver.Rows, error)

Query works like a normal prepared query but it uses the unnamed prepared statement to save a message. We don't use the simple query protocol here because it does not allow us to stream the rows

type PgInputType

type PgInputType int

PgInputType is the one-byte type of a postgres response from the server.

const (
	ErrorResponse          PgInputType = 'E'
	CommandComplete        PgInputType = 'C'
	DataRow                PgInputType = 'D'
	CopyInResponse         PgInputType = 'G'
	CopyOutResponse        PgInputType = 'H'
	EmptyQueryResponse     PgInputType = 'I'
	BackEndKeyData         PgInputType = 'K'
	NoticeResponse         PgInputType = 'N'
	AuthenticationResponse PgInputType = 'R'
	ParameterStatus        PgInputType = 'S'
	RowDescription         PgInputType = 'T'
	CopyBothResponse       PgInputType = 'W'
	ReadyForQuery          PgInputType = 'Z'
	CopyDone               PgInputType = 'c'
	CopyData               PgInputType = 'd'
	HotStandbyFeedback     PgInputType = 'h'
	SenderKeepalive        PgInputType = 'k'
	NoData                 PgInputType = 'n'
	StandbyStatusUpdate    PgInputType = 'r'
	PortalSuspended        PgInputType = 's'
	ParameterDescription   PgInputType = 't'
	WALData                PgInputType = 'w'
	ParseComplete          PgInputType = '1'
	BindComplete           PgInputType = '2'
	CloseComplete          PgInputType = '3'
)

Various types of messages that represent one-byte message types.

func (PgInputType) String

func (i PgInputType) String() string

type PgOutputType

type PgOutputType int

PgOutputType represents the type of an output message to the server.

const (
	Bind            PgOutputType = 'B'
	Close           PgOutputType = 'C'
	DescribeMsg     PgOutputType = 'D'
	Execute         PgOutputType = 'E'
	Flush           PgOutputType = 'H'
	Parse           PgOutputType = 'P'
	Query           PgOutputType = 'Q'
	Sync            PgOutputType = 'S'
	Terminate       PgOutputType = 'X'
	CopyDoneOut     PgOutputType = 'c'
	CopyDataOut     PgOutputType = 'd'
	PasswordMessage PgOutputType = 'p'
)

Constants representing output message types.

func (PgOutputType) String

func (i PgOutputType) String() string

type PgRows

type PgRows struct {
	// contains filtered or unexported fields
}

PgRows implements the Rows interface. All the rows are saved up before we return this, so "Next" does no I/O.

func (*PgRows) Close

func (r *PgRows) Close() error

Close syncs the connection so that it can be made ready for the next time we need to use it. We can do this even if we didn't fetch all the rows.

func (*PgRows) Columns

func (r *PgRows) Columns() []string

Columns returns the column names. The statement will already have them described before this structure is created.

func (*PgRows) Next

func (r *PgRows) Next(dest []driver.Value) error

Next iterates through the rows. If no rows have been fetched yet (either because we came to the end of a batch, or if we have not called Execute yet) then we fetch some rows.

type PgStmt

type PgStmt struct {
	// contains filtered or unexported fields
}

PgStmt implements the Stmt interface. It does only "simple" queries now

func (*PgStmt) Close

func (s *PgStmt) Close() error

Close removes knowledge of the statement from the server by sending a Close message.

func (*PgStmt) Exec

func (s *PgStmt) Exec(args []driver.Value) (driver.Result, error)

Exec uses the "default" Postgres portal to Bind and then Execute, and retrieve all the rows.

func (*PgStmt) NumInput

func (s *PgStmt) NumInput() int

NumInput uses a "describe" message to get information about the inputs, but only if we haven't described the statement

func (*PgStmt) Query

func (s *PgStmt) Query(args []driver.Value) (driver.Rows, error)

Query uses the "default" Postgres portal to Bind. However we will defer Execute until we get all the rows.

type PgTransaction

type PgTransaction struct {
	// contains filtered or unexported fields
}

PgTransaction is a simple wrapper for transaction SQL

func (*PgTransaction) Commit

func (t *PgTransaction) Commit() error

Commit just runs the "commit" statement

func (*PgTransaction) Rollback

func (t *PgTransaction) Rollback() error

Rollback just runs the "rollback" statement

type PgType

type PgType int

PgType represents a Postgres type OID.

const (
	Bytea       PgType = 17
	Int8        PgType = 20
	Int2        PgType = 21
	Int4        PgType = 23
	OID         PgType = 26
	Float4      PgType = 700
	Float8      PgType = 701
	Timestamp   PgType = 1114
	TimestampTZ PgType = 1184
)

Constants for well-known OIDs that we care about

func ParseParameterDescription

func ParseParameterDescription(m *InputMessage) ([]PgType, error)

ParseParameterDescription looks at a Parameter description and returns a list of types.

func (PgType) String

func (i PgType) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL