diff --git a/logger.go b/logger.go index 989beb25..e3f5fe44 100644 --- a/logger.go +++ b/logger.go @@ -41,30 +41,3 @@ func init() { // default_logger = log.New(os.Stdout, "\r\n", 0) default_logger = defaultLogger{log.New(os.Stdout, "\r\n", 0)} } - -func (s *Chain) print(level string, v ...interface{}) { - if s.d.logMode || s.debug_mode || level == "debug" { - if _, ok := s.d.logger.(Logger); !ok { - fmt.Println("logger haven't been set, using os.Stdout") - s.d.logger = default_logger - } - args := []interface{}{level} - s.d.logger.(Logger).Print(append(args, v...)...) - } -} - -func (s *Chain) warn(v ...interface{}) { - go s.print("warn", v...) -} - -func (s *Chain) info(v ...interface{}) { - go s.print("info", v...) -} - -func (s *Chain) slog(sql string, t time.Time, vars ...interface{}) { - go s.print("sql", time.Now().Sub(t), sql, vars) -} - -func (s *Chain) debug(v ...interface{}) { - go s.print("debug", v...) -} diff --git a/main.go b/main.go index ef881a47..2b3c677d 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,10 @@ package gorm import ( - "errors" - "database/sql" + "errors" ) + import "github.com/jinzhu/gorm/dialect" type DB struct { @@ -49,29 +49,6 @@ func (s *DB) SingularTable(b bool) { s.parent.singularTable = b } -func (s *DB) clone() *DB { - db := &DB{db: s.db, parent: s.parent, search: s.parent.search.clone()} - db.search.db = db - return db -} - -func (s *DB) do(data interface{}) *Do { - s.data = data - return &Do{db: s} -} - -func (s *DB) err(err error) error { - if err != nil { - s.Error = err - s.warn(err) - } - return err -} - -func (s *DB) hasError() bool { - return s.Error != nil -} - func (s *DB) Where(query interface{}, args ...interface{}) *DB { return s.clone().search.where(query, args...).db } @@ -172,22 +149,22 @@ func (s *DB) Model(value interface{}) *DB { } func (s *DB) Related(value interface{}, foreign_keys ...string) *DB { - s.clone().do(value).related(s.value, foreign_keys...) + s.clone().do(value).related(s.data, foreign_keys...) return s } func (s *DB) Pluck(column string, value interface{}) *DB { - s.clone().search.selects(column).do(s.value).pluck(column, value) + s.clone().search.selects(column).db.do(s.data).pluck(column, value) return s } func (s *DB) Count(value interface{}) *DB { - s.clone().search.selects("count(*)").do(s.value).count(value) + s.clone().search.selects("count(*)").db.do(s.data).count(value) return s } func (s *DB) Table(name string) *DB { - return s.clone().table(name).db + return s.clone().search.table(name).db } // Debug diff --git a/private.go b/private.go new file mode 100644 index 00000000..8844bfc1 --- /dev/null +++ b/private.go @@ -0,0 +1,56 @@ +package gorm + +import ( + "fmt" + "time" +) + +func (s *DB) clone() *DB { + db := &DB{db: s.db, parent: s.parent, search: s.parent.search.clone()} + db.search.db = db + return db +} + +func (s *DB) do(data interface{}) *Do { + s.data = data + return &Do{db: s} +} + +func (s *DB) err(err error) error { + if err != nil { + s.Error = err + s.warn(err) + } + return err +} + +func (s *DB) hasError() bool { + return s.Error != nil +} + +func (s *DB) print(level string, v ...interface{}) { + if s.d.logMode || s.debug_mode || level == "debug" { + if _, ok := s.d.logger.(Logger); !ok { + fmt.Println("logger haven't been set, using os.Stdout") + s.d.logger = default_logger + } + args := []interface{}{level} + s.d.logger.(Logger).Print(append(args, v...)...) + } +} + +func (s *DB) warn(v ...interface{}) { + go s.print("warn", v...) +} + +func (s *DB) info(v ...interface{}) { + go s.print("info", v...) +} + +func (s *DB) slog(sql string, t time.Time, vars ...interface{}) { + go s.print("sql", time.Now().Sub(t), sql, vars) +} + +func (s *DB) debug(v ...interface{}) { + go s.print("debug", v...) +}