From ca6b074dc6b62b0e502df1e5bb338b477f1cb4a6 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sat, 16 Nov 2013 13:28:22 +0800 Subject: [PATCH] Refact do --- do.go | 11 ++++++++--- field.go | 4 ++-- gorm_test.go | 2 +- private.go | 7 ++++++- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/do.go b/do.go index d7518487..72c9f0a1 100644 --- a/do.go +++ b/do.go @@ -5,6 +5,7 @@ import ( "database/sql/driver" "errors" "fmt" + "github.com/jinzhu/gorm/dialect" "reflect" "regexp" "strconv" @@ -37,6 +38,10 @@ func (s *Do) table() string { return s.tableName } +func (s *Do) dialect() dialect.Dialect { + return s.db.parent.dialect +} + func (s *Do) err(err error) error { if err != nil { s.db.err(err) @@ -57,7 +62,7 @@ func (s *Do) setModel(value interface{}) *Do { func (s *Do) addToVars(value interface{}) string { s.sqlVars = append(s.sqlVars, value) - return fmt.Sprintf(s.db.dialect.BinVar(), len(s.sqlVars)) + return fmt.Sprintf(s.dialect().BinVar(), len(s.sqlVars)) } func (s *Do) trace(t time.Time) { @@ -97,7 +102,7 @@ func (s *Do) prepareCreateSql() { s.table(), strings.Join(columns, ","), strings.Join(sqls, ","), - s.db.dialect.ReturningStr(s.model.primaryKeyDb()), + s.dialect().ReturningStr(s.model.primaryKeyDb()), ) return } @@ -173,7 +178,7 @@ func (s *Do) create() (i interface{}) { var id interface{} now := time.Now() - if s.db.dialect.SupportLastInsertId() { + if s.dialect().SupportLastInsertId() { if sql_result, err := s.db.db.Exec(s.sql, s.sqlVars...); s.err(err) == nil { id, err = sql_result.LastInsertId() s.err(err) diff --git a/field.go b/field.go index 3624c45c..f5190c39 100644 --- a/field.go +++ b/field.go @@ -61,9 +61,9 @@ func (f *Field) sqlTag() (str string) { if len(typ) == 0 { if f.isPrimaryKey { - typ = f.model.do.db.dialect.PrimaryKeyTag(value, size) + typ = f.model.do.dialect().PrimaryKeyTag(value, size) } else { - typ = f.model.do.db.dialect.SqlTag(value, size) + typ = f.model.do.dialect().SqlTag(value, size) } } diff --git a/gorm_test.go b/gorm_test.go index a5807af8..04582fab 100644 --- a/gorm_test.go +++ b/gorm_test.go @@ -147,7 +147,7 @@ func init() { func TestSaveAndFind(t *testing.T) { name := "save_and_find" u := &User{Name: name, Age: 1} - db.Save(u) + db.Debug().Save(u) if u.Id == 0 { t.Errorf("Should have ID after create record") } diff --git a/private.go b/private.go index 82248ad0..b46c8ab8 100644 --- a/private.go +++ b/private.go @@ -6,7 +6,12 @@ import ( ) func (s *DB) clone() *DB { - db := &DB{db: s.db, parent: s.parent, search: s.parent.search.clone()} + db := &DB{db: s.db, parent: s.parent} + if s.parent.search == nil { + db.search = &search{} + } else { + db.search = s.parent.search.clone() + } db.search.db = db return db }