Documentation ¶
Index ¶
- Variables
- func NewClientTLSConfig(caPem, certPem, keyPem []byte, insecureSkipVerify bool, serverName string) *tls.Config
- type Conn
- func Connect(addr string, user string, password string, dbName string, ...) (*Conn, error)
- func ConnectWithContext(ctx context.Context, addr string, user string, password string, dbName string, ...) (*Conn, error)
- func ConnectWithDialer(ctx context.Context, network string, addr string, user string, password string, ...) (*Conn, error)
- func (c *Conn) Begin() error
- func (c *Conn) CapabilityString() string
- func (c *Conn) Close() error
- func (c *Conn) Commit() error
- func (c *Conn) CompareServerVersion(v string) (int, error)
- func (c *Conn) Execute(command string, args ...interface{}) (*Result, error)
- func (c *Conn) ExecuteMultiple(query string, perResultCallback ExecPerResultCallback) (*Result, error)
- func (c *Conn) ExecuteSelectStreaming(command string, result *Result, perRowCallback SelectPerRowCallback, ...) error
- func (c *Conn) FieldList(table string, wildcard string) ([]*Field, error)
- func (c *Conn) GetCharset() string
- func (c *Conn) GetCollation() string
- func (c *Conn) GetConnectionID() uint32
- func (c *Conn) GetDB() string
- func (c *Conn) GetServerVersion() string
- func (c *Conn) HandleErrorPacket(data []byte) error
- func (c *Conn) HandleOKPacket(data []byte) *Result
- func (c *Conn) IsAutoCommit() bool
- func (c *Conn) IsInTransaction() bool
- func (c *Conn) Ping() error
- func (c *Conn) Prepare(query string) (*Stmt, error)
- func (c *Conn) Quit() error
- func (c *Conn) ReadOKPacket() (*Result, error)
- func (c *Conn) Rollback() error
- func (c *Conn) SetAttributes(attributes map[string]string)
- func (c *Conn) SetAutoCommit() error
- func (c *Conn) SetCapability(cap uint32)
- func (c *Conn) SetCharset(charset string) error
- func (c *Conn) SetCollation(collation string) error
- func (c *Conn) SetTLSConfig(config *tls.Config)
- func (c *Conn) StatusString() string
- func (c *Conn) UnsetCapability(cap uint32)
- func (c *Conn) UseDB(dbName string) error
- func (c *Conn) UseSSL(insecureSkipVerify bool)
- type Connection
- type ConnectionStats
- type Dialer
- type ExecPerResultCallback
- type LogFunc
- type Pool
- type PoolOption
- type SelectPerResultCallback
- type SelectPerRowCallback
- type Stmt
- type Timestamp
Constants ¶
This section is empty.
Variables ¶
var ( // MaxIdleTimeoutWithoutPing - If the connection has been idle for more than this time, // then ping will be performed before use to check if it alive MaxIdleTimeoutWithoutPing = 10 * time.Second // DefaultIdleTimeout - If the connection has been idle for more than this time, // we can close it (but we should remember about Pool.minAlive) DefaultIdleTimeout = 30 * time.Second // MaxNewConnectionAtOnce - If we need to create new connections, // then we will create no more than this number of connections at a time. // This restriction will be ignored on pool initialization. MaxNewConnectionAtOnce = 5 )
Functions ¶
Types ¶
type Conn ¶
func Connect ¶
func Connect(addr string, user string, password string, dbName string, options ...func(*Conn)) (*Conn, error)
Connect to a MySQL server, addr can be ip:port, or a unix socket domain like /var/sock. Accepts a series of configuration functions as a variadic argument.
func ConnectWithContext ¶ added in v1.8.0
func ConnectWithContext(ctx context.Context, addr string, user string, password string, dbName string, options ...func(*Conn)) (*Conn, error)
ConnectWithContext to a MySQL addr using the provided context.
func ConnectWithDialer ¶ added in v1.4.0
func ConnectWithDialer(ctx context.Context, network string, addr string, user string, password string, dbName string, dialer Dialer, options ...func(*Conn)) (*Conn, error)
ConnectWithDialer to a MySQL server using the given Dialer.
func (*Conn) CapabilityString ¶ added in v1.8.0
func (*Conn) CompareServerVersion ¶ added in v1.8.0
func (*Conn) ExecuteMultiple ¶ added in v1.5.0
func (c *Conn) ExecuteMultiple(query string, perResultCallback ExecPerResultCallback) (*Result, error)
ExecuteMultiple will call perResultCallback for every result of the multiple queries that are executed.
When ExecuteMultiple is used, the connection should have the SERVER_MORE_RESULTS_EXISTS flag set to signal the server multiple queries are executed. Handling the responses is up to the implementation of perResultCallback.
Example:
queries := "SELECT 1; SELECT NOW();" conn.ExecuteMultiple(queries, func(result *mysql.Result, err error) { // Use the result as you want })
func (*Conn) ExecuteSelectStreaming ¶
func (c *Conn) ExecuteSelectStreaming(command string, result *Result, perRowCallback SelectPerRowCallback, perResultCallback SelectPerResultCallback) error
ExecuteSelectStreaming will call perRowCallback for every row in resultset WITHOUT saving any row data to Result.{Values/RawPkg/RowDatas} fields. When given, perResultCallback will be called once per result
ExecuteSelectStreaming should be used only for SELECT queries with a large response resultset for memory preserving.
Example:
var result mysql.Result conn.ExecuteSelectStreaming(`SELECT ... LIMIT 100500`, &result, func(row []mysql.FieldValue) error { // Use the row as you want. // You must not save FieldValue.AsString() value after this callback is done. Copy it if you need. return nil }, nil)
func (*Conn) GetCharset ¶
func (*Conn) GetCollation ¶ added in v1.8.0
func (*Conn) GetConnectionID ¶
func (*Conn) GetServerVersion ¶ added in v1.8.0
func (*Conn) HandleErrorPacket ¶
func (*Conn) HandleOKPacket ¶
func (*Conn) IsAutoCommit ¶
func (*Conn) IsInTransaction ¶
func (*Conn) ReadOKPacket ¶
func (*Conn) SetAttributes ¶ added in v1.5.0
func (*Conn) SetAutoCommit ¶
func (*Conn) SetCapability ¶ added in v1.4.0
SetCapability enables the use of a specific capability
func (*Conn) SetCharset ¶
func (*Conn) SetCollation ¶ added in v1.8.0
func (*Conn) SetTLSConfig ¶
SetTLSConfig: use user-specified TLS config pass to options when connect
func (*Conn) StatusString ¶ added in v1.8.0
func (*Conn) UnsetCapability ¶ added in v1.4.0
UnsetCapability disables the use of a specific capability
type Connection ¶ added in v1.3.0
type Connection struct {
// contains filtered or unexported fields
}
type ConnectionStats ¶ added in v1.3.0
type Dialer ¶ added in v1.4.0
Dialer connects to the address on the named network using the provided context.
type ExecPerResultCallback ¶ added in v1.5.0
type ExecPerResultCallback func(result *Result, err error)
This function will be called once per result from ExecuteMultiple
type Pool ¶ added in v1.3.0
type Pool struct {
// contains filtered or unexported fields
}
func NewPool
deprecated
added in
v1.3.0
func NewPool( logFunc LogFunc, minAlive int, maxAlive int, maxIdle int, addr string, user string, password string, dbName string, options ...func(conn *Conn), ) *Pool
NewPool initializes new connection pool and uses params: addr, user, password, dbName and options. minAlive specifies the minimum number of open connections that the pool will try to maintain. maxAlive specifies the maximum number of open connections (for internal reasons, may be greater by 1 inside newConnectionProducer). maxIdle specifies the maximum number of idle connections (see DefaultIdleTimeout).
Deprecated: use NewPoolWithOptions
func NewPoolWithOptions ¶ added in v1.8.0
func NewPoolWithOptions( addr string, user string, password string, dbName string, options ...PoolOption, ) (*Pool, error)
NewPoolWithOptions initializes new connection pool and uses params: addr, user, password, dbName and options.
func (*Pool) Close ¶ added in v1.8.0
func (pool *Pool) Close()
Close only shutdown idle connections. we couldn't control the connection which not in the pool. So before call Close, Call PutConn to put all connections that in use back to connection pool first.
func (*Pool) GetStats ¶ added in v1.3.0
func (pool *Pool) GetStats(stats *ConnectionStats)
type PoolOption ¶ added in v1.8.0
type PoolOption func(o *poolOptions)
func WithConnOptions ¶ added in v1.8.0
func WithConnOptions(options ...func(conn *Conn)) PoolOption
func WithLogFunc ¶ added in v1.8.0
func WithLogFunc(f LogFunc) PoolOption
func WithNewPoolPingTimeout ¶ added in v1.8.0
func WithNewPoolPingTimeout(timeout time.Duration) PoolOption
WithNewPoolPingTimeout enables connect & ping to DB during the pool initialization
func WithPoolLimits ¶ added in v1.8.0
func WithPoolLimits(minAlive, maxAlive, maxIdle int) PoolOption
WithPoolLimits sets pool limits:
- minAlive specifies the minimum number of open connections that the pool will try to maintain.
- maxAlive specifies the maximum number of open connections (for internal reasons, may be greater by 1 inside newConnectionProducer).
- maxIdle specifies the maximum number of idle connections (see DefaultIdleTimeout).
type SelectPerResultCallback ¶ added in v1.4.0
type SelectPerResultCallback func(result *Result) error
This function will be called once per result from ExecuteSelectStreaming
type SelectPerRowCallback ¶
type SelectPerRowCallback func(row []FieldValue) error
This function will be called for every row in resultset from ExecuteSelectStreaming.
type Stmt ¶
type Stmt struct {
// contains filtered or unexported fields
}
func (*Stmt) ExecuteSelectStreaming ¶ added in v1.4.0
func (s *Stmt) ExecuteSelectStreaming(result *Result, perRowCb SelectPerRowCallback, perResCb SelectPerResultCallback, args ...interface{}) error