2013-11-16 07:41:31 +04:00
|
|
|
package gorm
|
|
|
|
|
|
|
|
import (
|
2013-12-23 13:19:15 +04:00
|
|
|
"regexp"
|
2013-11-16 07:41:31 +04:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *DB) clone() *DB {
|
2014-08-20 13:05:02 +04:00
|
|
|
db := DB{db: s.db, parent: s.parent, logMode: s.logMode, Value: s.Value, Error: s.Error, values: map[string]interface{}{}}
|
|
|
|
|
|
|
|
for key, value := range s.values {
|
|
|
|
db.values[key] = value
|
|
|
|
}
|
2013-11-16 10:15:33 +04:00
|
|
|
|
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 {
|
2014-01-04 13:36:58 +04:00
|
|
|
s.search = nil
|
|
|
|
return s.clone()
|
2013-11-24 04:29:56 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:41:31 +04:00
|
|
|
func (s *DB) err(err error) error {
|
2014-01-04 13:36:58 +04:00
|
|
|
if err != nil {
|
2014-01-26 17:23:53 +04:00
|
|
|
if err != RecordNotFound {
|
|
|
|
if s.logMode == 0 {
|
2014-01-04 13:36:58 +04:00
|
|
|
go s.print(fileWithLineNum(), err)
|
2014-01-26 17:23:53 +04:00
|
|
|
} else {
|
|
|
|
s.log(err)
|
|
|
|
}
|
|
|
|
if regexp.MustCompile(`^sql: Scan error on column index`).MatchString(err.Error()) {
|
|
|
|
return nil
|
2013-12-17 15:16:03 +04:00
|
|
|
}
|
|
|
|
}
|
2014-01-04 13:36:58 +04:00
|
|
|
s.Error = err
|
2013-11-16 07:41:31 +04:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-01-04 13:36:58 +04:00
|
|
|
func (s *DB) print(v ...interface{}) {
|
2014-01-22 05:05:43 +04:00
|
|
|
s.parent.logger.(logger).Print(v...)
|
2013-11-16 07:41:31 +04:00
|
|
|
}
|
|
|
|
|
2014-01-04 13:36:58 +04:00
|
|
|
func (s *DB) log(v ...interface{}) {
|
2014-07-29 14:21:36 +04:00
|
|
|
if s != nil && s.logMode == 2 {
|
2014-01-04 13:36:58 +04:00
|
|
|
s.print(append([]interface{}{"log", fileWithLineNum()}, v...)...)
|
|
|
|
}
|
2013-11-16 07:41:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2013-11-16 07:41:31 +04:00
|
|
|
}
|