mysql

package
v0.2.16 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2021 License: Apache-2.0 Imports: 13 Imported by: 7

Documentation

Index

Constants

View Source
const (
	DefaultCharSet     = "utf8mb4"
	ReplicationMaster  = "master"
	ReplicationSlave   = "slave"
	ReplicationRelay   = "relay" // it has master and slave roles at the same time
	HostString         = "host"
	PortString         = "port"
	ShowSlaveStatusSQL = "show slave status"
)
View Source
const (
	DefaultMaxConnections     = 20
	DefaultInitConnections    = 5
	DefaultMaxIdleConnections = 10
	DefaultMaxIdleTime        = 1800 // Seconds
	DefaultKeepAliveInterval  = 300  // Seconds
	DefaultKeepAliveChunkSize = 5
	DefaultSleepTime          = 1 // Seconds
)

Variables

This section is empty.

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 InitMySQLGlobalPool added in v0.2.1

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

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

func InitMySQLGlobalPoolWithConfig added in v0.2.1

func InitMySQLGlobalPoolWithConfig(config Config, maxConnections, initConnections, maxIdleConnections, maxIdleTime, keepAliveInterval int) error

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

func InitMySQLGlobalPoolWithDefault added in v0.2.1

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

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

func InitMySQLGlobalPoolWithPoolConfig added in v0.2.1

func InitMySQLGlobalPoolWithPoolConfig(config PoolConfig) error

InitMySQLGlobalPoolWithPoolConfig 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 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 NewMySQLConfig added in v0.2.0

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

NewMySQLConfig returns a new Config

type Conn

type Conn struct {
	Config
	client.Conn
}

func NewMySQLConn

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

NewMySQLConn 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) GetReplicationRole

func (conn *Conn) GetReplicationRole() (role string, 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 *mysql.Result, err error)

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

type Pool added in v0.2.0

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

func NewMySQLPool added in v0.2.0

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

NewMySQLPool returns a new *Pool

func NewMySQLPoolWithConfig added in v0.2.0

func NewMySQLPoolWithConfig(config Config, maxConnections, initConnections, maxIdleConnections, maxIdleTime, keepAliveInterval int) (*Pool, error)

NewMySQLPoolWithConfig returns a new *Pool with a Config object

func NewMySQLPoolWithDefault added in v0.2.0

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

NewMySQLPoolWithDefault returns a new *Pool with default configuration

func NewMySQLPoolWithPoolConfig added in v0.2.0

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

NewMySQLPoolWithPoolConfig 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) 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
	KeepAliveInterval  int
}

func NewPoolConfig added in v0.2.0

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

NewPoolConfig returns a new PoolConfig

func NewPoolConfigWithConfig added in v0.2.0

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

NewPoolConfigWithConfig returns a new PoolConfig

func (*PoolConfig) Validate added in v0.2.0

func (cfg *PoolConfig) Validate() (bool, 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 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(addr, dbName, dbUser, dbPass string, pool *Pool) (*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.0

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) IsValid added in v0.2.0

func (pc *PoolConn) IsValid() bool

IsValid validates if connection is valid

type Result added in v0.2.8

type Result struct {
	*mysql.Result
}

func NewResult added in v0.2.8

func NewResult(r *mysql.Result) *Result

func (*Result) ColumnExists added in v0.2.11

func (r *Result) ColumnExists(name string) bool

ColumnExists check if column exists in the result

func (*Result) ColumnNumber added in v0.2.8

func (r *Result) ColumnNumber() int

ColumnNumber returns how many columns in the result

func (*Result) GetFloat added in v0.2.8

func (r *Result) GetFloat(row, column int) (float64, error)

GetFloat returns float64 type value of given row and column number

func (*Result) GetFloatByName added in v0.2.8

func (r *Result) GetFloatByName(row int, name string) (float64, error)

GetFloatByName returns float64 type value of given row number and column name

func (*Result) GetInt added in v0.2.8

func (r *Result) GetInt(row, column int) (int64, error)

GetInt returns int64 type value of given row and column number

func (*Result) GetIntByName added in v0.2.8

func (r *Result) GetIntByName(row int, name string) (int64, error)

GetIntByName returns int64 type value of given row number and column name

func (*Result) GetString added in v0.2.8

func (r *Result) GetString(row, column int) (string, error)

GetString returns string type value of given row and column number

func (*Result) GetStringByName added in v0.2.8

func (r *Result) GetStringByName(row int, name string) (string, error)

GetStringByName returns string type value of given row number and column name

func (*Result) GetUint added in v0.2.8

func (r *Result) GetUint(row, column int) (uint64, error)

GetUint returns uint64 type value of given row and column number

func (*Result) GetUintByName added in v0.2.8

func (r *Result) GetUintByName(row int, name string) (uint64, error)

GetUintByName returns uint64 type value of given row number and column name

func (*Result) GetValue added in v0.2.8

func (r *Result) GetValue(row, column int) (interface{}, error)

GetValue returns interface{} type value of given row and column number

func (*Result) GetValueByName added in v0.2.8

func (r *Result) GetValueByName(row int, name string) (interface{}, error)

GetValueByName returns interface{} type value of given row number and column name

func (*Result) IsNull added in v0.2.8

func (r *Result) IsNull(row, column int) (bool, error)

IsNull checks if value of given row and column index is nil

func (*Result) IsNullByName added in v0.2.8

func (r *Result) IsNullByName(row int, name string) (bool, error)

IsNullByName checks if value of given row number and column name is nil

func (*Result) MapToStructByRowIndex added in v0.2.15

func (r *Result) MapToStructByRowIndex(in interface{}, row int, tag string) error

MapToStructByRowIndex maps row of given index result to the struct first argument must be a pointer to struct, each column in the row maps to a field of the struct, tag argument is the tag of the field, it represents the column name, if there is no such tag in the field, this field will be ignored, so set tag to each field that need to be mapped, using "middleware" as the tag is recommended.

func (*Result) MapToStructSlice added in v0.2.12

func (r *Result) MapToStructSlice(in interface{}, tag string) error

MapToStructSlice maps each row to a struct of the first argument, first argument must be a slice of pointers to structs, each row in the result maps to a struct in the slice, each column in the row maps to a field of the struct, tag argument is the tag of the field, it represents the column name, if there is no such tag in the field, this field will be ignored, so set tag to each field that need to be mapped, using "middleware" as the tag is recommended.

func (*Result) NameIndex added in v0.2.8

func (r *Result) NameIndex(name string) (int, error)

NameIndex returns index of given column

func (*Result) RowNumber added in v0.2.8

func (r *Result) RowNumber() int

RowNumber returns how many rows in the result

Jump to

Keyboard shortcuts

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