mirror of https://github.com/go-gorm/gorm.git
Refactor NamingStrategy, close #3540
This commit is contained in:
parent
9eec6ae066
commit
a2faa41cbe
|
@ -42,7 +42,7 @@ func (ns NamingStrategy) ColumnName(table, column string) string {
|
||||||
// JoinTableName convert string to join table name
|
// JoinTableName convert string to join table name
|
||||||
func (ns NamingStrategy) JoinTableName(str string) string {
|
func (ns NamingStrategy) JoinTableName(str string) string {
|
||||||
if strings.ToLower(str) == str {
|
if strings.ToLower(str) == str {
|
||||||
return str
|
return ns.TablePrefix + str
|
||||||
}
|
}
|
||||||
|
|
||||||
if ns.SingularTable {
|
if ns.SingularTable {
|
||||||
|
@ -53,17 +53,18 @@ func (ns NamingStrategy) JoinTableName(str string) string {
|
||||||
|
|
||||||
// RelationshipFKName generate fk name for relation
|
// RelationshipFKName generate fk name for relation
|
||||||
func (ns NamingStrategy) RelationshipFKName(rel Relationship) string {
|
func (ns NamingStrategy) RelationshipFKName(rel Relationship) string {
|
||||||
return fmt.Sprintf("fk_%s_%s", rel.Schema.Table, toDBName(rel.Name))
|
return strings.Replace(fmt.Sprintf("fk_%s_%s", rel.Schema.Table, toDBName(rel.Name)), ".", "_", -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckerName generate checker name
|
// CheckerName generate checker name
|
||||||
func (ns NamingStrategy) CheckerName(table, column string) string {
|
func (ns NamingStrategy) CheckerName(table, column string) string {
|
||||||
return fmt.Sprintf("chk_%s_%s", table, column)
|
return strings.Replace(fmt.Sprintf("chk_%s_%s", table, column), ".", "_", -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IndexName generate index name
|
// IndexName generate index name
|
||||||
func (ns NamingStrategy) IndexName(table, column string) string {
|
func (ns NamingStrategy) IndexName(table, column string) string {
|
||||||
idxName := fmt.Sprintf("idx_%v_%v", table, toDBName(column))
|
idxName := fmt.Sprintf("idx_%v_%v", table, toDBName(column))
|
||||||
|
idxName = strings.Replace(idxName, ".", "_", -1)
|
||||||
|
|
||||||
if utf8.RuneCountInString(idxName) > 64 {
|
if utf8.RuneCountInString(idxName) > 64 {
|
||||||
h := sha1.New()
|
h := sha1.New()
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package schema
|
package schema
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -34,27 +33,34 @@ func TestToDBName(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type NewNamingStrategy struct {
|
func TestNamingStrategy(t *testing.T) {
|
||||||
NamingStrategy
|
var ns = NamingStrategy{
|
||||||
|
TablePrefix: "public.",
|
||||||
|
SingularTable: true,
|
||||||
|
}
|
||||||
|
idxName := ns.IndexName("public.table", "name")
|
||||||
|
|
||||||
|
if idxName != "idx_public_table_name" {
|
||||||
|
t.Errorf("invalid index name generated, got %v", idxName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ns NewNamingStrategy) ColumnName(table, column string) string {
|
chkName := ns.CheckerName("public.table", "name")
|
||||||
baseColumnName := ns.NamingStrategy.ColumnName(table, column)
|
if chkName != "chk_public_table_name" {
|
||||||
|
t.Errorf("invalid checker name generated, got %v", chkName)
|
||||||
if table == "" {
|
|
||||||
return baseColumnName
|
|
||||||
}
|
}
|
||||||
|
|
||||||
s := strings.Split(table, "_")
|
joinTable := ns.JoinTableName("user_languages")
|
||||||
|
if joinTable != "public.user_languages" {
|
||||||
|
t.Errorf("invalid join table generated, got %v", joinTable)
|
||||||
|
}
|
||||||
|
|
||||||
var prefix string
|
joinTable2 := ns.JoinTableName("UserLanguage")
|
||||||
switch len(s) {
|
if joinTable2 != "public.user_language" {
|
||||||
case 1:
|
t.Errorf("invalid join table generated, got %v", joinTable2)
|
||||||
prefix = s[0][:3]
|
}
|
||||||
case 2:
|
|
||||||
prefix = s[0][:1] + s[1][:2]
|
tableName := ns.TableName("Company")
|
||||||
default:
|
if tableName != "public.company" {
|
||||||
prefix = s[0][:1] + s[1][:1] + s[2][:1]
|
t.Errorf("invalid table name generated, got %v", tableName)
|
||||||
}
|
}
|
||||||
return prefix + "_" + baseColumnName
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue