runner

package
v0.0.0-...-a4cd505 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2019 License: MIT, MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

Cache caches query results.

View Source
var Callbacks = struct {
	SlowQuery SlowQuery
	SqlError  SqlError
}{
	SlowQuery: func(elapsed time.Duration, sql string, args []interface{}) {},
	SqlError:  func(err error, msg string, statement string, args []interface{}) {},
}
View Source
var ErrTxRollbacked = errors.New("Nested transaction already rollbacked")

ErrTxRollbacked occurs when Commit() or Rollback() is called on a transaction that has already been rollbacked.

View Source
var LogErrNoRows bool

LogErrNoRows tells runner to log `sql.ErrNoRows`

View Source
var LogQueriesThreshold time.Duration

LogQueriesThreshold is the threshold for logging "slow" queries

Functions

func MustPing

func MustPing(db *sql.DB)

MustPing pings a database with an exponential backoff. The function panics if the database cannot be pinged after 15 minutes

func SetCache

func SetCache(store kvs.KeyValueStore)

SetCache sets this runner's cache. The default cache is in-memory based. See cache.MemoryKeyValueStore.

Types

type Connection

type Connection interface {
	Begin() (*Tx, error)
	Call(sproc string, args ...interface{}) *dat.CallBuilder
	DeleteFrom(table string) *dat.DeleteBuilder
	Exec(cmd string, args ...interface{}) (*dat.Result, error)
	ExecBuilder(b dat.Builder) error
	ExecMulti(commands ...*dat.Expression) (int, error)
	InsertInto(table string) *dat.InsertBuilder
	Insect(table string) *dat.InsectBuilder
	Select(columns ...string) *dat.SelectBuilder
	SelectDoc(columns ...string) *dat.SelectDocBuilder
	SQL(sql string, args ...interface{}) *dat.RawBuilder
	Update(table string) *dat.UpdateBuilder
	Upsert(table string) *dat.UpsertBuilder
}

Connection is a queryable connection and represents a DB or Tx.

type DB

type DB struct {
	DB *sqlx.DB
	*Queryable
	Version int64
}

DB represents an abstract database connection pool.

func NewDB

func NewDB(db *sql.DB, driverName string) *DB

NewDB instantiates a Connection for a given database/sql connection

func NewDBFromSqlx

func NewDBFromSqlx(dbx *sqlx.DB) *DB

NewDBFromSqlx creates a new Connection object from existing Sqlx.DB.

func NewDBFromString

func NewDBFromString(driver string, connectionString string) *DB

NewDBFromString instantiates a Connection from a given driver and connection string.

func (*DB) Begin

func (db *DB) Begin() (*Tx, error)

Begin creates a transaction for the given database

func (*DB) MustCreateMetaTable

func (db *DB) MustCreateMetaTable()

MustCreateMetaTable creates the dat__meta table or panics.

func (*DB) MustRegisterFunction

func (db *DB) MustRegisterFunction(name string, version string, body string)

MustRegisterFunction registers a user defined function but will not recreate it unles the hash has changed with version. This is useful for keeping user defined functions defined in source code.

type Execer

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

Execer executes queries against a database.

func NewExecer

func NewExecer(database database, builder dat.Builder) *Execer

NewExecer creates a new instance of Execer.

func (*Execer) Cache

func (ex *Execer) Cache(id string, ttl time.Duration, invalidate bool) dat.Execer

Cache caches the results of queries for Select and SelectDoc.

func (*Execer) Cancel

func (ex *Execer) Cancel() error

Cancel cancels last query with a queryID. If queryID was not set then ErrInvalidOperation is returned.

func (*Execer) Exec

func (ex *Execer) Exec() (*dat.Result, error)

Exec executes a builder's query.

func (*Execer) Interpolate

func (ex *Execer) Interpolate() (string, []interface{}, error)

Interpolate tells the associated builder to interpolate itself.

func (*Execer) QueryJSON

func (ex *Execer) QueryJSON() ([]byte, error)

QueryJSON wraps the builder's query within a `to_json` then executes and returns the JSON []byte representation.

func (*Execer) QueryObject

func (ex *Execer) QueryObject(dest interface{}) error

QueryObject wraps the builder's query within a `to_json` then executes and unmarshals the result into dest.

func (*Execer) QueryScalar

func (ex *Execer) QueryScalar(destinations ...interface{}) error

QueryScalar executes builder's query and scans returned row into destinations.

func (*Execer) QuerySlice

func (ex *Execer) QuerySlice(dest interface{}) error

QuerySlice executes builder's query and builds a slice of values from each row, where each row only has one column.

func (*Execer) QueryStruct

func (ex *Execer) QueryStruct(dest interface{}) error

QueryStruct executes builders' query and scans the result row into dest.

func (*Execer) QueryStructs

func (ex *Execer) QueryStructs(dest interface{}) error

QueryStructs executes builders' query and scans each row as an item in a slice of structs.

func (*Execer) Queryx

func (ex *Execer) Queryx() (*sqlx.Rows, error)

Queryx executes builder's query and returns rows.

func (*Execer) Timeout

func (ex *Execer) Timeout(timeout time.Duration) dat.Execer

Timeout sets the timeout for current query.

type Queryable

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

Queryable is an object that can be queried.

func WrapSqlxExt

func WrapSqlxExt(e sqlx.Ext) *Queryable

WrapSqlxExt converts a sqlx.Ext to a *Queryable

func (*Queryable) Call

func (q *Queryable) Call(sproc string, args ...interface{}) *dat.CallBuilder

Call creates a new CallBuilder for the given sproc and args.

func (*Queryable) DeleteFrom

func (q *Queryable) DeleteFrom(table string) *dat.DeleteBuilder

DeleteFrom creates a new DeleteBuilder for the given table.

func (*Queryable) Exec

func (q *Queryable) Exec(cmd string, args ...interface{}) (*dat.Result, error)

Exec executes a SQL query with optional arguments.

func (*Queryable) ExecBuilder

func (q *Queryable) ExecBuilder(b dat.Builder) error

ExecBuilder executes the SQL in builder.

func (*Queryable) ExecMulti

func (q *Queryable) ExecMulti(commands ...*dat.Expression) (int, error)

ExecMulti executes multiple SQL statements returning the number of statements executed, or the index at which an error occurred.

func (*Queryable) Insect

func (q *Queryable) Insect(table string) *dat.InsectBuilder

Insect inserts or selects.

func (*Queryable) InsertInto

func (q *Queryable) InsertInto(table string) *dat.InsertBuilder

InsertInto creates a new InsertBuilder for the given table.

func (*Queryable) SQL

func (q *Queryable) SQL(sql string, args ...interface{}) *dat.RawBuilder

SQL creates a new raw SQL builder.

func (*Queryable) Select

func (q *Queryable) Select(columns ...string) *dat.SelectBuilder

Select creates a new SelectBuilder for the given columns.

func (*Queryable) SelectDoc

func (q *Queryable) SelectDoc(columns ...string) *dat.SelectDocBuilder

SelectDoc creates a new SelectBuilder for the given columns.

func (*Queryable) Update

func (q *Queryable) Update(table string) *dat.UpdateBuilder

Update creates a new UpdateBuilder for the given table.

func (*Queryable) Upsert

func (q *Queryable) Upsert(table string) *dat.UpsertBuilder

Upsert creates a new UpdateBuilder for the given table.

type SlowQuery

type SlowQuery func(elapsed time.Duration, sql string, args []interface{})

type SqlError

type SqlError func(err error, msg string, statement string, args []interface{})

type Tx

type Tx struct {
	sync.Mutex
	*sqlx.Tx
	*Queryable
	IsRollbacked bool
	// contains filtered or unexported fields
}

Tx is a transaction for the given Session

func WrapSqlxTx

func WrapSqlxTx(tx *sqlx.Tx) *Tx

WrapSqlxTx creates a Tx from a sqlx.Tx

func (*Tx) AutoCommit

func (tx *Tx) AutoCommit() error

AutoCommit commits a transaction IF neither Commit or Rollback were called.

func (*Tx) AutoRollback

func (tx *Tx) AutoRollback() error

AutoRollback rolls back transaction IF neither Commit or Rollback were called.

func (*Tx) Begin

func (tx *Tx) Begin() (*Tx, error)

Begin returns this transaction

func (*Tx) Commit

func (tx *Tx) Commit() error

Commit commits the transaction

func (*Tx) Rollback

func (tx *Tx) Rollback() error

Rollback cancels the transaction

func (*Tx) Select

func (tx *Tx) Select(columns ...string) *dat.SelectBuilder

Select creates a new SelectBuilder for the given columns. This disambiguates between Queryable.Select and sqlx's Select

Jump to

Keyboard shortcuts

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