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"
|
2013-10-26 05:49:40 +04:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2013-10-26 10:40:37 +04:00
|
|
|
"regexp"
|
2013-10-26 05:49:40 +04:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func valuesToBinVar(values []interface{}) string {
|
|
|
|
var sqls []string
|
|
|
|
for index, _ := range values {
|
|
|
|
sqls = append(sqls, fmt.Sprintf("$%d", index+1))
|
|
|
|
}
|
|
|
|
return strings.Join(sqls, ",")
|
|
|
|
}
|
2013-10-26 06:06:57 +04:00
|
|
|
|
|
|
|
func quoteMap(values []string) (results []string) {
|
|
|
|
for _, value := range values {
|
|
|
|
results = append(results, "\""+value+"\"")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2013-10-26 10:40:37 +04:00
|
|
|
|
2013-10-26 10:23:02 +04:00
|
|
|
func toSnake(s string) string {
|
|
|
|
buf := bytes.NewBufferString("")
|
|
|
|
for i, v := range s {
|
|
|
|
if i > 0 && v >= 'A' && v <= 'Z' {
|
|
|
|
buf.WriteRune('_')
|
|
|
|
}
|
|
|
|
buf.WriteRune(v)
|
|
|
|
}
|
|
|
|
return strings.ToLower(buf.String())
|
|
|
|
}
|
|
|
|
|
2013-10-26 10:40:37 +04:00
|
|
|
func snakeToUpperCamel(s string) string {
|
|
|
|
buf := bytes.NewBufferString("")
|
|
|
|
for _, v := range strings.Split(s, "_") {
|
|
|
|
if len(v) > 0 {
|
|
|
|
buf.WriteString(strings.ToUpper(v[:1]))
|
|
|
|
buf.WriteString(v[1:])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func interfaceToTableName(f interface{}) string {
|
2013-10-26 10:23:02 +04:00
|
|
|
t := reflect.TypeOf(f)
|
|
|
|
for {
|
|
|
|
c := false
|
|
|
|
switch t.Kind() {
|
|
|
|
case reflect.Array, reflect.Chan, reflect.Map, reflect.Ptr, reflect.Slice:
|
|
|
|
t = t.Elem()
|
|
|
|
c = true
|
|
|
|
}
|
|
|
|
if !c {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2013-10-26 10:40:37 +04:00
|
|
|
reg, _ := regexp.Compile("s*$")
|
|
|
|
return reg.ReplaceAllString(toSnake(t.Name()), "s")
|
2013-10-26 10:23:02 +04:00
|
|
|
}
|
2013-10-26 10:10:47 +04:00
|
|
|
|
|
|
|
func debug(value interface{}) {
|
|
|
|
fmt.Printf("***************\n")
|
|
|
|
fmt.Printf("%+v\n\n", value)
|
|
|
|
}
|