pg_util

package module
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2020 License: MIT Imports: 8 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.

ctx: Context to bind the query to. Defaults to context.Background().

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. Defaults to context.Background(). 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

func ResuseArgs added in v0.7.0

func ResuseArgs(args []interface{})

Put argument slice back into the pool for reuse. args must not be used after this.

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.
	// 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