postgres

package module
v0.0.0-...-c1cb7b8 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorHandler

func ErrorHandler(err error, object string) error

ErrorHandler handles of errors that may occur when working with the database.

func NewPool

func NewPool(cfg *PoolConfig) (*pgxpool.Pool, error)

NewPool returns a new postgres driver connection pool.

Types

type Driver

type Driver interface {
	// Begin acquires a connection from the Pool and starts a transaction. Unlike database/sql, the
	// context only affects the begin command. i.e. there is no auto-rollback on context cancellation.
	// Begin initiates a transaction block without explicitly setting a transaction mode for the block
	// (see BeginTx with TxOptions if transaction mode is required). *pgxpool.Tx is returned, which
	// implements the pgx.Tx interface. Commit or Rollback must be called on the returned transaction
	// to finalize the transaction block.
	Begin(ctx context.Context) (pgx.Tx, error)

	// Query acquires a connection and executes a query that returns pgx.Rows. Arguments should be
	// referenced positionally from the SQL string as $1, $2, etc. See pgx.Rows documentation to
	// close the returned Rows and return the acquired connection to the Pool.
	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)

	// QueryRow acquires a connection and executes a query that is expected to return at most one
	// row (pgx.Row). Errors are deferred until pgx.Row's Scan method is called. If the query selects
	//  no rows, pgx.Row's Scan will return ErrNoRows. Otherwise, pgx.Row's Scan scans the first
	// selected row and discards the rest. The acquired connection is returned to the Pool when
	// pgx.Row's Scan method is called.
	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row

	// Exec acquires a connection from the Pool and executes the given SQL. SQL can be either a
	// prepared statement name or an SQL string. Arguments should be referenced positionally from
	// the SQL string as $1, $2, etc. The acquired connection is returned to the pool when the Exec
	// function returns.
	Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)

	// Close closes all connections in the pool and rejects future Acquire calls. Blocks until all
	// connections are returned to pool and closed.
	Close()
}

Driver contains methods that can be used by the client.

type PoolConfig

type PoolConfig struct {
	// URL is used to establish a connection to the database, and it may also contain some
	// connection configuration.
	URL string

	// MaxConns and MinConns number of pool connections to the database. Without a specified
	// value, the pgx settings will be used.
	MaxConns, MinConns int32

	// Logger implementation that will be used by the driver.
	Logger tracelog.Logger
}

PoolConfig stores configurations for connecting and working with the database.

func (*PoolConfig) Configure

func (c *PoolConfig) Configure(cfg *pgxpool.Config)

Configure the postgres driver to connect to the database.

Jump to

Keyboard shortcuts

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