Documentation ¶
Index ¶
- Variables
- type DB
- type Database
- func (db *Database) BeginTx(ctx context.Context, opts *sql.TxOptions) (Tx, error)
- func (db *Database) Delete(ctx context.Context, statementName string, args ...interface{}) (rowsAffected int64, err error)
- func (db *Database) DeleteTx(ctx context.Context, tx Tx, statementName string, args ...interface{}) (rowsAffected int64, err error)
- func (db *Database) Exec(ctx context.Context, statementName string, args ...interface{}) (sql.Result, error)
- func (db *Database) ExecTx(ctx context.Context, tx Tx, statementName string, args ...interface{}) (sql.Result, error)
- func (db *Database) Get(ctx context.Context, dest interface{}, statementName string, ...) error
- func (db *Database) GetTx(ctx context.Context, tx Tx, dest interface{}, statementName string, ...) error
- func (db *Database) Insert(ctx context.Context, statementName string, args ...interface{}) (lastInsertID int64, err error)
- func (db *Database) InsertTx(ctx context.Context, tx Tx, statementName string, args ...interface{}) (lastInsertID int64, err error)
- func (db *Database) RegisterStatement(name, statement string) error
- func (db *Database) Select(ctx context.Context, dest interface{}, statementName string, ...) error
- func (db *Database) SelectTx(ctx context.Context, tx Tx, dest interface{}, statementName string, ...) error
- func (db *Database) Update(ctx context.Context, statementName string, args ...interface{}) (rowsAffected int64, err error)
- func (db *Database) UpdateTx(ctx context.Context, tx Tx, statementName string, args ...interface{}) (rowsAffected int64, err error)
- func (db *Database) ValidateStatement(statement string) error
- func (db *Database) With(mw ...Middleware) *Database
- type Middleware
- type Tx
Constants ¶
This section is empty.
Variables ¶
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 (*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 ¶
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 ¶
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.