Help us shape the future of Go! Take the 2026 Go Developer Survey

sqle

package module
v2.0.0-rc3+incompatible Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2018 License: Apache-2.0 Imports: 4 Imported by: 1

README

sqle

Package sqle is a general purpose, transparent, non-magical helper package for sql.DB that simplifies and reduces error checking for various SQL operations.

GoDoc Go Report Card FOSSA Status

Installation

To install SQLE, simly run:

$ go get github.com/harwoeck/sqle

License

FOSSA Status

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Essentials

type Essentials interface {
	NoContext() EssentialsNoContext

	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 EssentialsNoContext

type EssentialsNoContext interface {
	WithContext() Essentials

	Exec(query string, args ...interface{}) error
	ExecTx(tx *sql.Tx, query string, args ...interface{}) error
	ExecID(query string, args ...interface{}) (lastInsertID int64, err error)
	ExecIDTx(tx *sql.Tx, query string, args ...interface{}) (lastInsertID int64, err error)
	ExecAffected(query string, args ...interface{}) (rowsAffected int64, err error)
	ExecAffectedTx(tx *sql.Tx, query string, args ...interface{}) (rowsAffected int64, err error)
	ExecRes(query string, args ...interface{}) (sql.Result, error)
	ExecResTx(tx *sql.Tx, query string, args ...interface{}) (sql.Result, error)

	Select(query string, args []interface{}, dests []interface{}) error
	SelectTx(tx *sql.Tx, query string, args []interface{}, dests []interface{}) error
	SelectExists(query string, args []interface{}, dests []interface{}) (exists bool, err error)
	SelectExistsTx(tx *sql.Tx, query string, args []interface{}, dests []interface{}) (exists bool, err error)
	SelectRange(query string, args []interface{}, dests []interface{}, handleRow func()) error
	SelectRangeTx(tx *sql.Tx, query string, args []interface{}, dests []interface{}, handleRow func()) error

	UnsafeExecBatch(statements []string) error
	UnsafeExecBatchTx(tx *sql.Tx, statements []string) error
}

EssentialsNoContext is like `Essentials`, but removes the requirement to pass a `context.Context` with each call.

type MySQL

type MySQL struct {
	Std *Std
}

MySQL extends sqle.Std with MySQL specific functions

func (*MySQL) Exists

func (s *MySQL) Exists(ctx context.Context, query string, args ...interface{}) (exists bool, err error)

Exists checks whether the statement defined by the `query` and `args` would return a result.

func (*MySQL) UnsafeCount

func (s *MySQL) UnsafeCount(ctx context.Context, table, column string) (count int64, err error)

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!

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 NewStd

func NewStd(db *sql.DB) *Std

NewStd initializes a new std

func (*Std) Exec

func (s *Std) Exec(ctx context.Context, query string, args ...interface{}) error

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

func (s *Std) ExecRes(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

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

func (s *Std) ExecTx(ctx context.Context, tx *sql.Tx, query string, args ...interface{}) error

ExecTx is the same as `Exec` but uses the passed transaction `tx` to execute the operations.

func (*Std) NoContext

func (s *Std) NoContext() EssentialsNoContext

NoContext converts `Std` to a `StdNoContext` instance, which implements the `EssentialsNoContext` interface.

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

func (s *Std) UnsafeExecBatch(ctx context.Context, statements []string) error

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

func (s *Std) UnsafeExecBatchTx(ctx context.Context, tx *sql.Tx, statements []string) error

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!

type StdNoCtx

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

StdNoCtx wraps `Std` and uses `context.Background()` for all calls to it.

func (*StdNoCtx) Exec

func (no *StdNoCtx) Exec(query string, args ...interface{}) error

Exec calls `Std.Exec` with `context.Background()`

func (*StdNoCtx) ExecAffected

func (no *StdNoCtx) ExecAffected(query string, args ...interface{}) (rowsAffected int64, err error)

ExecAffected calls `Std.ExecAffected` with `context.Background()`

func (*StdNoCtx) ExecAffectedTx

func (no *StdNoCtx) ExecAffectedTx(tx *sql.Tx, query string, args ...interface{}) (rowsAffected int64, err error)

ExecAffectedTx calls `Std.ExecAffectedTx` with `context.Background()`

func (*StdNoCtx) ExecID

func (no *StdNoCtx) ExecID(query string, args ...interface{}) (lastInsertID int64, err error)

ExecID calls `Std.ExecID` with `context.Background()`

func (*StdNoCtx) ExecIDTx

func (no *StdNoCtx) ExecIDTx(tx *sql.Tx, query string, args ...interface{}) (lastInsertID int64, err error)

ExecIDTx calls `Std.ExecIDTx` with `context.Background()`

func (*StdNoCtx) ExecRes

func (no *StdNoCtx) ExecRes(query string, args ...interface{}) (sql.Result, error)

ExecRes calls `Std.ExecRes` with `context.Background()`

func (*StdNoCtx) ExecResTx

func (no *StdNoCtx) ExecResTx(tx *sql.Tx, query string, args ...interface{}) (sql.Result, error)

ExecResTx calls `Std.ExecResTx` with `context.Background()`

func (*StdNoCtx) ExecTx

func (no *StdNoCtx) ExecTx(tx *sql.Tx, query string, args ...interface{}) error

ExecTx calls `Std.ExecTx` with `context.Background()`

func (*StdNoCtx) Select

func (no *StdNoCtx) Select(query string, args []interface{}, dests []interface{}) error

Select calls `Std.Select` with `context.Background()`

func (*StdNoCtx) SelectExists

func (no *StdNoCtx) SelectExists(query string, args []interface{}, dests []interface{}) (exists bool, err error)

SelectExists calls `Std.SelectExists` with `context.Background()`

func (*StdNoCtx) SelectExistsTx

func (no *StdNoCtx) SelectExistsTx(tx *sql.Tx, query string, args []interface{}, dests []interface{}) (exists bool, err error)

SelectExistsTx calls `Std.SelectExistsTx` with `context.Background()`

func (*StdNoCtx) SelectRange

func (no *StdNoCtx) SelectRange(query string, args []interface{}, dests []interface{}, handleRow func()) error

SelectRange calls `Std.SelectRange` with `context.Background()`

func (*StdNoCtx) SelectRangeTx

func (no *StdNoCtx) SelectRangeTx(tx *sql.Tx, query string, args []interface{}, dests []interface{}, handleRow func()) error

SelectRangeTx calls `Std.SelectRangeTx` with `context.Background()`

func (*StdNoCtx) SelectTx

func (no *StdNoCtx) SelectTx(tx *sql.Tx, query string, args []interface{}, dests []interface{}) error

SelectTx calls `Std.SelectTx` with `context.Background()`

func (*StdNoCtx) UnsafeExecBatch

func (no *StdNoCtx) UnsafeExecBatch(statements []string) error

UnsafeExecBatch calls `Std.UnsafeExecBatch` with `context.Background()`

func (*StdNoCtx) UnsafeExecBatchTx

func (no *StdNoCtx) UnsafeExecBatchTx(tx *sql.Tx, statements []string) error

UnsafeExecBatchTx calls `Std.UnsafeExecBatchTx` with `context.Background()`

func (*StdNoCtx) WithContext

func (no *StdNoCtx) WithContext() Essentials

WithContext returns the internal `Std` instance used.

Jump to

Keyboard shortcuts

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