Documentation ¶
Overview ¶
Package db handles database connections, transactions, and prepared statements for #webscale.
Index ¶
- Variables
- func Init(dataSource string)
- func IsConstraint(err error, name string) bool
- func WaitAll()
- type Error
- type Row
- type Rows
- type Stmt
- type Tx
- func (tx *Tx) Cancel()
- func (tx *Tx) Commit() error
- func (tx *Tx) Exec(stmt *Stmt, args ...interface{}) (int64, error)
- func (tx *Tx) Prepare(query string) (*Stmt, error)
- func (tx *Tx) Query(stmt *Stmt, args ...interface{}) (*Rows, error)
- func (tx *Tx) QueryRow(stmt *Stmt, args ...interface{}) *Row
- func (tx *Tx) Rollback() error
Constants ¶
This section is empty.
Variables ¶
var FilterAppendErrorDetails = hook.NewFilter(&applyFilterAppendErrorDetails).(func(func([]byte, context.Context) ([]byte, error), int))
FilterAppendErrorDetails registers a function to be called when an error is being described. Filter functions should either return the byte slice unchanged or append text starting with a newline '\n'.
Functions ¶
func Init ¶
func Init(dataSource string)
Init sets the data source name and allows prepared statements and transactions to run.
func IsConstraint ¶
IsConstraint returns true if the given error is a violation of a constraint with the given name.
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error is the type of most errors returned by package db.
func (*Error) StackTrace ¶
func (err *Error) StackTrace() errors.StackTrace
StackTrace implements the stackTrace interface from github.com/pkg/errors.
type Row ¶
type Row struct {
// contains filtered or unexported fields
}
Row is the result of calling QueryRow to select a single row.
type Rows ¶
type Rows struct {
// contains filtered or unexported fields
}
Rows is the result of a query. Its cursor starts before the first row of the result set. Use Next to advance through the rows:
rows, err := tx.Query(stmt, ...) ... defer rows.Close() for rows.Next() { var id int var name string err = rows.Scan(&id, &name) ... } err = rows.Err() // get any error encountered during iteration ...
func (*Rows) Close ¶
func (rows *Rows) Close()
Close closes the Rows, preventing further enumeration. Unlike sql.Rows, Close does not return an error. Call Err to get the error.
func (*Rows) Err ¶
Err returns the error, if any, that was encountered during iteration. Err may be called after an explicit or implicit Close.
func (*Rows) Next ¶
Next prepares the next result row for reading with the Scan method. It returns true on success, or false if there is no next result row or an error happened while preparing it. Err should be consulted to distinguish between the two cases.
Every call to Scan, even the first one, must be preceded by a call to Next.
func (*Rows) Scan ¶
Scan copies the columns in the current row into the values pointed at by dest. The number of values in dest must be the same as the number of columns in Rows.
Scan converts columns read from the database into the following common Go types and special types provided by the sql package:
*string *[]byte *int, *int8, *int16, *int32, *int64 *uint, *uint8, *uint16, *uint32, *uint64 *bool *float32, *float64 *interface{} *RawBytes any type implementing Scanner (see Scanner docs)
In the most simple case, if the type of the value from the source column is an integer, bool or string type T and dest is of type *T, Scan simply assigns the value through the pointer.
Scan also converts between string and numeric types, as long as no information would be lost. While Scan stringifies all numbers scanned from numeric database columns into *string, scans into numeric types are checked for overflow. For example, a float64 with value 300 or a string with value "300" can scan into a uint16, but not into a uint8, though float64(255) or "255" can scan into a uint8. One exception is that scans of some float64 numbers to strings may lose information when stringifying. In general, scan floating point columns into *float64.
If a dest argument has type *[]byte, Scan saves in that argument a copy of the corresponding data. The copy is owned by the caller and can be modified and held indefinitely. The copy can be avoided by using an argument of type *RawBytes instead; see the documentation for RawBytes for restrictions on its use.
If an argument has type *interface{}, Scan copies the value provided by the underlying driver without conversion. When scanning from a source value of type []byte to *interface{}, a copy of the slice is made and the caller owns the result.
Source values of type time.Time may be scanned into values of type *time.Time, *interface{}, *string, or *[]byte. When converting to the latter two, time.Format3339Nano is used.
Source values of type bool may be scanned into types *bool, *interface{}, *string, *[]byte, or *RawBytes.
For scanning into *bool, the source may be true, false, 1, 0, or string inputs parseable by strconv.ParseBool.
type Stmt ¶
type Stmt struct {
// contains filtered or unexported fields
}
Stmt is a prepared SQL statement.
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
Tx is a database transaction.
func Begin ¶
Begin starts a transaction. If a non-default isolation level is used that the driver doesn't support an error will be returned. Different drivers may have slightly different meanings for the same isolation level.
func (*Tx) Cancel ¶
func (tx *Tx) Cancel()
Cancel rolls back the transaction without returning an error. It is assumed that by the time Cancel is called, Commit has already been called or another (more important) error has occurred.
func (*Tx) Exec ¶
Exec executes a prepared statement with the given arguments and returns the number of rows affected for INSERT and UPDATE statements.
func (*Tx) Query ¶
Query executes a prepared query statement with the given arguments and returns the query results as a *Rows.
func (*Tx) QueryRow ¶
QueryRow executes a prepared query statement with the given arguments. If an error occurs during the execution of the statement, that error will be returned by a call to Scan on the returned *Row, which is always non-nil. If the query selects no rows, the *Row's Scan will return ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.
Example usage:
var name string err := tx.QueryRow(nameByUseridStmt, id).Scan(&name)