db

package
v0.0.0-...-a9a9191 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2016 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package db handles database connections, transactions, and prepared statements for #webscale.

Index

Constants

This section is empty.

Variables

View Source
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

func IsConstraint(err error, name string) bool

IsConstraint returns true if the given error is a violation of a constraint with the given name.

func WaitAll

func WaitAll()

WaitAll waits for all prepared statements to be ready.

Types

type Error

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

Error is the type of most errors returned by package db.

func (*Error) Cause

func (err *Error) Cause() error

Cause implements the cause interface from github.com/pkg/errors.

func (*Error) Error

func (err *Error) Error() string

Error implements the error interface.

func (*Error) StackTrace

func (err *Error) StackTrace() errors.StackTrace

StackTrace implements the stackTrace interface from github.com/pkg/errors.

func (*Error) Temporary

func (err *Error) Temporary() bool

Temporary returns true if the cause of this error was temporary.

func (*Error) Timeout

func (err *Error) Timeout() bool

Timeout returns true if the cause of this error was a timeout.

type Row

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

Row is the result of calling QueryRow to select a single row.

func (*Row) Scan

func (row *Row) Scan(dest ...interface{}) error

Scan copies the columns from the matched row into the values pointed at by dest. See the documentation on Rows.Scan for details. If more than one row matches the query, Scan uses the first row and discards the rest. If no row matches the query, Scan returns ErrNoRows.

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

func (rows *Rows) Err() error

Err returns the error, if any, that was encountered during iteration. Err may be called after an explicit or implicit Close.

func (*Rows) Next

func (rows *Rows) Next() bool

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

func (rows *Rows) Scan(dest ...interface{}) error

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.

func Prepare

func Prepare(query string) *Stmt

Prepare creates a prepared SQL statement. The database interaction happens asynchronously, and the program terminates if the query is malformed.

func (*Stmt) Wait

func (stmt *Stmt) Wait()

Wait waits for the prepared statement to be ready.

type Tx

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

Tx is a database transaction.

func Begin

func Begin(ctx context.Context) (*Tx, error)

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) Commit

func (tx *Tx) Commit() error

Commit commits the transaction.

func (*Tx) Exec

func (tx *Tx) Exec(stmt *Stmt, args ...interface{}) (int64, error)

Exec executes a prepared statement with the given arguments and returns the number of rows affected for INSERT and UPDATE statements.

func (*Tx) Prepare

func (tx *Tx) Prepare(query string) (*Stmt, error)

Prepare prepares a statement local to this transaction.

func (*Tx) Query

func (tx *Tx) Query(stmt *Stmt, args ...interface{}) (*Rows, error)

Query executes a prepared query statement with the given arguments and returns the query results as a *Rows.

func (*Tx) QueryRow

func (tx *Tx) QueryRow(stmt *Stmt, args ...interface{}) *Row

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)

func (*Tx) Rollback

func (tx *Tx) Rollback() error

Rollback aborts the transaction.

Jump to

Keyboard shortcuts

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