Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
This repository is currently being migrated. It's locked while the migration is in progress.

Commit

Permalink
support local sql log
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Jun 27, 2019
1 parent 4c80660 commit 5cd89d4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func (engine *Engine) Ping() error {
return session.Ping()
}

// logging sql
// logSQL save sql
func (engine *Engine) logSQL(sqlStr string, sqlArgs ...interface{}) {
if engine.showSQL && !engine.showExecTime {
if len(sqlArgs) > 0 {
Expand Down
24 changes: 23 additions & 1 deletion session.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type Session struct {
//beforeSQLExec func(string, ...interface{})
lastSQL string
lastSQLArgs []interface{}
showSQL bool

ctx context.Context
sessionType sessionType
Expand All @@ -72,6 +73,7 @@ func (session *Session) Clone() *Session {
func (session *Session) Init() {
session.statement.Init()
session.statement.Engine = session.engine
session.showSQL = session.engine.showSQL
session.isAutoCommit = true
session.isCommitedOrRollbacked = false
session.isAutoClose = false
Expand Down Expand Up @@ -226,6 +228,16 @@ func (session *Session) Cascade(trueOrFalse ...bool) *Session {
return session
}

// MustLogSQL means record SQL or not and don't follow engine's setting
func (session *Session) MustLogSQL(log ...bool) *Session {
if len(log) > 0 {
session.showSQL = log[0]
} else {
session.showSQL = true
}
return session
}

// NoCache ask this session do not retrieve data from cache system and
// get data from database directly.
func (session *Session) NoCache() *Session {
Expand Down Expand Up @@ -842,7 +854,17 @@ func (session *Session) slice2Bean(scanResults []interface{}, fields []string, b
func (session *Session) saveLastSQL(sql string, args ...interface{}) {
session.lastSQL = sql
session.lastSQLArgs = args
session.engine.logSQL(sql, args...)
session.logSQL(sql, args...)
}

func (session *Session) logSQL(sqlStr string, sqlArgs ...interface{}) {
if session.showSQL && !session.engine.showExecTime {
if len(sqlArgs) > 0 {
session.engine.logger.Infof("[SQL] %v %#v", sqlStr, sqlArgs)
} else {
session.engine.logger.Infof("[SQL] %v", sqlStr)
}
}
}

// LastSQL returns last query information
Expand Down
4 changes: 3 additions & 1 deletion session_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ func (session *Session) queryRows(sqlStr string, args ...interface{}) (*core.Row

session.queryPreprocess(&sqlStr, args...)

if session.engine.showSQL {
if session.showSQL {
session.lastSQL = sqlStr
session.lastSQLArgs = args
if session.engine.showExecTime {
b4ExecTime := time.Now()
defer func() {
Expand Down

0 comments on commit 5cd89d4

Please sign in to comment.