Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrNoRows = errors.New("no rows, expected 1")
ErrNoRows is returned by ScanOne when a query returns no rows
Functions ¶
func FeedScanner ¶
func FeedScanner(scanner RowScanner, rows ...[]interface{}) error
FeedScanner feeds the rows to scanner. NOTE: Only use this func in tests. Value conversion may differ from the one used in actual SQL execution.
Types ¶
type Database ¶
type Database interface {
io.Closer
Queryer
Preparer
// Transaction performs work in transaction.
// Transaction is committed if work returns nil, and rolled back otherwise.
Transaction(ctx context.Context, work func(Transaction) error) error
// PrepareStatement prepares a statement for later queries.
//
// In addition to preparing a statement on a single connection, the returned
// PreparedStatement can use other connections from the Database's connection
// pool re-preparing the statement as needed.
// PreparedStatement is safe for concurrent use.
// Refer to sql.Stmt's godoc for details.
//
// It is best to use this API for queries that are executed frequently from
// different contexts. For example, using a PreparedStatement to fetch data
// served via a service's HTTP API endpoint by many goroutines can reduce
// the latency considerably.
// In other scenarios, prefer using the Prepared method: it takes care of
// closing the Statement and is thus less error-prone.
//
// The provided context is used for the preparation of the statement, not
// for its execution.
// The caller must call Close on the returned PreparedStatement when it is
// no longer needed.
PrepareStatement(ctx context.Context, sql string) (PreparedStatement, error)
}
Database provides fluent database API.
type PreparedStatement ¶ added in v1.1.0
PreparedStatement is a Statement that must be closed by the caller
type Preparer ¶
type Preparer interface {
// Prepared prepares a sql statement and runs work.
// The prepared statement is always closed when this method returns.
Prepared(ctx context.Context, sql string, work func(Statement) error) error
}
Preparer prepares a sql statement
type Queryer ¶
type Queryer interface {
// Scan executes sql and scans result rows with RowScanner
Scan(ctx context.Context, scanner RowScanner, sql string, args ...interface{}) error
// ScanOne executes sql and scans the first result row with RowScanner.
// Returns ErrNoRows if no rows were returned. Remaining rows are discarded
ScanOne(ctx context.Context, scanner RowScanner, sql string, args ...interface{}) error
// Update executes sql insert, update, or delete
Update(ctx context.Context, sql string, args ...interface{}) (sql.Result, error)
// UpdateAndGetRowsAffected sql insert, update, or delete and returns affected row count
// Shorthand for Update(...) followed by UpdateResult.RowsAffected
UpdateAndGetRowsAffected(ctx context.Context, sql string, args ...interface{}) (int64, error)
// UpdateAndGetLastInsertID sql insert, update, or delete and returns last generated row id.
// Shorthand for Update(...) followed by UpdateResult.LastInsertId
UpdateAndGetLastInsertID(ctx context.Context, sql string, args ...interface{}) (int64, error)
}
Queryer performs scans and updates
type RowScanner ¶
type RowScanner interface {
// Into returns a slice of pointers to scan into.
// Should always return a slice of same pointers.
Into() []interface{}
// RowScanned notifies scanner that a row has been scanned into slice returned by Into()
RowScanned() error
}
RowScanner scans database rows into arbitrary data structures
func Into ¶
func Into(valuePointers ...interface{}) RowScanner
Into creates a RowScanner that scans values into the passed pointers. Only suitable for scanning single or last row.
type Statement ¶
type Statement interface {
// Scan executes the prepared statement and scans result rows with RowScanner
Scan(ctx context.Context, scanner RowScanner, args ...interface{}) error
// ScanOne executes the prepared statement and scans the first result row with RowScanner.
// Returns ErrNoRows if no rows were returned. Remaining rows are discarded
ScanOne(ctx context.Context, scanner RowScanner, args ...interface{}) error
// Update executes the prepared insert, update, or delete
Update(ctx context.Context, args ...interface{}) (sql.Result, error)
// UpdateAndGetRowsAffected the prepared insert, update, or delete and returns affected row count
// Shorthand for Update(...) followed by UpdateResult.RowsAffected
UpdateAndGetRowsAffected(ctx context.Context, args ...interface{}) (int64, error)
// UpdateAndGetLastInsertID the prepared insert, update, or delete and returns last generated row id.
// Shorthand for Update(...) followed by UpdateResult.LastInsertId
UpdateAndGetLastInsertID(ctx context.Context, args ...interface{}) (int64, error)
}
Statement is a prepared statement
type Transaction ¶
Transaction represents an open transaction
Source Files
¶
Click to show internal directories.
Click to hide internal directories.