Skip to content

Commit

Permalink
Refact do
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Nov 16, 2013
1 parent c3d2746 commit ca6b074
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
11 changes: 8 additions & 3 deletions do.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql/driver"
"errors"
"fmt"
"github.com/jinzhu/gorm/dialect"
"reflect"
"regexp"
"strconv"
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion gorm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
7 changes: 6 additions & 1 deletion private.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit ca6b074

Please sign in to comment.