From cb53b5ed852397937a4a59247cfb18e6f2285cd8 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sat, 16 Nov 2013 12:45:29 +0800 Subject: [PATCH] Refact do.go --- do.go | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/do.go b/do.go index f17037ca..cfc8a181 100644 --- a/do.go +++ b/do.go @@ -13,13 +13,16 @@ import ( ) type Do struct { - db *DB - model *Model - tableName string - value interface{} - sql string - sqlVars []interface{} - startedTransaction bool + db *DB + model *Model + tableName string + value interface{} + update_attrs map[string]interface{} + hasUpdate bool + ignoreProtectedAttrs bool + sql string + sqlVars []interface{} + startedTransaction bool } func (s *Do) table() string { @@ -187,20 +190,28 @@ func (s *Do) create() (i interface{}) { return } -func (s *Do) setUpdateAttrs(values interface{}, ignore_protected_attrs ...bool) *Do { - switch vs := values.(type) { +func (s *Do) updateAttrs(values interface{}, ignore_protected_attrs ...bool) *Do { + switch value := values.(type) { 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{}: - for _, value := range vs { - s.setUpdateAttrs(value, ignore_protected_attrs...) + for _, v := range value { + s.updateAttrs(v) } case interface{}: m := &Model{data: values, do: s} - s.updateAttrs = map[string]interface{}{} + attrs := map[string]interface{}{} 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]