mirror of https://github.com/go-gorm/gorm.git
refact
This commit is contained in:
parent
275de4f133
commit
7bebf685f4
37
do.go
37
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
|
||||
}
|
||||
|
|
12
main.go
12
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 {
|
||||
|
|
3
scope.go
3
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) {
|
||||
|
|
Loading…
Reference in New Issue