diff --git a/chain.go b/chain.go index d26fadc8..91ab87b0 100644 --- a/chain.go +++ b/chain.go @@ -12,6 +12,7 @@ import ( type Chain struct { db *sql.DB driver string + debug bool value interface{} Errors []error @@ -27,10 +28,20 @@ type Chain struct { unscoped bool } +func (s *Chain) msg(str string) { + if s.debug { + debug(str) + } +} + func (s *Chain) err(err error) error { if err != nil { s.Errors = append(s.Errors, err) s.Error = err + + if s.debug { + debug(err) + } } return err } @@ -206,6 +217,11 @@ func (s *Chain) Table(name string) *Chain { return s } +func (s *Chain) Debug() *Chain { + s.debug = true + return s +} + func (s *Chain) validSql(str string) (result bool) { result = regexp.MustCompile("^\\s*[\\w][\\w\\s,.]*[\\w]\\s*$").MatchString(str) if !result { diff --git a/gorm_test.go b/gorm_test.go index e1d96806..7b84a60c 100644 --- a/gorm_test.go +++ b/gorm_test.go @@ -48,6 +48,7 @@ func init() { panic(fmt.Sprintf("No error should happen when connect database, but got %+v", err)) } db.SetPool(10) + db.DebugMode = true err = db.Exec("drop table users;").Error if err != nil { diff --git a/main.go b/main.go index 1308151f..bfb3d5c7 100644 --- a/main.go +++ b/main.go @@ -3,8 +3,9 @@ package gorm import "database/sql" type DB struct { - db *sql.DB - driver string + db *sql.DB + driver string + DebugMode bool } func Open(driver, source string) (db DB, err error) { @@ -18,7 +19,7 @@ func (s *DB) SetPool(n int) { } func (s *DB) buildChain() *Chain { - return &Chain{db: s.db, driver: s.driver} + return &Chain{db: s.db, driver: s.driver, debug: s.DebugMode} } func (s *DB) Where(querystring interface{}, args ...interface{}) *Chain { @@ -81,6 +82,10 @@ func (s *DB) Table(name string) *Chain { return s.buildChain().Table(name) } +func (s *DB) Debug() *Chain { + return s.buildChain().Debug() +} + func (s *DB) CreateTable(value interface{}) *Chain { return s.buildChain().CreateTable(value) }