Documentation
¶
Index ¶
- Variables
- func Insert(db *sql.DB, ctx context.Context, query squirrel.InsertBuilder) (sql.Result, error)
- func InsertObjects(db *sql.DB, ctx context.Context, query squirrel.InsertBuilder, ...) ([]sql.Result, error)
- func Select(db *sql.DB, ctx context.Context, query squirrel.SelectBuilder, ...) (interface{}, error)
- func SelectOne(db *sql.DB, ctx context.Context, query squirrel.SelectBuilder, ...) (interface{}, error)
- func Update(db *sql.DB, ctx context.Context, query squirrel.UpdateBuilder) (sql.Result, error)
- func UpdateObjects(db *sql.DB, ctx context.Context, query squirrel.UpdateBuilder, ...) ([]sql.Result, error)
- func WithReadTx(db *sql.DB, ctx context.Context, fn TransactionHandler) (interface{}, error)
- func WithTx(db *sql.DB, ctx context.Context, opts *sql.TxOptions, fn TransactionHandler) (interface{}, error)
- func WithWriteTx(db *sql.DB, ctx context.Context, fn TransactionHandler) (interface{}, error)
- type TransactionHandler
Constants ¶
This section is empty.
Variables ¶
var (
ErrObjsEmpty = errors.New("`objs` is empty")
)
Functions ¶
func Insert ¶ added in v1.1.0
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
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
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
WithWriteTx is a convenience wrapper around WithTx.
See WithTx documentation for information about parameters and return value.