Documentation ¶
Overview ¶
Package query implements helpers around database/sql to execute various kinds of very common SQL queries.
Index ¶
- Variables
- func Count(tx database.Tx, table, where string, args ...interface{}) (int, error)
- func DeleteConfig(tx database.Tx, table string, keys []string) error
- func DeleteObject(tx database.Tx, table string, id int64) (bool, error)
- func InsertStrings(tx database.Tx, stmt string, values []string) error
- func IsRetriableError(err error) bool
- func Params(n int) string
- func Retry(sleeper clock.Sleeper, f func() error) error
- func SelectConfig(tx database.Tx, table string, where string, args ...interface{}) (map[string]string, error)
- func SelectIntegers(tx database.Tx, query string, args ...interface{}) ([]int, error)
- func SelectObjects(tx database.Tx, dest Dest, query string, args ...interface{}) error
- func SelectStrings(tx database.Tx, query string, args ...interface{}) ([]string, error)
- func Transaction(db database.DB, f func(database.Tx) error) error
- func UpdateConfig(tx database.Tx, table string, values map[string]string) error
- func UpsertConfig(tx database.Tx, table string, values map[string]string) error
- func UpsertObject(tx database.Tx, table string, columns []string, values []interface{}) (int64, error)
- type Dest
Constants ¶
This section is empty.
Variables ¶
var StmtCount = func(table, where string) string { stmt := fmt.Sprintf("SELECT COUNT(*) FROM %s", table) if where != "" { stmt += fmt.Sprintf(" WHERE %s", where) } return stmt }
StmtCount provides a function for creating the sql template for querying.
Functions ¶
func DeleteConfig ¶
DeleteConfig defines a way to delete the given key rows from the given config table.
func DeleteObject ¶
DeleteObject removes the row identified by the given ID. The given table must have a primary key column called 'id'.
It returns a flag indicating if a matching row was actually found and deleted or not.
func InsertStrings ¶
InsertStrings inserts a new row for each of the given strings, using the given insert statement template, which must define exactly one insertion column and one substitution placeholder for the values. For example: InsertStrings(tx, "INSERT INTO foo(name) VALUES %s", []string{"bar"}).
func IsRetriableError ¶
IsRetriableError returns true if the given error might be transient and the interaction can be safely retried.
func Params ¶
Params returns a parameters expression with the given number of '?' placeholders. E.g. Params(2) -> "(?, ?)". Useful for IN and VALUES expressions.
func Retry ¶
Retry wraps a function that interacts with the database, and retries it in case a transient error is hit.
This should by typically used to wrap transactions.
func SelectConfig ¶
func SelectConfig(tx database.Tx, table string, where string, args ...interface{}) (map[string]string, error)
SelectConfig executes a query statement against a "config" table, which must have 'key' and 'value' columns. By default this query returns all keys, but additional WHERE filters can be specified.
Returns a map of key names to their associated values.
func SelectIntegers ¶
SelectIntegers executes a statement which must yield rows with a single integer column. It returns the list of column values.
func SelectObjects ¶
SelectObjects executes a statement which must yield rows with a specific columns schema. It invokes the given Dest hook for each yielded row.
func SelectStrings ¶
SelectStrings executes a statement which must yield rows with a single string column. It returns the list of column values.
func Transaction ¶
Transaction executes the given function within a database transaction.
func UpdateConfig ¶
UpdateConfig updates the given keys in the given table. Config keys set to empty values will be deleted.
func UpsertConfig ¶
UpsertConfig defines a way to Insert or updates the key/value rows of the given config table.
func UpsertObject ¶
func UpsertObject(tx database.Tx, table string, columns []string, values []interface{}) (int64, error)
UpsertObject inserts or replaces a new row with the given column values, to the given table using columns order. For example:
UpsertObject(tx, "cars", []string{"id", "brand"}, []interface{}{1, "ferrari"})
The number of elements in 'columns' must match the one in 'values'.