db

package
v0.0.0-...-1388524 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2020 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

db contains database-related utilities for Postgres.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Conn

func Conn() *sqlx.DB

Conn returns a new database connection for the judging database.

func InTransaction

func InTransaction(ctx context.Context, fn func(*sqlx.Tx) error) error

InTransaction executes the given function within a transaction with serializable isolation. If the function returns an error, the transaction will be rollbacked, otherwise committed. Errors from the function will be propagated upwards unchanged.

func NewListener

func NewListener() *pq.Listener

NewListener returns a new Postgres notify listener.

func RetriableError

func RetriableError(err error) bool

RetriableError checks if the given Postgres error should be retried. Generally, this indicates the user attempted a transaction that conflicted with a concurrent transaction that should be retried.

func SetInParam

func SetInParam(str string, params *[]interface{}, param []interface{}) string

SetInParam appends a slice to the parameter list and updates the query string with the correct bindvar. For example, if first three parameters are being set, the string "id IN (%s)" would become "id IN ($1,$2,$3)"

func SetInParamInt

func SetInParamInt(str string, params *[]interface{}, param []int32) string

SetInParamInt calls SetInParam, but first converts the int32 param slice.

func SetParam

func SetParam(query string, params *[]interface{}, param interface{}) string

For example, if the 3rd parameter is being set, the string "id = %s" would become "id = $3"

Types

type PgError

type PgError int

A PgError is a Postgres-specific error that can be returned by queries.

const (
	NotPgError PgError = iota
	OtherError
	SerializationFailure
	DeadlockDetected
	UniquenessViolation
)

func PgErrCode

func PgErrCode(err error) PgError

PgErrCode converts an error returned from a transaction into a typed Postgres error.

Jump to

Keyboard shortcuts

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