Move non-public DB method to private.go

This commit is contained in:
Jinzhu 2013-11-16 11:41:31 +08:00
parent b3db0b2959
commit ea67d1d377
3 changed files with 62 additions and 56 deletions

View File

@ -41,30 +41,3 @@ func init() {
// default_logger = log.New(os.Stdout, "\r\n", 0) // default_logger = log.New(os.Stdout, "\r\n", 0)
default_logger = defaultLogger{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...)
}

35
main.go
View File

@ -1,10 +1,10 @@
package gorm package gorm
import ( import (
"errors"
"database/sql" "database/sql"
"errors"
) )
import "github.com/jinzhu/gorm/dialect" import "github.com/jinzhu/gorm/dialect"
type DB struct { type DB struct {
@ -49,29 +49,6 @@ func (s *DB) SingularTable(b bool) {
s.parent.singularTable = b 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 { func (s *DB) Where(query interface{}, args ...interface{}) *DB {
return s.clone().search.where(query, args...).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 { 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 return s
} }
func (s *DB) Pluck(column string, value interface{}) *DB { 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 return s
} }
func (s *DB) Count(value interface{}) *DB { 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 return s
} }
func (s *DB) Table(name string) *DB { func (s *DB) Table(name string) *DB {
return s.clone().table(name).db return s.clone().search.table(name).db
} }
// Debug // Debug

56
private.go Normal file
View File

@ -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...)
}