diff --git a/chain.go b/chain.go index 620f9d40..d17cda24 100644 --- a/chain.go +++ b/chain.go @@ -37,6 +37,10 @@ func (s *Chain) err(err error) error { return err } +func (s *Chain) hasError() bool { + return len(s.Errors) > 0 +} + func (s *Chain) deleteLastError() { s.Error = nil s.Errors = s.Errors[:len(s.Errors)-1] diff --git a/do.go b/do.go index cb60fbdc..d80693aa 100644 --- a/do.go +++ b/do.go @@ -55,7 +55,7 @@ func (s *Do) err(err error) error { } func (s *Do) hasError() bool { - return len(s.chain.Errors) > 0 + return s.chain.hasError() } 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) { if s.hasError() { return - } else if len(sqls) > 0 { - _, err = s.db.Exec(sqls[0]) - } else if len(s.sql) > 0 { + } else { + if len(sqls) > 0 { + s.sql = sqls[0] + } _, err = s.db.Exec(s.sql, s.sqlVars...) + slog(s.sql, s.sqlVars...) } return s.err(err) } @@ -187,6 +189,7 @@ func (s *Do) create() (i interface{}) { s.err(err) } } + slog(s.sql, s.sqlVars...) if !s.hasError() { result := reflect.Indirect(reflect.ValueOf(s.value)) @@ -382,6 +385,7 @@ func (s *Do) query() { s.prepareQuerySql() if !s.hasError() { rows, err := s.db.Query(s.sql, s.sqlVars...) + slog(s.sql, s.sqlVars...) if s.err(err) != nil { return } @@ -437,6 +441,7 @@ func (s *Do) count(value interface{}) { s.prepareQuerySql() if !s.hasError() { rows, err := s.db.Query(s.sql, s.sqlVars...) + slog(s.sql, s.sqlVars...) if s.err(err) != nil { return } @@ -466,6 +471,7 @@ func (s *Do) pluck(column string, value interface{}) { if !s.hasError() { rows, err := s.db.Query(s.sql, s.sqlVars...) + slog(s.sql, s.sqlVars...) if s.err(err) != nil { return } diff --git a/gorm_test.go b/gorm_test.go index 5d2265da..0cbe21f8 100644 --- a/gorm_test.go +++ b/gorm_test.go @@ -86,7 +86,7 @@ func init() { // GRANT ALL ON gorm.* TO 'gorm'@'localhost'; // db, err = Open("mysql", "gorm:gorm@/gorm?charset=utf8&parseTime=True") // db, err = Open("sqlite3", "/tmp/gorm.db") - db.LogMode(true) + db.LogMode(false) if err != nil { panic(fmt.Sprintf("No error should happen when connect database, but got %+v", err)) diff --git a/logger.go b/logger.go index fb344997..a67eabfa 100644 --- a/logger.go +++ b/logger.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "os" + "regexp" ) var logger interface{} @@ -13,7 +14,7 @@ type Logger interface { Print(v ...interface{}) } -func Print(level string, v ...interface{}) { +func print(level string, v ...interface{}) { if logger_disabled { return } @@ -29,13 +30,17 @@ func Print(level string, v ...interface{}) { } func warn(v ...interface{}) { - go Print("warn", v...) + go print("warn", v...) } 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{}) { - go Print("debug", v...) + go print("debug", v...) }