This commit is contained in:
Jinzhu 2014-01-28 10:23:31 +08:00
parent 275de4f133
commit 7bebf685f4
3 changed files with 8 additions and 44 deletions

37
do.go
View File

@ -772,40 +772,3 @@ func (s *Do) autoMigrate() *Do {
} }
return s 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
}

12
main.go
View File

@ -150,9 +150,9 @@ func (s *DB) Scan(dest interface{}) *DB {
func (s *DB) FirstOrInit(out interface{}, where ...interface{}) *DB { func (s *DB) FirstOrInit(out interface{}, where ...interface{}) *DB {
c := s.clone() c := s.clone()
if c.First(out, where...).Error == RecordNotFound { if c.First(out, where...).Error == RecordNotFound {
return c.NewScope(out).inlineCondition(where).initialize().db c.NewScope(out).inlineCondition(where).initialize()
} else if len(s.search.assignAttrs) > 0 { } else {
return c.do(out).updateAttrs(s.search.assignAttrs).db c.NewScope(out).updatedAttrsWithValues(convertInterfaceToMap(s.search.assignAttrs), false)
} }
return c return c
} }
@ -160,9 +160,9 @@ func (s *DB) FirstOrInit(out interface{}, where ...interface{}) *DB {
func (s *DB) FirstOrCreate(out interface{}, where ...interface{}) *DB { func (s *DB) FirstOrCreate(out interface{}, where ...interface{}) *DB {
c := s.clone() c := s.clone()
if c.First(out, where...).Error == RecordNotFound { 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 { } 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 return c
} }
@ -268,7 +268,7 @@ func (s *DB) Rollback() *DB {
} }
func (s *DB) NewRecord(value interface{}) bool { 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 { func (s *DB) RecordNotFound() bool {

View File

@ -332,11 +332,12 @@ func (scope *Scope) Raw(sql string) {
scope.Sql = strings.Replace(sql, "$$", "?", -1) scope.Sql = strings.Replace(sql, "$$", "?", -1)
} }
func (scope *Scope) Exec() { func (scope *Scope) Exec() *Scope {
if !scope.HasError() { if !scope.HasError() {
_, err := scope.DB().Exec(scope.Sql, scope.SqlVars...) _, err := scope.DB().Exec(scope.Sql, scope.SqlVars...)
scope.Err(err) scope.Err(err)
} }
return scope
} }
func (scope *Scope) Get(name string) (value interface{}, ok bool) { func (scope *Scope) Get(name string) (value interface{}, ok bool) {