forked from mirror/gorm
Remove unnecessary field
This commit is contained in:
parent
f82d036f14
commit
0cb1c1ba32
2
chain.go
2
chain.go
|
@ -12,7 +12,6 @@ type Chain struct {
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
driver string
|
driver string
|
||||||
debug bool
|
debug bool
|
||||||
singularTableName bool
|
|
||||||
value interface{}
|
value interface{}
|
||||||
|
|
||||||
Errors []error
|
Errors []error
|
||||||
|
@ -69,7 +68,6 @@ func (s *Chain) do(value interface{}) *Do {
|
||||||
do.limitStr = s.limitStr
|
do.limitStr = s.limitStr
|
||||||
do.specifiedTableName = s.specifiedTableName
|
do.specifiedTableName = s.specifiedTableName
|
||||||
do.unscoped = s.unscoped
|
do.unscoped = s.unscoped
|
||||||
do.singularTableName = s.singularTableName
|
|
||||||
do.debug = s.debug
|
do.debug = s.debug
|
||||||
|
|
||||||
s.value = value
|
s.value = value
|
||||||
|
|
3
do.go
3
do.go
|
@ -37,13 +37,12 @@ type Do struct {
|
||||||
unscoped bool
|
unscoped bool
|
||||||
updateAttrs map[string]interface{}
|
updateAttrs map[string]interface{}
|
||||||
ignoreProtectedAttrs bool
|
ignoreProtectedAttrs bool
|
||||||
singularTableName bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Do) tableName() string {
|
func (s *Do) tableName() string {
|
||||||
if s.specifiedTableName == "" {
|
if s.specifiedTableName == "" {
|
||||||
var err error
|
var err error
|
||||||
s.guessedTableName, err = s.model.tableName(s.singularTableName)
|
s.guessedTableName, err = s.model.tableName()
|
||||||
s.err(err)
|
s.err(err)
|
||||||
return s.guessedTableName
|
return s.guessedTableName
|
||||||
} else {
|
} else {
|
||||||
|
|
10
gorm_test.go
10
gorm_test.go
|
@ -1218,27 +1218,29 @@ func TestTableName(t *testing.T) {
|
||||||
var table string
|
var table string
|
||||||
|
|
||||||
model := &Model{data: Order{}}
|
model := &Model{data: Order{}}
|
||||||
table, _ = model.tableName(false)
|
table, _ = model.tableName()
|
||||||
if table != "orders" {
|
if table != "orders" {
|
||||||
t.Errorf("Order table name should be orders")
|
t.Errorf("Order table name should be orders")
|
||||||
}
|
}
|
||||||
|
|
||||||
table, _ = model.tableName(true)
|
db.SingularTable(true)
|
||||||
|
table, _ = model.tableName()
|
||||||
if table != "order" {
|
if table != "order" {
|
||||||
t.Errorf("Order's singular table name should be order")
|
t.Errorf("Order's singular table name should be order")
|
||||||
}
|
}
|
||||||
|
|
||||||
model2 := &Model{data: Cart{}}
|
model2 := &Model{data: Cart{}}
|
||||||
table, _ = model2.tableName(false)
|
table, _ = model2.tableName()
|
||||||
if table != "shopping_cart" {
|
if table != "shopping_cart" {
|
||||||
t.Errorf("Cart's singular table name should be shopping_cart")
|
t.Errorf("Cart's singular table name should be shopping_cart")
|
||||||
}
|
}
|
||||||
|
|
||||||
model3 := &Model{data: &Cart{}}
|
model3 := &Model{data: &Cart{}}
|
||||||
table, _ = model3.tableName(false)
|
table, _ = model3.tableName()
|
||||||
if table != "shopping_cart" {
|
if table != "shopping_cart" {
|
||||||
t.Errorf("Cart's singular table name should be shopping_cart")
|
t.Errorf("Cart's singular table name should be shopping_cart")
|
||||||
}
|
}
|
||||||
|
db.SingularTable(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
type BigEmail struct {
|
type BigEmail struct {
|
||||||
|
|
7
main.go
7
main.go
|
@ -2,11 +2,12 @@ package gorm
|
||||||
|
|
||||||
import "database/sql"
|
import "database/sql"
|
||||||
|
|
||||||
|
var singularTableName bool
|
||||||
|
|
||||||
type DB struct {
|
type DB struct {
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
driver string
|
driver string
|
||||||
DebugMode bool
|
DebugMode bool
|
||||||
SingularTableName bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Open(driver, source string) (db DB, err error) {
|
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) {
|
func (s *DB) SingularTable(result bool) {
|
||||||
s.SingularTableName = result
|
singularTableName = result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) buildChain() *Chain {
|
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 {
|
func (s *DB) Where(querystring interface{}, args ...interface{}) *Chain {
|
||||||
|
|
2
model.go
2
model.go
|
@ -278,7 +278,7 @@ func (m *Model) typeName() string {
|
||||||
return typ.Name()
|
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 {
|
if m.data == nil {
|
||||||
err = errors.New("Model haven't been set")
|
err = errors.New("Model haven't been set")
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue