database

package
v1.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 10, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const ContextKeyManager = "gohacks/database@v1"

Key used to store the instance in the context's user value.

Variables

View Source
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")
)
View Source
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")
)
View Source
var (
	ErrNotAWorkerPool = errors.Base("not configured for worker pools")
	ErrNoPoolWorker   = errors.Base("no pool worker function provided")
)
View Source
var (
	// The empty cursor.
	EmptyCursor = &Cursor{Offset: 0, Limit: 0}
)
View Source
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

func SetManager(ctx context.Context, inst Manager) (context.Context, error)

Set Manager stores the instance in the context map.

func SetManagerIfAbsent added in v1.0.6

func SetManagerIfAbsent(ctx context.Context, inst Manager) (context.Context, error)

SetManagerIfAbsent sets only if not already present.

func WithManager added in v1.0.6

func WithManager(ctx context.Context, fn func(Manager))

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.

func NewConfig

func NewConfig() *Config

Create a new configuration object.

func (*Config) ToDSN

func (c *Config) ToDSN() string

Return the DSN for this database configuration.

func (*Config) Validate added in v0.3.3

func (c *Config) Validate() []error

Validate the configuration.

type Cursor added in v0.3.3

type Cursor struct {
	Offset int64 `json:"offset"`
	Limit  int64 `json:"limit"`
}

TODO: Look, this sucks... offset/limit cursors are just fail. TODO: Rework this to be a proper cursor!

func NewCursor added in v0.3.3

func NewCursor(offset, limit int64) *Cursor

Create a new cursor.

func (Cursor) Valid added in v0.3.3

func (c Cursor) Valid() bool

Is the cursor valid?

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
}

func FromDB added in v0.5.0

func FromDB(db *sql.DB, driver string) Database

Create a new database object using an existing `sql` object.

func Open

func Open(driver string, dsn string) (Database, error)

Open a connection using the relevant driver to the given data source name.

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

func FromManager(ctx context.Context) Manager

FromManager returns the instance or the fallback.

func GetManager added in v1.0.6

func GetManager(ctx context.Context) (Manager, error)

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

func MustGetManager(ctx context.Context) Manager

Attempt to get the instance from the given context. Panics if the operation fails.

func NewManager added in v0.5.2

func NewManager() Manager

Create a new manager.

func TryGetManager added in v1.0.6

func TryGetManager(ctx context.Context) (Manager, bool)

TryGetManager returns the instance and true if present and typed.

type NullBool added in v0.3.3

type NullBool struct {
	sql.NullBool
}

func (NullBool) MarshalJSON added in v0.3.3

func (x NullBool) MarshalJSON() ([]byte, error)

type NullByte added in v0.3.3

type NullByte struct {
	sql.NullByte
}

func (NullByte) MarshalJSON added in v0.3.3

func (x NullByte) MarshalJSON() ([]byte, error)

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

type NullInt16 struct {
	sql.NullInt16
}

func (NullInt16) MarshalJSON added in v0.3.3

func (x NullInt16) MarshalJSON() ([]byte, error)

type NullInt32 added in v0.3.3

type NullInt32 struct {
	sql.NullInt32
}

func (NullInt32) MarshalJSON added in v0.3.3

func (x NullInt32) MarshalJSON() ([]byte, error)

type NullInt64 added in v0.3.3

type NullInt64 struct {
	sql.NullInt64
}

func (NullInt64) MarshalJSON added in v0.3.3

func (x NullInt64) MarshalJSON() ([]byte, error)

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

type NullTime struct {
	sql.NullTime
}

func (NullTime) MarshalJSON added in v0.3.3

func (x NullTime) MarshalJSON() ([]byte, error)

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 TxnFn added in v1.0.6

type TxnFn func(context.Context, Runner) error

type TxnProvider added in v1.0.6

type TxnProvider interface {
	Txn(context.Context, Runner) error
}

Any object that contains a `Txn` function can be used for callbacks.

type Worker added in v1.0.6

type Worker interface {
	Name() string
	Database() Database
	Start()
	Stop()
	SubmitBatch(dynworker.UserData) error
	SubmitJob(WorkerJob) error
}

func NewWorker added in v1.0.6

func NewWorker(parent context.Context, cfg *Config, dbase Database, handler BatchJob) Worker

type WorkerJob added in v1.0.6

type WorkerJob interface {
	Run(ctx context.Context, runner Runner) error
}

WorkerJob is a user-supplied unit of work which will be executed inside a database transaction.

Implement `Run' with your SQL using the provided `Runner' (`*sqlx.DB` or `*sqlx.Tx`).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL