pg_util

package module
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2022 License: MIT Imports: 9 Imported by: 0

README

pg_util

PostgreSQL utilities for Go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildInsert added in v0.7.0

func BuildInsert(o InsertOpts) (sql string, args []interface{})

Build and cache insert statement for all fields of data. This includes embedded struct fields.

See InsertOpts for further documentation.

func ExecAll

func ExecAll(ctx context.Context, tx pgx.Tx, q ...string) error

Execute all SQL statement strings and return on first error, if any.

func ExtractException added in v1.1.0

func ExtractException(err error) string

Try to extract an exception message, if err is *pgconn.PgError

func InTransaction

func InTransaction(
	ctx context.Context,
	conn TxStarter,
	fn func(pgx.Tx) error,
) (err error)

InTransaction runs a function inside a transaction and handles commiting and rollback on error.

Can also be used for nested pseudotransactions via savepoints.

ctx: Context to bind the query to conn: Anything, that can start a new transaction or subtransaction. fn: Function to execute on the transaction.

func Listen

func Listen(opts ListenOpts) (err error)

Listen assigns a function to listen to Postgres notifications on a channel

Types

type InsertOpts added in v0.7.0

type InsertOpts struct {
	// Table to insert into
	Table string

	// Struct that will have all its public fields written to the database.
	//
	// Use `db:"name"` to override the default name of a column.
	//
	// Tags with ",string" after the name will be converted to a string before
	// being passed to the driver. This is useful in some cases like encoding to
	// Postgres domains. This also works, if the name part of the tag is empty.
	// Examples: `db:"name,string"` `db:",string"`
	//
	// Fields with a `db:"-"` tag will be skipped
	//
	// First the fields in struct itself are scanned and then the fields in any
	// embedded structs using depth first search.
	// If duplicate column names (from the struct field name or `db` struct tag)
	// exist, the first found value will ber used.
	Data interface{}

	// Optional prefix to statement
	Prefix string

	// Optional suffix to statement
	Suffix string
}

Options for building insert statement

type ListenOpts

type ListenOpts struct {
	// Prevent identical messages from triggering the handler for up to
	// DebounceInterval. If 0, all messages trigger the handler.
	DebounceInterval time.Duration

	// URL to connect to the database on. Required.
	ConnectionURL string

	// Channel to listen on. Required.
	Channel string

	// Message handler. Required.
	OnMsg func(msg string) error

	// Optional error handler
	OnError func(err error)

	// Optional handler for database connection loss. The connection will be
	// automatically reestablished regardless, but this can be used to hook
	// extra logic on the library user's side of the application.
	OnConnectionLoss func()

	// Optional handler for reconnection after database connection loss
	OnReconnect func()

	// Optional context for cancelling listening
	Context context.Context
}

Options for calling Listen()

type TxStarter added in v0.7.1

type TxStarter interface {
	Begin(context.Context) (pgx.Tx, error)
}

Interface required to start a transaction or subtransation via savepoints

Jump to

Keyboard shortcuts

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