pqx

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2020 License: MIT Imports: 14 Imported by: 5

README

pqx GoDoc Go Report Card CircleCI Coverage Status Latest Release

Helpers for use with Go Postgres driver github.com/lib/pq.

Documentation

Overview

Package pqx provides helpers for use with github.com/lib/pq (Postgres driver for the database/sql package).

It also registers own driver for the database/sql package named "pqx". This driver is a thin wrapper around github.com/lib/pq driver used to make it easier to control connections opened by github.com/lib/pq. By default it'll just enable TCP keepalives, but you can set Dial to your own hook and have full control over opened connections.

Index

Constants

This section is empty.

Variables

Dial is a hook which should be set before connecting to PostgreSQL server using "pqx" driver.

Functions

func EnsureTempDB added in v0.5.0

func EnsureTempDB(log Logger, suffix string, dbCfg Config) (_ *sql.DB, cleanup func(), err error)

EnsureTempDB will drop/create new temporary db with suffix in db name and return opened temporary db together with cleanup func which will close and drop temporary db. It will use dbCfg to connect to existing database first, and then reuse same dbCfg with modified DBName to connect to temporary db.

It'll also create schema with name set to dbCfg.User in temporary db.

Recommended value for suffix is your package's import path.

func IsSerializationFailure added in v0.5.0

func IsSerializationFailure(err error) bool

IsSerializationFailure returns a boolean indicating whether the error is 40001 (serialization_failure).

It tries to unwrap err using github.com/pkg/errors.Cause() or errors.As().

func Serialize added in v0.5.0

func Serialize(doTx func() error) error

Serialize executes given func, which is supposed to run single transaction and return (possibly wrapped) error if transaction fail.

It will re-execute given func for up to 10 times in case it fails with 40001 (serialization_failure) error.

Returns value returned by last doTx call.

Types

type Config

type Config struct {
	DBName                          string
	User                            string
	Pass                            string
	Host                            string
	Port                            int
	FallbackApplicationName         string
	ConnectTimeout                  time.Duration // Round to seconds.
	SSLMode                         SSLMode
	SSLCert                         string             // PEM file path.
	SSLKey                          string             // PEM file path.
	SSLRootCert                     string             // PEM file path.
	SearchPath                      string             // Specifies the order in which schemas are searched.
	DefaultTransactionIsolation     sql.IsolationLevel // One of: LevelDefault, LevelReadUncommitted, LevelReadCommitted, LevelRepeatableRead, LevelSerializable.
	StatementTimeout                time.Duration      // Round to milliseconds.
	LockTimeout                     time.Duration      // Round to milliseconds.
	IdleInTransactionSessionTimeout time.Duration      // Round to milliseconds.
	Other                           map[string]string  // Any other parameters from https://www.postgresql.org/docs/current/runtime-config-client.html.
}

Config described connection parameters for github.com/lib/pq.

func (Config) FormatDSN

func (c Config) FormatDSN() string

FormatDSN returns dataSourceName string with properly escaped connection parameters suitable for sql.Open.

func (Config) FormatURL added in v0.6.0

func (c Config) FormatURL() string

FormatURL returns dataSourceName url suitable for sql.Open.

type DialFunc added in v0.4.0

type DialFunc func(network, address string, timeout time.Duration) (net.Conn, error)

DialFunc used to open new connections to PostgreSQL server.

func KeepAliveDial added in v0.4.0

func KeepAliveDial(keepAlivePeriod time.Duration) DialFunc

KeepAliveDial returns hook which adds TCP keepalives.

type Logger added in v0.5.0

type Logger interface {
	Print(...interface{})
}

Logger interface consumed by this package.

type SSLMode

type SSLMode string

SSLMode determines whether or with what priority a secure SSL TCP/IP connection will be negotiated with the server..

const (
	SSLDisable    SSLMode = "disable"     // Only try a non-SSL connection.
	SSLRequire    SSLMode = "require"     // Only try an SSL connection. If a root CA file is present, verify the certificate in the same way as if verify-ca was specified.
	SSLVerifyCA   SSLMode = "verify-ca"   // Only try an SSL connection, and verify that the server certificate is issued by a trusted certificate authority (CA).
	SSLVerifyFull SSLMode = "verify-full" // Only try an SSL connection, verify that the server certificate is issued by a trusted CA and that the requested server host name matches that in the certificate.
)

SSL modes.

Jump to

Keyboard shortcuts

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