Documentation
¶
Index ¶
- Constants
- Variables
- func SetManager(ctx context.Context, inst Manager) (context.Context, error)
- func SetManagerIfAbsent(ctx context.Context, inst Manager) (context.Context, error)
- func WithManager(ctx context.Context, fn func(Manager))
- type BatchJob
- type Config
- type Cursor
- type Database
- type Manager
- type NullBool
- type NullByte
- type NullFloat64
- type NullInt16
- type NullInt32
- type NullInt64
- type NullString
- type NullTime
- type Runner
- type TxnFn
- type TxnProvider
- type Worker
- type WorkerJob
Constants ¶
const ContextKeyManager = "gohacks/database@v1"
Key used to store the instance in the context's user value.
Variables ¶
var ( // Signalled when there is no SQL driver. ErrNoDriver error = errors.Base("no driver provided") // Signalled when there is no SQL username given. ErrNoUsername error = errors.Base("no username provided") // Signalled when there is no SQL password given. ErrNoPassword error = errors.Base("no password provided") // Signalled when there is no SQL server hostname given. ErrNoHostname error = errors.Base("no hostname provided") // Signalled when there is no SQL database name provided. ErrNoDatabase error = errors.Base("no database name provided") )
var ( ErrServerConnClosed = errors.Base("server connection closed") ErrLostConn = errors.Base("lost connection during query") ErrTxnDeadlock = errors.Base("deadlock found when trying to get lock") ErrTxnSerialization = errors.Base("serialization failure") )
var ( ErrNotAWorkerPool = errors.Base("not configured for worker pools") ErrNoPoolWorker = errors.Base("no pool worker function provided") )
var ( // The empty cursor. EmptyCursor = &Cursor{Offset: 0, Limit: 0} )
var ErrValueNotManager = errors.Base("value is not Manager")
Signalled if the instance associated with the context key is not of type Manager.
Functions ¶
func SetManager ¶ added in v1.0.6
Set Manager stores the instance in the context map.
func SetManagerIfAbsent ¶ added in v1.0.6
SetManagerIfAbsent sets only if not already present.
func WithManager ¶ added in v1.0.6
WithManager calls fn with the instance or fallback.
Types ¶
type BatchJob ¶ added in v1.0.6
type BatchJob interface {
Run(ctx context.Context, runner Runner, data []dynworker.UserData) error
}
BatchJob provides a means to invoke a user-supplied function with a batch of jobs.
type Config ¶
type Config struct {
Driver string `json:"driver"`
Username string `json:"username"`
Password string `config_obscure:"true" json:"password"`
Hostname string `json:"hostname"`
Port int `json:"port"`
Database string `json:"database"`
BatchSize int `json:"batch_size"`
BatchTimeout types.Duration `json:"batch_timeout"`
SetPoolLimits bool `json:"set_pool_limits"`
MaxIdleConns int `json:"max_idle_conns"`
MaxOpenConns int `json:"max_open_conns"`
UsePool bool `json:"use_worker_pool"`
PoolMinWorkers int `json:"pool_min_workers"`
PoolMaxWorkers int `json:"pool_max_workers"`
PoolIdleTimeout types.Duration `json:"pool_idle_timeout"`
PoolDrainTimeout types.Duration `json:"pool_drain_timeout"`
// contains filtered or unexported fields
}
SQL configuration structure.
type Cursor ¶ added in v0.3.3
TODO: Look, this sucks... offset/limit cursors are just fail. TODO: Rework this to be a proper cursor!
type Database ¶
type Database interface {
// Pings the database connection to ensure it is alive and connected.
Ping() error
// Close a database connection. This does nothing if the connection
// is already closed.
Close() error
// Set the maximum idle connections.
SetMaxIdleConns(int)
// Set the maximum open connections.
SetMaxOpenConns(int)
// Rebind query placeholders to the chosen SQL backend.
Rebind(string) string
// Parses the given error looking for common MySQL error conditions.
//
// If one is found, then a Golang error describing the condition is
// raised.
//
// If nothing interesting is found, then the original error is
// returned.
GetError(error) error
// Run a query function within the context of a database transaction.
//
// If there is no error, then the transaction is committed.
//
// If there is an error, then the transaction is rolled back.
WithTransaction(context.Context, TxnFn) error
// Exposes the database's pool as a `Runner`.
Runner() Runner
}
type Manager ¶ added in v0.5.0
type Manager interface {
Open(string, string) (Database, error)
OpenConfig(*Config) (Database, error)
OpenWorker(context.Context, *Config, BatchJob) (Worker, error)
CheckDB(Database) error
}
Database management.
This is a series of wrappers around Go's internal DB stuff to ensure that we set up max idle/open connections et al.
func FromManager ¶ added in v1.0.6
FromManager returns the instance or the fallback.
func GetManager ¶ added in v1.0.6
Get the logger from the given context.
Will return ErrValueNotManager if the value in the context is not of type Manager.
func MustGetManager ¶ added in v1.0.6
Attempt to get the instance from the given context. Panics if the operation fails.
type NullBool ¶ added in v0.3.3
func (NullBool) MarshalJSON ¶ added in v0.3.3
type NullByte ¶ added in v0.3.3
func (NullByte) MarshalJSON ¶ added in v0.3.3
type NullFloat64 ¶ added in v0.3.3
type NullFloat64 struct {
sql.NullFloat64
}
func (NullFloat64) MarshalJSON ¶ added in v0.3.3
func (x NullFloat64) MarshalJSON() ([]byte, error)
type NullInt16 ¶ added in v0.3.3
func (NullInt16) MarshalJSON ¶ added in v0.3.3
type NullInt32 ¶ added in v0.3.3
func (NullInt32) MarshalJSON ¶ added in v0.3.3
type NullInt64 ¶ added in v0.3.3
func (NullInt64) MarshalJSON ¶ added in v0.3.3
type NullString ¶ added in v0.3.3
type NullString struct {
sql.NullString
}
func (NullString) MarshalJSON ¶ added in v0.3.3
func (x NullString) MarshalJSON() ([]byte, error)
type NullTime ¶ added in v0.3.3
func (NullTime) MarshalJSON ¶ added in v0.3.3
type Runner ¶ added in v1.0.6
type Runner interface {
sqlx.ExtContext
GetContext(context.Context, any, string, ...any) error
SelectContext(context.Context, any, string, ...any) error
}
Runner is "anything that can run sqlx queries with context". Both *sqlx.DB and *sqlx.Tx satisfy this.
type TxnProvider ¶ added in v1.0.6
Any object that contains a `Txn` function can be used for callbacks.