2013-10-25 14:04:48 +04:00
|
|
|
package gorm
|
2013-10-26 05:49:40 +04:00
|
|
|
|
|
|
|
import (
|
2013-10-26 10:23:02 +04:00
|
|
|
"bytes"
|
2016-01-16 07:18:04 +03:00
|
|
|
"fmt"
|
2016-01-18 07:20:27 +03:00
|
|
|
"reflect"
|
2013-10-26 05:49:40 +04:00
|
|
|
"strings"
|
2015-08-31 10:07:06 +03:00
|
|
|
"sync"
|
2013-10-26 05:49:40 +04:00
|
|
|
)
|
|
|
|
|
2015-02-18 05:19:34 +03:00
|
|
|
// Copied from golint
|
|
|
|
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", "UI", "UID", "UUID", "URI", "URL", "UTF8", "VM", "XML", "XSRF", "XSS"}
|
|
|
|
var 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...)
|
|
|
|
}
|
|
|
|
|
2015-08-31 10:07:06 +03:00
|
|
|
type safeMap struct {
|
|
|
|
m map[string]string
|
|
|
|
l *sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *safeMap) Set(key string, value string) {
|
|
|
|
s.l.Lock()
|
|
|
|
defer s.l.Unlock()
|
|
|
|
s.m[key] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *safeMap) Get(key string) string {
|
|
|
|
s.l.RLock()
|
|
|
|
defer s.l.RUnlock()
|
|
|
|
return s.m[key]
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSafeMap() *safeMap {
|
|
|
|
return &safeMap{l: new(sync.RWMutex), m: make(map[string]string)}
|
|
|
|
}
|
|
|
|
|
|
|
|
var smap = newSafeMap()
|
2013-12-15 06:09:19 +04:00
|
|
|
|
2016-01-15 16:03:35 +03:00
|
|
|
type strCase bool
|
2016-01-10 17:56:22 +03:00
|
|
|
|
|
|
|
const (
|
2016-01-15 16:03:35 +03:00
|
|
|
lower strCase = false
|
|
|
|
upper strCase = true
|
2016-01-10 17:56:22 +03:00
|
|
|
)
|
|
|
|
|
2015-02-18 05:19:34 +03:00
|
|
|
func ToDBName(name string) string {
|
2015-08-31 10:07:06 +03:00
|
|
|
if v := smap.Get(name); v != "" {
|
2013-11-17 04:28:30 +04:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2016-01-16 04:40:13 +03:00
|
|
|
if name == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-01-10 17:56:22 +03:00
|
|
|
var (
|
|
|
|
value = commonInitialismsReplacer.Replace(name)
|
|
|
|
buf = bytes.NewBufferString("")
|
2016-01-15 16:03:35 +03:00
|
|
|
lastCase, currCase, nextCase strCase
|
2016-01-10 17:56:22 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
for i, v := range value[:len(value)-1] {
|
|
|
|
nextCase = value[i+1] >= 'A' && value[i+1] <= 'Z'
|
|
|
|
if i > 0 {
|
|
|
|
if currCase == upper {
|
|
|
|
if lastCase == upper && nextCase == upper {
|
|
|
|
buf.WriteRune(v)
|
|
|
|
} else {
|
|
|
|
buf.WriteRune('_')
|
|
|
|
buf.WriteRune(v)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buf.WriteRune(v)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
currCase = upper
|
|
|
|
buf.WriteRune(v)
|
2013-10-26 10:23:02 +04:00
|
|
|
}
|
2016-01-10 17:56:22 +03:00
|
|
|
lastCase = currCase
|
|
|
|
currCase = nextCase
|
2013-10-26 10:23:02 +04:00
|
|
|
}
|
2013-11-17 04:28:30 +04:00
|
|
|
|
2016-01-10 17:56:22 +03:00
|
|
|
buf.WriteByte(value[len(value)-1])
|
|
|
|
|
|
|
|
s := strings.Replace(strings.ToLower(buf.String()), "__", "_", -1)
|
|
|
|
|
2015-08-31 10:07:06 +03:00
|
|
|
smap.Set(name, s)
|
2013-11-17 04:28:30 +04:00
|
|
|
return s
|
2013-10-26 10:23:02 +04:00
|
|
|
}
|
|
|
|
|
2015-02-24 17:06:35 +03:00
|
|
|
type expr struct {
|
|
|
|
expr string
|
|
|
|
args []interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Expr(expression string, args ...interface{}) *expr {
|
|
|
|
return &expr{expr: expression, args: args}
|
2014-07-29 13:52:23 +04:00
|
|
|
}
|
2016-01-16 07:18:04 +03:00
|
|
|
|
2016-01-18 07:20:27 +03:00
|
|
|
func indirect(reflectValue reflect.Value) reflect.Value {
|
|
|
|
for reflectValue.Kind() == reflect.Ptr {
|
|
|
|
reflectValue = reflectValue.Elem()
|
|
|
|
}
|
|
|
|
return reflectValue
|
|
|
|
}
|
|
|
|
|
2016-01-16 07:18:04 +03:00
|
|
|
func toQueryMarks(primaryValues [][]interface{}) string {
|
|
|
|
var results []string
|
|
|
|
|
|
|
|
for _, primaryValue := range primaryValues {
|
|
|
|
var marks []string
|
|
|
|
for _ = range primaryValue {
|
|
|
|
marks = append(marks, "?")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(marks) > 1 {
|
|
|
|
results = append(results, fmt.Sprintf("(%v)", strings.Join(marks, ",")))
|
|
|
|
} else {
|
|
|
|
results = append(results, strings.Join(marks, ""))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Join(results, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
func toQueryCondition(scope *Scope, columns []string) string {
|
|
|
|
var newColumns []string
|
|
|
|
for _, column := range columns {
|
|
|
|
newColumns = append(newColumns, scope.Quote(column))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(columns) > 1 {
|
|
|
|
return fmt.Sprintf("(%v)", strings.Join(newColumns, ","))
|
|
|
|
}
|
|
|
|
return strings.Join(newColumns, ",")
|
|
|
|
}
|
|
|
|
|
2016-01-17 12:46:56 +03:00
|
|
|
func toQueryValues(values [][]interface{}) (results []interface{}) {
|
|
|
|
for _, value := range values {
|
|
|
|
for _, v := range value {
|
|
|
|
results = append(results, v)
|
2016-01-16 07:18:04 +03:00
|
|
|
}
|
|
|
|
}
|
2016-01-17 12:46:56 +03:00
|
|
|
return
|
2016-01-16 07:18:04 +03:00
|
|
|
}
|