代码拉取完成,页面将自动刷新
package gmysql
import (
"context"
"sync/atomic"
)
// Mysql主从组
type DBGroup struct {
mCounter uint64
Master []*DBConn
sCounter uint64
Slave []*DBConn
orm *DBOrm
}
func newDBGroup(groupConf *DBGroupConf) (dbGroup *DBGroup, err error) {
masterConf := groupConf.Master
slaveConf := groupConf.Slaves
dbGroup = &DBGroup{
Master: make([]*DBConn, 0),
Slave: make([]*DBConn, 0),
}
dbGroup.orm, _ = newDBOrm(dbGroup)
var dbConn *DBConn
// master连接池
if masterConf != nil && masterConf.Instances != nil {
for i := 0; i < len(masterConf.Instances); i++ {
if dbConn, err = newDBConnection(groupConf, masterConf, &masterConf.Instances[i]); err != nil {
return
}
dbGroup.Master = append(dbGroup.Master, dbConn)
}
}
// slave连接池
if slaveConf != nil && slaveConf.Instances != nil {
for i := 0; i < len(slaveConf.Instances); i++ {
if dbConn, err = newDBConnection(groupConf, slaveConf, &slaveConf.Instances[i]); err != nil {
return
}
dbGroup.Slave = append(dbGroup.Slave, dbConn)
}
}
return
}
// 选择连接
func (dbGroup *DBGroup) chooseConn(forceMaster bool) (conn *DBConn) {
if !forceMaster && len(dbGroup.Slave) != 0 { // 选择slave
counter := atomic.AddUint64(&dbGroup.sCounter, 1)
return dbGroup.Slave[counter%uint64(len(dbGroup.Slave))]
} else if len(dbGroup.Master) != 0 { // 选择master
counter := atomic.AddUint64(&dbGroup.mCounter, 1)
return dbGroup.Master[counter%uint64(len(dbGroup.Master))]
}
return
}
// SQL查询
func (dbGroup *DBGroup) Query(context context.Context, result interface{}, sql string, values ...interface{}) (err error) {
dbConn := dbGroup.chooseConn(false)
if dbConn == nil {
err = ERR_DB_CONN_NOT_FOUND
return
}
err = dbConn.Query(context, result, sql, values...)
return
}
func (dbGroup *DBGroup) QueryWithOptions(context context.Context, dbOptions *DBOptions, result interface{}, sql string, values ...interface{}) (err error) {
if dbOptions == nil {
dbOptions = DEFAULT_DBOPTION
}
dbConn := dbGroup.chooseConn(dbOptions.ForceMaster)
if dbConn == nil {
err = ERR_DB_CONN_NOT_FOUND
return
}
err = dbConn.Query(context, result, sql, values...)
return
}
func (DBGroup *DBGroup) ORM() *DBOrm {
return DBGroup.orm
}
// SQL写入
func (dbGroup *DBGroup) Exec(context context.Context, sql string, values ...interface{}) (result int64, err error) {
dbConn := dbGroup.chooseConn(true)
if dbConn == nil {
err = ERR_DB_CONN_NOT_FOUND
return
}
result, err = dbConn.Exec(context, sql, values...)
return
}
// 开启事务
func (dbGroup *DBGroup) Begin(context context.Context) (tx *DBConn, err error) {
dbConn := dbGroup.chooseConn(true)
if dbConn == nil {
err = ERR_DB_CONN_NOT_FOUND
return
}
tx, err = dbConn.Begin(context)
return
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。