Documentation
¶
Overview ¶
The Database queries package.
Index ¶
- Constants
- Variables
- func Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
- func ExecStmt(ctx context.Context, stmt *stmt, args ...any) (sql.Result, error)
- func Get(ctx context.Context, dest any, query string, args ...any) error
- func NamedExec(ctx context.Context, query string, arg any) (sql.Result, error)
- func Prepare(ctx context.Context, query string, args ...any) (*stmt, error)
- func Queryx(ctx context.Context, query string, args ...any) (*sqlx.Rows, error)
- func QueryxContext(ctx context.Context, query string, args ...any) (*sqlx.Rows, error)
- func Select(ctx context.Context, dest any, query string, args ...any) error
- type Config
- type Cursor
- type Database
- type Manager
- type NullBool
- type NullByte
- type NullFloat64
- type NullInt16
- type NullInt32
- type NullInt64
- type NullString
- type NullTime
Constants ¶
const ( KeyTransaction string = "_DB_TXN" StringDeadlock string = "Error 1213" // Deadlock detected. StringConnClosed string = "Error 2006" // MySQL server connection closed. StringLostConn string = "Error 2013" // Lost connection during query. )
Variables ¶
var ( ErrNoDriver error = errors.Base("no driver provided") ErrNoUsername error = errors.Base("no username provided") ErrNoPassword error = errors.Base("no password provided") ErrNoHostname error = errors.Base("no hostname provided") ErrNoDatabase error = errors.Base("no database name provided") )
var ( ErrTxnKeyNotFound error = errors.Base("transaction key not found") ErrTxnKeyNotTxn error = errors.Base("key value is not a transaction") ErrTxnContext error = errors.Base("could not create transaction context") ErrTxnStart error = errors.Base("could not start transaction") ErrTxnDeadlock error = errors.Base("deadlock found when trying to get lock") ErrServerConnClosed error = errors.Base("server connection closed") ErrLostConn error = errors.Base("lost connection during query") )
var ( // The empty cursor. EmptyCursor = &Cursor{Offset: 0, Limit: 0} )
Functions ¶
func Exec ¶ added in v0.5.3
Wrapper around `Tx.Exec`.
The transaction should be passed via a context value.
func ExecStmt ¶ added in v0.5.3
Wrapper around `Tx.ExecStmt`.
The transaction should be passed via a context value.
func Get ¶ added in v0.5.3
Wrapper around `Tx.Get`.
The transaction should be passed via a context value.
func NamedExec ¶ added in v0.5.3
Wrapper around `Tx.NamedExec`.
The transaction should be passed via a context value.
func Prepare ¶ added in v0.5.3
Wrapper around `Tx.Prepare`.
The transaction should be passed via a context value.
func Queryx ¶ added in v0.5.3
Wrapper around `Tx.Queryx`.
The transaction should be passed via a context value.
func QueryxContext ¶ added in v0.5.3
Wrapper around `Tx.QueryxContext`.
The transaction should be passed via a context value.
Types ¶
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"`
SetPoolLimits bool `json:"set_pool_limits"`
MaxIdleConns int `json:"max_idle_conns"`
MaxOpenConns int `json:"max_open_conns"`
// 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)
// Return the transaction (if any) from the given context.
Tx(context.Context) (*sqlx.Tx, error)
// Initiate a transaction. Returns a new context that contains the
// database transaction session as a value.
Begin(context.Context) (context.Context, error)
// Initiate a transaction commit.
Commit(context.Context) error
// Initiate a transaction rollback.
Rollback(context.Context) error
// 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
}
type Manager ¶ added in v0.5.0
type Manager interface {
Open(string, string) (Database, error)
OpenConfig(*Config) (Database, 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.
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)