database

package
v0.0.0-...-f7c0027 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2023 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMalformedDNS is the error returned if the database DSN does not adhere
	// to the expected format.
	ErrMalformedDNS = fmt.Errorf("DSN should have format 'name://source'")
	// ErrUnsupportedDriver is the error returned if the DSN does not specify
	// either mysql://, or sqlite:// as driver name.
	ErrUnsupportedDriver = fmt.Errorf("unsupported database driver")
	// ErrNoConnection is the error returned if database actions are performed
	// without establishing a connection (pool) with the database.
	ErrNoConnection = fmt.Errorf("no connection")

	// ErrImportFailed is the error returned if a sequence of sql statements to
	// import failed.
	ErrImportFailed = fmt.Errorf("failed to import sql code")
	// ErrNotFound is the error returned if the requested object does not exist
	// in the database.
	ErrNotFound = fmt.Errorf("object not found")
	// ErrInsertFailed is the error returned if the insert failed.
	ErrInsertFailed = fmt.Errorf("failed to insert")
	// ErrUpdateFailed is the error returned if the update failed.
	ErrUpdateFailed = fmt.Errorf("failed to update")
	// ErrQueryFailed is the error returned if a database statement failed.
	ErrQueryFailed = fmt.Errorf("query failed")

	// ErrUnknownColumn is the error returned if a specified column is unknown
	// for the Persistable.
	ErrUnknownColumn = fmt.Errorf("unknown column")
)

Functions

func ImportToDB

func ImportToDB(db *sql.Tx, fh io.Reader) error

ImportToDB executes SQL from a reader within the database transaction.

Types

type Configuration

type Configuration struct {
	// DSN is the URI of the database to connect to.
	DSN string `` /* 133-byte string literal not displayed */
	// ConnMaxLifetime is the maximum amount of time a connection may be reused.
	ConnMaxLifetime time.Duration `` /* 147-byte string literal not displayed */
	// MaxIdleConns is the maximum amount of idle connections in the pool.
	MaxIdleConns int `` /* 129-byte string literal not displayed */
	// MaxOpenConns is the maximum amount of open connections to the database.
	MaxOpenConns int `` /* 133-byte string literal not displayed */
	// PingTimeout is the maximum time a ping will wait for a response.
	PingTimeout time.Duration `` /* 132-byte string literal not displayed */
}

Configuration is the structure with database settings.

type Instance

type Instance struct {
	// Pool is the database connection pool.
	Pool *sql.DB
	// contains filtered or unexported fields
}

Instance is a database instance with a connection pool.

func Connect

func Connect(cfg Configuration) (Instance, error)

Connect creates a database.Instance based on the provided Configuration.

func (*Instance) Close

func (i *Instance) Close() error

Close terminates a connection pool for a database Instance.

func (*Instance) Import

func (i *Instance) Import(fh io.Reader) error

Import executes SQL from a reader into the database instance.

func (*Instance) Ping

func (i *Instance) Ping() error

Ping checks the availability of a connection pool on the database Instance and returns true if a connection responds to a Ping command. Otherwise this function returns false.

type Migratable

type Migratable interface {
	GetID() int64
	Migrate(row Scanner, columns []string) error
}

Migratable is an interface for a PErsistable model that has migration code to make structure adjustments between versions.

type Operator

type Operator struct {
	Table          string
	NewPersistable func() Persistable
}

Operator provides a collection of functions to store and retrieve a Persistable in the database.

func (Operator) GetByID

func (o Operator) GetByID(ctx context.Context, db Instance, columns []string, id int64) (Persistable, error)

GetByID returns a persistable model by the primary key.

func (Operator) GetByQuery

func (o Operator) GetByQuery(ctx context.Context, db Instance, columns []string, clause string, args ...interface{}) ([]Persistable, error)

GetByQuery returns zero or persistables by the provided query.

func (Operator) GetOneByQuery

func (o Operator) GetOneByQuery(ctx context.Context, db Instance, columns []string, clause string, args ...interface{}) (Persistable, error)

GetOneByQuery returns a single persistable model by the provided query.

func (Operator) InsertByQuery

func (o Operator) InsertByQuery(ctx context.Context, db Instance, obj Persistable, columns []string, clause string, args ...interface{}) (int64, error)

InsertByQuery adds a persistable to the database, with an optional query, and returns the newly inserted id.

func (Operator) Migrate

func (o Operator) Migrate(ctx context.Context, db Instance) error

Migrate updates the entire table if the object is a Migratable.

func (Operator) UpdateByQuery

func (o Operator) UpdateByQuery(ctx context.Context, db Instance, obj Persistable, columns []string, clause string, args ...interface{}) (int64, error)

UpdateByQuery updates one model in the database, and returns the affected id.

type Persistable

type Persistable interface {
	GetID() int64
	ExtractFields(fields []string) ([]interface{}, error)
	UpdateFromScanner(row Scanner, columns []string) error
}

Persistable is an interface to a struct that can be stored in, and retrieved from a database.

type Scanner

type Scanner interface {
	Scan(dest ...interface{}) error
}

Scanner is an interface that implements the Scan function. Scan is implemented by *sql.Row and *sql.Rows.

Jump to

Keyboard shortcuts

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