2013-11-16 07:41:31 +04:00
|
|
|
package gorm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *DB) clone() *DB {
|
2013-11-16 10:15:33 +04:00
|
|
|
db := DB{db: s.db, parent: s.parent, logMode: s.logMode, data: s.data, Error: s.Error}
|
|
|
|
|
2013-11-16 11:01:31 +04:00
|
|
|
if s.search == nil {
|
2013-11-16 09:28:22 +04:00
|
|
|
db.search = &search{}
|
|
|
|
} else {
|
2013-11-16 11:01:31 +04:00
|
|
|
db.search = s.search.clone()
|
2013-11-16 09:28:22 +04:00
|
|
|
}
|
2013-11-16 11:01:31 +04:00
|
|
|
|
2013-11-16 10:15:33 +04:00
|
|
|
db.search.db = &db
|
|
|
|
return &db
|
2013-11-16 07:41:31 +04:00
|
|
|
}
|
|
|
|
|
2013-11-24 04:29:56 +04:00
|
|
|
func (s *DB) new() *DB {
|
|
|
|
db := DB{db: s.db, parent: s.parent, logMode: s.logMode, data: s.data, Error: s.Error, search: &search{}}
|
|
|
|
db.search.db = &db
|
|
|
|
return &db
|
|
|
|
}
|
|
|
|
|
2013-11-16 07:41:31 +04:00
|
|
|
func (s *DB) do(data interface{}) *Do {
|
|
|
|
s.data = data
|
2013-11-16 09:19:29 +04:00
|
|
|
do := Do{db: s}
|
|
|
|
do.setModel(data)
|
|
|
|
return &do
|
2013-11-16 07:41:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) err(err error) error {
|
|
|
|
if err != nil {
|
|
|
|
s.Error = err
|
2013-12-17 15:16:03 +04:00
|
|
|
if s.logMode == 0 {
|
|
|
|
if err != RecordNotFound {
|
|
|
|
go fmt.Println(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s.warn(err)
|
|
|
|
}
|
2013-11-16 07:41:31 +04:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) hasError() bool {
|
|
|
|
return s.Error != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) print(level string, v ...interface{}) {
|
2013-12-17 15:16:03 +04:00
|
|
|
if s.logMode == 2 || level == "debug" {
|
2013-11-16 08:19:35 +04:00
|
|
|
if _, ok := s.parent.logger.(Logger); !ok {
|
2013-11-16 07:41:31 +04:00
|
|
|
fmt.Println("logger haven't been set, using os.Stdout")
|
2013-11-16 08:19:35 +04:00
|
|
|
s.parent.logger = default_logger
|
2013-11-16 07:41:31 +04:00
|
|
|
}
|
|
|
|
args := []interface{}{level}
|
2013-11-16 08:19:35 +04:00
|
|
|
s.parent.logger.(Logger).Print(append(args, v...)...)
|
2013-11-16 07:41:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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...)
|
|
|
|
}
|