Refact do.go

This commit is contained in:
Jinzhu 2013-11-16 12:45:29 +08:00
parent f884a94495
commit cb53b5ed85
1 changed files with 25 additions and 14 deletions

39
do.go
View File

@ -13,13 +13,16 @@ import (
) )
type Do struct { type Do struct {
db *DB db *DB
model *Model model *Model
tableName string tableName string
value interface{} value interface{}
sql string update_attrs map[string]interface{}
sqlVars []interface{} hasUpdate bool
startedTransaction bool ignoreProtectedAttrs bool
sql string
sqlVars []interface{}
startedTransaction bool
} }
func (s *Do) table() string { func (s *Do) table() string {
@ -187,20 +190,28 @@ func (s *Do) create() (i interface{}) {
return return
} }
func (s *Do) setUpdateAttrs(values interface{}, ignore_protected_attrs ...bool) *Do { func (s *Do) updateAttrs(values interface{}, ignore_protected_attrs ...bool) *Do {
switch vs := values.(type) { switch value := values.(type) {
case map[string]interface{}: case map[string]interface{}:
s.updateAttrs = vs if len(value) > 0 {
results, has_update := s.model.updatedColumnsAndValues(value)
if len(results) > 0 {
s.update_attrs = results
} else if has_update {
s.hasUpdate = has_update
}
}
case []interface{}: case []interface{}:
for _, value := range vs { for _, v := range value {
s.setUpdateAttrs(value, ignore_protected_attrs...) s.updateAttrs(v)
} }
case interface{}: case interface{}:
m := &Model{data: values, do: s} m := &Model{data: values, do: s}
s.updateAttrs = map[string]interface{}{} attrs := map[string]interface{}{}
for _, field := range m.columnsHasValue("other") { for _, field := range m.columnsHasValue("other") {
s.updateAttrs[field.dbName] = field.Value attrs[field.dbName] = field.Value
} }
s.updateAttrs(attrs)
} }
s.ignoreProtectedAttrs = len(ignore_protected_attrs) > 0 && ignore_protected_attrs[0] s.ignoreProtectedAttrs = len(ignore_protected_attrs) > 0 && ignore_protected_attrs[0]