Documentation ¶
Index ¶
- Variables
- type Config
- func (c *Config) SetConnIdleTimeMs(connIdleTimeMs int)
- func (c *Config) SetConnTimeoutMs(connTimeoutMs int)
- func (c *Config) SetConnWaitTimeMs(connWaitTimeMs int)
- func (c *Config) SetDBName(dbName string)
- func (c *Config) SetMaster(master string)
- func (c *Config) SetMaxIdleConnCount(maxIdleConnCount int)
- func (c *Config) SetMaxOpenConnCount(maxOpenConnCount int)
- func (c *Config) SetPassword(password string)
- func (c *Config) SetPort(port int)
- func (c *Config) SetReadTimeoutMs(readTimeoutMs int)
- func (c *Config) SetSlaves(slaves []string)
- func (c *Config) SetUserName(userName string)
- func (c *Config) SetWriteTimeoutMs(writeTimeoutMs int)
- type Mysql
- func (m *Mysql) Begin() (*sql.Tx, error)
- func (m *Mysql) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
- func (m *Mysql) Close() error
- func (m *Mysql) Conn(ctx context.Context) (*sql.Conn, error)
- func (m *Mysql) Exec(query string, args ...interface{}) (sql.Result, error)
- func (m *Mysql) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (m *Mysql) Master() *sql.DB
- func (m *Mysql) Query(query string, args ...interface{}) (*sql.Rows, error)
- func (m *Mysql) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (m *Mysql) QueryRow(query string, args ...interface{}) *sql.Row
- func (m *Mysql) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidConfig invalid mysql config ErrInvalidConfig = errors.New("mysql config invalid") )
Error define
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config mysql config master = "127.0.0.1" slaves = ["127.0.0.1", "127.0.0.2"] port = 3306 username = "root" password = "root" dbname = "test" max_open_conn_count = 100 (设置最大打开的连接数,默认值为0表示不限制) max_idle_conn_count = 50 (设置连接池数量) conn_wait_time_ms = 200 conn_idle_time_ms = 21600000 conn_timeout_ms = 5000 (dsn parameter) read_timeout_ms = 3000 (dsn parameter) write_timeout_ms = 3000 (dsn parameter)
func (*Config) SetConnIdleTimeMs ¶
SetConnIdleTimeMs set max idle time ms
func (*Config) SetConnTimeoutMs ¶
SetConnTimeoutMs set connection timeout ms
func (*Config) SetConnWaitTimeMs ¶
SetConnWaitTimeMs set max wait time ms
func (*Config) SetMaxIdleConnCount ¶
SetMaxIdleConnCount set mysql max idle connections 设置连接池数量,通常设置小于最大的连接数
func (*Config) SetMaxOpenConnCount ¶
SetMaxOpenConnCount set mysql max open connections 设置最大的连接数, 可以避免并发太高导致连接mysql出现too many connections的错误。
func (*Config) SetPassword ¶
SetPassword set mysql password
func (*Config) SetReadTimeoutMs ¶
SetReadTimeoutMs set read timeout ms
func (*Config) SetUserName ¶
SetUserName set mysql user name
func (*Config) SetWriteTimeoutMs ¶
SetWriteTimeoutMs set write timeout ms
type Mysql ¶
type Mysql struct {
// contains filtered or unexported fields
}
Mysql define
func (*Mysql) Begin ¶
Begin starts a transaction. The default isolation level is dependent on the driver.
func (*Mysql) BeginTx ¶
BeginTx starts a transaction.
The provided context is used until the transaction is committed or rolled back. If the context is canceled, the sql package will roll back the transaction. Tx.Commit will return an error if the context provided to BeginTx is canceled.
The provided TxOptions is optional and may be nil if defaults should be used. If a non-default isolation level is used that the driver doesn't support, an error will be returned.
func (*Mysql) Close ¶
Close closes the database, releasing any open resources. It is rare to Close a DB, as the DB handle is meant to be long-lived and shared between many goroutines.
func (*Mysql) Conn ¶
Conn returns a single connection by either opening a new connection or returning an existing connection from the connection pool. Conn will block until either a connection is returned or ctx is canceled. Queries run on the same Conn will be run in the same database session.
Every Conn must be returned to the database pool after use by calling Conn.Close.
func (*Mysql) Exec ¶
Exec executes a query without returning any rows. The args are for any placeholder parameters in the query.
func (*Mysql) ExecContext ¶
func (m *Mysql) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
ExecContext executes a query without returning any rows. The args are for any placeholder parameters in the query.
func (*Mysql) Query ¶
Query executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.
func (*Mysql) QueryContext ¶
func (m *Mysql) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
QueryContext executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.
func (*Mysql) QueryRow ¶
QueryRow executes a query that is expected to return at most one row. QueryRow always returns a non-nil value. Errors are deferred until Row's Scan method is called. If the query selects no rows, the *Row's Scan will return ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.
func (*Mysql) QueryRowContext ¶
QueryRowContext executes a query that is expected to return at most one row. QueryRowContext always returns a non-nil value. Errors are deferred until Row's Scan method is called. If the query selects no rows, the *Row's Scan will return ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.