mysql

package
v0.3.19 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2023 License: Apache-2.0 Imports: 17 Imported by: 7

Documentation

Index

Constants

View Source
const (
	DefaultAddrSliceLen = 2
	ValueStr            = "Value"
	VariableOn          = "ON"
	VariableOFF         = "OFF"

	DefaultCharSet                = "utf8mb4"
	HostString                    = "host"
	PortString                    = "port"
	DefaultCheckInstanceStatusSQL = "select 1 as ok;"
	SelectVersionSQL              = "select @@version"
	ShowSlaveStatusSQL            = "show slave status"
	ShowReplicaStatusSQL          = "show replica status"
	ShowSlaveHostsSQL             = "show slave hosts"

	// ReplicationSource represents mysql master, it's an alternative name
	ReplicationSource ReplicationRole = "source"
	// ReplicationReplica represents mysql slave, it's an alternative name
	ReplicationReplica ReplicationRole = "replica"
	// ReplicationRelay means this mysql instance has source and replica roles at the same time
	ReplicationRelay ReplicationRole = "relay"
)
View Source
const (
	DefaultMaxConnections      = 20
	DefaultInitConnections     = 5
	DefaultMaxIdleConnections  = 10
	DefaultMaxIdleTime         = 1800 // seconds
	DefaultMaxWaitTime         = 1    // seconds
	DefaultMaxRetryCount       = -1
	DefaultKeepAliveInterval   = 300 // seconds
	DefaultKeepAliveChunkSize  = 5
	DefaultSleepTime           = 1 // seconds
	DefaultFreeChanLengthTimes = 2

	DefaultUnlimitedWaitTime   = -1 // seconds
	DefaultUnlimitedRetryCount = -1
	DefaultDelayTime           = 5 // milliseconds
)
View Source
const (
	LessThan    = -1
	Equal       = 0
	GreaterThan = 1
)

Variables

View Source
var Version8 = &version{8, 0, 0}

Functions

func Close added in v0.2.1

func Close() error

Close closes global pool, it sets global pool to nil pointer

func Execute added in v0.2.1

func Execute(sql string, args ...interface{}) (middleware.Result, error)

Execute execute given sql statement

func InitGlobalPool added in v0.3.0

func InitGlobalPool(addr, dbName, dbUser, dbPass string,
	maxConnections, initConnections, maxIdleConnections, maxIdleTime, maxWaitTime, maxRetryCount, keepAliveInterval int) error

InitGlobalPool returns a new *Pool and replaces it as global pool

func InitGlobalPoolWithConfig added in v0.3.0

func InitGlobalPoolWithConfig(config Config, maxConnections, initConnections, maxIdleConnections, maxIdleTime,
	maxWaitTime, maxRetryCount, keepAliveInterval int) error

InitGlobalPoolWithConfig returns a new *Pool with a Config object and replaces it as global pool

func InitGlobalPoolWithDefault added in v0.3.0

func InitGlobalPoolWithDefault(addr, dbName, dbUser, dbPass string) error

InitGlobalPoolWithDefault returns a new *Pool with default configuration and replaces it as global pool

func InitGlobalPoolWithPoolConfig added in v0.3.0

func InitGlobalPoolWithPoolConfig(config PoolConfig) error

InitGlobalPoolWithPoolConfig returns a new *Pool with a PoolConfig object and replaces it as global pool

func IsClosed added in v0.2.1

func IsClosed() bool

IsClosed returns if global pool had been closed

func MockClientExecute added in v0.3.18

func MockClientExecute(conn *Conn, sql string, args ...interface{}) *gomonkey.Patches

func MockClientNewConn added in v0.3.18

func MockClientNewConn() *gomonkey.Patches

func Release added in v0.2.1

func Release(num int) error

Release releases given number of connections of global pool, each connection will disconnect with database

func ReplaceGlobalPool added in v0.2.1

func ReplaceGlobalPool(pool *Pool) error

ReplaceGlobalPool replaces given pool as global pool

func Supply added in v0.2.1

func Supply(num int) error

Supply creates given number of connections and add them to free connection channel of global pool

Types

type Config added in v0.2.0

type Config struct {
	Addr   string
	DBName string
	DBUser string
	DBPass string
}

func NewConfig added in v0.3.0

func NewConfig(addr string, dbName string, dbUser string, dbPass string) Config

NewConfig returns a new Config

func (Config) GetAddr added in v0.3.17

func (c Config) GetAddr() string

func (Config) GetDBName added in v0.3.17

func (c Config) GetDBName() string

func (Config) GetDBPass added in v0.3.17

func (c Config) GetDBPass() string

func (Config) GetDBUser added in v0.3.17

func (c Config) GetDBUser() string

type Conn

type Conn struct {
	Config
	*client.Conn
}

func NewConn added in v0.3.0

func NewConn(addr string, dbName string, dbUser string, dbPass string) (*Conn, error)

NewConn returns connection to mysql database, be aware that addr is host:port style, default charset is utf8mb4

func (*Conn) CheckInstanceStatus added in v0.2.0

func (conn *Conn) CheckInstanceStatus() bool

CheckInstanceStatus checks mysql instance status

func (*Conn) Execute added in v0.2.23

func (conn *Conn) Execute(command string, args ...interface{}) (*Result, error)

Execute executes given sql and placeholders and returns a result

func (*Conn) ExecuteContext added in v0.3.0

func (conn *Conn) ExecuteContext(ctx context.Context, command string, args ...interface{}) (*Result, error)

ExecuteContext executes given sql and placeholders with context and returns a result

func (*Conn) GetReplicationRole

func (conn *Conn) GetReplicationRole() (role ReplicationRole, err error)

GetReplicationRole returns replication role

func (*Conn) GetReplicationSlaveList

func (conn *Conn) GetReplicationSlaveList() (slaveList []string, err error)

GetReplicationSlaveList returns slave list of this server

func (*Conn) GetReplicationSlavesStatus

func (conn *Conn) GetReplicationSlavesStatus() (result *Result, err error)

GetReplicationSlavesStatus returns replication slave status, like sql: "show slave status;"

func (*Conn) GetVersion added in v0.2.32

func (conn *Conn) GetVersion() (Version, error)

GetVersion returns mysql version

func (*Conn) IsMGR added in v0.3.17

func (conn *Conn) IsMGR() (bool, error)

IsMGR checks if this server is a member of MGR cluster

func (*Conn) IsMaster added in v0.3.17

func (conn *Conn) IsMaster() (bool, error)

IsMaster checks if this server is master

func (*Conn) IsReadOnly added in v0.3.17

func (conn *Conn) IsReadOnly() (bool, error)

IsReadOnly checks if this server is read only

func (*Conn) IsReplicationSlave added in v0.3.17

func (conn *Conn) IsReplicationSlave() (bool, error)

IsReplicationSlave checks if this server is slave

func (*Conn) IsSuperReadOnly added in v0.3.17

func (conn *Conn) IsSuperReadOnly() (bool, error)

IsSuperReadOnly checks if this server is super read only

func (*Conn) Prepare added in v0.3.0

func (conn *Conn) Prepare(command string) (*Statement, error)

Prepare prepares a statement and returns a *Statement

func (*Conn) PrepareContext added in v0.3.0

func (conn *Conn) PrepareContext(ctx context.Context, command string) (*Statement, error)

PrepareContext prepares a statement with context and returns a *Statement

func (*Conn) SetGlobalVariable added in v0.3.17

func (conn *Conn) SetGlobalVariable(variable, value string) error

SetGlobalVariable sets global variable

func (*Conn) SetGlobalVariables added in v0.3.17

func (conn *Conn) SetGlobalVariables(variables map[string]string) error

SetGlobalVariables sets global variables

func (*Conn) SetReadOnly added in v0.3.17

func (conn *Conn) SetReadOnly(readOnly bool) error

SetReadOnly sets read only

func (*Conn) SetSuperReadOnly added in v0.3.17

func (conn *Conn) SetSuperReadOnly(superReadOnly bool) error

SetSuperReadOnly sets super read only

func (*Conn) ShowGlobalVariable added in v0.3.17

func (conn *Conn) ShowGlobalVariable(variable string) (string, error)

ShowGlobalVariable returns the value of the given variable

type Pool added in v0.2.0

type Pool struct {
	sync.Mutex
	PoolConfig
	// contains filtered or unexported fields
}

func NewPool added in v0.3.0

func NewPool(addr, dbName, dbUser, dbPass string,
	maxConnections, initConnections, maxIdleConnections, maxIdleTime, maxWaitTime, maxRetryCount, keepAliveInterval int) (*Pool, error)

NewPool returns a new *Pool

func NewPoolWithConfig added in v0.3.0

func NewPoolWithConfig(config Config, maxConnections, initConnections,
	maxIdleConnections, maxIdleTime, maxWaitTime, maxRetryCount, keepAliveInterval int) (*Pool, error)

NewPoolWithConfig returns a new *Pool with a Config object

func NewPoolWithDefault added in v0.3.0

func NewPoolWithDefault(addr, dbName, dbUser, dbPass string) (*Pool, error)

NewPoolWithDefault returns a new *Pool with default configuration

func NewPoolWithPoolConfig added in v0.3.0

func NewPoolWithPoolConfig(config PoolConfig) (*Pool, error)

NewPoolWithPoolConfig returns a new *Pool with a PoolConfig object

func (*Pool) Close added in v0.2.0

func (p *Pool) Close() error

Close releases each connection in the pool

func (*Pool) Get added in v0.2.0

func (p *Pool) Get() (middleware.PoolConn, error)

Get is an exported alias of get() function with routine safe

func (*Pool) IsClosed added in v0.2.0

func (p *Pool) IsClosed() bool

IsClosed returns if pool had been closed

func (*Pool) Release added in v0.2.0

func (p *Pool) Release(num int) error

Release is an exported alias of release() function

func (*Pool) Supply added in v0.2.0

func (p *Pool) Supply(num int) error

Supply is an exported alias of supply() function with routine safe

func (*Pool) Transaction added in v0.2.22

func (p *Pool) Transaction() (middleware.Transaction, error)

Transaction simply returns *PoolConn, because it had implemented Transaction interface

func (*Pool) UsedConnections added in v0.2.1

func (p *Pool) UsedConnections() int

UsedConnections returns used connection number

type PoolConfig added in v0.2.0

type PoolConfig struct {
	Config
	MaxConnections     int
	InitConnections    int
	MaxIdleConnections int
	MaxIdleTime        int
	MaxWaitTime        int
	MaxRetryCount      int
	KeepAliveInterval  int
}

func NewPoolConfig added in v0.2.0

func NewPoolConfig(addr, dbName, dbUser, dbPass string,
	maxConnections, initConnections, maxIdleConnections, maxIdleTime, maxWaitTime, maxRetryCount, keepAliveInterval int) PoolConfig

NewPoolConfig returns a new PoolConfig

func NewPoolConfigWithConfig added in v0.2.0

func NewPoolConfigWithConfig(config Config, maxConnections, initConnections, maxIdleConnections,
	maxIdleTime, maxWaitTime, maxRetryCount, keepAliveInterval int) PoolConfig

NewPoolConfigWithConfig returns a new PoolConfig

func (*PoolConfig) Validate added in v0.2.0

func (cfg *PoolConfig) Validate() error

Validate validates pool config

type PoolConn added in v0.2.0

type PoolConn struct {
	*Conn
	Pool *Pool
}

func Get added in v0.2.1

func Get() (*PoolConn, error)

Get gets a connection from pool and validate it, if there is no valid connection in the pool, it will create a new connection

func NewPoolConn added in v0.2.0

func NewPoolConn(addr, dbName, dbUser, dbPass string) (*PoolConn, error)

NewPoolConn returns a new *PoolConn

func NewPoolConnWithPool added in v0.2.0

func NewPoolConnWithPool(pool *Pool, addr, dbName, dbUser, dbPass string) (*PoolConn, error)

NewPoolConnWithPool returns a new *PoolConn

func (*PoolConn) Close added in v0.2.0

func (pc *PoolConn) Close() error

Close returns connection back to the pool

func (*PoolConn) Disconnect added in v0.2.24

func (pc *PoolConn) Disconnect() error

Disconnect disconnects from mysql, normally when using connection pool, there is no need to disconnect manually, consider to use Close() instead.

func (*PoolConn) Execute added in v0.2.5

func (pc *PoolConn) Execute(command string, args ...interface{}) (middleware.Result, error)

Execute executes given sql and placeholders on the mysql server

func (*PoolConn) ExecuteContext added in v0.3.0

func (pc *PoolConn) ExecuteContext(ctx context.Context, command string, args ...interface{}) (middleware.Result, error)

ExecuteContext executes given sql and placeholders on the mysql server note that ExecuteContext does not use context, it's only for implementing some interfaces

func (*PoolConn) IsValid added in v0.2.0

func (pc *PoolConn) IsValid() bool

IsValid validates if connection is valid

func (*PoolConn) Prepare added in v0.3.0

func (pc *PoolConn) Prepare(command string) (middleware.Statement, error)

Prepare prepares a statement and returns a *Statement

func (*PoolConn) PrepareContext added in v0.3.0

func (pc *PoolConn) PrepareContext(ctx context.Context, command string) (middleware.Statement, error)

PrepareContext prepares a statement with context and returns a *Statement, note that PrepareContext does not use context, it's only for implementing some interfaces

type ReplicationRole added in v0.2.32

type ReplicationRole string

type Result added in v0.2.8

type Result struct {
	Raw *mysql.Result
	*result.Rows
	result.Map
}

func NewResult added in v0.2.8

func NewResult(r *mysql.Result) *Result

NewResult returns *Result

func (*Result) GetRaw added in v0.3.3

func (r *Result) GetRaw() interface{}

func (*Result) LastInsertID added in v0.2.23

func (r *Result) LastInsertID() (int, error)

LastInsertID returns the database's auto-generated ID after, for example, an INSERT into a table with primary key.

func (*Result) RowsAffected added in v0.2.23

func (r *Result) RowsAffected() (int, error)

RowsAffected returns the number of rows affected by the query.

type Statement added in v0.3.0

type Statement struct {
	*client.Stmt
}

func NewStatement added in v0.3.0

func NewStatement(stmt *client.Stmt) *Statement

NewStatement returns a new *Statement with given *client.Stmt

func (*Statement) Close added in v0.3.17

func (stmt *Statement) Close() error

Close closes the statement

func (*Statement) Commit added in v0.3.17

func (stmt *Statement) Commit() error

Commit commits the transaction

func (*Statement) Execute added in v0.3.0

func (stmt *Statement) Execute(args ...interface{}) (middleware.Result, error)

Execute executes given sql and placeholders and returns a result

func (*Statement) ExecuteContext added in v0.3.0

func (stmt *Statement) ExecuteContext(ctx context.Context, args ...interface{}) (middleware.Result, error)

ExecuteContext executes given sql and placeholders with context and returns a result

func (*Statement) Rollback added in v0.3.17

func (stmt *Statement) Rollback() error

Rollback rollbacks the transaction

type Version added in v0.2.32

type Version interface {
	// GetMajor returns major
	GetMajor() int
	// GetMinor returns minor
	GetMinor() int
	// GetRelease returns release
	GetRelease() int
	// Compare compares two versions
	Compare(other Version) int
	// LessThan returns true if current version is less than other version
	LessThan(other Version) bool
	// Equal returns true if current version is equal to other version
	Equal(other Version) bool
	// GreaterThan returns true if current version is greater than other version
	GreaterThan(other Version) bool
	// IsMySQL8 returns true if current version is greater than or equal to 8.0.0
	IsMySQL8() bool
	// String returns string format of version
	String() string
}

func NewVersion added in v0.2.32

func NewVersion(major, minor, release int) Version

NewVersion returns a new instance of Version

func Parse added in v0.2.32

func Parse(v string) (Version, error)

Parse parses given version string and returns a new instance of Version

Jump to

Keyboard shortcuts

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