This commit is contained in:
liumingmin 2018-09-17 14:48:34 +00:00 committed by GitHub
commit aef1944456
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 3 deletions

7
engine.go Normal file → Executable file
View File

@ -313,6 +313,13 @@ func (engine *Engine) NewSession() *Session {
return session
}
func (engine *Engine) NewMasterSession() *Session {
session := &Session{engine: engine}
session.Init()
session.isMasterOwn=true
return session
}
// Close the engine
func (engine *Engine) Close() error {
return engine.db.Close()

2
session.go Normal file → Executable file
View File

@ -27,6 +27,7 @@ type Session struct {
isAutoCommit bool
isCommitedOrRollbacked bool
isAutoClose bool
isMasterOwn bool
// Automatically reset the statement after operations that execute a SQL
// query such as Count(), Find(), Get(), ...
@ -69,6 +70,7 @@ func (session *Session) Init() {
session.isAutoClose = false
session.autoResetStatement = true
session.prepareStmt = false
session.isMasterOwn = false
// !nashtsai! is lazy init better?
session.afterInsertBeans = make(map[interface{}]*[]func(interface{}), 0)

12
session_raw.go Normal file → Executable file
View File

@ -49,12 +49,18 @@ func (session *Session) queryRows(sqlStr string, args ...interface{}) (*core.Row
if session.isAutoCommit {
var db *core.DB
if session.engine.engineGroup != nil {
db = session.engine.engineGroup.Slave().DB()
} else {
if session.isMasterOwn{
db = session.DB()
}else{
if session.engine.engineGroup != nil {
db = session.engine.engineGroup.Slave().DB()
} else {
db = session.DB()
}
}
if session.prepareStmt {
// don't clear stmt since session will cache them
stmt, err := session.doPrepare(db, sqlStr)