show sql query log

This commit is contained in:
Jinzhu 2013-11-11 11:53:56 +08:00
parent 9bc11d6637
commit 50a1b6e3e5
4 changed files with 24 additions and 9 deletions

View File

@ -37,6 +37,10 @@ func (s *Chain) err(err error) error {
return err return err
} }
func (s *Chain) hasError() bool {
return len(s.Errors) > 0
}
func (s *Chain) deleteLastError() { func (s *Chain) deleteLastError() {
s.Error = nil s.Error = nil
s.Errors = s.Errors[:len(s.Errors)-1] s.Errors = s.Errors[:len(s.Errors)-1]

14
do.go
View File

@ -55,7 +55,7 @@ func (s *Do) err(err error) error {
} }
func (s *Do) hasError() bool { func (s *Do) hasError() bool {
return len(s.chain.Errors) > 0 return s.chain.hasError()
} }
func (s *Do) setModel(value interface{}) *Do { func (s *Do) setModel(value interface{}) *Do {
@ -76,10 +76,12 @@ func (s *Do) addToVars(value interface{}) string {
func (s *Do) exec(sqls ...string) (err error) { func (s *Do) exec(sqls ...string) (err error) {
if s.hasError() { if s.hasError() {
return return
} else if len(sqls) > 0 { } else {
_, err = s.db.Exec(sqls[0]) if len(sqls) > 0 {
} else if len(s.sql) > 0 { s.sql = sqls[0]
}
_, err = s.db.Exec(s.sql, s.sqlVars...) _, err = s.db.Exec(s.sql, s.sqlVars...)
slog(s.sql, s.sqlVars...)
} }
return s.err(err) return s.err(err)
} }
@ -187,6 +189,7 @@ func (s *Do) create() (i interface{}) {
s.err(err) s.err(err)
} }
} }
slog(s.sql, s.sqlVars...)
if !s.hasError() { if !s.hasError() {
result := reflect.Indirect(reflect.ValueOf(s.value)) result := reflect.Indirect(reflect.ValueOf(s.value))
@ -382,6 +385,7 @@ func (s *Do) query() {
s.prepareQuerySql() s.prepareQuerySql()
if !s.hasError() { if !s.hasError() {
rows, err := s.db.Query(s.sql, s.sqlVars...) rows, err := s.db.Query(s.sql, s.sqlVars...)
slog(s.sql, s.sqlVars...)
if s.err(err) != nil { if s.err(err) != nil {
return return
} }
@ -437,6 +441,7 @@ func (s *Do) count(value interface{}) {
s.prepareQuerySql() s.prepareQuerySql()
if !s.hasError() { if !s.hasError() {
rows, err := s.db.Query(s.sql, s.sqlVars...) rows, err := s.db.Query(s.sql, s.sqlVars...)
slog(s.sql, s.sqlVars...)
if s.err(err) != nil { if s.err(err) != nil {
return return
} }
@ -466,6 +471,7 @@ func (s *Do) pluck(column string, value interface{}) {
if !s.hasError() { if !s.hasError() {
rows, err := s.db.Query(s.sql, s.sqlVars...) rows, err := s.db.Query(s.sql, s.sqlVars...)
slog(s.sql, s.sqlVars...)
if s.err(err) != nil { if s.err(err) != nil {
return return
} }

View File

@ -86,7 +86,7 @@ func init() {
// GRANT ALL ON gorm.* TO 'gorm'@'localhost'; // GRANT ALL ON gorm.* TO 'gorm'@'localhost';
// db, err = Open("mysql", "gorm:gorm@/gorm?charset=utf8&parseTime=True") // db, err = Open("mysql", "gorm:gorm@/gorm?charset=utf8&parseTime=True")
// db, err = Open("sqlite3", "/tmp/gorm.db") // db, err = Open("sqlite3", "/tmp/gorm.db")
db.LogMode(true) db.LogMode(false)
if err != nil { if err != nil {
panic(fmt.Sprintf("No error should happen when connect database, but got %+v", err)) panic(fmt.Sprintf("No error should happen when connect database, but got %+v", err))

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"regexp"
) )
var logger interface{} var logger interface{}
@ -13,7 +14,7 @@ type Logger interface {
Print(v ...interface{}) Print(v ...interface{})
} }
func Print(level string, v ...interface{}) { func print(level string, v ...interface{}) {
if logger_disabled { if logger_disabled {
return return
} }
@ -29,13 +30,17 @@ func Print(level string, v ...interface{}) {
} }
func warn(v ...interface{}) { func warn(v ...interface{}) {
go Print("warn", v...) go print("warn", v...)
} }
func info(v ...interface{}) { func info(v ...interface{}) {
go Print("info", v...) go print("info", v...)
}
func slog(sql string, vars ...interface{}) {
go print("sql", fmt.Sprintf(regexp.MustCompile(`\$\d|\?`).ReplaceAllString(sql, "'%v'"), vars...))
} }
func debug(v ...interface{}) { func debug(v ...interface{}) {
go Print("debug", v...) go print("debug", v...)
} }