forked from mirror/gorm
Set table name handler
This commit is contained in:
parent
cbebcf6d6f
commit
b96ca76e59
4
main.go
4
main.go
|
@ -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
|
|
||||||
}
|
|
||||||
|
|
|
@ -13,11 +13,19 @@ import (
|
||||||
|
|
||||||
var modelStructs = map[reflect.Type]*ModelStruct{}
|
var modelStructs = map[reflect.Type]*ModelStruct{}
|
||||||
|
|
||||||
|
var DefaultTableNameHandler = func(db *DB, defaultTableName string) string {
|
||||||
|
return defaultTableName
|
||||||
|
}
|
||||||
|
|
||||||
type ModelStruct struct {
|
type ModelStruct struct {
|
||||||
PrimaryFields []*StructField
|
PrimaryFields []*StructField
|
||||||
StructFields []*StructField
|
StructFields []*StructField
|
||||||
ModelType reflect.Type
|
ModelType reflect.Type
|
||||||
TableName func(*DB) string
|
defaultTableName string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s ModelStruct) TableName(db *DB) string {
|
||||||
|
return DefaultTableNameHandler(db, s.defaultTableName)
|
||||||
}
|
}
|
||||||
|
|
||||||
type StructField struct {
|
type StructField struct {
|
||||||
|
@ -94,14 +102,14 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set tablename
|
// Set tablename
|
||||||
if fm := reflect.New(scopeType).MethodByName("TableName"); fm.IsValid() {
|
type tabler interface {
|
||||||
if results := fm.Call([]reflect.Value{}); len(results) > 0 {
|
TableName() string
|
||||||
if name, ok := results[0].Interface().(string); ok {
|
}
|
||||||
modelStruct.TableName = func(*DB) string {
|
|
||||||
return name
|
if tabler, ok := reflect.New(scopeType).Interface().(interface {
|
||||||
}
|
TableName() string
|
||||||
}
|
}); ok {
|
||||||
}
|
modelStruct.defaultTableName = tabler.TableName()
|
||||||
} else {
|
} else {
|
||||||
name := ToDBName(scopeType.Name())
|
name := ToDBName(scopeType.Name())
|
||||||
if scope.db == nil || !scope.db.parent.singularTable {
|
if scope.db == nil || !scope.db.parent.singularTable {
|
||||||
|
@ -112,9 +120,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
modelStruct.TableName = func(*DB) string {
|
modelStruct.defaultTableName = name
|
||||||
return name
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get all fields
|
// Get all fields
|
||||||
|
|
7
scope.go
7
scope.go
|
@ -251,12 +251,7 @@ func (scope *Scope) TableName() string {
|
||||||
return tabler.TableName(scope.db)
|
return tabler.TableName(scope.db)
|
||||||
}
|
}
|
||||||
|
|
||||||
if scope.GetModelStruct().TableName != nil {
|
return scope.GetModelStruct().TableName(scope.db)
|
||||||
return scope.GetModelStruct().TableName(scope.db)
|
|
||||||
}
|
|
||||||
|
|
||||||
scope.Err(errors.New("wrong table name"))
|
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) QuotedTableName() (name string) {
|
func (scope *Scope) QuotedTableName() (name string) {
|
||||||
|
|
Loading…
Reference in New Issue