pge

package module
v0.0.0-...-7d9afc7 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2022 License: Apache-2.0 Imports: 15 Imported by: 0

README

pge

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// "1" is the number we arbitrarily choose for the schema migrations lock
	LockMigrations = NewQuery("acquire advisory lock for schema migrations", `
		SELECT pg_advisory_xact_lock(1)
	`)

	CreateTableSchemaVersion = NewQuery("create table schema_versions", `
		CREATE TABLE IF NOT EXISTS schema_versions (
			version int,
			migrated timestamptz NOT NULL DEFAULT NOW(),
			PRIMARY KEY (version, migrated)
		)
	`)

	SelectLatestSchemaVersion = NewQuery("select latest schema version", `
		SELECT version
		FROM schema_versions
		ORDER BY migrated DESC
		LIMIT 1
	`)

	InsertSchemaVersion = NewQuery("insert schema version", `
		INSERT INTO schema_versions(version)
		VALUES ($1)
	`)
)

Functions

func Bulk

func Bulk(total, batch int, fn func(i, j int) error) (n int, err error)

func BulkInsert

func BulkInsert(ctx context.Context, numRows int, q queryable, query Query, fn func(int) []interface{}) (results []interface{}, err error)

func Listen

func Listen(ctx context.Context, conn *pgx.Conn, channel string) error

Listen takes a *pgx.Conn as an argument because we want the LISTEN to be effective for a specific connection.

func Unlisten

func Unlisten(ctx context.Context, conn *pgx.Conn, channel string) error

Unlisten takes a *pgx.Conn as an argument because we want the UNLISTEN to be effective for a specific connection.

Types

type Conn

type Conn interface {
	Execute(ctx context.Context, query Query, args ...interface{}) (pgconn.CommandTag, error)

	Get(ctx context.Context, dst interface{}, query Query, args ...interface{}) error

	Select(ctx context.Context, dst interface{}, query Query, args ...interface{}) error

	PaginatedSelect(ctx context.Context, dst interface{}, query Query, args ...interface{}) (Cursors, error)
}

type Cursors

type Cursors struct {
	// ...after <cursor> (optional)
	AfterCursor string
	// ...or before <cursor> (optional)
	BeforeCursor string
}

Cursors can either be used as part of pagination input to a SELECT query (as part of a Paginator), or new cursors provided as a result of the SELECT.

type Migration

type Migration struct {
	Name    string
	Queries []Query
}

type Order

type Order string
const (
	Ascending  Order = "ASC"
	Descending Order = "DESC"
)

func (Order) Reverse

func (o Order) Reverse() Order

type Paginator

type Paginator struct {
	// as in "get the first <n> results..."
	First int64
	// as in "get the last <n> results..."
	Last int64

	Cursors
}

Paginator provides pagination parameters for a select query.

func (Paginator) QueryParams

func (p Paginator) QueryParams() url.Values

QueryParams returns query parameters which encode the pagination settings and offset.

type Query

type Query struct {
	Name string
	// contains filtered or unexported fields
}

func NewQuery

func NewQuery(name, query string, opts ...QueryOption) Query

func (Query) AscendingOrDescending

func (q Query) AscendingOrDescending() Order

func (Query) CursorColumn

func (q Query) CursorColumn() string

func (Query) CursorFromString

func (q Query) CursorFromString(cursor string) (interface{}, error)

CursorFromString converts a cursor from a string to the format we want to compare in the DB query.

func (Query) NextParamNumber

func (q Query) NextParamNumber() int

func (Query) String

func (q Query) String() string

func (Query) WithPaginator

func (q Query) WithPaginator(p *Paginator) (Query, error)

func (Query) WithPrefix

func (q Query) WithPrefix(prefix string) Query

func (Query) WithSuffix

func (q Query) WithSuffix(suffix string) Query

func (Query) WithTemplateParameter

func (q Query) WithTemplateParameter(param string, paramValue interface{}) Query

func (Query) WithValues

func (q Query) WithValues(rows int) Query

type QueryError

type QueryError struct {
	Query Query
	Err   error
}

func (*QueryError) Error

func (e *QueryError) Error() string

func (*QueryError) Unwrap

func (e *QueryError) Unwrap() error

type QueryOption

type QueryOption func(*Query)

func WithCursorColumn

func WithCursorColumn(column string, paramNumber int, ascendingOrDescending Order) QueryOption

func WithCursorFromString

func WithCursorFromString(cursorFromString func(string) (interface{}, error)) QueryOption

func WithInsertColumns

func WithInsertColumns(cols int) QueryOption

func WithSuffix

func WithSuffix(suffix string) QueryOption

type Store

type Store interface {
	Conn
	io.Closer

	Migrate(ctx context.Context, migrations []Migration) error

	Tx(ctx context.Context, fn func(tx Tx) error, opts ...TxOption) error
}

func NewStore

func NewStore(ctx context.Context, cfg *pgxpool.Config) (Store, error)

type Tx

type Tx interface {
	pgx.Tx
	Conn
}

type TxInfo

type TxInfo struct {
	// contains filtered or unexported fields
}

type TxOption

type TxOption func(*TxInfo)

func WithAccessMode

func WithAccessMode(mode pgx.TxAccessMode) TxOption

func WithDeferrableMode

func WithDeferrableMode(mode pgx.TxDeferrableMode) TxOption

func WithIsoLevel

func WithIsoLevel(level pgx.TxIsoLevel) TxOption

Directories

Path Synopsis
sql

Jump to

Keyboard shortcuts

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