Merge branch 'master' of github.com:go-xorm/xorm

This commit is contained in:
Lunny Xiao 2015-08-06 14:25:02 +08:00
commit 6cc6e18143
2 changed files with 26 additions and 8 deletions

View File

@ -45,7 +45,7 @@ func newRows(session *Session, bean interface{}) (*Rows, error) {
sqlStr = filter.Do(sqlStr, session.Engine.dialect, rows.session.Statement.RefTable) sqlStr = filter.Do(sqlStr, session.Engine.dialect, rows.session.Statement.RefTable)
} }
rows.session.Engine.logSQL(sqlStr, args) rows.session.saveLastSQL(sqlStr, args)
var err error var err error
rows.stmt, err = rows.session.DB().Prepare(sqlStr) rows.stmt, err = rows.session.DB().Prepare(sqlStr)
if err != nil { if err != nil {

View File

@ -46,6 +46,10 @@ type Session struct {
stmtCache map[uint32]*core.Stmt //key: hash.Hash32 of (queryStr, len(queryStr)) stmtCache map[uint32]*core.Stmt //key: hash.Hash32 of (queryStr, len(queryStr))
cascadeDeep int cascadeDeep int
// !evalphobia! stored the last executed query on this session
lastSQL string
lastSQLArgs []interface{}
} }
// Method Init reset the session as the init status. // Method Init reset the session as the init status.
@ -63,6 +67,9 @@ func (session *Session) Init() {
session.afterDeleteBeans = make(map[interface{}]*[]func(interface{}), 0) session.afterDeleteBeans = make(map[interface{}]*[]func(interface{}), 0)
session.beforeClosures = make([]func(interface{}), 0) session.beforeClosures = make([]func(interface{}), 0)
session.afterClosures = make([]func(interface{}), 0) session.afterClosures = make([]func(interface{}), 0)
session.lastSQL = ""
session.lastSQLArgs = []interface{}{}
} }
// Method Close release the connection from pool // Method Close release the connection from pool
@ -325,8 +332,7 @@ func (session *Session) Begin() error {
session.IsAutoCommit = false session.IsAutoCommit = false
session.IsCommitedOrRollbacked = false session.IsCommitedOrRollbacked = false
session.Tx = tx session.Tx = tx
session.saveLastSQL("BEGIN TRANSACTION")
session.Engine.logSQL("BEGIN TRANSACTION")
} }
return nil return nil
} }
@ -334,7 +340,7 @@ func (session *Session) Begin() error {
// When using transaction, you can rollback if any error // When using transaction, you can rollback if any error
func (session *Session) Rollback() error { func (session *Session) Rollback() error {
if !session.IsAutoCommit && !session.IsCommitedOrRollbacked { if !session.IsAutoCommit && !session.IsCommitedOrRollbacked {
session.Engine.logSQL(session.Engine.dialect.RollBackStr()) session.saveLastSQL(session.Engine.dialect.RollBackStr())
session.IsCommitedOrRollbacked = true session.IsCommitedOrRollbacked = true
return session.Tx.Rollback() return session.Tx.Rollback()
} }
@ -344,7 +350,7 @@ func (session *Session) Rollback() error {
// When using transaction, Commit will commit all operations. // When using transaction, Commit will commit all operations.
func (session *Session) Commit() error { func (session *Session) Commit() error {
if !session.IsAutoCommit && !session.IsCommitedOrRollbacked { if !session.IsAutoCommit && !session.IsCommitedOrRollbacked {
session.Engine.logSQL("COMMIT") session.saveLastSQL("COMMIT")
session.IsCommitedOrRollbacked = true session.IsCommitedOrRollbacked = true
var err error var err error
if err = session.Tx.Commit(); err == nil { if err = session.Tx.Commit(); err == nil {
@ -465,7 +471,7 @@ func (session *Session) exec(sqlStr string, args ...interface{}) (sql.Result, er
sqlStr = filter.Do(sqlStr, session.Engine.dialect, session.Statement.RefTable) sqlStr = filter.Do(sqlStr, session.Engine.dialect, session.Statement.RefTable)
} }
session.Engine.logSQL(sqlStr, args...) session.saveLastSQL(sqlStr, args...)
return session.Engine.LogSQLExecutionTime(sqlStr, args, func() (sql.Result, error) { return session.Engine.LogSQLExecutionTime(sqlStr, args, func() (sql.Result, error) {
if session.IsAutoCommit { if session.IsAutoCommit {
@ -1457,7 +1463,7 @@ func (session *Session) isTableEmpty(tableName string) (bool, error) {
var total int64 var total int64
sql := fmt.Sprintf("select count(*) from %s", session.Engine.Quote(tableName)) sql := fmt.Sprintf("select count(*) from %s", session.Engine.Quote(tableName))
err := session.DB().QueryRow(sql).Scan(&total) err := session.DB().QueryRow(sql).Scan(&total)
session.Engine.logSQL(sql) session.saveLastSQL(sql)
if err != nil { if err != nil {
return true, err return true, err
} }
@ -1995,7 +2001,7 @@ func (session *Session) queryPreprocess(sqlStr *string, paramStr ...interface{})
*sqlStr = filter.Do(*sqlStr, session.Engine.dialect, session.Statement.RefTable) *sqlStr = filter.Do(*sqlStr, session.Engine.dialect, session.Statement.RefTable)
} }
session.Engine.logSQL(*sqlStr, paramStr...) session.saveLastSQL(*sqlStr, paramStr...)
} }
func (session *Session) query(sqlStr string, paramStr ...interface{}) (resultsSlice []map[string][]byte, err error) { func (session *Session) query(sqlStr string, paramStr ...interface{}) (resultsSlice []map[string][]byte, err error) {
@ -3871,6 +3877,18 @@ func (session *Session) Delete(bean interface{}) (int64, error) {
return res.RowsAffected() return res.RowsAffected()
} }
// saveLastSQL stores executed query information
func (session *Session) saveLastSQL(sql string, args ...interface{}) {
session.lastSQL = sql
session.lastSQLArgs = args
session.Engine.logSQL(sql, args...)
}
// LastSQL returns last query information
func (session *Session) LastSQL() (string, []interface{}) {
return session.lastSQL, session.lastSQLArgs
}
func (s *Session) Sync2(beans ...interface{}) error { func (s *Session) Sync2(beans ...interface{}) error {
engine := s.Engine engine := s.Engine