transactions

package
v1.2.4 Latest Latest
Warning

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

Go to latest
Published: May 7, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrObjsEmpty = errors.New("`objs` is empty")
)

Functions

func Insert added in v1.1.0

func Insert(db *sql.DB, ctx context.Context, query squirrel.InsertBuilder) (sql.Result, error)

Insert executes the given query.

If bound arguments are saved in the query builder, they are used.

func InsertObjects added in v1.1.0

func InsertObjects(db *sql.DB, ctx context.Context, query squirrel.InsertBuilder, objs []interface{}) ([]sql.Result, error)

InsertObjects executes the given query for every object provided.

If bound arguments are saved in the query builder, they are prepended to query parameters.

func Select added in v1.1.0

func Select(db *sql.DB, ctx context.Context, query squirrel.SelectBuilder, m mapping.Mapping) (interface{}, error)

Select runs a given query. If a `limit` is set on the given SelectBuilder instance, only `limit` number of rows are fetched. If an error is encounted, the error and all results up until the error are returned.

Results are returned as an interface{} castable to []*<type>, example:

type MyType struct {
	// ...
}

func main() {
	// ...
	r, err := transactions.Select(db, nil, query, mapping.GetMapping(MyType{}))
	// ... some err handling
	results := r.([]*MyType)
}

func SelectOne added in v1.1.0

func SelectOne(db *sql.DB, ctx context.Context, query squirrel.SelectBuilder, m mapping.Mapping) (interface{}, error)

SelectOne runs a given query with limit 1 agains the given database within a read-only transaction, returning the mapped result or any error that occured.

func Update added in v1.1.0

func Update(db *sql.DB, ctx context.Context, query squirrel.UpdateBuilder) (sql.Result, error)

Update executes the given query.

If bound arguments are saved in the query builder, they are used.

func UpdateObjects added in v1.1.0

func UpdateObjects(db *sql.DB, ctx context.Context, query squirrel.UpdateBuilder, objs []interface{}) ([]sql.Result, error)

UpdateObjects executes the given query len(objs) times.

UpdateObjects uses mapping.ValuesOf(obj) to get the args for each execution. Any bound values associated with the query builder are prepended to object values.

Return value numExecutions contains the value ob to the first error encountered, if any. Rows affected contains the total of rows affected.

func WithReadTx added in v1.1.0

func WithReadTx(db *sql.DB, ctx context.Context, fn TransactionHandler) (interface{}, error)

WithReadTx is a convenience wrapper around WithTx, defining the sql-opts as read-only.

See WithTx documentation for information about parameters and return value.

func WithTx added in v1.1.0

func WithTx(db *sql.DB, ctx context.Context, opts *sql.TxOptions, fn TransactionHandler) (interface{}, error)

WithTx starts a new transaction with ctx as parent context and opts specifying the sql options to use. The given transaction handler function is executed in the context of the transaction and provided with a child scoped context and transaction handle.

If the supplied context is nil, an empty no-op context is automatically created.

Returns any errors and the result as provided by the transaction handler func.

func WithWriteTx added in v1.1.0

func WithWriteTx(db *sql.DB, ctx context.Context, fn TransactionHandler) (interface{}, error)

WithWriteTx is a convenience wrapper around WithTx.

See WithTx documentation for information about parameters and return value.

Types

type TransactionHandler added in v1.1.0

type TransactionHandler = func(ctx context.Context, tx *sql.Tx) (result interface{}, err error)

Jump to

Keyboard shortcuts

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