Documentation ¶
Overview ¶
Adapter 实现读写分离的gorm适配器 通过实现gorm里的SQLCommon接口实现读写分离的适配 TODO 读库可能存在多个,可以通过基于权重的副在均衡算法完成连接工作的分配
Index ¶
- type Model
- type ReadWriteAdapter
- func (adapter ReadWriteAdapter) Begin() (*sql.Tx, error)
- func (adapter ReadWriteAdapter) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
- func (adapter ReadWriteAdapter) Exec(query string, args ...interface{}) (sql.Result, error)
- func (adapter ReadWriteAdapter) Master() *sql.DB
- func (adapter ReadWriteAdapter) Ping() error
- func (adapter ReadWriteAdapter) Prepare(query string) (*sql.Stmt, error)
- func (adapter ReadWriteAdapter) Query(query string, args ...interface{}) (*sql.Rows, error)
- func (adapter ReadWriteAdapter) QueryRow(query string, args ...interface{}) *sql.Row
- func (adapter ReadWriteAdapter) SetConnMaxLifetime(d time.Duration)
- func (adapter ReadWriteAdapter) SetMaxIdleConns(n int)
- func (adapter ReadWriteAdapter) SetMaxOpenConns(n int)
- func (adapter ReadWriteAdapter) Slave() *sql.DB
- type Timestamp
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Model ¶
type Model struct { Id int `gorm:"primary_key;AUTO_INCREMENT;column:id" json:"id"` CreatedAt int `gorm:"column:created_at" json:"created_at"` UpdatedAt int `gorm:"column:updated_at" json:"updated_at"` }
基础模型
type ReadWriteAdapter ¶ added in v0.9.0
type ReadWriteAdapter struct {
// contains filtered or unexported fields
}
ReadWriteAdapter 读写分离适配器
func NewReadWriteAdapter ¶ added in v0.9.0
func NewReadWriteAdapter(dialectType string, dataSourceNames []string) (*ReadWriteAdapter, error)
NewReadWriteAdapter 通过多个dsn打开多个实例的连接 Open concurrently opens each underlying physical db. dataSourceNames must be a semi-comma separated list of DSNs with the first one being used as the master and the rest as slaves.
func (ReadWriteAdapter) Begin ¶ added in v0.9.0
func (adapter ReadWriteAdapter) Begin() (*sql.Tx, error)
Begin starts a transaction on the master. The isolation level is dependent on the driver.
func (ReadWriteAdapter) BeginTx ¶ added in v0.9.0
BeginTx starts a transaction with the provided context on the master.
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 (ReadWriteAdapter) Exec ¶ added in v0.9.0
func (adapter ReadWriteAdapter) Exec(query string, args ...interface{}) (sql.Result, error)
Exec
func (ReadWriteAdapter) Master ¶ added in v0.9.0
func (adapter ReadWriteAdapter) Master() *sql.DB
Master 获取master连接
func (ReadWriteAdapter) Ping ¶ added in v0.9.0
func (adapter ReadWriteAdapter) Ping() error
Ping verifies if a connection to each physical database is still alive, establishing a connection if necessary.
func (ReadWriteAdapter) Prepare ¶ added in v0.9.0
func (adapter ReadWriteAdapter) Prepare(query string) (*sql.Stmt, error)
Prepare
func (ReadWriteAdapter) Query ¶ added in v0.9.0
func (adapter ReadWriteAdapter) Query(query string, args ...interface{}) (*sql.Rows, error)
Query
func (ReadWriteAdapter) QueryRow ¶ added in v0.9.0
func (adapter ReadWriteAdapter) QueryRow(query string, args ...interface{}) *sql.Row
QueryRow
func (ReadWriteAdapter) SetConnMaxLifetime ¶ added in v0.9.0
func (adapter ReadWriteAdapter) SetConnMaxLifetime(d time.Duration)
SetConnMaxLifetime sets the maximum amount of time a connection may be reused. Expired connections may be closed lazily before reuse. If d <= 0, connections are reused forever.
func (ReadWriteAdapter) SetMaxIdleConns ¶ added in v0.9.0
func (adapter ReadWriteAdapter) SetMaxIdleConns(n int)
SetMaxIdleConns sets the maximum number of connections in the idle connection pool for each underlying physical db. If MaxOpenConns is greater than 0 but less than the new MaxIdleConns then the new MaxIdleConns will be reduced to match the MaxOpenConns limit If n <= 0, no idle connections are retained.
func (ReadWriteAdapter) SetMaxOpenConns ¶ added in v0.9.0
func (adapter ReadWriteAdapter) SetMaxOpenConns(n int)
SetMaxOpenConns sets the maximum number of open connections to each physical database. If MaxIdleConns is greater than 0 and the new MaxOpenConns is less than MaxIdleConns, then MaxIdleConns will be reduced to match the new MaxOpenConns limit. If n <= 0, then there is no limit on the number of open connections. The default is 0 (unlimited).
func (ReadWriteAdapter) Slave ¶ added in v0.9.0
func (adapter ReadWriteAdapter) Slave() *sql.DB
Slave 获取slave链接