Documentation
¶
Overview ¶
Package sqle is a general purpose, transparent, non-magical helper package for sql.DB that simplifies and reduces error checking for various SQL operations.
Index ¶
- type Essentials
- type MySQL
- type Std
- func (s *Std) Exec(ctx context.Context, query string, args ...interface{}) error
- func (s *Std) ExecAffected(ctx context.Context, query string, args ...interface{}) (rowsAffected int64, err error)
- func (s *Std) ExecAffectedTx(ctx context.Context, tx *sql.Tx, query string, args ...interface{}) (rowsAffected int64, err error)
- func (s *Std) ExecID(ctx context.Context, query string, args ...interface{}) (lastInsertID int64, err error)
- func (s *Std) ExecIDTx(ctx context.Context, tx *sql.Tx, query string, args ...interface{}) (lastInsertID int64, err error)
- func (s *Std) ExecRes(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (s *Std) ExecResTx(ctx context.Context, tx *sql.Tx, query string, args ...interface{}) (res sql.Result, err error)
- func (s *Std) ExecTx(ctx context.Context, tx *sql.Tx, query string, args ...interface{}) error
- func (s *Std) Select(ctx context.Context, query string, args []interface{}, dest []interface{}) error
- func (s *Std) SelectExists(ctx context.Context, query string, args []interface{}, dest []interface{}) (exists bool, err error)
- func (s *Std) SelectExistsTx(ctx context.Context, tx *sql.Tx, query string, args []interface{}, ...) (exists bool, err error)
- func (s *Std) SelectRange(ctx context.Context, query string, args []interface{}, dest []interface{}, ...) error
- func (s *Std) SelectRangeTx(ctx context.Context, tx *sql.Tx, query string, args []interface{}, ...) (err error)
- func (s *Std) SelectTx(ctx context.Context, tx *sql.Tx, query string, args []interface{}, ...) error
- func (s *Std) UnsafeExecBatch(ctx context.Context, statements []string) error
- func (s *Std) UnsafeExecBatchTx(ctx context.Context, tx *sql.Tx, statements []string) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Essentials ¶
type Essentials interface {
Exec(ctx context.Context, query string, args ...interface{}) error
ExecTx(ctx context.Context, tx *sql.Tx, query string, args ...interface{}) error
ExecID(ctx context.Context, query string, args ...interface{}) (lastInsertID int64, err error)
ExecIDTx(ctx context.Context, tx *sql.Tx, query string, args ...interface{}) (lastInsertID int64, err error)
ExecAffected(ctx context.Context, query string, args ...interface{}) (rowsAffected int64, err error)
ExecAffectedTx(ctx context.Context, tx *sql.Tx, query string, args ...interface{}) (rowsAffected int64, err error)
ExecRes(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
ExecResTx(ctx context.Context, tx *sql.Tx, query string, args ...interface{}) (sql.Result, error)
Select(ctx context.Context, query string, args []interface{}, dests []interface{}) error
SelectTx(ctx context.Context, tx *sql.Tx, query string, args []interface{}, dests []interface{}) error
SelectExists(ctx context.Context, query string, args []interface{}, dests []interface{}) (exists bool, err error)
SelectExistsTx(ctx context.Context, tx *sql.Tx, query string, args []interface{}, dests []interface{}) (exists bool, err error)
SelectRange(ctx context.Context, query string, args []interface{}, dests []interface{}, handleRow func()) error
SelectRangeTx(ctx context.Context, tx *sql.Tx, query string, args []interface{}, dests []interface{}, handleRow func()) error
UnsafeExecBatch(ctx context.Context, statements []string) error
UnsafeExecBatchTx(ctx context.Context, tx *sql.Tx, statements []string) error
}
Essentials is a common minimum accros all sqle implementations. It defines only functions that can be executed using database/sql.DB from the standard library.
type MySQL ¶
type MySQL struct {
Std *Std
}
MySQL extends sqle.Std with MySQL specific functions
func (*MySQL) UnsafeCount ¶
UnsafeCount counts the rows for a single column in a specified table.
This method IS NOT SAFE AGAINST SQL-INJECTION. Use it only with trusted input!
func (*MySQL) UnsafeExists ¶
func (s *MySQL) UnsafeExists(ctx context.Context, query string, args ...interface{}) (exists bool, err error)
UnsafeExists checks whether the statement defined by the `query` and `args` would return a result.
This method IS NOT SAFE AGAINST SQL-INJECTION. Use it only with trusted input!
type Std ¶
type Std struct {
// contains filtered or unexported fields
}
Std implements the `Essentials` interface with only using the standard library `database/sql.DB` internally. This should provide great cross- database support.
It is fully transparent and non-magical (e.g. the arguments, etc. are just passed into the standard libarary).
The primary use case of `Std` is to drastically reduce error checking (from 6 error checks to 1).
func (*Std) Exec ¶
Exec will execute the query with it's argument against the database and returns any occurring errors.
func (*Std) ExecAffected ¶
func (s *Std) ExecAffected(ctx context.Context, query string, args ...interface{}) (rowsAffected int64, err error)
ExecAffected executes the query with it's arguments against the database and returns any occurring errors.
Additionally it returns the number of rows affected by an update, insert, or delete. Not every database or database driver may support this.
func (*Std) ExecAffectedTx ¶
func (s *Std) ExecAffectedTx(ctx context.Context, tx *sql.Tx, query string, args ...interface{}) (rowsAffected int64, err error)
ExecAffectedTx is the same as `ExecAffected` but uses the passed transaction `tx` to execute the statement.
Additionally it returns the number of rows affected by an update, insert, or delete. Not every database or database driver may support this.
func (*Std) ExecID ¶
func (s *Std) ExecID(ctx context.Context, query string, args ...interface{}) (lastInsertID int64, err error)
ExecID executes the query with it's arguments against the database and returns any occurring errors.
Additionally it returns the integer generated by the database in response to a command. Typically this will be from an "auto increment" column when inserting a new row. Not all databases support this feature, and the syntax of such statements varies.
func (*Std) ExecIDTx ¶
func (s *Std) ExecIDTx(ctx context.Context, tx *sql.Tx, query string, args ...interface{}) (lastInsertID int64, err error)
ExecIDTx is the same as `ExecID` but uses the passed transaction `tx` to execute the statement.
Additionally it returns the integer generated by the database in response to a command. Typically this will be from an "auto increment" column when inserting a new row. Not all databases support this feature, and the syntax of such statements varies.
func (*Std) ExecRes ¶
ExecRes executes the query with it's arguments against the database and returns any occurring errors.
func (*Std) ExecResTx ¶
func (s *Std) ExecResTx(ctx context.Context, tx *sql.Tx, query string, args ...interface{}) (res sql.Result, err error)
ExecResTx is the same as `ExecRes` but uses the passed transaction `tx` to execute the statement.
func (*Std) ExecTx ¶
ExecTx is the same as `Exec` but uses the passed transaction `tx` to execute the operations.
func (*Std) Select ¶
func (s *Std) Select(ctx context.Context, query string, args []interface{}, dest []interface{}) error
Select selects the `query` string from the database. The `args` interface slice should contain all primitive value arguments. The `dest` interface slice should contain a collection of pointer to primitive types. The results of the query will be saved into these pointers.
func (*Std) SelectExists ¶
func (s *Std) SelectExists(ctx context.Context, query string, args []interface{}, dest []interface{}) (exists bool, err error)
SelectExists is the same as `Select`, but additionally returns an boolean value, whether or not the query returned a row.
func (*Std) SelectExistsTx ¶
func (s *Std) SelectExistsTx(ctx context.Context, tx *sql.Tx, query string, args []interface{}, dest []interface{}) (exists bool, err error)
SelectExistsTx is the same as `SelectExists` but uses the passed transaction `tx` to execute the statement.
func (*Std) SelectRange ¶
func (s *Std) SelectRange(ctx context.Context, query string, args []interface{}, dest []interface{}, handleRow func()) error
SelectRange selects a range of results from the database, defined by the `query` and it's arguments. The `args` interface slice should contain all primitive value arguments. The `dest` interface slice should contain a collection of pointer to primitive types. The results of the query will be saved into these pointers.
As soon as one row has been loaded the `handleRow` callback will be called. It is the package caller's responsibility to copy the values from the `dest` pointers into another data structure. After returning the `handleRow` function the values of `dest` will be overwritten with the next row's values.
func (*Std) SelectRangeTx ¶
func (s *Std) SelectRangeTx(ctx context.Context, tx *sql.Tx, query string, args []interface{}, dest []interface{}, handleRow func()) (err error)
SelectRangeTx is the same as `SelectRange` but uses the passed transaction `tx` to execute the statement.
func (*Std) SelectTx ¶
func (s *Std) SelectTx(ctx context.Context, tx *sql.Tx, query string, args []interface{}, dest []interface{}) error
SelectTx is the same as `Select` but uses the passed transaction `tx` to execute the statement.
func (*Std) UnsafeExecBatch ¶
UnsafeExecBatch runs `Exec` without arguments for every entry in the `statements` collection.
This method IS NOT SAFE AGAINST SQL-INJECTION. Use it only with trusted input!
func (*Std) UnsafeExecBatchTx ¶
UnsafeExecBatchTx is the same as `UnsafeExecBatch` but uses the passed transaction `tx` to execute the statements.
This method IS NOT SAFE AGAINST SQL-INJECTION. Use it only with trusted input!