Documentation
¶
Overview ¶
Package querysql is a newer interface to the capabilities in query ("query v2" in a sense). Intention is to also replace misc testutils.Scan functions ec. May be separated into another repo longer term, but is convenient to test it together with testdb package for now.
Index ¶
- Variables
- func Call[T any](visit func(row T) error) func() Result[int]
- func DeepFieldNames(v interface{}) []string
- func DeepFieldPointers(obj interface{}) []interface{}
- func Exec(querier CtxQuerier, qry string, args ...any) (sql.Result, error)
- func ExecContext(ctx context.Context, querier CtxQuerier, qry string, args ...any) (sql.Result, error)
- func Iter[T any](ctx context.Context, querier CtxQuerier, visit func(T) error, qry string, ...) error
- func MustIter[T any](ctx context.Context, querier CtxQuerier, visit func(T) error, qry string, ...)
- func MustNext(rs *ResultSets, scanner Target)
- func MustNextResult[T any](rs *ResultSets, typ func() Result[T]) T
- func MustSingle[T any](ctx context.Context, querier CtxQuerier, qry string, args ...any) T
- func MustSlice[T any](ctx context.Context, querier CtxQuerier, qry string, args ...any) []T
- func MustStructType(v reflect.Type) reflect.Type
- func MustStructValue(v reflect.Value) reflect.Value
- func Next(rs *ResultSets, scanner Target) error
- func NextNoScanner(rs *ResultSets) error
- func NextResult[T any](rs *ResultSets, typ func() Result[T]) (T, error)
- func ParseSQLUUIDBytes(v []uint8) (uuid.UUID, error)
- func Query(targets []Target, ctx context.Context, querier CtxQuerier, qry string, ...) error
- func Query2[T1 any, T2 any](type1 func() Result[T1], type2 func() Result[T2], ctx context.Context, ...) (T1, T2, error)
- func Query3[T1 any, T2 any, T3 any](type1 func() Result[T1], type2 func() Result[T2], type3 func() Result[T3], ...) (T1, T2, T3, error)
- func Query4[T1 any, T2 any, T3 any, T4 any](type1 func() Result[T1], type2 func() Result[T2], type3 func() Result[T3], ...) (T1, T2, T3, T4, error)
- func Single[T any](ctx context.Context, querier CtxQuerier, qry string, args ...any) (T, error)
- func SingleOrNil[T any](ctx context.Context, querier CtxQuerier, qry string, args ...any) (*T, error)
- func Slice[T any](ctx context.Context, querier CtxQuerier, qry string, args ...any) ([]T, error)
- func WithDispatcher(ctx context.Context, dispatcher RowsGoDispatcher) context.Context
- func WithLogger(ctx context.Context, logger RowsLogger) context.Context
- type CtxQuerier
- type NameAndTag
- type NotImplementedSqlResult
- type QuerySqlError
- type Result
- type ResultSets
- type RowScanner
- type RowsGoDispatcher
- type RowsLogger
- type Target
Constants ¶
This section is empty.
Variables ¶
var ErrNoMoreSets = fmt.Errorf("no more result sets")
var ErrNotDone = fmt.Errorf("there are more result sets after reading last expected result")
var ManyRowsExpectedOne = QuerySqlError{ // contains filtered or unexported fields }
var ZeroRowsExpectedOne = QuerySqlError{ // contains filtered or unexported fields }
Functions ¶
func Call ¶
Call lets you provide a callback visit function that is called for each row. The returned data is the number of rows scanned.
func DeepFieldNames ¶
func DeepFieldNames(v interface{}) []string
Return names of fields of struct instance v, recursing into embedded structs (but not named struct members)
func DeepFieldPointers ¶
func DeepFieldPointers(obj interface{}) []interface{}
Return pointers to fields of struct instance v, recursing into embedded structs (but not named struct members)
func ExecContext ¶
func MustNext ¶
func MustNext(rs *ResultSets, scanner Target)
func MustNextResult ¶
func MustNextResult[T any](rs *ResultSets, typ func() Result[T]) T
func MustSingle ¶
func Next ¶
func Next(rs *ResultSets, scanner Target) error
Next reads the next result set from `rs`, passing each row to `scanner`; taking care of checking errors and advancing result sets. On errors, `rs` will be closed. If EnsureDoneAfterNext is used, `rs` will also be closed on successful return.
func NextNoScanner ¶
func NextNoScanner(rs *ResultSets) error
func NextResult ¶
func NextResult[T any](rs *ResultSets, typ func() Result[T]) (T, error)
NextResult reads the next result set from `rs`, into the type/scanner provided in the `typ` argument. Typical arguments for `typ` is `SliceOf[int]`, `SingleOf[MyStruct]`, `Call[MyStruct](func(MyStruct) error { ... })`
func SingleOrNil ¶ added in v1.0.2
func WithDispatcher ¶
func WithDispatcher(ctx context.Context, dispatcher RowsGoDispatcher) context.Context
func WithLogger ¶
func WithLogger(ctx context.Context, logger RowsLogger) context.Context
WithLogger will return the context with a logger registered for use with querysql; during queries, querysql will use Logger() to extract the logger from the context
Types ¶
type CtxQuerier ¶
type NameAndTag ¶
type NotImplementedSqlResult ¶
type NotImplementedSqlResult struct{}
func (NotImplementedSqlResult) LastInsertId ¶
func (_ NotImplementedSqlResult) LastInsertId() (int64, error)
func (NotImplementedSqlResult) RowsAffected ¶
func (_ NotImplementedSqlResult) RowsAffected() (int64, error)
type QuerySqlError ¶
type QuerySqlError struct {
// contains filtered or unexported fields
}
func (QuerySqlError) Error ¶
func (e QuerySqlError) Error() string
func (QuerySqlError) Is ¶
func (e QuerySqlError) Is(other error) bool
func (QuerySqlError) Unwrap ¶
func (e QuerySqlError) Unwrap() error
type Result ¶
type ResultSets ¶
type ResultSets struct { Rows *sql.Rows // Err is set to defer errors from constructors until the first method call Err error // Set DoneAfterNext to enable a mode where an error is returned if we did not exhaust the resultset DoneAfterNext bool // Logger is used for outputting select statements with the special log column (see README) // By default it is set by New to the value provided by Logger(ctx), but feel free to set or change it. Logger RowsLogger // TODO(dsf): Perhaps remove the LogKeyLowercase. I'm not 100% this feature is justified // By default, the use of an underscore column, "select _log=info, ...", will trigger logging // This lets you specify a custom key such as "loglevel" for the same purpose in addition. // It will be compared with the lowercase name of the column. LogKeyLowercase string // "select _function=MyFunction" will attempt to fall a Go function (in this case MyFunction) // with the remaining arguments to the select as arguments to the function call Dispatcher RowsGoDispatcher // contains filtered or unexported fields }
ResultSets is a tiny wrapper around sql.Rows to help managing whether to call NextResultSet or not. It is fine to instantiate this struct yourself.
Template methods are not allowed in Go, so the "methods" of this struct are provided as functions prefixed with ScanRow; NextStructSlice, NextInt, etc
func New ¶
func New(ctx context.Context, querier CtxQuerier, qry string, args ...any) *ResultSets
func (*ResultSets) Close ¶
func (rs *ResultSets) Close() error
func (*ResultSets) Done ¶
func (rs *ResultSets) Done() bool
func (*ResultSets) EnsureDoneAfterNext ¶
func (rs *ResultSets) EnsureDoneAfterNext() *ResultSets
EnsureDoneAfterNext sets the DoneAfterNext flag. The receiver rs is returned for syntactical brevity, a copy is not made
type RowScanner ¶
type RowScanner[T any] struct { // contains filtered or unexported fields }
type RowsGoDispatcher ¶
RowsGoDispatcher takes a sql.Rows and calls a Go function. The first argument, __function is the function name, the other arguments are the arguments to the Go function.
func Dispatcher ¶
func Dispatcher(ctx context.Context) RowsGoDispatcher
func GoMSSQLDispatcher ¶
func GoMSSQLDispatcher(fs []interface{}) RowsGoDispatcher
type RowsLogger ¶
RowsLogger takes a sql.Rows and logs it. A default implementation is available, but sometimes one might wish to improve on the formatting of different data types, hence this low level interface is available.
The convention is that the first column will always contain the log level.
func Logger ¶
func Logger(ctx context.Context) RowsLogger
func LogrusMSSQLLogger ¶
func LogrusMSSQLLogger(logger logrus.FieldLogger, defaultLogLevel logrus.Level) RowsLogger
LogrusMSSQLLogger returns a basic RowsLogger suitable for the combination of MS SQL and logrus
type Target ¶
func SingleInto ¶
SingleInto set up reading a single row into `target`. If there is not exactly 1 row in the resultset an error is returned.