From 672ba4ffc9a67dd341ffc13f9d9571e3ceb81d21 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 18 Feb 2015 09:26:35 +0800 Subject: [PATCH] Rename ToSnake to ToDBColumnName --- association.go | 2 +- model_struct.go | 18 +++++++++--------- scope.go | 2 +- scope_private.go | 12 ++++++------ utils.go | 49 ++++-------------------------------------------- utils_private.go | 4 ++-- 6 files changed, 23 insertions(+), 64 deletions(-) diff --git a/association.go b/association.go index 4f94d15f..24faf013 100644 --- a/association.go +++ b/association.go @@ -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" { diff --git a/model_struct.go b/model_struct.go index adfa776f..ae91c229 100644 --- a/model_struct.go +++ b/model_struct.go @@ -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, } diff --git a/scope.go b/scope.go index c0e35f2e..ce9c66c0 100644 --- a/scope.go +++ b/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) } diff --git a/scope_private.go b/scope_private.go index 5c125b0a..f10586fc 100644 --- a/scope_private.go +++ b/scope_private.go @@ -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) } diff --git a/utils.go b/utils.go index 182b7376..b1b3166f 100644 --- a/utils.go +++ b/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{} diff --git a/utils_private.go b/utils_private.go index 1ebd49fe..09791ec1 100644 --- a/utils_private.go +++ b/utils_private.go @@ -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}