dber

package
v0.27.4 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2024 License: Apache-2.0 Imports: 12 Imported by: 13

Documentation

Overview

Package dber is an interfaceization of database/sql.DB. It even implements a wrapper for *sql.DB: SqlDBer.

Index

Constants

View Source
const (
	TxUndecided = TxState(iota)
	TxRolledBack
	TxCommited
)
View Source
const ExpectAny = "{{ExpectAny}}"

Variables

View Source
var (
	Debug = func(string, ...interface{}) {}

	ErrQueryMismatch       = errors.New("query mismatch")
	ErrArgsMismatch        = errors.New("args mismatch")
	ErrTxAlreadyRolledBack = errors.New("transaction already rolled back")
	ErrTxAlreadyCommited   = errors.New("transaction already commited")
	ErrNotImplemented      = errors.New("not implemented")
)

Functions

This section is empty.

Types

type DBer

type DBer interface {
	Begin() (Txer, error)
	Queryer
	Execer
	io.Closer
}

type Execer

type Execer interface {
	ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
}

type Mock

type Mock interface {
	WithArgs(...interface{}) Mock
	WillReturnRows(...[]interface{}) Mock
	WithResult(ID, Affected int64) Mock
	WillSetArgs(map[int]interface{}) Mock
}

type Preparer

type Preparer interface {
	PrepareContext(context.Context, string) (*sql.Stmt, error)
}

type Queryer

type Queryer interface {
	QueryContext(context.Context, string, ...interface{}) (Rowser, error)
	QueryRowContext(context.Context, string, ...interface{}) Scanner
}

type ResultMock

type ResultMock struct {
	ID, Affected int64
}

func (ResultMock) LastInsertId

func (res ResultMock) LastInsertId() (int64, error)

func (ResultMock) RowsAffected

func (res ResultMock) RowsAffected() (int64, error)

type Rowser

type Rowser interface {
	Close() error
	Err() error
	Next() bool
	Scanner
}

type Scanner

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

type SqlDBer

type SqlDBer struct {
	*sql.DB
}

SqlDBer is a wrapper for *sql.DB to implement DBer

func (SqlDBer) Begin

func (db SqlDBer) Begin() (Txer, error)

func (SqlDBer) BeginTx

func (db SqlDBer) BeginTx(ctx context.Context, opt *sql.TxOptions) (Txer, error)

func (SqlDBer) Query

func (db SqlDBer) Query(qry string, args ...interface{}) (Rowser, error)

func (SqlDBer) QueryContext

func (db SqlDBer) QueryContext(ctx context.Context, qry string, args ...interface{}) (Rowser, error)

func (SqlDBer) QueryRow

func (db SqlDBer) QueryRow(qry string, args ...interface{}) Scanner

func (SqlDBer) QueryRowContext

func (db SqlDBer) QueryRowContext(ctx context.Context, qry string, args ...interface{}) Scanner

type SqlRowser

type SqlRowser struct {
	*sql.Rows
}

SqlRowser is a wrapper for *sql.Rows to implement Rowser.

type SqlScanner

type SqlScanner struct {
	*sql.Row
}

SqlScanner is a wrapper for *sql.Row to implement Scanner.

type SqlTxer

type SqlTxer struct {
	*sql.Tx
}

SqlTxer is a wrapper for *sql.Tx to implement Txer

func (SqlTxer) PrepareContext

func (tx SqlTxer) PrepareContext(ctx context.Context, qry string) (*sql.Stmt, error)

func (SqlTxer) Query

func (tx SqlTxer) Query(qry string, args ...interface{}) (Rowser, error)

func (SqlTxer) QueryContext

func (tx SqlTxer) QueryContext(ctx context.Context, qry string, args ...interface{}) (Rowser, error)

func (SqlTxer) QueryRow

func (tx SqlTxer) QueryRow(qry string, args ...interface{}) Scanner

func (SqlTxer) QueryRowContext

func (tx SqlTxer) QueryRowContext(ctx context.Context, qry string, args ...interface{}) Scanner

type Tx

type Tx struct {
	Expects []*expectQuery
	// contains filtered or unexported fields
}

func (*Tx) Commit

func (tx *Tx) Commit() error

func (*Tx) Exec

func (tx *Tx) Exec(qry string, params ...interface{}) (sql.Result, error)

Execute checks whether the given query matches with the next expected.

func (*Tx) ExecContext

func (tx *Tx) ExecContext(ctx context.Context, qry string, params ...interface{}) (sql.Result, error)

func (*Tx) ExpectQuery

func (p *Tx) ExpectQuery(qry string) Mock

ExpectQuery adds the query to the list of expected queries. Iff the query starts and ends with "/", it is treated as a regexp, otherwise as plain text.

func (*Tx) PrepAndExe

func (tx *Tx) PrepAndExe(qry string, params ...interface{}) (uint64, error)

func (*Tx) PrepareContext

func (p *Tx) PrepareContext(_ context.Context, _ string) (*sql.Stmt, error)

func (*Tx) Query

func (tx *Tx) Query(qry string, params ...interface{}) (Rowser, error)

func (*Tx) QueryContext

func (tx *Tx) QueryContext(ctx context.Context, qry string, params ...interface{}) (Rowser, error)

func (*Tx) QueryRow

func (tx *Tx) QueryRow(qry string, params ...interface{}) Scanner

func (*Tx) QueryRowContext

func (tx *Tx) QueryRowContext(ctx context.Context, qry string, params ...interface{}) Scanner

func (*Tx) Rollback

func (tx *Tx) Rollback() error

type TxState

type TxState uint8

type Txer

type Txer interface {
	Commit() error
	Rollback() error
	Execer
	Preparer
	Queryer
}

Jump to

Keyboard shortcuts

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