mysqldb

package
v1.7.19 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2023 License: MIT Imports: 23 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{}, dbType string) ([]interface{}, map[int]int, 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 {
	DbType      string // db type
	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"`

	// 数据库类型指定 dm mysql 缺省:mysql
	DbType string
	// contains filtered or unexported fields
}

func (*MysqlClient) Add

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

Add DM不支持该方法 推荐使用AddEscapeAutoIncr、AddEscapeAutoIncrAndRetLastId、InsertOrUpdateOnDup.

func (*MysqlClient) AddEscapeAutoIncr

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

AddEscapeAutoIncr DM支持该方法 插入/插入更新 ondupUpdate 插入更新,必须指定自主列名称 atuoincrkey.

func (*MysqlClient) AddEscapeAutoIncrAndRetLastId

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

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

func (*MysqlClient) Close

func (c *MysqlClient) Close()

func (*MysqlClient) Delete

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

Update DM支持该方法 根据condition删除数据 .

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)

GetCount DM支持该方法 获取表中数据总数 .

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)

InsertOrUpdateOnDup 根据主键/插入更新 useSqlOnDup=false,根据primaryKeys更新数据。useSqlOnDup=true,主键重复则更新updateFields,否则插入新数据主键ID自增.

func (*MysqlClient) IsExistTable added in v1.7.13

func (c *MysqlClient) IsExistTable(tableName string) (bool, error)

IsExistTable DM支持该方法 判断表是否存在

func (*MysqlClient) Query

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

Query DM支持该方法 获取数据,无数据返回nil,nil.

func (*MysqlClient) QueryList

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

QueryList DM支持该方法 获取数据列表.

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

Update DM支持该方法 根据主键primaryKeys更新数据.

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(indexMap map[int]int, 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 TableName added in v1.7.17

type TableName struct {
	TableName string `json:"table_name" mysqlField:"TABLE_NAME"`
}

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