pgdriver

package module
v1.2.6 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 License: BSD-2-Clause Imports: 27 Imported by: 365

README

pgdriver

PkgGoDev

pgdriver is a database/sql driver for PostgreSQL based on go-pg code.

You can install it with:

go get github.com/uptrace/bun/driver/pgdriver

And then create a sql.DB using it:

import _ "github.com/uptrace/bun/driver/pgdriver"

dsn := "postgres://postgres:@localhost:5432/test"
db, err := sql.Open("pg", dsn)

Alternatively:

dsn := "postgres://postgres:@localhost:5432/test"
db := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))

Benchmark:

BenchmarkInsert/pg-12 	            7254	    148380 ns/op	     900 B/op	      13 allocs/op
BenchmarkInsert/pgx-12         	    6494	    166391 ns/op	    2076 B/op	      26 allocs/op
BenchmarkSelect/pg-12          	    9100	    132952 ns/op	    1417 B/op	      18 allocs/op
BenchmarkSelect/pgx-12         	    8199	    154920 ns/op	    3679 B/op	      60 allocs/op

See documentation for more details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Logger logging = &logger{
	log: log.New(os.Stderr, "pgdriver: ", log.LstdFlags|log.Lshortfile),
}

Functions

func CopyFrom added in v1.0.18

func CopyFrom(
	ctx context.Context, conn bun.Conn, r io.Reader, query string, args ...interface{},
) (res sql.Result, err error)

CopyFrom copies data from the reader to the query destination.

func CopyTo added in v1.0.18

func CopyTo(
	ctx context.Context, conn bun.Conn, w io.Writer, query string, args ...interface{},
) (res sql.Result, err error)

CopyTo copies data from the query source to the writer.

func Notify added in v1.0.18

func Notify(ctx context.Context, db *bun.DB, channel, payload string) error

Notify sends a notification on the channel using `NOTIFY` command.

func ParseTime added in v1.1.6

func ParseTime(s string) (time.Time, error)

Types

type ChannelOption

type ChannelOption func(c *channel)

func WithChannelSize

func WithChannelSize(size int) ChannelOption

type Config added in v0.1.3

type Config struct {
	// Network type, either tcp or unix.
	// Default is tcp.
	Network string
	// TCP host:port or Unix socket depending on Network.
	Addr string
	// Dial timeout for establishing new connections.
	// Default is 5 seconds.
	DialTimeout time.Duration
	// Dialer creates new network connection and has priority over
	// Network and Addr options.
	Dialer func(ctx context.Context, network, addr string) (net.Conn, error)

	// TLS config for secure connections.
	TLSConfig *tls.Config

	User     string
	Password string
	Database string
	AppName  string
	// PostgreSQL session parameters updated with `SET` command when a connection is created.
	ConnParams map[string]interface{}

	// Timeout for socket reads. If reached, commands fail with a timeout instead of blocking.
	ReadTimeout time.Duration
	// Timeout for socket writes. If reached, commands fail with a timeout instead of blocking.
	WriteTimeout time.Duration

	// ResetSessionFunc is called prior to executing a query on a connection that has been used before.
	ResetSessionFunc func(context.Context, *Conn) error
}

type Conn

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

func (*Conn) Begin

func (cn *Conn) Begin() (driver.Tx, error)

func (*Conn) BeginTx added in v0.1.11

func (cn *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error)

func (*Conn) Close

func (cn *Conn) Close() error

func (*Conn) Conn added in v1.1.8

func (cn *Conn) Conn() net.Conn

func (*Conn) ExecContext

func (cn *Conn) ExecContext(
	ctx context.Context, query string, args []driver.NamedValue,
) (driver.Result, error)

func (*Conn) IsValid added in v0.2.0

func (cn *Conn) IsValid() bool

func (*Conn) Ping added in v0.1.10

func (cn *Conn) Ping(ctx context.Context) error

func (*Conn) Prepare

func (cn *Conn) Prepare(query string) (driver.Stmt, error)

func (*Conn) QueryContext

func (cn *Conn) QueryContext(
	ctx context.Context, query string, args []driver.NamedValue,
) (driver.Rows, error)

func (*Conn) ResetSession added in v1.1.8

func (cn *Conn) ResetSession(ctx context.Context) error

type Connector added in v0.3.7

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

func NewConnector

func NewConnector(opts ...Option) *Connector

func (*Connector) Config added in v0.3.7

func (c *Connector) Config() *Config

func (*Connector) Connect added in v0.3.7

func (c *Connector) Connect(ctx context.Context) (driver.Conn, error)

func (*Connector) Driver added in v0.3.7

func (c *Connector) Driver() driver.Driver

type Driver

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

func NewDriver

func NewDriver() Driver

func (Driver) Open

func (d Driver) Open(name string) (driver.Conn, error)

func (Driver) OpenConnector

func (d Driver) OpenConnector(name string) (driver.Connector, error)

type DriverOption

type DriverOption = Option

Deprecated. Use Option instead.

type Error

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

Error represents an error returned by PostgreSQL server using PostgreSQL ErrorResponse protocol.

https://www.postgresql.org/docs/current/static/protocol-message-formats.html

func (Error) Error

func (err Error) Error() string

func (Error) Field

func (err Error) Field(k byte) string

Field returns a string value associated with an error field.

https://www.postgresql.org/docs/current/static/protocol-error-fields.html

func (Error) IntegrityViolation

func (err Error) IntegrityViolation() bool

IntegrityViolation reports whether the error is a part of Integrity Constraint Violation class of errors.

https://www.postgresql.org/docs/current/static/errcodes-appendix.html

func (Error) StatementTimeout added in v1.0.12

func (err Error) StatementTimeout() bool

StatementTimeout reports whether the error is a statement timeout error.

type Listener

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

func NewListener

func NewListener(db *bun.DB) *Listener

func (*Listener) Channel

func (ln *Listener) Channel(opts ...ChannelOption) <-chan Notification

Channel returns a channel for concurrently receiving notifications. It periodically sends Ping notification to test connection health.

The channel is closed with Listener. Receive* APIs can not be used after channel is created.

func (*Listener) Close

func (ln *Listener) Close() error

Close closes the listener, releasing any open resources.

func (*Listener) Listen

func (ln *Listener) Listen(ctx context.Context, channels ...string) error

Listen starts listening for notifications on channels.

func (*Listener) Receive

func (ln *Listener) Receive(ctx context.Context) (channel string, payload string, err error)

Receive indefinitely waits for a notification. This is low-level API and in most cases Channel should be used instead.

func (*Listener) ReceiveTimeout

func (ln *Listener) ReceiveTimeout(
	ctx context.Context, timeout time.Duration,
) (channel, payload string, err error)

ReceiveTimeout waits for a notification until timeout is reached. This is low-level API and in most cases Channel should be used instead.

func (*Listener) Unlisten

func (ln *Listener) Unlisten(ctx context.Context, channels ...string) error

Unlisten stops listening for notifications on channels.

type Notification

type Notification struct {
	Channel string
	Payload string
}

Notification received with LISTEN command.

type Option added in v1.0.12

type Option func(cfg *Config)

func WithAddr

func WithAddr(addr string) Option

func WithApplicationName

func WithApplicationName(appName string) Option

func WithConnParams added in v1.0.9

func WithConnParams(params map[string]interface{}) Option

func WithDSN

func WithDSN(dsn string) Option

func WithDatabase

func WithDatabase(database string) Option

func WithDialTimeout

func WithDialTimeout(dialTimeout time.Duration) Option

func WithInsecure added in v1.0.13

func WithInsecure(on bool) Option

func WithNetwork added in v1.0.12

func WithNetwork(network string) Option

func WithPassword

func WithPassword(password string) Option

func WithReadTimeout

func WithReadTimeout(readTimeout time.Duration) Option

func WithResetSessionFunc added in v1.1.8

func WithResetSessionFunc(fn func(context.Context, *Conn) error) Option

WithResetSessionFunc configures a function that is called prior to executing a query on a connection that has been used before. If the func returns driver.ErrBadConn, the connection is discarded.

func WithTLSConfig added in v0.2.4

func WithTLSConfig(tlsConfig *tls.Config) Option

func WithTimeout

func WithTimeout(timeout time.Duration) Option

func WithUser

func WithUser(user string) Option

func WithWriteTimeout

func WithWriteTimeout(writeTimeout time.Duration) Option

Jump to

Keyboard shortcuts

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