dbutil

package
v2.9.3 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultMaxOpenConns is the default maximum number of open connections; if you change
	// this, also consider changing the default from the environment in
	// serviceenv.GlobalConfiguration.
	DefaultMaxOpenConns = 10
	// DefaultMaxIdleConns is the default number of idle database connections to maintain.  (2
	// comes from the default in database/sql.go.)
	DefaultMaxIdleConns = 2
	// DefaultConnMaxLifetime is the default maximum amount of time a connection may be reused
	// for.  Defaults to no maximum.
	DefaultConnMaxLifetime = 0
	// DefaultConnMaxIdleTime is the default maximum amount of time a connection may be idle.
	// Defaults to no maximum.
	DefaultConnMaxIdleTime = 0
)
View Source
const (
	SSLModeDisable = "disable"

	DefaultSSLMode = SSLModeDisable
)

Variables

This section is empty.

Functions

func GetDSN

func GetDSN(opts ...Option) string

GetDSN returns the string for connecting to the postgres instance with the parameters specified in 'opts'. This is needed because 'listen' operations are not supported in generic SQL libraries and they need to be run in a side session.

func IsNotNullViolation added in v2.8.0

func IsNotNullViolation(err error, columnName string) bool

func IsUniqueViolation

func IsUniqueViolation(err error) bool

IsUniqueViolation returns true if the error is a UniqueContraintViolation

func NewDB

func NewDB(opts ...Option) (*pachsql.DB, error)

NewDB creates a new DB.

func WaitUntilReady

func WaitUntilReady(ctx context.Context, db *pachsql.DB) error

WaitUntilReady attempts to ping the database until the context is cancelled. Progress information is written to log

func WithTx

func WithTx(ctx context.Context, db *pachsql.DB, cb func(cbCtx context.Context, tx *pachsql.Tx) error, opts ...WithTxOption) error

WithTx calls cb with a transaction, The transaction is committed IFF cb returns nil. If cb returns an error the transaction is rolled back.

Types

type Option

type Option func(*dbConfig)

Option configures a DB.

func WithConnMaxIdleTime

func WithConnMaxIdleTime(d time.Duration) Option

WithConnMaxIdleTime sets the maximum time a database connection may be idle for.

func WithConnMaxLifetime

func WithConnMaxLifetime(d time.Duration) Option

WithConnMaxLifetime sets the maximum time a database connection may be reused for.

func WithDBName

func WithDBName(DBName string) Option

WithDBName sets the name for the DB.

func WithHostPort

func WithHostPort(host string, port int) Option

WithHostPort sets the host and port for the DB.

func WithMaxIdleConns

func WithMaxIdleConns(n int) Option

WithMaxIdleConns sets the maximum number of idle database connections to keep available for future queries.

func WithMaxOpenConns

func WithMaxOpenConns(n int) Option

WithMaxOpenConns sets the maximum number of concurrent database connections to be allocated before blocking new acquisitions.

func WithQueryLog added in v2.7.0

func WithQueryLog(enable bool, name string) Option

WithQueryLog enables logging of SQL queries to zap at severity debug; see log.NewPGX.

func WithSSLMode

func WithSSLMode(mode string) Option

WithSSLMode sets the SSL mode for connections to the database.

func WithUserPassword

func WithUserPassword(user, password string) Option

WithUserPassword sets the user and password for the DB.

type WithTxOption

type WithTxOption func(c *withTxConfig)

WithTxOption parameterizes the WithTx function

func WithBackOff

func WithBackOff(bo backoff.BackOff) WithTxOption

TODO: Unused, but probably acceptable to leave in place WithBackOff sets the BackOff used when retrying

func WithIsolationLevel

func WithIsolationLevel(x sql.IsolationLevel) WithTxOption

WithIsolationLevel runs the transaction with the specified isolation level.

func WithReadOnly

func WithReadOnly() WithTxOption

TODO: Unused, but probably acceptable to leave in place WithReadOnly causes WithTx to run the transaction as read only

Jump to

Keyboard shortcuts

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