forked from mirror/gorm
Support NameReplace for NamingStrategy, close #3779
This commit is contained in:
parent
6186a4daa7
commit
66e8a72bf1
|
@ -24,19 +24,20 @@ type Namer interface {
|
|||
type NamingStrategy struct {
|
||||
TablePrefix string
|
||||
SingularTable bool
|
||||
NameReplacer *strings.Replacer
|
||||
}
|
||||
|
||||
// TableName convert string to table name
|
||||
func (ns NamingStrategy) TableName(str string) string {
|
||||
if ns.SingularTable {
|
||||
return ns.TablePrefix + toDBName(str)
|
||||
return ns.TablePrefix + ns.toDBName(str)
|
||||
}
|
||||
return ns.TablePrefix + inflection.Plural(toDBName(str))
|
||||
return ns.TablePrefix + inflection.Plural(ns.toDBName(str))
|
||||
}
|
||||
|
||||
// ColumnName convert string to column name
|
||||
func (ns NamingStrategy) ColumnName(table, column string) string {
|
||||
return toDBName(column)
|
||||
return ns.toDBName(column)
|
||||
}
|
||||
|
||||
// JoinTableName convert string to join table name
|
||||
|
@ -46,14 +47,14 @@ func (ns NamingStrategy) JoinTableName(str string) string {
|
|||
}
|
||||
|
||||
if ns.SingularTable {
|
||||
return ns.TablePrefix + toDBName(str)
|
||||
return ns.TablePrefix + ns.toDBName(str)
|
||||
}
|
||||
return ns.TablePrefix + inflection.Plural(toDBName(str))
|
||||
return ns.TablePrefix + inflection.Plural(ns.toDBName(str))
|
||||
}
|
||||
|
||||
// RelationshipFKName generate fk name for relation
|
||||
func (ns NamingStrategy) RelationshipFKName(rel Relationship) string {
|
||||
return strings.Replace(fmt.Sprintf("fk_%s_%s", rel.Schema.Table, toDBName(rel.Name)), ".", "_", -1)
|
||||
return strings.Replace(fmt.Sprintf("fk_%s_%s", rel.Schema.Table, ns.toDBName(rel.Name)), ".", "_", -1)
|
||||
}
|
||||
|
||||
// CheckerName generate checker name
|
||||
|
@ -63,7 +64,7 @@ func (ns NamingStrategy) CheckerName(table, column string) string {
|
|||
|
||||
// IndexName generate index name
|
||||
func (ns NamingStrategy) IndexName(table, column string) string {
|
||||
idxName := fmt.Sprintf("idx_%v_%v", table, toDBName(column))
|
||||
idxName := fmt.Sprintf("idx_%v_%v", table, ns.toDBName(column))
|
||||
idxName = strings.Replace(idxName, ".", "_", -1)
|
||||
|
||||
if utf8.RuneCountInString(idxName) > 64 {
|
||||
|
@ -91,13 +92,17 @@ func init() {
|
|||
commonInitialismsReplacer = strings.NewReplacer(commonInitialismsForReplacer...)
|
||||
}
|
||||
|
||||
func toDBName(name string) string {
|
||||
func (ns NamingStrategy) toDBName(name string) string {
|
||||
if name == "" {
|
||||
return ""
|
||||
} else if v, ok := smap.Load(name); ok {
|
||||
return v.(string)
|
||||
}
|
||||
|
||||
if ns.NameReplacer != nil {
|
||||
name = ns.NameReplacer.Replace(name)
|
||||
}
|
||||
|
||||
var (
|
||||
value = commonInitialismsReplacer.Replace(name)
|
||||
buf strings.Builder
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package schema
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
@ -26,9 +27,10 @@ func TestToDBName(t *testing.T) {
|
|||
"ThisIsActuallyATestSoWeMayBeAbleToUseThisCodeInGormPackageAlsoIdCanBeUsedAtTheEndAsID": "this_is_actually_a_test_so_we_may_be_able_to_use_this_code_in_gorm_package_also_id_can_be_used_at_the_end_as_id",
|
||||
}
|
||||
|
||||
ns := NamingStrategy{}
|
||||
for key, value := range maps {
|
||||
if toDBName(key) != value {
|
||||
t.Errorf("%v toName should equal %v, but got %v", key, value, toDBName(key))
|
||||
if ns.toDBName(key) != value {
|
||||
t.Errorf("%v toName should equal %v, but got %v", key, value, ns.toDBName(key))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -37,6 +39,7 @@ func TestNamingStrategy(t *testing.T) {
|
|||
var ns = NamingStrategy{
|
||||
TablePrefix: "public.",
|
||||
SingularTable: true,
|
||||
NameReplacer: strings.NewReplacer("CID", "Cid"),
|
||||
}
|
||||
idxName := ns.IndexName("public.table", "name")
|
||||
|
||||
|
@ -63,4 +66,9 @@ func TestNamingStrategy(t *testing.T) {
|
|||
if tableName != "public.company" {
|
||||
t.Errorf("invalid table name generated, got %v", tableName)
|
||||
}
|
||||
|
||||
columdName := ns.ColumnName("", "NameCID")
|
||||
if columdName != "name_cid" {
|
||||
t.Errorf("invalid column name generated, got %v", columdName)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue