Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoMapPointer represents error when no map pointer
	ErrNoMapPointer = errors.New("mp should be a map's pointer")
	// ErrNoStructPointer represents error when no struct pointer
	ErrNoStructPointer = errors.New("mp should be a struct's pointer")
)
View Source
var (
	// DefaultCacheSize sets the default cache size
	DefaultCacheSize = 200
)

Functions

func MapToSlice

func MapToSlice(query string, mp interface{}) (string, []interface{}, error)

MapToSlice map query and struct as sql and args

func StructToSlice

func StructToSlice(query string, st interface{}) (string, []interface{}, error)

StructToSlice converts a query and struct as sql and args

Types

type DB

type DB struct {
	*sql.DB
	Mapper names.Mapper

	Logger log.ContextLogger
	// contains filtered or unexported fields
}

DB is a wrap of sql.DB with extra contents

func FromDB

func FromDB(db *sql.DB) *DB

FromDB creates a DB from a sql.DB

func Open

func Open(driverName, dataSourceName string) (*DB, error)

Open opens a database

func (*DB) AddHook

func (db *DB) AddHook(h ...contexts.Hook)

AddHook adds hook

func (*DB) Begin

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

Begin begins a transaction

func (*DB) BeginTx

func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)

BeginTx begin a transaction with option

func (*DB) ExecContext

func (db *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

ExecContext exec query with args

func (*DB) ExecMap

func (db *DB) ExecMap(query string, mp interface{}) (sql.Result, error)

ExecMap exec query with map

func (*DB) ExecMapContext

func (db *DB) ExecMapContext(ctx context.Context, query string, mp interface{}) (sql.Result, error)

ExecMapContext exec map with context.ContextHook insert into (name) values (?) insert into (name) values (?name)

func (*DB) ExecStruct

func (db *DB) ExecStruct(query string, st interface{}) (sql.Result, error)

ExecStruct exec query with struct

func (*DB) ExecStructContext

func (db *DB) ExecStructContext(ctx context.Context, query string, st interface{}) (sql.Result, error)

ExecStructContext exec query with map

func (*DB) NeedLogSQL

func (db *DB) NeedLogSQL(ctx context.Context) bool

NeedLogSQL returns true if need to log SQL

func (*DB) Prepare

func (db *DB) Prepare(query string) (*Stmt, error)

Prepare creates a prepare statement

func (*DB) PrepareContext

func (db *DB) PrepareContext(ctx context.Context, query string) (*Stmt, error)

PrepareContext creates a prepare statement

func (*DB) Query

func (db *DB) Query(query string, args ...interface{}) (*Rows, error)

Query overwrites sql.DB.Query

func (*DB) QueryContext

func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)

QueryContext overwrites sql.DB.QueryContext

func (*DB) QueryMap

func (db *DB) QueryMap(query string, mp interface{}) (*Rows, error)

QueryMap executes query with parameters via map

func (*DB) QueryMapContext

func (db *DB) QueryMapContext(ctx context.Context, query string, mp interface{}) (*Rows, error)

QueryMapContext executes query with parameters via map and context

func (*DB) QueryRow

func (db *DB) QueryRow(query string, args ...interface{}) *Row

QueryRow query row with args

func (*DB) QueryRowContext

func (db *DB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row

QueryRowContext query row with args

func (*DB) QueryRowMap

func (db *DB) QueryRowMap(query string, mp interface{}) *Row

QueryRowMap query row with map

func (*DB) QueryRowMapContext

func (db *DB) QueryRowMapContext(ctx context.Context, query string, mp interface{}) *Row

QueryRowMapContext query row with map

func (*DB) QueryRowStruct

func (db *DB) QueryRowStruct(query string, st interface{}) *Row

QueryRowStruct query row with struct

func (*DB) QueryRowStructContext

func (db *DB) QueryRowStructContext(ctx context.Context, query string, st interface{}) *Row

QueryRowStructContext query row with struct

func (*DB) QueryStruct

func (db *DB) QueryStruct(query string, st interface{}) (*Rows, error)

QueryStruct query rows with struct

func (*DB) QueryStructContext

func (db *DB) QueryStructContext(ctx context.Context, query string, st interface{}) (*Rows, error)

QueryStructContext query rows with struct

type EmptyScanner

type EmptyScanner struct {
}

EmptyScanner represents an empty scanner

func (EmptyScanner) Scan

func (EmptyScanner) Scan(src interface{}) error

Scan implements

type Executer

type Executer interface {
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
}

Executer represents an interface to execute a SQL

type NullTime

type NullTime time.Time

NullTime defines a customize type NullTime

func (*NullTime) Scan

func (ns *NullTime) Scan(value interface{}) error

Scan implements driver.Valuer

func (NullTime) Value

func (ns NullTime) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type QueryExecuter

type QueryExecuter interface {
	Queryer
	Executer
}

QueryExecuter combines the Queryer and Executer

type Queryer

type Queryer interface {
	QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)
}

Queryer represents an interface to query a SQL to get data from database

type Row

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

Row reprents a row of a tab

func ErrorRow

func ErrorRow(err error) *Row

ErrorRow return an error row

func NewRow

func NewRow(rows *Rows, err error) *Row

NewRow from rows

func (*Row) Columns

func (row *Row) Columns() ([]string, error)

Columns returns all columns of the row

func (*Row) Scan

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

Scan retrieves all row column values

func (*Row) ScanMap

func (row *Row) ScanMap(dest interface{}) error

ScanMap scan data to a map's pointer

func (*Row) ScanSlice

func (row *Row) ScanSlice(dest interface{}) error

ScanSlice scan data to a slice's pointer, slice's length should equal to columns' number

func (*Row) ScanStructByIndex

func (row *Row) ScanStructByIndex(dest interface{}) error

ScanStructByIndex retrieves all row column values into a struct

func (*Row) ScanStructByName

func (row *Row) ScanStructByName(dest interface{}) error

ScanStructByName retrieves all row column values into a struct

func (*Row) ToMapString

func (row *Row) ToMapString() (map[string]string, error)

ToMapString returns all clumns of this record

type Rows

type Rows struct {
	*sql.Rows
	// contains filtered or unexported fields
}

Rows represents rows of table

func (*Rows) ScanMap

func (rs *Rows) ScanMap(dest interface{}) error

ScanMap scan data to a map's pointer

func (*Rows) ScanSlice

func (rs *Rows) ScanSlice(dest interface{}) error

ScanSlice scan data to a slice's pointer, slice's length should equal to columns' number

func (*Rows) ScanStructByIndex

func (rs *Rows) ScanStructByIndex(dest ...interface{}) error

ScanStructByIndex scan data to a struct's pointer according field index

func (*Rows) ScanStructByName

func (rs *Rows) ScanStructByName(dest interface{}) error

ScanStructByName scan data to a struct's pointer according field name

func (*Rows) ToMapString

func (rs *Rows) ToMapString() ([]map[string]string, error)

ToMapString returns all records

type Stmt

type Stmt struct {
	*sql.Stmt
	// contains filtered or unexported fields
}

Stmt reprents a stmt objects

func (*Stmt) ExecContext

func (s *Stmt) ExecContext(ctx context.Context, args ...interface{}) (sql.Result, error)

ExecContext with args

func (*Stmt) ExecMap

func (s *Stmt) ExecMap(mp interface{}) (sql.Result, error)

ExecMap executes with map

func (*Stmt) ExecMapContext

func (s *Stmt) ExecMapContext(ctx context.Context, mp interface{}) (sql.Result, error)

ExecMapContext execute with map

func (*Stmt) ExecStruct

func (s *Stmt) ExecStruct(st interface{}) (sql.Result, error)

ExecStruct executes with struct

func (*Stmt) ExecStructContext

func (s *Stmt) ExecStructContext(ctx context.Context, st interface{}) (sql.Result, error)

ExecStructContext executes with struct

func (*Stmt) Query

func (s *Stmt) Query(args ...interface{}) (*Rows, error)

Query query with args

func (*Stmt) QueryContext

func (s *Stmt) QueryContext(ctx context.Context, args ...interface{}) (*Rows, error)

QueryContext query with args

func (*Stmt) QueryMap

func (s *Stmt) QueryMap(mp interface{}) (*Rows, error)

QueryMap query with map

func (*Stmt) QueryMapContext

func (s *Stmt) QueryMapContext(ctx context.Context, mp interface{}) (*Rows, error)

QueryMapContext query with map

func (*Stmt) QueryRow

func (s *Stmt) QueryRow(args ...interface{}) *Row

QueryRow query row with args

func (*Stmt) QueryRowContext

func (s *Stmt) QueryRowContext(ctx context.Context, args ...interface{}) *Row

QueryRowContext query row with args

func (*Stmt) QueryRowMap

func (s *Stmt) QueryRowMap(mp interface{}) *Row

QueryRowMap query row with map

func (*Stmt) QueryRowMapContext

func (s *Stmt) QueryRowMapContext(ctx context.Context, mp interface{}) *Row

QueryRowMapContext query row with map

func (*Stmt) QueryRowStruct

func (s *Stmt) QueryRowStruct(st interface{}) *Row

QueryRowStruct query row with struct

func (*Stmt) QueryRowStructContext

func (s *Stmt) QueryRowStructContext(ctx context.Context, st interface{}) *Row

QueryRowStructContext query row with struct

func (*Stmt) QueryStruct

func (s *Stmt) QueryStruct(st interface{}) (*Rows, error)

QueryStruct query with struct

func (*Stmt) QueryStructContext

func (s *Stmt) QueryStructContext(ctx context.Context, st interface{}) (*Rows, error)

QueryStructContext query with struct

type Tx

type Tx struct {
	*sql.Tx
	// contains filtered or unexported fields
}

Tx represents a transaction

func (*Tx) Commit

func (tx *Tx) Commit() error

Commit submit the transaction

func (*Tx) ExecContext

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

ExecContext executes a query with args

func (*Tx) ExecMap

func (tx *Tx) ExecMap(query string, mp interface{}) (sql.Result, error)

ExecMap executes query with args in a map

func (*Tx) ExecMapContext

func (tx *Tx) ExecMapContext(ctx context.Context, query string, mp interface{}) (sql.Result, error)

ExecMapContext executes query with args in a map

func (*Tx) ExecStruct

func (tx *Tx) ExecStruct(query string, st interface{}) (sql.Result, error)

ExecStruct executes query with args in a struct

func (*Tx) ExecStructContext

func (tx *Tx) ExecStructContext(ctx context.Context, query string, st interface{}) (sql.Result, error)

ExecStructContext executes query with args in a struct

func (*Tx) Prepare

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

Prepare prepare the query

func (*Tx) PrepareContext

func (tx *Tx) PrepareContext(ctx context.Context, query string) (*Stmt, error)

PrepareContext prepare the query

func (*Tx) Query

func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error)

Query query with args

func (*Tx) QueryContext

func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)

QueryContext query with args

func (*Tx) QueryMap

func (tx *Tx) QueryMap(query string, mp interface{}) (*Rows, error)

QueryMap query with args in a map

func (*Tx) QueryMapContext

func (tx *Tx) QueryMapContext(ctx context.Context, query string, mp interface{}) (*Rows, error)

QueryMapContext query with args in a map

func (*Tx) QueryRow

func (tx *Tx) QueryRow(query string, args ...interface{}) *Row

QueryRow query one row with args

func (*Tx) QueryRowContext

func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row

QueryRowContext query one row with args

func (*Tx) QueryRowMap

func (tx *Tx) QueryRowMap(query string, mp interface{}) *Row

QueryRowMap query one row with args in a map

func (*Tx) QueryRowMapContext

func (tx *Tx) QueryRowMapContext(ctx context.Context, query string, mp interface{}) *Row

QueryRowMapContext query one row with args in a map

func (*Tx) QueryRowStruct

func (tx *Tx) QueryRowStruct(query string, st interface{}) *Row

QueryRowStruct query one row with args in struct

func (*Tx) QueryRowStructContext

func (tx *Tx) QueryRowStructContext(ctx context.Context, query string, st interface{}) *Row

QueryRowStructContext query one row with args in struct

func (*Tx) QueryStruct

func (tx *Tx) QueryStruct(query string, st interface{}) (*Rows, error)

QueryStruct query with args in struct

func (*Tx) QueryStructContext

func (tx *Tx) QueryStructContext(ctx context.Context, query string, st interface{}) (*Rows, error)

QueryStructContext query with args in struct

func (*Tx) Rollback

func (tx *Tx) Rollback() error

Rollback rollback the transaction

func (*Tx) Stmt

func (tx *Tx) Stmt(stmt *Stmt) *Stmt

Stmt creates Stmt

func (*Tx) StmtContext

func (tx *Tx) StmtContext(ctx context.Context, stmt *Stmt) *Stmt

StmtContext creates Stmt with context