Documentation ¶
Overview ¶
Package dbrutil provides utilities and helpers for dbr query builder.
Index ¶
- func NewContextWithTxRunner(parentCtx context.Context, txRunner TxRunner) context.Context
- func NewContextWithTxRunnerByKey(parentCtx context.Context, txRunner TxRunner, ctxKey interface{}) context.Context
- func Open(cfg *dbkit.Config, ping bool, eventReceiver dbr.EventReceiver) (*dbr.Connection, error)
- func ParseAnnotationInQuery(query, prefix string, modifier func(string) string) string
- func TxReadOnlyRunnerMiddleware(dbConn *dbr.Connection, isolationLevel sql.IsolationLevel) func(next http.Handler) http.Handler
- func TxReadOnlyRunnerMiddlewareWithOpts(dbConn *dbr.Connection, isolationLevel sql.IsolationLevel, ...) func(next http.Handler) http.Handler
- func TxRunnerMiddleware(dbConn *dbr.Connection, isolationLevel sql.IsolationLevel) func(next http.Handler) http.Handler
- func TxRunnerMiddlewareWithOpts(dbConn *dbr.Connection, isolationLevel sql.IsolationLevel, ...) func(next http.Handler) http.Handler
- type CompositeEventReceiver
- func (r *CompositeEventReceiver) Event(eventName string)
- func (r *CompositeEventReceiver) EventErr(eventName string, err error) error
- func (r *CompositeEventReceiver) EventErrKv(eventName string, err error, kvs map[string]string) error
- func (r *CompositeEventReceiver) EventKv(eventName string, kvs map[string]string)
- func (r *CompositeEventReceiver) Timing(eventName string, nanoseconds int64)
- func (r *CompositeEventReceiver) TimingKv(eventName string, nanoseconds int64, kvs map[string]string)
- type NewTxRunnerFunc
- type QueryMetricsEventReceiver
- type QueryMetricsEventReceiverOpts
- type RetryableTxSession
- type SlowQueryLogEventReceiver
- type SlowQueryLogEventReceiverOpts
- type TxBeginError
- type TxCommitError
- type TxRollbackError
- type TxRunner
- func GetTxRunnerFromContext(ctx context.Context) TxRunner
- func GetTxRunnerFromContextByKey(ctx context.Context, ctxKey interface{}) TxRunner
- func NewRetryableTxRunner(conn *dbr.Connection, opts *sql.TxOptions, eventReceiver dbr.EventReceiver, ...) TxRunner
- func NewTxRunner(conn *dbr.Connection, opts *sql.TxOptions, eventReceiver dbr.EventReceiver) TxRunner
- type TxRunnerMiddlewareOpts
- type TxSession
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewContextWithTxRunner ¶
NewContextWithTxRunner creates a new context with TxRunner.
func NewContextWithTxRunnerByKey ¶
func NewContextWithTxRunnerByKey(parentCtx context.Context, txRunner TxRunner, ctxKey interface{}) context.Context
NewContextWithTxRunnerByKey creates a new context and put TxRunner there by specified key.
func Open ¶
Open opens database (using dbr query builder) with specified configuration parameters and verifies (if ping argument is true) that connection can be established.
func ParseAnnotationInQuery ¶
ParseAnnotationInQuery parses annotation from comments in SQL query with specified prefix. If SQL query contains multiple annotations, they will be concatenated with "|" character.
func TxReadOnlyRunnerMiddleware ¶
func TxReadOnlyRunnerMiddleware(dbConn *dbr.Connection, isolationLevel sql.IsolationLevel) func(next http.Handler) http.Handler
TxReadOnlyRunnerMiddleware is a middleware that injects TxRunner to the request's context (readonly).
func TxReadOnlyRunnerMiddlewareWithOpts ¶
func TxReadOnlyRunnerMiddlewareWithOpts(dbConn *dbr.Connection, isolationLevel sql.IsolationLevel, opts TxRunnerMiddlewareOpts, ) func(next http.Handler) http.Handler
TxReadOnlyRunnerMiddlewareWithOpts is a more configurable version of the TxReadOnlyRunnerMiddleware middleware.
func TxRunnerMiddleware ¶
func TxRunnerMiddleware(dbConn *dbr.Connection, isolationLevel sql.IsolationLevel) func(next http.Handler) http.Handler
TxRunnerMiddleware is a middleware that injects TxRunner to the request's context.
func TxRunnerMiddlewareWithOpts ¶
func TxRunnerMiddlewareWithOpts( dbConn *dbr.Connection, isolationLevel sql.IsolationLevel, opts TxRunnerMiddlewareOpts, ) func(next http.Handler) http.Handler
TxRunnerMiddlewareWithOpts is a more configurable version of the TxRunnerMiddleware middleware.
Types ¶
type CompositeEventReceiver ¶
type CompositeEventReceiver struct {
Receivers []dbr.EventReceiver
}
CompositeEventReceiver represents a composition of event receivers from dbr package and implements Composite design pattern.
func NewCompositeReceiver ¶
func NewCompositeReceiver(receivers []dbr.EventReceiver) *CompositeEventReceiver
NewCompositeReceiver creates a new CompositeEventReceiver.
func (*CompositeEventReceiver) Event ¶
func (r *CompositeEventReceiver) Event(eventName string)
Event receives a simple notification when various events occur.
func (*CompositeEventReceiver) EventErr ¶
func (r *CompositeEventReceiver) EventErr(eventName string, err error) error
EventErr receives a notification of an error if one occurs.
func (*CompositeEventReceiver) EventErrKv ¶
func (r *CompositeEventReceiver) EventErrKv(eventName string, err error, kvs map[string]string) error
EventErrKv receives a notification of an error if one occurs along with optional key/value data.
func (*CompositeEventReceiver) EventKv ¶
func (r *CompositeEventReceiver) EventKv(eventName string, kvs map[string]string)
EventKv receives a notification when various events occur along with optional key/value data.
func (*CompositeEventReceiver) Timing ¶
func (r *CompositeEventReceiver) Timing(eventName string, nanoseconds int64)
Timing receives the time an event took to happen.
type NewTxRunnerFunc ¶
type NewTxRunnerFunc func(conn *dbr.Connection, opts *sql.TxOptions, eventReceiver dbr.EventReceiver) TxRunner
NewTxRunnerFunc - factory function for create TxRunner objects.
type QueryMetricsEventReceiver ¶
type QueryMetricsEventReceiver struct { *dbr.NullEventReceiver // contains filtered or unexported fields }
QueryMetricsEventReceiver implements the dbr.EventReceiver interface and collects metrics about SQL queries. To be collected SQL query should be annotated (comment starting with specified prefix).
func NewQueryMetricsEventReceiver ¶
func NewQueryMetricsEventReceiver(mc *dbkit.MetricsCollector, annotationPrefix string) *QueryMetricsEventReceiver
NewQueryMetricsEventReceiver creates a new QueryMetricsEventReceiver.
func NewQueryMetricsEventReceiverWithOpts ¶
func NewQueryMetricsEventReceiverWithOpts(mc *dbkit.MetricsCollector, options QueryMetricsEventReceiverOpts) *QueryMetricsEventReceiver
NewQueryMetricsEventReceiverWithOpts creates a new QueryMetricsEventReceiver with additinal options.
func (*QueryMetricsEventReceiver) TimingKv ¶
func (er *QueryMetricsEventReceiver) TimingKv(eventName string, nanoseconds int64, kvs map[string]string)
TimingKv is called when SQL query is executed. It receives the duration of how long the query takes, parses annotation from SQL comment and collects metrics.
type QueryMetricsEventReceiverOpts ¶
type QueryMetricsEventReceiverOpts struct { AnnotationPrefix string AnnotationModifier func(string) string }
QueryMetricsEventReceiverOpts consists options for QueryMetricsEventReceiver.
type RetryableTxSession ¶
type RetryableTxSession struct { TxSession // contains filtered or unexported fields }
RetryableTxSession is a wrapper around TxSession that makes transaction executed with DoInTx retryable.
func NewRetryableTxSession ¶
func NewRetryableTxSession(conn *dbr.Connection, opts *sql.TxOptions, p retry.Policy) *RetryableTxSession
NewRetryableTxSession creates a new RetryableTxSession.
type SlowQueryLogEventReceiver ¶
type SlowQueryLogEventReceiver struct { *dbr.NullEventReceiver // contains filtered or unexported fields }
SlowQueryLogEventReceiver implements the dbr.EventReceiver interface and logs long SQL queries. To be logged SQL query should be annotated (comment starting with specified prefix).
func NewSlowQueryLogEventReceiver ¶
func NewSlowQueryLogEventReceiver(logger log.FieldLogger, longQueryTime time.Duration, annotationPrefix string) *SlowQueryLogEventReceiver
NewSlowQueryLogEventReceiver creates a new SlowQueryLogEventReceiver.
func NewSlowQueryLogEventReceiverWithOpts ¶
func NewSlowQueryLogEventReceiverWithOpts(logger log.FieldLogger, longQueryTime time.Duration, options SlowQueryLogEventReceiverOpts) *SlowQueryLogEventReceiver
NewSlowQueryLogEventReceiverWithOpts creates a new SlowQueryLogEventReceiver with additinal options.
func (*SlowQueryLogEventReceiver) TimingKv ¶
func (er *SlowQueryLogEventReceiver) TimingKv(eventName string, nanoseconds int64, kvs map[string]string)
TimingKv is called when SQL query is executed. It receives the duration of how long the query takes, parses annotation from SQL comment and logs last if execution time is long.
type SlowQueryLogEventReceiverOpts ¶
type SlowQueryLogEventReceiverOpts struct { AnnotationPrefix string AnnotationModifier func(string) string }
SlowQueryLogEventReceiverOpts consists options for SlowQueryLogEventReceiver.
type TxBeginError ¶
type TxBeginError struct {
Inner error
}
TxBeginError is a error that may occur when begging transaction is failed.
func (*TxBeginError) Error ¶
func (e *TxBeginError) Error() string
Error returns a string representation of TxBeginError.
func (*TxBeginError) Unwrap ¶
func (e *TxBeginError) Unwrap() error
Unwrap unwraps internal error for IsRetryable algorithm
type TxCommitError ¶
type TxCommitError struct {
Inner error
}
TxCommitError is a error that may occur when committing transaction is failed.
func (*TxCommitError) Error ¶
func (e *TxCommitError) Error() string
Error returns a string representation of TxCommitError.
func (*TxCommitError) Unwrap ¶
func (e *TxCommitError) Unwrap() error
Unwrap unwraps internal error for IsRetryable algorithm
type TxRollbackError ¶
type TxRollbackError struct {
Inner error
}
TxRollbackError is an error that may occur when rollback has failed.
func (*TxRollbackError) Error ¶
func (e *TxRollbackError) Error() string
Error returns a string representation of TxRollbackError.
func (*TxRollbackError) Unwrap ¶
func (e *TxRollbackError) Unwrap() error
Unwrap unwraps internal error for IsRetryable algorithm
type TxRunner ¶
type TxRunner interface { BeginTx(ctx context.Context) (*dbr.Tx, error) DoInTx(ctx context.Context, fn func(runner dbr.SessionRunner) error) error }
TxRunner can begin a new transaction and provides the ability to execute code inside already started one. Wrappers from dbr query builder are used.
func GetTxRunnerFromContext ¶
GetTxRunnerFromContext extracts TxRunner from the context.
func GetTxRunnerFromContextByKey ¶
GetTxRunnerFromContextByKey extracts TxRunner from the context by specified key.
func NewRetryableTxRunner ¶
func NewRetryableTxRunner(conn *dbr.Connection, opts *sql.TxOptions, eventReceiver dbr.EventReceiver, p retry.Policy) TxRunner
NewRetryableTxRunner creates a new object of TxRunner with retries.
func NewTxRunner ¶
func NewTxRunner(conn *dbr.Connection, opts *sql.TxOptions, eventReceiver dbr.EventReceiver) TxRunner
NewTxRunner creates a new object of TxRunner.
type TxRunnerMiddlewareOpts ¶
type TxRunnerMiddlewareOpts struct { ContextKey interface{} SlowQueryLog struct { MinTime time.Duration AnnotationPrefix string } NewTxRunner NewTxRunnerFunc }
TxRunnerMiddlewareOpts represents an options for the TxRunnerMiddleware middleware.
type TxSession ¶
TxSession contains Session form dbr query builder (represents a business unit of execution (e.g. a web request or some worker's job)) and options for starting transactions.
func NewTxSession ¶
NewTxSession creates a new TxSession.