From 7bebf685f42b13349be87d4b377221699284d376 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 28 Jan 2014 10:23:31 +0800 Subject: [PATCH] refact --- do.go | 37 ------------------------------------- main.go | 12 ++++++------ scope.go | 3 ++- 3 files changed, 8 insertions(+), 44 deletions(-) diff --git a/do.go b/do.go index 031989db..e7bfb2b4 100644 --- a/do.go +++ b/do.go @@ -772,40 +772,3 @@ func (s *Do) autoMigrate() *Do { } return s } - -func (s *Do) begin() *Do { - if db, ok := s.db.db.(sqlDb); ok { - if tx, err := db.Begin(); err == nil { - s.db.db = interface{}(tx).(sqlCommon) - s.startedTransaction = true - } - } - return s -} - -func (s *Do) commit_or_rollback() *Do { - if s.startedTransaction { - if db, ok := s.db.db.(sqlTx); ok { - if s.db.hasError() { - db.Rollback() - } else { - db.Commit() - } - s.db.db = s.db.parent.db - } - } - return s -} - -func (s *Do) initialize() *Do { - for _, clause := range s.search.whereClause { - s.updateAttrs(clause["query"]) - } - for _, attrs := range s.search.initAttrs { - s.updateAttrs(attrs) - } - for _, attrs := range s.search.assignAttrs { - s.updateAttrs(attrs) - } - return s -} diff --git a/main.go b/main.go index ec77330b..6bfb7c6d 100644 --- a/main.go +++ b/main.go @@ -150,9 +150,9 @@ func (s *DB) Scan(dest interface{}) *DB { func (s *DB) FirstOrInit(out interface{}, where ...interface{}) *DB { c := s.clone() if c.First(out, where...).Error == RecordNotFound { - return c.NewScope(out).inlineCondition(where).initialize().db - } else if len(s.search.assignAttrs) > 0 { - return c.do(out).updateAttrs(s.search.assignAttrs).db + c.NewScope(out).inlineCondition(where).initialize() + } else { + c.NewScope(out).updatedAttrsWithValues(convertInterfaceToMap(s.search.assignAttrs), false) } return c } @@ -160,9 +160,9 @@ func (s *DB) FirstOrInit(out interface{}, where ...interface{}) *DB { func (s *DB) FirstOrCreate(out interface{}, where ...interface{}) *DB { c := s.clone() if c.First(out, where...).Error == RecordNotFound { - return c.do(out).where(where...).initialize().db.Save(out) + c.NewScope(out).inlineCondition(where).initialize().callCallbacks(s.parent.callback.creates) } else if len(s.search.assignAttrs) > 0 { - return c.do(out).updateAttrs(s.search.assignAttrs).update().db + c.NewScope(out).Set("gorm:update_interface", s.search.assignAttrs).callCallbacks(s.parent.callback.updates) } return c } @@ -268,7 +268,7 @@ func (s *DB) Rollback() *DB { } func (s *DB) NewRecord(value interface{}) bool { - return s.clone().do(value).model.primaryKeyZero() + return s.clone().NewScope(value).PrimaryKeyZero() } func (s *DB) RecordNotFound() bool { diff --git a/scope.go b/scope.go index cdbcbf8a..6e1456d0 100644 --- a/scope.go +++ b/scope.go @@ -332,11 +332,12 @@ func (scope *Scope) Raw(sql string) { scope.Sql = strings.Replace(sql, "$$", "?", -1) } -func (scope *Scope) Exec() { +func (scope *Scope) Exec() *Scope { if !scope.HasError() { _, err := scope.DB().Exec(scope.Sql, scope.SqlVars...) scope.Err(err) } + return scope } func (scope *Scope) Get(name string) (value interface{}, ok bool) {