Documentation ¶
Index ¶
- Constants
- func WithIndex(ctx context.Context, idx int64) context.Context
- type Config
- type ConfigInstance
- type Factory
- type MySQL
- func (mysql *MySQL) BeginTx(opts *sql.TxOptions) (tx *Tx, err error)
- func (mysql *MySQL) Exec(query string, args ...interface{}) (result Result, err error)
- func (mysql *MySQL) Ping() (err error)
- func (mysql *MySQL) Prepare(query string) (stmt *Stmt, err error)
- func (mysql *MySQL) Query(query string, args ...interface{}) (rows *Rows, err error)
- func (mysql *MySQL) QueryRow(query string, args ...interface{}) (row *Row, err error)
- func (mysql *MySQL) Stats() sql.DBStats
- func (mysql *MySQL) UseMaster() *MySQL
- type Result
- type Row
- type Rows
- type Stmt
- type Tx
- func (tx *Tx) Commit() (err error)
- func (tx *Tx) Exec(query string, args ...interface{}) (result Result, err error)
- func (tx *Tx) Prepare(query string) (stmt *Stmt, err error)
- func (tx *Tx) Query(query string, args ...interface{}) (rows *Rows, err error)
- func (tx *Tx) QueryRow(query string, args ...interface{}) (row *Row, err error)
- func (tx *Tx) Rollback() error
- func (tx *Tx) Stmt(stmt *Stmt) (txStmt *Stmt, err error)
Constants ¶
View Source
const ( // DefaultConnMaxLifetime 代表默认的连接的最大保持时间,当前设置为 1h 时间。 DefaultConnMaxLifetime time.Duration = time.Hour // DefaultMaxIdleConns 代表默认的最大空闲连接数,当前设置为 10。 DefaultMaxIdleConns = 10 )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct { DSN string `config:"dsn"` // DSN 是 MySQL 主库的连接字符串。 DSNSlave string `config:"dsn_slave"` // DSNSlave 是从库的 MySQL 连接字符串,所有只读的 Query/QueryRow 都会走这个连接,默认与 DSN 相同。 Mod int64 `config:"mod"` // Mod 是 hash 分桶的余数,比如设置为 10 就会将 hash%10 来计算命中哪一个实例,默认不分桶。 Instances []ConfigInstance `config:"instances"` // Instances 是分桶后的数据库连接配置。 ConnMaxLifetime time.Duration `config:"conn_max_life_time"` // ConnMaxLifetime 设置连接的最大保持时间,默认是 DefaultConnMaxLifetime。 MaxIdleConns int `config:"max_idle_conns"` // MaxIdleConns 设置最多保持多少个空闲连接,默认是 DefaultMaxIdleConns。 MaxOpenConns int `config:"max_open_conns"` // MaxOpenConns 设置最大同时连接数,默认是不限制。 }
Config 代表 MySQL 的配置。
type ConfigInstance ¶
type ConfigInstance struct { DSN string `config:"dsn"` // DSN 是 MySQL 主库的连接字符串。 DSNSlave string `config:"dsn_slave"` // DSNSlave 是从库的 MySQL 连接字符串,所有只读的 Query/QueryRow 都会走这个连接,默认与 DSN 相同。 Buckets []int64 `config:"buckets"` // Buckets 表示这个实例对应的 bucket 号,可以是多个号,比如 [0, 1, 2]。 }
ConfigInstance 代表一组 MySQL 实例的连接字符串。
type Factory ¶
type Factory struct {
// contains filtered or unexported fields
}
Factory 代表一个用于创建 MySQL 连接的工厂。 创建 Factory 之后必须调用 `Factory#Conn` 方法建立连接, 否则后续无法通过 `Factory#New` 方法创建 MySQL 实例。
func NewFactory ¶
NewFactory 实例化一个工厂。 创建工厂后需要调用 `Factory#Conn` 才能真正建立连接,后续才能使用 `Factory#New`。
type MySQL ¶
type MySQL struct {
// contains filtered or unexported fields
}
MySQL 代表一个数据库的链接。
type Rows ¶
type Rows struct {
// contains filtered or unexported fields
}
Rows 代表一个查询结果。
func (*Rows) ColumnTypes ¶
func (rs *Rows) ColumnTypes() ([]*sql.ColumnType, error)
ColumnTypes 返回列类型信息。
func (*Rows) NextResultSet ¶
NextResultSet 查询是否存在下一条记录,但是并不会真的返回下一条结果。 真正 Scan 之前,还得调用 Next 来实际获取这条结果。
type Stmt ¶
type Stmt struct {
// contains filtered or unexported fields
}
Stmt 代表一个准备好的语句,可以绑定参数并执行。
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
Tx 代表一个事务。
Source Files ¶
Click to show internal directories.
Click to hide internal directories.