Documentation
¶
Index ¶
- Variables
- func ConstantDelayRetryPolicy(interval time.Duration, retryAttempts ...uint64) backoff.BackOff
- func E(ctx context.Context, db ExecContexter, query string, options *Options, ...) (sql.Result, error)
- func ExponentialRetryPolicy(maxElapsedTime time.Duration, retryAttempts ...uint64) backoff.BackOff
- func FlattenArgs(args ...interface{}) []interface{}
- func INSERT(tableName string, columns []string, rows int, dbtype ...Database) stringdeprecated
- func INSERTStmt(tableName string, columns []string, rows int, dbtype ...Database) string
- func MustE(ctx context.Context, db ExecContexter, query string, options *Options, ...) sql.Result
- func MustQ(ctx context.Context, db interface{}, query string, options *Options, ...) interface{}
- func MustQs(ctx context.Context, db interface{}, query string, ConcreteStruct interface{}, ...) interface{}
- func Ph(nCols, nRows int, incr int, dbtype ...Database) string
- func Q(ctx context.Context, db interface{}, query string, options *Options, ...) (out interface{}, rErr error)
- func Qs(ctx context.Context, db interface{}, query string, ConcreteStruct interface{}, ...) (out interface{}, rErr error)
- func Struct(strct interface{}, tagName ...string) []interface{}
- func Tx(ctx context.Context, db interface{}, ...) error
- type BeginTxer
- type Database
- type EFn
- type ExecContexter
- type Options
- func (o *Options) MustQ(ctx context.Context, db interface{}, query string, args ...interface{}) interface{}
- func (o *Options) MustQs(ctx context.Context, db interface{}, query string, ConcreteStruct interface{}, ...) interface{}
- func (o *Options) Q(ctx context.Context, db interface{}, query string, args ...interface{}) (out interface{}, rErr error)
- func (o *Options) Qs(ctx context.Context, db interface{}, query string, ConcreteStruct interface{}, ...) (out interface{}, rErr error)
- type PostUnmarshaler
- type QFn
- type QueryContexter
- type SQLBasic
- type ScanFaster
- type StructorConfig
- type TxCommit
Constants ¶
This section is empty.
Variables ¶
var SingleResult = &Options{SingleResult: true}
SingleResult is a convenient option for the common case of expecting a single result from a query.
Functions ¶
func ConstantDelayRetryPolicy ¶ added in v2.2.0
ConstantDelayRetryPolicy is a retry policy with constant intervals between each retry attempt. It will retry forever unless restricted by retryAttempts.
See: https://godoc.org/gopkg.in/cenkalti/backoff.v4#ConstantBackOff
func E ¶
func E(ctx context.Context, db ExecContexter, query string, options *Options, args ...interface{}) (sql.Result, error)
E is used for "Exec" queries such as insert, update and delete.
args is a list of values to replace the placeholders in the query. When an arg is a slice, the values of the slice will automatically be flattened to a list of interface{}.
func ExponentialRetryPolicy ¶ added in v2.2.0
ExponentialRetryPolicy is a retry policy with exponentially increasing intervals between each retry attempt. If maxElapsedTime is 0, it will retry forever unless restricted by retryAttempts.
See: https://godoc.org/gopkg.in/cenkalti/backoff.v4#ExponentialBackOff
func FlattenArgs ¶ added in v2.5.0
func FlattenArgs(args ...interface{}) []interface{}
FlattenArgs will accept a list of values and flatten any slices encountered.
Example:
args1 := []string{"A", "B", "C"} args2 := []interface{}{2, "D"} args3 := dbq.Struct(Row{"Brad Pitt", 45, time.Now()}) dbq.FlattenArgs(args1, args2, args3) // Output: []interface{}{"A", "B", "C", 2, "D", "Brad Pitt", 45, time.Now()}
func INSERTStmt ¶ added in v2.2.0
INSERTStmt will generate an INSERT statement. It can be used for bulk inserts.
NOTE: You may have to escape the column names. For MySQL, use backticks. Databases also have a limit to the number of query placeholders you can have. This will limit the number of rows you can insert.
func MustE ¶
func MustE(ctx context.Context, db ExecContexter, query string, options *Options, args ...interface{}) sql.Result
MustE is a wrapper around the E function. It will panic upon encountering an error. This can erradicate boiler-plate error handing code.
func MustQ ¶
func MustQ(ctx context.Context, db interface{}, query string, options *Options, args ...interface{}) interface{}
MustQ is a wrapper around the Q function. It will panic upon encountering an error. This can erradicate boiler-plate error handing code.
func MustQs ¶ added in v2.3.0
func MustQs(ctx context.Context, db interface{}, query string, ConcreteStruct interface{}, options *Options, args ...interface{}) interface{}
MustQs is a wrapper around the Qs function. It will panic upon encountering an error. This can erradicate boiler-plate error handing code.
func Ph ¶
Ph generates the placeholders for SQL queries. For a bulk insert operation, nRows is the number of rows you intend to insert, and nCols is the number of fields per row. For the IN function, set nRows to 1. For PostgreSQL, you can use incr to increment the placeholder starting count.
NOTE: The function panics if either nCols or nRows is 0.
Example:
dbq.Ph(3, 1, 0) // Output: ( ?,?,? ) dbq.Ph(3, 2, 0) // Output: ( ?,?,? ),( ?,?,? ) dbq.Ph(3, 2, 6, dbq.PostgreSQL) // Output: ($7,$8,$9),($10,$11,$12)
func Q ¶
func Q(ctx context.Context, db interface{}, query string, options *Options, args ...interface{}) (out interface{}, rErr error)
Q is used for querying a SQL database. A []map[string]interface{} is ordinarily returned. Each returned row (an item in the slice) contains a map where the keys are the columns, and the values are the data for each column. However, when a ConcreteStruct is provided via the options, the mapstructure package is used to automatically return []*struct instead. To bypass the mapstructure package, ScanFaster interface can be implemented.
args is a list of values to replace the placeholders in the query. When an arg is a slice, the values of the slice will automatically be flattened to a list of interface{}.
NOTE: sql.ErrNoRows is never returned as an error: A slice is always returned, unless the behavior is modified by the SingleResult Option.
func Qs ¶ added in v2.3.0
func Qs(ctx context.Context, db interface{}, query string, ConcreteStruct interface{}, options *Options, args ...interface{}) (out interface{}, rErr error)
Qs operates the same as Q except it requires you to provide a ConcreteStruct as an argument. This allows you to recycle common options and conveniently provide a different ConcreteStruct.
func Struct ¶
func Struct(strct interface{}, tagName ...string) []interface{}
Struct converts the fields of the struct into a slice of values. You can use it to convert a struct into the placeholder arguments required by the Q and E function. tagName is used to indicate the struct tag (default is "dbq"). The function panics if strct is not an actual struct.
func Tx ¶ added in v2.2.0
func Tx(ctx context.Context, db interface{}, fn func(tx interface{}, Q QFn, E EFn, txCommit TxCommit), retryPolicy ...backoff.BackOff) error
Tx is used to perform an arbitrarily complex operation and not have to worry about rolling back a transaction. The transaction is automatically rolled back unless explicitly committed by calling txCommit. tx is only exposed for performance purposes. Do not use it to commit or rollback.
NOTE: Until this note is removed, this function is not necessarily backward compatible.
Example:
ctx := context.Background() pool, _ := sql.Open("mysql", "user:password@tcp(localhost:3306)/db") dbq.Tx(ctx, pool, func(tx interface{}, Q dbq.QFn, E dbq.EFn, txCommit dbq.TxCommit) { stmt := dbq.INSERTStmt("table", []string{"name", "age", "created_at"}, 1) res, err := E(ctx, stmt, nil, "test name", 34, time.Now()) if err != nil { return // Automatic rollback } txCommit() })
Types ¶
type Database ¶
type Database int
Database is used to set the Database. Different databases have different syntax for placeholders etc.
type EFn ¶ added in v2.2.0
type EFn func(ctx context.Context, query string, options *Options, args ...interface{}) (sql.Result, error)
EFn is shorthand for E. It will automatically use the appropriate transaction.
type ExecContexter ¶
type ExecContexter interface {
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
}
ExecContexter is for modifying the database state.
type Options ¶
type Options struct { // ConcreteStruct can be set to any concrete struct (not a pointer). // When set, the mapstructure package is used to convert the returned // results automatically from a map to a struct. The `dbq` struct tag // can be used to map column names to the struct's fields. // // See: https://godoc.org/github.com/mitchellh/mapstructure ConcreteStruct interface{} // DecoderConfig is used to configure the decoder used by the mapstructure // package. If it's not supplied, a default StructorConfig is assumed. This means // WeaklyTypedInput is set to true and no DecodeHook is provided. Alternatively, if you require // a configuration for common time-based conversions, StdTimeConversionConfig is available. // // See: https://godoc.org/github.com/mitchellh/mapstructure DecoderConfig *StructorConfig // SingleResult can be set to true if you know the query will return at most 1 result. // When true, a nil is returned if no result is found. Alternatively, it will return the // single result directly (instead of wrapped in a slice). This makes it easier to // type assert. SingleResult bool // PostFetch is called after all results are fetched but before PostUnmarshaler is called (if applicable). // It can be used to return a database connection back to the pool. PostFetch func(ctx context.Context) error // ConcurrentPostUnmarshal can be set to true if PostUnmarshal must be called concurrently. ConcurrentPostUnmarshal bool // RawResults can be set to true for results to be returned unprocessed ([]byte). // This option does nothing if ConcreteStruct is provided. RawResults bool // RetryPolicy can be set if you want to retry the query in the event of failure. // // Example: // // dbq.ExponentialRetryPolicy(60 * time.Second, 3) // RetryPolicy backoff.BackOff }
Options is used to modify the default behavior.
func (*Options) MustQ ¶ added in v2.3.0
func (o *Options) MustQ(ctx context.Context, db interface{}, query string, args ...interface{}) interface{}
MustQ is a convenience function that calls dbq.MustQ. It allows you to recycle common options.
func (*Options) MustQs ¶ added in v2.3.0
func (o *Options) MustQs(ctx context.Context, db interface{}, query string, ConcreteStruct interface{}, args ...interface{}) interface{}
MustQs is a convenience function that calls dbq.MustQs. It allows you to recycle common options.
type PostUnmarshaler ¶
type PostUnmarshaler interface { // PostUnmarshal is called for each row after all results have been fetched. // You can use it to further modify the values of each ConcreteStruct. PostUnmarshal(ctx context.Context, row, total int) error }
PostUnmarshaler allows you to further modify all results after unmarshaling. The ConcreteStruct pointer must implement this interface to make use of this feature.
Example:
type user struct { ID int `dbq:"id"` Name string `dbq:"name"` HashedID string `dbq:"-"` // Obfuscated ID } func (u *user) PostUnmarshal(ctx context.Context, row, total int) error { u.HashedID = obfuscate(u.ID) return nil }
type QFn ¶ added in v2.2.0
type QFn func(ctx context.Context, query string, options *Options, args ...interface{}) (interface{}, error)
QFn is shorthand for Q. It will automatically use the appropriate transaction.
type QueryContexter ¶
type QueryContexter interface {
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
}
QueryContexter is for querying the database.
type SQLBasic ¶
type SQLBasic interface { ExecContexter QueryContexter }
SQLBasic allows for querying and executing statements.
type ScanFaster ¶ added in v2.4.0
type ScanFaster interface { // ScanFast is used to directly scan the results from the query to the ConcreteStruct pointer. // The number of columns returned from the query must match the length of the slice returned. // // See: https://golang.org/pkg/database/sql/#Rows.Scan // // WARNING: "SELECT * FROM ..." may return more columns in the future if the table structure changes. ScanFast() []interface{} }
ScanFaster eradicates the use of the reflect package when unmarshaling. The ConcreteStruct pointer must implement this interface to make use of this feature. If you don't need to perform fancy time conversions or interpret weakly typed data (via mapstructure pkg), then this is the recommended approach as it is more performant.
Example:
type user struct { ID int `dbq:"id"` Name string `dbq:"name"` } func (u *user) ScanFast() []interface{} { return []interface{}{&u.ID, &u.Name} }
type StructorConfig ¶
type StructorConfig struct { // DecodeHook, if set, will be called before any decoding and any // type conversion (if WeaklyTypedInput is on). This lets you modify // the values before they're set down onto the resulting struct. // // If an error is returned, the entire decode will fail with that // error. DecodeHook mapstructure.DecodeHookFunc // If WeaklyTypedInput is true, the decoder will make the following // "weak" conversions: // // - bools to string (true = "1", false = "0") // - numbers to string (base 10) // - bools to int/uint (true = 1, false = 0) // - strings to int/uint (base implied by prefix) // - int to bool (true if value != 0) // - string to bool (accepts: 1, t, T, TRUE, true, True, 0, f, F, // FALSE, false, False. Anything else is an error) // - empty array = empty map and vice versa // - negative numbers to overflowed uint values (base 10) // - slice of maps to a merged map // - single values are converted to slices if required. Each // element is weakly decoded. For example: "4" can become []int{4} // if the target type is an int slice. // WeaklyTypedInput bool }
StructorConfig is used to expose a subset of the configuration options provided by the mapstructure package.
See: https://godoc.org/github.com/mitchellh/mapstructure#DecoderConfig
func StdTimeConversionConfig ¶
func StdTimeConversionConfig(dbtype ...Database) *StructorConfig
StdTimeConversionConfig provides a standard configuration for unmarshaling to time-related fields in a struct. It properly converts timestamps and datetime columns into time.Time objects. It assumes a MySQL database as default.