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