name_strategy.go 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. package olap
  2. import (
  3. "regexp"
  4. "strings"
  5. "github.com/jinzhu/inflection"
  6. "gorm.io/gorm/schema"
  7. )
  8. type ChNamingStrategy struct {
  9. schema.NamingStrategy
  10. }
  11. func (ns ChNamingStrategy) SchemaName(table string) string {
  12. table = strings.TrimPrefix(table, ns.TablePrefix)
  13. if ns.SingularTable {
  14. return ns.toSchemaName(table)
  15. }
  16. return ns.toSchemaName(inflection.Singular(table))
  17. }
  18. var 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"}
  19. func (ns ChNamingStrategy) toSchemaName(name string) string {
  20. name = strings.ReplaceAll(strings.ReplaceAll(name, "_", " "), ".", " ")
  21. result := strings.ReplaceAll(strings.Title(name), " ", "")
  22. for _, initialism := range commonInitialisms {
  23. result = regexp.MustCompile(strings.Title(strings.ToLower(initialism))+"([A-Z]|$|_)").ReplaceAllString(result, initialism+"$1")
  24. }
  25. return result
  26. }