gorm/schema/naming.go

141 lines
3.7 KiB
Go
Raw Normal View History

2020-01-31 09:17:02 +03:00
package schema
import (
2020-02-21 18:51:38 +03:00
"crypto/sha1"
2020-01-31 09:17:02 +03:00
"fmt"
"strings"
"sync"
2020-02-21 18:51:38 +03:00
"unicode/utf8"
2020-01-31 09:17:02 +03:00
2020-06-02 04:25:55 +03:00
"github.com/jinzhu/inflection"
2020-01-31 09:17:02 +03:00
)
// Namer namer interface
type Namer interface {
TableName(table string) string
ColumnName(table, column string) string
JoinTableName(joinTable string) string
2020-02-22 06:15:51 +03:00
RelationshipFKName(Relationship) string
CheckerName(table, column string) string
IndexName(table, column string) string
2020-01-31 09:17:02 +03:00
}
// NamingStrategy tables, columns naming strategy
type NamingStrategy struct {
TablePrefix string
SingularTable bool
}
// TableName convert string to table name
func (ns NamingStrategy) TableName(str string) string {
if ns.SingularTable {
return ns.TablePrefix + toDBName(str)
}
return ns.TablePrefix + inflection.Plural(toDBName(str))
}
// ColumnName convert string to column name
2020-02-21 18:51:38 +03:00
func (ns NamingStrategy) ColumnName(table, column string) string {
return toDBName(column)
}
2020-02-22 06:15:51 +03:00
// JoinTableName convert string to join table name
func (ns NamingStrategy) JoinTableName(str string) string {
2020-09-06 05:51:21 +03:00
if strings.ToLower(str) == str {
2020-09-28 05:55:27 +03:00
return ns.TablePrefix + str
2020-09-06 05:51:21 +03:00
}
2020-06-30 02:29:15 +03:00
if ns.SingularTable {
return ns.TablePrefix + toDBName(str)
}
2020-02-22 06:15:51 +03:00
return ns.TablePrefix + inflection.Plural(toDBName(str))
}
// RelationshipFKName generate fk name for relation
func (ns NamingStrategy) RelationshipFKName(rel Relationship) string {
2020-09-28 05:55:27 +03:00
return strings.Replace(fmt.Sprintf("fk_%s_%s", rel.Schema.Table, toDBName(rel.Name)), ".", "_", -1)
2020-02-22 06:15:51 +03:00
}
// CheckerName generate checker name
func (ns NamingStrategy) CheckerName(table, column string) string {
2020-09-28 05:55:27 +03:00
return strings.Replace(fmt.Sprintf("chk_%s_%s", table, column), ".", "_", -1)
2020-02-22 06:15:51 +03:00
}
// IndexName generate index name
2020-02-21 18:51:38 +03:00
func (ns NamingStrategy) IndexName(table, column string) string {
idxName := fmt.Sprintf("idx_%v_%v", table, toDBName(column))
2020-09-28 05:55:27 +03:00
idxName = strings.Replace(idxName, ".", "_", -1)
2020-02-21 18:51:38 +03:00
if utf8.RuneCountInString(idxName) > 64 {
h := sha1.New()
h.Write([]byte(idxName))
bs := h.Sum(nil)
idxName = fmt.Sprintf("idx%v%v", table, column)[0:56] + string(bs)[:8]
}
return idxName
2020-01-31 09:17:02 +03:00
}
var (
smap sync.Map
// https://github.com/golang/lint/blob/master/lint.go#L770
commonInitialisms = []string{"API", "ASCII", "CPU", "CSS", "DNS", "EOF", "GUID", "HTML", "HTTP", "HTTPS", "ID", "IP", "JSON", "LHS", "QPS", "RAM", "RHS", "RPC", "SLA", "SMTP", "SSH", "TLS", "TTL", "UID", "UI", "UUID", "URI", "URL", "UTF8", "VM", "XML", "XSRF", "XSS"}
commonInitialismsReplacer *strings.Replacer
)
func init() {
var commonInitialismsForReplacer []string
for _, initialism := range commonInitialisms {
commonInitialismsForReplacer = append(commonInitialismsForReplacer, initialism, strings.Title(strings.ToLower(initialism)))
}
commonInitialismsReplacer = strings.NewReplacer(commonInitialismsForReplacer...)
}
func toDBName(name string) string {
if name == "" {
return ""
} else if v, ok := smap.Load(name); ok {
return v.(string)
2020-01-31 09:17:02 +03:00
}
var (
value = commonInitialismsReplacer.Replace(name)
buf strings.Builder
lastCase, nextCase, nextNumber bool // upper case == true
curCase = value[0] <= 'Z' && value[0] >= 'A'
)
for i, v := range value[:len(value)-1] {
nextCase = value[i+1] <= 'Z' && value[i+1] >= 'A'
nextNumber = value[i+1] >= '0' && value[i+1] <= '9'
if curCase {
if lastCase && (nextCase || nextNumber) {
buf.WriteRune(v + 32)
} else {
if i > 0 && value[i-1] != '_' && value[i+1] != '_' {
buf.WriteByte('_')
}
buf.WriteRune(v + 32)
}
} else {
buf.WriteRune(v)
}
lastCase = curCase
curCase = nextCase
}
if curCase {
if !lastCase && len(value) > 1 {
buf.WriteByte('_')
}
buf.WriteByte(value[len(value)-1] + 32)
} else {
buf.WriteByte(value[len(value)-1])
}
ret := buf.String()
smap.Store(name, ret)
return ret
2020-01-31 09:17:02 +03:00
}