Set table name handler

This commit is contained in:
Jinzhu 2015-05-27 12:19:48 +08:00
parent cbebcf6d6f
commit b96ca76e59
3 changed files with 22 additions and 25 deletions

View File

@ -485,7 +485,3 @@ func (s *DB) SetJoinTableHandler(source interface{}, column string, handler Join
}
}
}
func (s *DB) SetTableNameHandler(source interface{}, handler func(*DB) string) {
s.NewScope(source).GetModelStruct().TableName = handler
}

View File

@ -13,11 +13,19 @@ import (
var modelStructs = map[reflect.Type]*ModelStruct{}
var DefaultTableNameHandler = func(db *DB, defaultTableName string) string {
return defaultTableName
}
type ModelStruct struct {
PrimaryFields []*StructField
StructFields []*StructField
ModelType reflect.Type
TableName func(*DB) string
PrimaryFields []*StructField
StructFields []*StructField
ModelType reflect.Type
defaultTableName string
}
func (s ModelStruct) TableName(db *DB) string {
return DefaultTableNameHandler(db, s.defaultTableName)
}
type StructField struct {
@ -94,14 +102,14 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
}
// Set tablename
if fm := reflect.New(scopeType).MethodByName("TableName"); fm.IsValid() {
if results := fm.Call([]reflect.Value{}); len(results) > 0 {
if name, ok := results[0].Interface().(string); ok {
modelStruct.TableName = func(*DB) string {
return name
}
}
}
type tabler interface {
TableName() string
}
if tabler, ok := reflect.New(scopeType).Interface().(interface {
TableName() string
}); ok {
modelStruct.defaultTableName = tabler.TableName()
} else {
name := ToDBName(scopeType.Name())
if scope.db == nil || !scope.db.parent.singularTable {
@ -112,9 +120,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
}
}
modelStruct.TableName = func(*DB) string {
return name
}
modelStruct.defaultTableName = name
}
// Get all fields

View File

@ -251,12 +251,7 @@ func (scope *Scope) TableName() string {
return tabler.TableName(scope.db)
}
if scope.GetModelStruct().TableName != nil {
return scope.GetModelStruct().TableName(scope.db)
}
scope.Err(errors.New("wrong table name"))
return ""
return scope.GetModelStruct().TableName(scope.db)
}
func (scope *Scope) QuotedTableName() (name string) {