diff --git a/chain.go b/chain.go index b6b14ab2..88a19dc3 100644 --- a/chain.go +++ b/chain.go @@ -9,11 +9,10 @@ import ( ) type Chain struct { - db *sql.DB - driver string - debug bool - singularTableName bool - value interface{} + db *sql.DB + driver string + debug bool + value interface{} Errors []error Error error @@ -69,7 +68,6 @@ func (s *Chain) do(value interface{}) *Do { do.limitStr = s.limitStr do.specifiedTableName = s.specifiedTableName do.unscoped = s.unscoped - do.singularTableName = s.singularTableName do.debug = s.debug s.value = value diff --git a/do.go b/do.go index b2f4afd8..5529b80c 100644 --- a/do.go +++ b/do.go @@ -37,13 +37,12 @@ type Do struct { unscoped bool updateAttrs map[string]interface{} ignoreProtectedAttrs bool - singularTableName bool } func (s *Do) tableName() string { if s.specifiedTableName == "" { var err error - s.guessedTableName, err = s.model.tableName(s.singularTableName) + s.guessedTableName, err = s.model.tableName() s.err(err) return s.guessedTableName } else { diff --git a/gorm_test.go b/gorm_test.go index cc643b8e..7dab681f 100644 --- a/gorm_test.go +++ b/gorm_test.go @@ -1218,27 +1218,29 @@ func TestTableName(t *testing.T) { var table string model := &Model{data: Order{}} - table, _ = model.tableName(false) + table, _ = model.tableName() if table != "orders" { t.Errorf("Order table name should be orders") } - table, _ = model.tableName(true) + db.SingularTable(true) + table, _ = model.tableName() if table != "order" { t.Errorf("Order's singular table name should be order") } model2 := &Model{data: Cart{}} - table, _ = model2.tableName(false) + table, _ = model2.tableName() if table != "shopping_cart" { t.Errorf("Cart's singular table name should be shopping_cart") } model3 := &Model{data: &Cart{}} - table, _ = model3.tableName(false) + table, _ = model3.tableName() if table != "shopping_cart" { t.Errorf("Cart's singular table name should be shopping_cart") } + db.SingularTable(false) } type BigEmail struct { diff --git a/main.go b/main.go index 55efbf0e..daa28535 100644 --- a/main.go +++ b/main.go @@ -2,11 +2,12 @@ package gorm import "database/sql" +var singularTableName bool + type DB struct { - db *sql.DB - driver string - DebugMode bool - SingularTableName bool + db *sql.DB + driver string + DebugMode bool } func Open(driver, source string) (db DB, err error) { @@ -20,11 +21,11 @@ func (s *DB) SetPool(n int) { } func (s *DB) SingularTable(result bool) { - s.SingularTableName = result + singularTableName = result } func (s *DB) buildChain() *Chain { - return &Chain{db: s.db, driver: s.driver, debug: s.DebugMode, singularTableName: s.SingularTableName} + return &Chain{db: s.db, driver: s.driver, debug: s.DebugMode} } func (s *DB) Where(querystring interface{}, args ...interface{}) *Chain { diff --git a/model.go b/model.go index 653f227e..c84d3bb1 100644 --- a/model.go +++ b/model.go @@ -278,7 +278,7 @@ func (m *Model) typeName() string { return typ.Name() } -func (m *Model) tableName(singularTableName bool) (str string, err error) { +func (m *Model) tableName() (str string, err error) { if m.data == nil { err = errors.New("Model haven't been set") return