123456789101112131415161718192021222324252627282930313233 |
- package olap
- import (
- "regexp"
- "strings"
- "github.com/jinzhu/inflection"
- "gorm.io/gorm/schema"
- )
- type ChNamingStrategy struct {
- schema.NamingStrategy
- }
- func (ns ChNamingStrategy) SchemaName(table string) string {
- table = strings.TrimPrefix(table, ns.TablePrefix)
- if ns.SingularTable {
- return ns.toSchemaName(table)
- }
- return ns.toSchemaName(inflection.Singular(table))
- }
- 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"}
- func (ns ChNamingStrategy) toSchemaName(name string) string {
- name = strings.ReplaceAll(strings.ReplaceAll(name, "_", " "), ".", " ")
- result := strings.ReplaceAll(strings.Title(name), " ", "")
- for _, initialism := range commonInitialisms {
- result = regexp.MustCompile(strings.Title(strings.ToLower(initialism))+"([A-Z]|$|_)").ReplaceAllString(result, initialism+"$1")
- }
- return result
- }
|