gorm/private.go

83 lines
1.6 KiB
Go
Raw Normal View History

package gorm
import (
"fmt"
"os"
"regexp"
"runtime"
"strings"
"time"
)
func (s *DB) clone() *DB {
2014-01-04 11:08:00 +04:00
db := DB{db: s.db, parent: s.parent, logMode: s.logMode, Value: s.Value, 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
db.search.db = &db
return &db
}
2013-11-24 04:29:56 +04:00
func (s *DB) new() *DB {
2014-01-04 13:36:58 +04:00
s.search = nil
return s.clone()
2013-11-24 04:29:56 +04:00
}
func (s *DB) do(data interface{}) *Do {
2014-01-04 11:08:00 +04:00
s.Value = data
2013-11-16 09:19:29 +04:00
do := Do{db: s}
do.setModel(data)
return &do
}
func (s *DB) err(err error) error {
2014-01-04 13:36:58 +04:00
if err != nil {
if s.logMode == 0 {
if err != RecordNotFound {
go s.print(fileWithLineNum(), err)
if regexp.MustCompile(`^sql: Scan error on column index`).MatchString(err.Error()) {
return nil
}
}
2014-01-04 13:36:58 +04:00
} else {
s.log(err)
}
2014-01-04 13:36:58 +04:00
s.Error = err
}
return err
}
func (s *DB) hasError() bool {
return s.Error != nil
}
2014-01-04 13:36:58 +04:00
func fileWithLineNum() string {
for i := 5; i < 15; i++ {
_, file, line, ok := runtime.Caller(i)
if ok && (!regexp.MustCompile(`jinzhu/gorm/.*.go`).MatchString(file) || regexp.MustCompile(`jinzhu/gorm/.*test.go`).MatchString(file)) {
return fmt.Sprintf("%v:%v", strings.TrimPrefix(file, os.Getenv("GOPATH")+"src/"), line)
}
}
2014-01-04 13:36:58 +04:00
return ""
}
2014-01-04 13:36:58 +04:00
func (s *DB) print(v ...interface{}) {
go s.parent.logger.(logger).Print(v...)
}
2014-01-04 13:36:58 +04:00
func (s *DB) log(v ...interface{}) {
if s.logMode == 2 {
s.print(append([]interface{}{"log", fileWithLineNum()}, v...)...)
}
}
func (s *DB) slog(sql string, t time.Time, vars ...interface{}) {
2014-01-04 13:36:58 +04:00
if s.logMode == 2 {
s.print("sql", fileWithLineNum(), time.Now().Sub(t), sql, vars)
}
}