database

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
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

View Source
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")
)
View Source
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")
)
View Source
var (
	// The empty cursor.
	EmptyCursor = &Cursor{Offset: 0, Limit: 0}
)

Functions

This section is empty.

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.

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

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)

	// 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
}

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)
	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 NewManager added in v0.5.2

func NewManager() Manager

Create a new manager.

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)

Jump to

Keyboard shortcuts

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