forked from mirror/gorm
99 lines
2.0 KiB
Go
99 lines
2.0 KiB
Go
package gorm
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"regexp"
|
|
"runtime"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func (s *DB) clone() *DB {
|
|
db := DB{db: s.db, parent: s.parent, logMode: s.logMode, Value: s.Value, Error: s.Error}
|
|
|
|
if s.search == nil {
|
|
db.search = &search{}
|
|
} else {
|
|
db.search = s.search.clone()
|
|
}
|
|
|
|
db.search.db = &db
|
|
return &db
|
|
}
|
|
|
|
func (s *DB) new() *DB {
|
|
db := DB{db: s.db, parent: s.parent, logMode: s.logMode, Value: s.Value, Error: s.Error, search: &search{}}
|
|
db.search.db = &db
|
|
return &db
|
|
}
|
|
|
|
func (s *DB) do(data interface{}) *Do {
|
|
s.Value = data
|
|
do := Do{db: s}
|
|
do.setModel(data)
|
|
return &do
|
|
}
|
|
|
|
func (s *DB) 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)
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (s *DB) err(err error) error {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
|
|
if s.logMode == 0 {
|
|
if err != RecordNotFound {
|
|
go fmt.Println(s.fileWithLineNum(), err)
|
|
error_str := err.Error()
|
|
if regexp.MustCompile(`^sql: Scan error on column index`).MatchString(error_str) {
|
|
return nil
|
|
}
|
|
}
|
|
} else {
|
|
s.warn(err)
|
|
}
|
|
|
|
s.Error = err
|
|
return err
|
|
}
|
|
|
|
func (s *DB) hasError() bool {
|
|
return s.Error != nil
|
|
}
|
|
|
|
func (s *DB) print(level string, v ...interface{}) {
|
|
if s.logMode == 2 || level == "debug" {
|
|
if _, ok := s.parent.logger.(logger); !ok {
|
|
fmt.Println("logger haven't been set, using os.Stdout")
|
|
s.parent.logger = default_logger
|
|
}
|
|
args := []interface{}{level}
|
|
s.parent.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...)
|
|
}
|