Fix master/slave bug (#2004)

Reviewed-on: https://gitea.com/xorm/xorm/pulls/2004
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-committed-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
Lunny Xiao 2021-07-22 11:07:53 +08:00
parent 47cfe0347f
commit 6f79e06376
2 changed files with 30 additions and 1 deletions

View File

@ -237,3 +237,31 @@ func (eg *EngineGroup) Slave() *Engine {
func (eg *EngineGroup) Slaves() []*Engine { func (eg *EngineGroup) Slaves() []*Engine {
return eg.slaves return eg.slaves
} }
// Query execcute a select SQL and return the result
func (eg *EngineGroup) Query(sqlOrArgs ...interface{}) (resultsSlice []map[string][]byte, err error) {
sess := eg.NewSession()
sess.isAutoClose = true
return sess.Query(sqlOrArgs...)
}
// QueryInterface execcute a select SQL and return the result
func (eg *EngineGroup) QueryInterface(sqlOrArgs ...interface{}) ([]map[string]interface{}, error) {
sess := eg.NewSession()
sess.isAutoClose = true
return sess.QueryInterface(sqlOrArgs...)
}
// QueryString execcute a select SQL and return the result
func (eg *EngineGroup) QueryString(sqlOrArgs ...interface{}) ([]map[string]string, error) {
sess := eg.NewSession()
sess.isAutoClose = true
return sess.QueryString(sqlOrArgs...)
}
// Rows execcute a select SQL and return the result
func (eg *EngineGroup) Rows(bean interface{}) (*Rows, error) {
sess := eg.NewSession()
sess.isAutoClose = true
return sess.Rows(bean)
}

View File

@ -6,6 +6,7 @@ package xorm
import ( import (
"database/sql" "database/sql"
"strings"
"xorm.io/xorm/core" "xorm.io/xorm/core"
) )
@ -32,7 +33,7 @@ func (session *Session) queryRows(sqlStr string, args ...interface{}) (*core.Row
if session.isAutoCommit { if session.isAutoCommit {
var db *core.DB var db *core.DB
if session.sessionType == groupSession { if session.sessionType == groupSession && strings.EqualFold(sqlStr[:6], "select") {
db = session.engine.engineGroup.Slave().DB() db = session.engine.engineGroup.Slave().DB()
} else { } else {
db = session.DB() db = session.DB()