forked from mirror/gorm
Rename ToSnake to ToDBColumnName
This commit is contained in:
parent
1ba5ce3bbc
commit
672ba4ffc9
|
@ -157,7 +157,7 @@ func (association *Association) Count() int {
|
|||
whereSql := fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), newScope.Quote(relationship.ForeignDBName))
|
||||
countScope := scope.db.Model("").Table(newScope.QuotedTableName()).Where(whereSql, association.PrimaryKey)
|
||||
if relationship.ForeignType != "" {
|
||||
countScope = countScope.Where(fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), newScope.Quote(ToSnake(relationship.ForeignType))), scope.TableName())
|
||||
countScope = countScope.Where(fmt.Sprintf("%v.%v = ?", newScope.QuotedTableName(), newScope.Quote(ToDBColumnName(relationship.ForeignType))), scope.TableName())
|
||||
}
|
||||
countScope.Count(&count)
|
||||
} else if relationship.Kind == "belongs_to" {
|
||||
|
|
|
@ -146,7 +146,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||
}
|
||||
}
|
||||
} else {
|
||||
modelStruct.TableName = ToSnake(scopeType.Name())
|
||||
modelStruct.TableName = ToDBColumnName(scopeType.Name())
|
||||
if scope.db == nil || !scope.db.parent.singularTable {
|
||||
for index, reg := range pluralMapKeys {
|
||||
if reg.MatchString(modelStruct.TableName) {
|
||||
|
@ -183,7 +183,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||
if value, ok := gormSettings["COLUMN"]; ok {
|
||||
field.DBName = value
|
||||
} else {
|
||||
field.DBName = ToSnake(fieldStruct.Name)
|
||||
field.DBName = ToDBColumnName(fieldStruct.Name)
|
||||
}
|
||||
|
||||
fieldType, indirectType := fieldStruct.Type, fieldStruct.Type
|
||||
|
@ -200,10 +200,10 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||
}
|
||||
|
||||
many2many := gormSettings["MANY2MANY"]
|
||||
foreignKey := SnakeToUpperCamel(gormSettings["FOREIGNKEY"])
|
||||
foreignType := SnakeToUpperCamel(gormSettings["FOREIGNTYPE"])
|
||||
associationForeignKey := SnakeToUpperCamel(gormSettings["ASSOCIATIONFOREIGNKEY"])
|
||||
if polymorphic := SnakeToUpperCamel(gormSettings["POLYMORPHIC"]); polymorphic != "" {
|
||||
foreignKey := gormSettings["FOREIGNKEY"]
|
||||
foreignType := gormSettings["FOREIGNTYPE"]
|
||||
associationForeignKey := gormSettings["ASSOCIATIONFOREIGNKEY"]
|
||||
if polymorphic := gormSettings["POLYMORPHIC"]; polymorphic != "" {
|
||||
foreignKey = polymorphic + "Id"
|
||||
foreignType = polymorphic + "Type"
|
||||
}
|
||||
|
@ -238,8 +238,8 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||
ForeignType: foreignType,
|
||||
ForeignFieldName: foreignKey,
|
||||
AssociationForeignFieldName: associationForeignKey,
|
||||
ForeignDBName: ToSnake(foreignKey),
|
||||
AssociationForeignDBName: ToSnake(associationForeignKey),
|
||||
ForeignDBName: ToDBColumnName(foreignKey),
|
||||
AssociationForeignDBName: ToDBColumnName(associationForeignKey),
|
||||
Kind: kind,
|
||||
}
|
||||
} else {
|
||||
|
@ -274,7 +274,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||
|
||||
field.Relationship = &Relationship{
|
||||
ForeignFieldName: foreignKey,
|
||||
ForeignDBName: ToSnake(foreignKey),
|
||||
ForeignDBName: ToDBColumnName(foreignKey),
|
||||
ForeignType: foreignType,
|
||||
Kind: kind,
|
||||
}
|
||||
|
|
2
scope.go
2
scope.go
|
@ -145,7 +145,7 @@ func (scope *Scope) SetColumn(column interface{}, value interface{}) error {
|
|||
return field.Set(value)
|
||||
}
|
||||
|
||||
dbName = ToSnake(dbName)
|
||||
dbName = ToDBColumnName(dbName)
|
||||
if field, ok := scope.Fields()[dbName]; ok {
|
||||
return field.Set(value)
|
||||
}
|
||||
|
|
|
@ -316,7 +316,7 @@ func (scope *Scope) updatedAttrsWithValues(values map[string]interface{}, ignore
|
|||
|
||||
fields := scope.Fields()
|
||||
for key, value := range values {
|
||||
if field, ok := fields[ToSnake(key)]; ok && field.Field.IsValid() {
|
||||
if field, ok := fields[ToDBColumnName(key)]; ok && field.Field.IsValid() {
|
||||
if !reflect.DeepEqual(field.Field, reflect.ValueOf(value)) {
|
||||
if !equalAsString(field.Field.Interface(), value) {
|
||||
hasUpdate = true
|
||||
|
@ -389,8 +389,8 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
|
|||
fromFields := scope.Fields()
|
||||
toFields := toScope.Fields()
|
||||
for _, foreignKey := range append(foreignKeys, toScope.typeName()+"Id", scope.typeName()+"Id") {
|
||||
fromField := fromFields[ToSnake(foreignKey)]
|
||||
toField := toFields[ToSnake(foreignKey)]
|
||||
fromField := fromFields[ToDBColumnName(foreignKey)]
|
||||
toField := toFields[ToDBColumnName(foreignKey)]
|
||||
|
||||
if fromField != nil {
|
||||
if relationship := fromField.Relationship; relationship != nil {
|
||||
|
@ -411,7 +411,7 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
|
|||
sql := fmt.Sprintf("%v = ?", scope.Quote(relationship.ForeignDBName))
|
||||
query := toScope.db.Where(sql, scope.PrimaryKeyValue())
|
||||
if relationship.ForeignType != "" && toScope.HasColumn(relationship.ForeignType) {
|
||||
query = query.Where(fmt.Sprintf("%v = ?", scope.Quote(ToSnake(relationship.ForeignType))), scope.TableName())
|
||||
query = query.Where(fmt.Sprintf("%v = ?", scope.Quote(ToDBColumnName(relationship.ForeignType))), scope.TableName())
|
||||
}
|
||||
scope.Err(query.Find(value).Error)
|
||||
}
|
||||
|
@ -439,8 +439,8 @@ func (scope *Scope) createJoinTable(field *StructField) {
|
|||
newScope.Raw(fmt.Sprintf("CREATE TABLE %v (%v)",
|
||||
field.Relationship.JoinTable,
|
||||
strings.Join([]string{
|
||||
scope.Quote(ToSnake(field.Relationship.ForeignFieldName)) + " " + primaryKeySqlType,
|
||||
scope.Quote(ToSnake(field.Relationship.AssociationForeignFieldName)) + " " + primaryKeySqlType}, ",")),
|
||||
scope.Quote(field.Relationship.ForeignDBName) + " " + primaryKeySqlType,
|
||||
scope.Quote(field.Relationship.AssociationForeignDBName) + " " + primaryKeySqlType}, ",")),
|
||||
).Exec()
|
||||
scope.Err(newScope.db.Error)
|
||||
}
|
||||
|
|
49
utils.go
49
utils.go
|
@ -3,35 +3,12 @@ package gorm
|
|||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type safeMap struct {
|
||||
m map[string]string
|
||||
l *sync.RWMutex
|
||||
}
|
||||
var smap = map[string]string{}
|
||||
|
||||
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()
|
||||
var umap = newSafeMap()
|
||||
|
||||
func ToSnake(u string) string {
|
||||
if v := smap.Get(u); v != "" {
|
||||
func ToDBColumnName(u string) string {
|
||||
if v, ok := smap[u]; ok {
|
||||
return v
|
||||
}
|
||||
|
||||
|
@ -44,28 +21,10 @@ func ToSnake(u string) string {
|
|||
}
|
||||
|
||||
s := strings.ToLower(buf.String())
|
||||
smap.Set(u, s)
|
||||
smap[u] = s
|
||||
return s
|
||||
}
|
||||
|
||||
func SnakeToUpperCamel(s string) string {
|
||||
if v := umap.Get(s); v != "" {
|
||||
return v
|
||||
}
|
||||
|
||||
buf := bytes.NewBufferString("")
|
||||
for _, v := range strings.Split(s, "_") {
|
||||
if len(v) > 0 {
|
||||
buf.WriteString(strings.ToUpper(v[:1]))
|
||||
buf.WriteString(v[1:])
|
||||
}
|
||||
}
|
||||
|
||||
u := buf.String()
|
||||
umap.Set(s, u)
|
||||
return u
|
||||
}
|
||||
|
||||
func parseTagSetting(str string) map[string]string {
|
||||
tags := strings.Split(str, ";")
|
||||
setting := map[string]string{}
|
||||
|
|
|
@ -44,7 +44,7 @@ func convertInterfaceToMap(values interface{}) map[string]interface{} {
|
|||
switch value := values.(type) {
|
||||
case map[string]interface{}:
|
||||
for k, v := range value {
|
||||
attrs[ToSnake(k)] = v
|
||||
attrs[ToDBColumnName(k)] = v
|
||||
}
|
||||
case []interface{}:
|
||||
for _, v := range value {
|
||||
|
@ -58,7 +58,7 @@ func convertInterfaceToMap(values interface{}) map[string]interface{} {
|
|||
switch reflectValue.Kind() {
|
||||
case reflect.Map:
|
||||
for _, key := range reflectValue.MapKeys() {
|
||||
attrs[ToSnake(key.Interface().(string))] = reflectValue.MapIndex(key).Interface()
|
||||
attrs[ToDBColumnName(key.Interface().(string))] = reflectValue.MapIndex(key).Interface()
|
||||
}
|
||||
default:
|
||||
scope := Scope{Value: values}
|
||||
|
|
Loading…
Reference in New Issue