csql: github.com/BurntSushi/csql Index | Files

package csql

import "github.com/BurntSushi/csql"

Package csql provides convenience functions for use with the types and functions defined in the standard library `database/sql` package.

Index

Package Files

csql.go doc.go insert.go rows.go

func Count Uses

func Count(db Queryer, query string, args ...interface{}) int

Count accepts any query of the form "SELECT COUNT(*) FROM ..." and returns the count. If an error occurs, it is panic'd as a SQLError.

Any args given are passed to the query.

func Exec Uses

func Exec(db Execer, query string, args ...interface{}) sql.Result

Exec returns the result of a running a query that doesn't return any rows. If an error occurs, it is panic'd as a SQLError.

func ForRow Uses

func ForRow(rows *sql.Rows, do func(RowScanner))

func Panic Uses

func Panic(err error)

Panic will wrap the given error in Error and pass it to panic. If the error is nil, this function does nothing.

func Prepare Uses

func Prepare(db Preparer, query string) *sql.Stmt

Prepare returns a prepared statement. If an error occurs, it is panic'd as a SQLError.

func Query Uses

func Query(db Queryer, query string, args ...interface{}) *sql.Rows

Query returns the result of a query that fetches many rows. If an error occurs, it is panic'd as a SQLError.

func Safe Uses

func Safe(errp *error)

Safe can be used to recover from a SQLError panic and convert it to an error. A pointer to the error must be passed. If a panic occurs with anything other than a SQLError, then it is re-panic'd.

This function is typically useful with 'defer' and a named error return value. For example:

func DoSomething(db Execer) (err error) {
	defer Safe(&err)

	Exec(db, "INSERT INTO ...")
	return
}

In this case, if the Exec (included in this package) fails, then it is converted to an error and used as the return value of DoSomething.

func SafeFunc Uses

func SafeFunc(f func()) (err error)

SafeFunc executes any function that may panic with a SQLError safely. In particular, if `f` panics with a SQLError, then Safe recovers and returns the error wrapped by SQLError.

If `f` panics with any other type of error, the panic is not recovered.

func Scan Uses

func Scan(scanner RowScanner, dest ...interface{})

Scan performs a scan on a row. If an error occurs, it is panic'd as a SQLError.

func Truncate Uses

func Truncate(db Execer, driver, table string)

Truncate truncates the table given. It uses the driver given to determine what kind of query to run.

func Tx Uses

func Tx(db Beginner, f func(*sql.Tx))

Tx runs the given function safely within a transaction. If the function panics with a SQLError, then the transaction is rolled back. Otherwise, the transaction is committed.

The first error that occurs (including beginning and ending the transaction) is panic'd.

func Value Uses

func Value(v Valuer) driver.Value

Value returns the driver value. If an error occurs, it is panic'd as a SQLError.

type Beginner Uses

type Beginner interface {
    Begin() (*sql.Tx, error)
}

Beginner describes values that can begin a transaction.

type Error Uses

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

Error satisfies the error interface. All panic'd errors in this package are Errors.

func (Error) Error Uses

func (se Error) Error() string

type Execer Uses

type Execer interface {
    Exec(query string, args ...interface{}) (sql.Result, error)
}

Execer describes values that can execute queries without returning any rows.

type Inserter Uses

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

func NewInserter Uses

func NewInserter(
    tx *sql.Tx,
    driver string,
    table string,
    columns ...string,
) (ins *Inserter, err error)

func (*Inserter) Exec Uses

func (in *Inserter) Exec(args ...interface{}) error

type Preparer Uses

type Preparer interface {
    Prepare(query string) (*sql.Stmt, error)
}

Preparer describes values that can create prepared statements.

type QExecer Uses

type QExecer interface {
    Exec(query string, args ...interface{}) (sql.Result, error)
    Query(query string, args ...interface{}) (*sql.Rows, error)
    QueryRow(query string, args ...interface{}) *sql.Row
}

QExecer is the composition of the Queryer and Execer interfaces.

type Queryer Uses

type Queryer interface {
    Query(query string, args ...interface{}) (*sql.Rows, error)
    QueryRow(query string, args ...interface{}) *sql.Row
}

Queryer describes values that can run queries which return 1 or many rows.

type RowScanner Uses

type RowScanner interface {
    Scan(dest ...interface{}) error
}

RowScanner describes values that can scan a row of values.

type Valuer Uses

type Valuer interface {
    Value() (driver.Value, error)
}

Valuer describes values that can convert themselves to a driver value.

Package csql imports 5 packages (graph) and is imported by 5 packages. Updated 2016-08-01. Refresh now. Tools for package owners.