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

25
do.go
View File

@ -17,6 +17,9 @@ type Do struct {
model *Model model *Model
tableName string tableName string
value interface{} value interface{}
update_attrs map[string]interface{}
hasUpdate bool
ignoreProtectedAttrs bool
sql string sql string
sqlVars []interface{} sqlVars []interface{}
startedTransaction bool startedTransaction bool
@ -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]