callback create.go

This commit is contained in:
Jinzhu 2014-01-26 21:23:53 +08:00
parent 8dd7b4ed91
commit 7b8e91377b
4 changed files with 18 additions and 15 deletions

View File

@ -15,15 +15,17 @@ func Create(scope *Scope) {
defer scope.Trace(time.Now()) defer scope.Trace(time.Now())
if !scope.HasError() { if !scope.HasError() {
scope.SetColumn("CreatedAt", time.Now())
scope.SetColumn("UpdatedAt", time.Now())
// set create sql // set create sql
var sqls, columns []string var sqls, columns []string
for _, field := range scope.Fields() { for _, field := range scope.Fields() {
if field.IsBlank || len(field.SqlTag) == 0 { if field.DBName != scope.PrimaryKey() && len(field.SqlTag) > 0 && !field.IsIgnored {
continue columns = append(columns, scope.quote(field.DBName))
sqls = append(sqls, scope.AddToVars(field.Value))
} }
columns = append(columns, scope.quote(field.DBName))
sqls = append(sqls, scope.AddToVars(field.Value))
} }
scope.Raw(fmt.Sprintf( scope.Raw(fmt.Sprintf(

View File

@ -22,10 +22,10 @@ type IgnoredEmbedStruct struct {
} }
type User struct { type User struct {
Id int64 // Id: Primary key Id int64 // Id: Primary key
Birthday time.Time // Time
Age int64 Age int64
Name string `sql:"size:255"` Name string `sql:"size:255"`
Birthday time.Time // Time
CreatedAt time.Time // CreatedAt: Time of record is created, will be insert automatically CreatedAt time.Time // CreatedAt: Time of record is created, will be insert automatically
UpdatedAt time.Time // UpdatedAt: Time of record is updated, will be updated automatically UpdatedAt time.Time // UpdatedAt: Time of record is updated, will be updated automatically
DeletedAt time.Time // DeletedAt: Time of record is deleted, refer Soft Delete for more DeletedAt time.Time // DeletedAt: Time of record is deleted, refer Soft Delete for more
@ -189,6 +189,7 @@ func TestPrecision(t *testing.T) {
var u User var u User
db.First(&u, "name = ?", "Precision") db.First(&u, "name = ?", "Precision")
if u.Latitude != f { if u.Latitude != f {
t.Errorf("Float64 should not be changed after query") t.Errorf("Float64 should not be changed after query")
} }
@ -868,6 +869,7 @@ func TestUpdate(t *testing.T) {
product1 := Product{Code: "123"} product1 := Product{Code: "123"}
product2 := Product{Code: "234"} product2 := Product{Code: "234"}
db.Save(&product1).Save(&product2).Update("code", "456") db.Save(&product1).Save(&product2).Update("code", "456")
if product2.Code != "456" { if product2.Code != "456" {
t.Errorf("Record should be updated with update attributes") t.Errorf("Record should be updated with update attributes")
} }

View File

@ -116,7 +116,6 @@ func (s *DB) First(out interface{}, where ...interface{}) *DB {
func (s *DB) Last(out interface{}, where ...interface{}) *DB { func (s *DB) Last(out interface{}, where ...interface{}) *DB {
return s.clone().do(out).where(where...).last().db return s.clone().do(out).where(where...).last().db
} }
func (s *DB) Find(out interface{}, where ...interface{}) *DB { func (s *DB) Find(out interface{}, where ...interface{}) *DB {
return s.clone().do(out).where(where...).query().db return s.clone().do(out).where(where...).query().db
} }
@ -180,7 +179,7 @@ func (s *DB) UpdateColumns(values interface{}, ignore_protected_attrs ...bool) *
func (s *DB) Save(value interface{}) *DB { func (s *DB) Save(value interface{}) *DB {
scope := s.clone().newScope(value) scope := s.clone().newScope(value)
if scope.PrimaryKeyZero() { if scope.PrimaryKeyZero() {
return scope.callCallbacks(s.parent.callback.creates).db return scope.callCallbacks(s.parent.callback.creates).db.do(value).db
} else { } else {
return s.clone().do(value).begin().save().commit_or_rollback().db return s.clone().do(value).begin().save().commit_or_rollback().db
} }

View File

@ -32,15 +32,15 @@ func (s *DB) do(data interface{}) *Do {
func (s *DB) err(err error) error { func (s *DB) err(err error) error {
if err != nil { if err != nil {
if s.logMode == 0 { if err != RecordNotFound {
if err != RecordNotFound { if s.logMode == 0 {
go s.print(fileWithLineNum(), err) go s.print(fileWithLineNum(), err)
if regexp.MustCompile(`^sql: Scan error on column index`).MatchString(err.Error()) { } else {
return nil s.log(err)
} }
if regexp.MustCompile(`^sql: Scan error on column index`).MatchString(err.Error()) {
return nil
} }
} else {
s.log(err)
} }
s.Error = err s.Error = err
} }