mysql

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2021 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoMasterConn = errors.New("mysql group: no master connection available")
	ErrNoSlaveConn  = errors.New("mysql group: no slave connection available")
)
View Source
var (
	AcquireQuery = func() *Query {
		return queryPool.Get().(*Query)
	}

	ReleaseQuery = func(query *Query) {
		query = query.reset()
		queryPool.Put(query)
	}
)

Functions

func FormatRows

func FormatRows(rows *sql.Rows, handler RowFormat)

func ToMap

func ToMap(rows *sql.Rows) ([]map[string]string, error)

Types

type ExecResult

type ExecResult struct {
	LastInsertId int64
	AffectedRows int64
}

type Group

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

func NewGroup

func NewGroup(groupOption *GroupOption) *Group

func (*Group) BatchInsert

func (g *Group) BatchInsert(table string, rows []map[string]interface{}) (result *ExecResult, err error)

func (*Group) Begin

func (g *Group) Begin() (trans *Transaction, err error)

func (*Group) BeginTx

func (g *Group) BeginTx(ctx context.Context, opts *sql.TxOptions) (trans *Transaction, err error)

func (*Group) DeleteAll

func (g *Group) DeleteAll(table string, where map[string]interface{}) (result *ExecResult, err error)

func (*Group) Find

func (g *Group) Find(query *Query, useMaster bool) (rows *sql.Rows, err error)

func (*Group) GetBadPool

func (g *Group) GetBadPool(isMaster bool) (list []int)

func (*Group) GetMaster

func (g *Group) GetMaster() (index int, mPoll *Pool)

func (*Group) GetSlave

func (g *Group) GetSlave() (index int, mPoll *Pool)

func (*Group) Insert

func (g *Group) Insert(table string, columns map[string]interface{}) (result *ExecResult, err error)

func (*Group) MasterExec

func (g *Group) MasterExec(handler func(mPool *Pool) (interface{}, error)) (result interface{}, err error)

func (*Group) SelectPool

func (g *Group) SelectPool(isMaster bool) (index int, mPool *Pool)

func (*Group) SlaveQuery

func (g *Group) SlaveQuery(handler func(mPool *Pool) (interface{}, error)) (result interface{}, err error)

func (*Group) UpdateAll

func (g *Group) UpdateAll(table string, set map[string]interface{}, where map[string]interface{}) (result *ExecResult, err error)

type GroupOption

type GroupOption struct {
	Masters []PoolOption `yaml:"masters" json:"masters"`
	Slaves  []PoolOption `yaml:"slaves" json:"slaves"`
	//单位s
	RetryInterval int64 `yaml:"retryInterval" json:"retryInterval"`
}

type Pool

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

func NewPool

func NewPool(option *PoolOption) (*Pool, error)

func (*Pool) BatchInsert

func (p *Pool) BatchInsert(table string, rows *[]map[string]interface{}) (result *ExecResult, err error)

func (*Pool) Begin

func (p *Pool) Begin() (trans *Transaction, err error)

func (*Pool) BeginTx

func (p *Pool) BeginTx(ctx context.Context, opts *sql.TxOptions) (trans *Transaction, err error)

func (*Pool) Db

func (p *Pool) Db() *sql.DB

func (*Pool) DeleteAll

func (p *Pool) DeleteAll(table string, where map[string]interface{}) (result *ExecResult, err error)

func (*Pool) Execute

func (p *Pool) Execute(sqlStr string, args ...interface{}) (result *ExecResult, err error)

func (*Pool) Find

func (p *Pool) Find(query *Query) (*sql.Rows, error)

func (*Pool) Insert

func (p *Pool) Insert(table string, columns *map[string]interface{}) (result *ExecResult, err error)

func (*Pool) Query

func (p *Pool) Query(sqlStr string, args ...interface{}) (rows *sql.Rows, err error)

func (*Pool) UpdateAll

func (p *Pool) UpdateAll(table string, set map[string]interface{}, where map[string]interface{}) (result *ExecResult, err error)

type PoolOption

type PoolOption struct {
	//格式:"userName:password@schema(host:port)/dbName",如:root:123456@tcp(127.0.0.1:3306)/test
	Dsn string `yaml:"dsn" json:"dsn"`
	//单位s
	MaxConnLifetime int `yaml:"maxConnLifetime" json:"maxConnLifetime"`
	MaxOpenConns    int `yaml:"maxOpenConns" json:"maxOpenConns"`
	MaxIdleConns    int `yaml:"maxIdleConns" json:"maxIdleConns"`
}

type Query

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

func (*Query) From

func (q *Query) From(table string) *Query

func (*Query) Group

func (q *Query) Group(fields ...string) *Query

func (*Query) Having

func (q *Query) Having(having string) *Query

func (*Query) Limit

func (q *Query) Limit(offset int64, limit int64) *Query

func (*Query) Offset

func (q *Query) Offset(offset int64) *Query

func (*Query) Order

func (q *Query) Order(orders ...string) *Query

func (*Query) Query

func (q *Query) Query(pool *Pool) (*sql.Rows, error)

func (*Query) QueryByGroup

func (q *Query) QueryByGroup(group *Group, useMaster bool) (*sql.Rows, error)

func (*Query) Select

func (q *Query) Select(columns ...string) *Query

func (*Query) Where

func (q *Query) Where(where map[string]interface{}) *Query

type RowFormat

type RowFormat func(fieldValue map[string][]byte)

type Transaction

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

func (*Transaction) BatchInsert

func (t *Transaction) BatchInsert(table string, rows *[]map[string]interface{}) (result *ExecResult, err error)

func (*Transaction) Commit

func (t *Transaction) Commit() error

func (*Transaction) DeleteAll

func (t *Transaction) DeleteAll(table string, where map[string]interface{}) (result *ExecResult, err error)

func (*Transaction) Execute

func (t *Transaction) Execute(sqlStr string, args ...interface{}) (sql.Result, error)

func (*Transaction) Find

func (t *Transaction) Find(query *Query) (*sql.Rows, error)

func (*Transaction) Insert

func (t *Transaction) Insert(table string, columns *map[string]interface{}) (result *ExecResult, err error)

func (*Transaction) Query

func (t *Transaction) Query(sqlStr string, args ...interface{}) (*sql.Rows, error)

func (*Transaction) Rollback

func (t *Transaction) Rollback() error

func (*Transaction) UpdateAll

func (t *Transaction) UpdateAll(table string, set map[string]interface{}, where map[string]interface{}) (result *ExecResult, err error)

Jump to

Keyboard shortcuts

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