database

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2020 License: MIT Imports: 7 Imported by: 0

README

database

Wrapper around sqlx

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnregisteredStatement is returned when there is an attempt to execute a statement that
	// was not previously registered.
	ErrUnregisteredStatement = errors.New("statement not registered")
)

Functions

This section is empty.

Types

type DB

type DB interface {
	Ping() error
	Preparex(query string) (*sqlx.Stmt, error)
	BeginTxx(ctx context.Context, opts *sql.TxOptions) (*sqlx.Tx, error)
	// contains filtered or unexported methods
}

DB represents the functions needed to access the db. This is satisfied by the sqlx.DB struct.

type Database

type Database struct {
	// contains filtered or unexported fields
}

Database is our wrapper around sqlx which enforces correctness of queries.

func New

func New(db DB) (*Database, error)

New creates a new wrapper around a sqlx.DB.

func (*Database) BeginTx

func (db *Database) BeginTx(ctx context.Context, opts *sql.TxOptions) (Tx, error)

BeginTx will start a db transaction with the given options.

func (*Database) Delete

func (db *Database) Delete(ctx context.Context, statementName string, args ...interface{}) (rowsAffected int64, err error)

Delete deletes records from the database. It returns the number of rows affected by the delete.

func (*Database) DeleteTx

func (db *Database) DeleteTx(ctx context.Context, tx Tx, statementName string, args ...interface{}) (rowsAffected int64, err error)

DeleteTx deletes records from the database inside a transaction. It returns the number of rows affected by the delete.

func (*Database) Exec

func (db *Database) Exec(ctx context.Context, statementName string, args ...interface{}) (sql.Result, error)

Exec executes an arbitrary statement on the database. It returns a sql.Result.

func (*Database) ExecTx

func (db *Database) ExecTx(ctx context.Context, tx Tx, statementName string, args ...interface{}) (sql.Result, error)

ExecTx executes an arbitrary statement on the database in a transaction. It returns a sql.Result.

func (*Database) Get

func (db *Database) Get(ctx context.Context, dest interface{}, statementName string, args ...interface{}) error

Get will run a select query to get 1 record from the database and bind the result to dest.

func (*Database) GetTx

func (db *Database) GetTx(ctx context.Context, tx Tx, dest interface{}, statementName string, args ...interface{}) error

GetTx will run a select query to get 1 record from the database in a transaction and bind the result to dest.

func (*Database) Insert

func (db *Database) Insert(ctx context.Context, statementName string, args ...interface{}) (lastInsertID int64, err error)

Insert is used to insert data into the database. It retuns the id of the inserted record.

func (*Database) InsertTx

func (db *Database) InsertTx(ctx context.Context, tx Tx, statementName string, args ...interface{}) (lastInsertID int64, err error)

InsertTx is used to insert data into the database inside a transaction. It returns the id of the inserted record.

func (*Database) RegisterStatement

func (db *Database) RegisterStatement(name, statement string) error

RegisterStatement registers the given statement with the given name. It validates the statement by using the Preparex func.

func (*Database) Select

func (db *Database) Select(ctx context.Context, dest interface{}, statementName string, args ...interface{}) error

Select will run a select query on the database and bind the results to dest.

func (*Database) SelectTx

func (db *Database) SelectTx(ctx context.Context, tx Tx, dest interface{}, statementName string, args ...interface{}) error

SelectTx will run a select query on the database inside a transaction and bind the results to dest.

func (*Database) Update

func (db *Database) Update(ctx context.Context, statementName string, args ...interface{}) (rowsAffected int64, err error)

Update updates records in the database. It returns the number of rows affected by the update.

func (*Database) UpdateTx

func (db *Database) UpdateTx(ctx context.Context, tx Tx, statementName string, args ...interface{}) (rowsAffected int64, err error)

UpdateTx updates records in the database inside a transaction. It returns the number of rows affected by the update.

func (*Database) ValidateStatement

func (db *Database) ValidateStatement(statement string) error

ValidateStatement is used to verify the syntax of the given statement.

func (*Database) With

func (db *Database) With(mw ...Middleware) *Database

With will add the middleware to the Database wrapper.

type Middleware

type Middleware interface {
	Before(ctx context.Context, name, statement string, args ...interface{}) (context.Context, error)
	After(ctx context.Context, err error, name, statement string, args ...interface{}) error
}

Middleware represents something you want to run on every query. This could be to start a newrelic segment, log the query, or anything else you want.

type Tx

type Tx interface {
	Commit() error
	Rollback() error
	// contains filtered or unexported methods
}

Tx represents the functions needed to operate in a transaction. This is satisfied by the sqlx.Tx struct.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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