mysqldb

package
v1.7.12 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2021 License: MIT Imports: 20 Imported by: 1

Documentation

Index

Constants

View Source
const (
	PcMysqlReadFail     = "mysql_read_fail"
	PcMysqlReadAllFail  = "mysql_read_all_fail"
	PcMysqlWriteAllFail = "mysql_write_all_fail"
	PcMysqlWriteFail    = "mysql_write_fail"
	PcMysqlRead         = "mysql_read"
	PcMysqlWrite        = "mysql_write"
	PcMysqlTransaction  = "mysql_transaction"
)
View Source
const (
	PcTransactionInsertDup = "transaction_insert_dup"
)

Variables

This section is empty.

Functions

func GetDataStructDests

func GetDataStructDests(data interface{}) ([]interface{}, error)

func GetDataStructFields

func GetDataStructFields(data interface{}) ([]string, error)

func GetDataStructValues

func GetDataStructValues(data interface{}) []driver.Value

func GetFields

func GetFields(v interface{}) (res []interface{}, errRet error)

func GetFieldsName

func GetFieldsName(v interface{}) (res string, errRet error)

func GetFieldsNameArray

func GetFieldsNameArray(v interface{}) (res []string, errRet error)

func IsDbConnError

func IsDbConnError(err error) bool

func IsTimeoutError

func IsTimeoutError(err error) bool

func MysqlEscapeString

func MysqlEscapeString(source string) string

Types

type CommonDbConf

type CommonDbConf struct {
	DbName      string
	ConnTime    string // connect timeout
	ReadTime    string // read timeout
	WriteTime   string // write timeout
	MaxOpen     int    // connect pool
	MaxIdle     int    // max idle connect
	MaxLifeTime int64  // connect life time

	Master *DbConnectConf
	Slave  *DbConnectConf
	// contains filtered or unexported fields
}

type DbConnectConf

type DbConnectConf struct {
	Addrs                []string
	User                 string
	Pass                 string
	CharSet              string // default utf8mb4
	ClientFoundRows      bool   // 对于update操作,若更改的字段值跟原来值相同,当clientFoundRows为false时,sql执行结果会返回0;当clientFoundRows为true,sql执行结果返回1
	IsProxy              bool
	EnableSqlSafeUpdates bool // (safe update mode),该模式不允许没有带WHERE条件的更新语句
}

type DbWrap

type DbWrap struct {
	Timeout time.Duration

	*sql.DB
	// contains filtered or unexported fields
}

func NewDbWrapped

func NewDbWrapped(host string, db *sql.DB, mysqlClient *MysqlClient, timeout time.Duration) *DbWrap

func NewDbWrappedRetry

func NewDbWrappedRetry(host string, db *sql.DB, mysqlClient *MysqlClient, timeout time.Duration, retry int) *DbWrap

func NewDbWrappedRetryProxy

func NewDbWrappedRetryProxy(host string, db *sql.DB, mysqlClient *MysqlClient, timeout time.Duration, retry int, proxy bool) *DbWrap

func (*DbWrap) Close

func (db *DbWrap) Close() error

func (*DbWrap) Exec

func (db *DbWrap) Exec(query string, args ...interface{}) (r sql.Result, err error)

func (*DbWrap) ExecContext

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

func (*DbWrap) ExecTransaction

func (db *DbWrap) ExecTransaction(transactionExec TransactionExec) (r sql.Result, err error)

func (*DbWrap) Query

func (db *DbWrap) Query(query string, args ...interface{}) (rs *sql.Rows, err error)

func (*DbWrap) QueryRow

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

type MysqlClient

type MysqlClient struct {
	DbConfig   *CommonDbConf `inject:"mysqlDbConfig" canNil:"true"`
	DbConf     *ini.File     `inject:"mysqlDbConf" canNil:"true"`
	DbConfPath string        `inject:"mysqlDbConfPath" canNil:"true"`
	DataBase   string        `inject:"mysqlDatabase" canNil:"true"`
	// contains filtered or unexported fields
}

func (*MysqlClient) Add

func (c *MysqlClient) Add(tableName string, d interface{}, ondupUpdate bool) error

d must be a struct pointer

func (*MysqlClient) AddEscapeAutoIncr

func (c *MysqlClient) AddEscapeAutoIncr(tableName string, d interface{}, ondupUpdate bool, atuoincrkey string) (int64, error)

func (*MysqlClient) AddEscapeAutoIncrAndRetLastId

func (c *MysqlClient) AddEscapeAutoIncrAndRetLastId(tableName string, d interface{}, atuoincrkey string) (int64, error)

AddEscapeAutoIncrAndRetLastId执行纯插入操作(若数据已存在,则返回失败),其会跳过由atuoincrkey指定的自增列,若执行成功,返回所插入的行id

func (*MysqlClient) Close

func (c *MysqlClient) Close()

func (*MysqlClient) Delete

func (c *MysqlClient) Delete(tableName string, condition map[string]interface{}) (int64, error)

condition value supports limit kinds of slice:[]int64,[]string,[]interface

func (*MysqlClient) ExecTransaction

func (c *MysqlClient) ExecTransaction(transactionExec TransactionExec) (int64, error)

func (*MysqlClient) Execute

func (c *MysqlClient) Execute(sql string, args ...interface{}) (int64, error)

func (*MysqlClient) GetCount

func (c *MysqlClient) GetCount(query string, args ...interface{}) (int64, error)

func (*MysqlClient) GetReadDbRandom

func (c *MysqlClient) GetReadDbRandom() (*DbWrap, error)

func (*MysqlClient) GetWriteDbs

func (c *MysqlClient) GetWriteDbs() *DbWrap

func (*MysqlClient) InsertOrUpdateOnDup

func (c *MysqlClient) InsertOrUpdateOnDup(tableName string, d interface{}, primaryKeys []string, updateFields []string, useSqlOnDup bool) (int64, error)

func (*MysqlClient) Query

func (c *MysqlClient) Query(dataType interface{}, query string, args ...interface{}) (interface{}, error)

no data return nil,nil

func (*MysqlClient) QueryList

func (c *MysqlClient) QueryList(dataType interface{}, query string, args ...interface{}) ([]interface{}, error)

func (*MysqlClient) Start

func (c *MysqlClient) Start() error

func (*MysqlClient) Update

func (c *MysqlClient) Update(tableName string, d interface{}, primaryKeys map[string]interface{}, fieldsToUpdate []string) error

d must be a struct pointer

type Row

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

Row is the result of calling QueryRow to select a single row.

func (*Row) Scan

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

Scan copies the columns from the matched row into the values pointed at by dest. See the documentation on Rows.Scan for details. If more than one row matches the query, Scan uses the first row and discards the rest. If no row matches the query, Scan returns ErrNoRows.

type SqlCondition

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

func NewSqlCondition

func NewSqlCondition() *SqlCondition

func (*SqlCondition) BuildShardWhereSql

func (c *SqlCondition) BuildShardWhereSql(shardKey string) (string, string, []interface{})
	Need to call WithTablePrefix() first
	returns:
	@tableName	string	tableName
	@sqlStr string      contains all contents after(include) WHERE
    @vars	[]interface	varsHolder

func (*SqlCondition) BuildWhereSql

func (c *SqlCondition) BuildWhereSql() (string, []interface{})
	returns:
	@sqlStr string      contains all contents after(include) WHERE
    @vars   []interface	varsHolder

func (*SqlCondition) Valid

func (c *SqlCondition) Valid(isGet bool) error

Valid SqlCondition,use before buildSql

func (*SqlCondition) WithCondition

func (c *SqlCondition) WithCondition(key, compare string, value interface{}) *SqlCondition

func (*SqlCondition) WithLimit

func (c *SqlCondition) WithLimit(limit int64) *SqlCondition

func (*SqlCondition) WithOffset

func (c *SqlCondition) WithOffset(offset int64) *SqlCondition

func (*SqlCondition) WithOrder

func (c *SqlCondition) WithOrder(key string, isdesc bool) *SqlCondition

func (*SqlCondition) WithTablePrefix

func (c *SqlCondition) WithTablePrefix(tablePrefix string) *SqlCondition

type TransactionExec

type TransactionExec func(ctx context.Context, tx *sql.Tx) (sql.Result, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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