commit
7d07b4afe8
@ -267,15 +267,16 @@ func (association *Association) Count() int {
|
|||||||
query = scope.DB()
|
query = scope.DB()
|
||||||
)
|
)
|
||||||
|
|
||||||
if relationship.Kind == "many_to_many" {
|
switch relationship.Kind {
|
||||||
|
case "many_to_many":
|
||||||
query = relationship.JoinTableHandler.JoinWith(relationship.JoinTableHandler, query, scope.Value)
|
query = relationship.JoinTableHandler.JoinWith(relationship.JoinTableHandler, query, scope.Value)
|
||||||
} else if relationship.Kind == "has_many" || relationship.Kind == "has_one" {
|
case "has_many", "has_one":
|
||||||
primaryKeys := scope.getColumnAsArray(relationship.AssociationForeignFieldNames, scope.Value)
|
primaryKeys := scope.getColumnAsArray(relationship.AssociationForeignFieldNames, scope.Value)
|
||||||
query = query.Where(
|
query = query.Where(
|
||||||
fmt.Sprintf("%v IN (%v)", toQueryCondition(scope, relationship.ForeignDBNames), toQueryMarks(primaryKeys)),
|
fmt.Sprintf("%v IN (%v)", toQueryCondition(scope, relationship.ForeignDBNames), toQueryMarks(primaryKeys)),
|
||||||
toQueryValues(primaryKeys)...,
|
toQueryValues(primaryKeys)...,
|
||||||
)
|
)
|
||||||
} else if relationship.Kind == "belongs_to" {
|
case "belongs_to":
|
||||||
primaryKeys := scope.getColumnAsArray(relationship.ForeignFieldNames, scope.Value)
|
primaryKeys := scope.getColumnAsArray(relationship.ForeignFieldNames, scope.Value)
|
||||||
query = query.Where(
|
query = query.Where(
|
||||||
fmt.Sprintf("%v IN (%v)", toQueryCondition(scope, relationship.AssociationForeignDBNames), toQueryMarks(primaryKeys)),
|
fmt.Sprintf("%v IN (%v)", toQueryCondition(scope, relationship.AssociationForeignDBNames), toQueryMarks(primaryKeys)),
|
||||||
@ -367,6 +368,7 @@ func (association *Association) saveAssociations(values ...interface{}) *Associa
|
|||||||
return association
|
return association
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// setErr set error when the error is not nil. And return Association.
|
||||||
func (association *Association) setErr(err error) *Association {
|
func (association *Association) setErr(err error) *Association {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
association.Error = err
|
association.Error = err
|
||||||
|
@ -59,7 +59,7 @@ func createCallback(scope *Scope) {
|
|||||||
|
|
||||||
for _, field := range scope.Fields() {
|
for _, field := range scope.Fields() {
|
||||||
if scope.changeableField(field) {
|
if scope.changeableField(field) {
|
||||||
if field.IsNormal {
|
if field.IsNormal && !field.IsIgnored {
|
||||||
if field.IsBlank && field.HasDefaultValue {
|
if field.IsBlank && field.HasDefaultValue {
|
||||||
blankColumnsWithDefaultValue = append(blankColumnsWithDefaultValue, scope.Quote(field.DBName))
|
blankColumnsWithDefaultValue = append(blankColumnsWithDefaultValue, scope.Quote(field.DBName))
|
||||||
scope.InstanceSet("gorm:blank_columns_with_default_value", blankColumnsWithDefaultValue)
|
scope.InstanceSet("gorm:blank_columns_with_default_value", blankColumnsWithDefaultValue)
|
||||||
|
@ -19,6 +19,11 @@ func queryCallback(scope *Scope) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//we are only preloading relations, dont touch base model
|
||||||
|
if _, skip := scope.InstanceGet("gorm:only_preload"); skip {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
defer scope.trace(NowFunc())
|
defer scope.trace(NowFunc())
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -100,7 +100,7 @@ func autoPreload(scope *Scope) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if val, ok := field.TagSettings["PRELOAD"]; ok {
|
if val, ok := field.TagSettingsGet("PRELOAD"); ok {
|
||||||
if preload, err := strconv.ParseBool(val); err != nil {
|
if preload, err := strconv.ParseBool(val); err != nil {
|
||||||
scope.Err(errors.New("invalid preload option"))
|
scope.Err(errors.New("invalid preload option"))
|
||||||
return
|
return
|
||||||
@ -161,14 +161,17 @@ func (scope *Scope) handleHasOnePreload(field *Field, conditions []interface{})
|
|||||||
)
|
)
|
||||||
|
|
||||||
if indirectScopeValue.Kind() == reflect.Slice {
|
if indirectScopeValue.Kind() == reflect.Slice {
|
||||||
|
foreignValuesToResults := make(map[string]reflect.Value)
|
||||||
|
for i := 0; i < resultsValue.Len(); i++ {
|
||||||
|
result := resultsValue.Index(i)
|
||||||
|
foreignValues := toString(getValueFromFields(result, relation.ForeignFieldNames))
|
||||||
|
foreignValuesToResults[foreignValues] = result
|
||||||
|
}
|
||||||
for j := 0; j < indirectScopeValue.Len(); j++ {
|
for j := 0; j < indirectScopeValue.Len(); j++ {
|
||||||
for i := 0; i < resultsValue.Len(); i++ {
|
indirectValue := indirect(indirectScopeValue.Index(j))
|
||||||
result := resultsValue.Index(i)
|
valueString := toString(getValueFromFields(indirectValue, relation.AssociationForeignFieldNames))
|
||||||
foreignValues := getValueFromFields(result, relation.ForeignFieldNames)
|
if result, found := foreignValuesToResults[valueString]; found {
|
||||||
if indirectValue := indirect(indirectScopeValue.Index(j)); equalAsString(getValueFromFields(indirectValue, relation.AssociationForeignFieldNames), foreignValues) {
|
indirectValue.FieldByName(field.Name).Set(result)
|
||||||
indirectValue.FieldByName(field.Name).Set(result)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -255,13 +258,21 @@ func (scope *Scope) handleBelongsToPreload(field *Field, conditions []interface{
|
|||||||
indirectScopeValue = scope.IndirectValue()
|
indirectScopeValue = scope.IndirectValue()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
foreignFieldToObjects := make(map[string][]*reflect.Value)
|
||||||
|
if indirectScopeValue.Kind() == reflect.Slice {
|
||||||
|
for j := 0; j < indirectScopeValue.Len(); j++ {
|
||||||
|
object := indirect(indirectScopeValue.Index(j))
|
||||||
|
valueString := toString(getValueFromFields(object, relation.ForeignFieldNames))
|
||||||
|
foreignFieldToObjects[valueString] = append(foreignFieldToObjects[valueString], &object)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for i := 0; i < resultsValue.Len(); i++ {
|
for i := 0; i < resultsValue.Len(); i++ {
|
||||||
result := resultsValue.Index(i)
|
result := resultsValue.Index(i)
|
||||||
if indirectScopeValue.Kind() == reflect.Slice {
|
if indirectScopeValue.Kind() == reflect.Slice {
|
||||||
value := getValueFromFields(result, relation.AssociationForeignFieldNames)
|
valueString := toString(getValueFromFields(result, relation.AssociationForeignFieldNames))
|
||||||
for j := 0; j < indirectScopeValue.Len(); j++ {
|
if objects, found := foreignFieldToObjects[valueString]; found {
|
||||||
object := indirect(indirectScopeValue.Index(j))
|
for _, object := range objects {
|
||||||
if equalAsString(getValueFromFields(object, relation.ForeignFieldNames), value) {
|
|
||||||
object.FieldByName(field.Name).Set(result)
|
object.FieldByName(field.Name).Set(result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,9 +21,7 @@ func saveAssociationCheck(scope *Scope, field *Field) (autoUpdate bool, autoCrea
|
|||||||
|
|
||||||
if v, ok := value.(string); ok {
|
if v, ok := value.(string); ok {
|
||||||
v = strings.ToLower(v)
|
v = strings.ToLower(v)
|
||||||
if v == "false" || v != "skip" {
|
return v == "true"
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
@ -36,26 +34,28 @@ func saveAssociationCheck(scope *Scope, field *Field) (autoUpdate bool, autoCrea
|
|||||||
if value, ok := scope.Get("gorm:save_associations"); ok {
|
if value, ok := scope.Get("gorm:save_associations"); ok {
|
||||||
autoUpdate = checkTruth(value)
|
autoUpdate = checkTruth(value)
|
||||||
autoCreate = autoUpdate
|
autoCreate = autoUpdate
|
||||||
} else if value, ok := field.TagSettings["SAVE_ASSOCIATIONS"]; ok {
|
saveReference = autoUpdate
|
||||||
|
} else if value, ok := field.TagSettingsGet("SAVE_ASSOCIATIONS"); ok {
|
||||||
autoUpdate = checkTruth(value)
|
autoUpdate = checkTruth(value)
|
||||||
autoCreate = autoUpdate
|
autoCreate = autoUpdate
|
||||||
|
saveReference = autoUpdate
|
||||||
}
|
}
|
||||||
|
|
||||||
if value, ok := scope.Get("gorm:association_autoupdate"); ok {
|
if value, ok := scope.Get("gorm:association_autoupdate"); ok {
|
||||||
autoUpdate = checkTruth(value)
|
autoUpdate = checkTruth(value)
|
||||||
} else if value, ok := field.TagSettings["ASSOCIATION_AUTOUPDATE"]; ok {
|
} else if value, ok := field.TagSettingsGet("ASSOCIATION_AUTOUPDATE"); ok {
|
||||||
autoUpdate = checkTruth(value)
|
autoUpdate = checkTruth(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
if value, ok := scope.Get("gorm:association_autocreate"); ok {
|
if value, ok := scope.Get("gorm:association_autocreate"); ok {
|
||||||
autoCreate = checkTruth(value)
|
autoCreate = checkTruth(value)
|
||||||
} else if value, ok := field.TagSettings["ASSOCIATION_AUTOCREATE"]; ok {
|
} else if value, ok := field.TagSettingsGet("ASSOCIATION_AUTOCREATE"); ok {
|
||||||
autoCreate = checkTruth(value)
|
autoCreate = checkTruth(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
if value, ok := scope.Get("gorm:association_save_reference"); ok {
|
if value, ok := scope.Get("gorm:association_save_reference"); ok {
|
||||||
saveReference = checkTruth(value)
|
saveReference = checkTruth(value)
|
||||||
} else if value, ok := field.TagSettings["ASSOCIATION_SAVE_REFERENCE"]; ok {
|
} else if value, ok := field.TagSettingsGet("ASSOCIATION_SAVE_REFERENCE"); ok {
|
||||||
saveReference = checkTruth(value)
|
saveReference = checkTruth(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,9 @@ func updateCallback(scope *Scope) {
|
|||||||
for _, field := range scope.Fields() {
|
for _, field := range scope.Fields() {
|
||||||
if scope.changeableField(field) {
|
if scope.changeableField(field) {
|
||||||
if !field.IsPrimaryKey && field.IsNormal {
|
if !field.IsPrimaryKey && field.IsNormal {
|
||||||
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface())))
|
if !field.IsForeignKey || !field.IsBlank || !field.HasDefaultValue {
|
||||||
|
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface())))
|
||||||
|
}
|
||||||
} else if relationship := field.Relationship; relationship != nil && relationship.Kind == "belongs_to" {
|
} else if relationship := field.Relationship; relationship != nil && relationship.Kind == "belongs_to" {
|
||||||
for _, foreignKey := range relationship.ForeignDBNames {
|
for _, foreignKey := range relationship.ForeignDBNames {
|
||||||
if foreignField, ok := scope.FieldByName(foreignKey); ok && !scope.changeableField(foreignField) {
|
if foreignField, ok := scope.FieldByName(foreignKey); ok && !scope.changeableField(foreignField) {
|
||||||
|
10
dialect.go
10
dialect.go
@ -83,7 +83,7 @@ var ParseFieldStructForDialect = func(field *StructField, dialect Dialect) (fiel
|
|||||||
// Get redirected field type
|
// Get redirected field type
|
||||||
var (
|
var (
|
||||||
reflectType = field.Struct.Type
|
reflectType = field.Struct.Type
|
||||||
dataType = field.TagSettings["TYPE"]
|
dataType, _ = field.TagSettingsGet("TYPE")
|
||||||
)
|
)
|
||||||
|
|
||||||
for reflectType.Kind() == reflect.Ptr {
|
for reflectType.Kind() == reflect.Ptr {
|
||||||
@ -112,15 +112,17 @@ var ParseFieldStructForDialect = func(field *StructField, dialect Dialect) (fiel
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Default Size
|
// Default Size
|
||||||
if num, ok := field.TagSettings["SIZE"]; ok {
|
if num, ok := field.TagSettingsGet("SIZE"); ok {
|
||||||
size, _ = strconv.Atoi(num)
|
size, _ = strconv.Atoi(num)
|
||||||
} else {
|
} else {
|
||||||
size = 255
|
size = 255
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default type from tag setting
|
// Default type from tag setting
|
||||||
additionalType = field.TagSettings["NOT NULL"] + " " + field.TagSettings["UNIQUE"]
|
notNull, _ := field.TagSettingsGet("NOT NULL")
|
||||||
if value, ok := field.TagSettings["DEFAULT"]; ok {
|
unique, _ := field.TagSettingsGet("UNIQUE")
|
||||||
|
additionalType = notNull + " " + unique
|
||||||
|
if value, ok := field.TagSettingsGet("DEFAULT"); ok {
|
||||||
additionalType = additionalType + " DEFAULT " + value
|
additionalType = additionalType + " DEFAULT " + value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ func (commonDialect) Quote(key string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *commonDialect) fieldCanAutoIncrement(field *StructField) bool {
|
func (s *commonDialect) fieldCanAutoIncrement(field *StructField) bool {
|
||||||
if value, ok := field.TagSettings["AUTO_INCREMENT"]; ok {
|
if value, ok := field.TagSettingsGet("AUTO_INCREMENT"); ok {
|
||||||
return strings.ToLower(value) != "false"
|
return strings.ToLower(value) != "false"
|
||||||
}
|
}
|
||||||
return field.IsPrimaryKey
|
return field.IsPrimaryKey
|
||||||
|
@ -33,9 +33,9 @@ func (s *mysql) DataTypeOf(field *StructField) string {
|
|||||||
|
|
||||||
// MySQL allows only one auto increment column per table, and it must
|
// MySQL allows only one auto increment column per table, and it must
|
||||||
// be a KEY column.
|
// be a KEY column.
|
||||||
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok {
|
if _, ok := field.TagSettingsGet("AUTO_INCREMENT"); ok {
|
||||||
if _, ok = field.TagSettings["INDEX"]; !ok && !field.IsPrimaryKey {
|
if _, ok = field.TagSettingsGet("INDEX"); !ok && !field.IsPrimaryKey {
|
||||||
delete(field.TagSettings, "AUTO_INCREMENT")
|
field.TagSettingsDelete("AUTO_INCREMENT")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,42 +45,42 @@ func (s *mysql) DataTypeOf(field *StructField) string {
|
|||||||
sqlType = "boolean"
|
sqlType = "boolean"
|
||||||
case reflect.Int8:
|
case reflect.Int8:
|
||||||
if s.fieldCanAutoIncrement(field) {
|
if s.fieldCanAutoIncrement(field) {
|
||||||
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
field.TagSettingsSet("AUTO_INCREMENT", "AUTO_INCREMENT")
|
||||||
sqlType = "tinyint AUTO_INCREMENT"
|
sqlType = "tinyint AUTO_INCREMENT"
|
||||||
} else {
|
} else {
|
||||||
sqlType = "tinyint"
|
sqlType = "tinyint"
|
||||||
}
|
}
|
||||||
case reflect.Int, reflect.Int16, reflect.Int32:
|
case reflect.Int, reflect.Int16, reflect.Int32:
|
||||||
if s.fieldCanAutoIncrement(field) {
|
if s.fieldCanAutoIncrement(field) {
|
||||||
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
field.TagSettingsSet("AUTO_INCREMENT", "AUTO_INCREMENT")
|
||||||
sqlType = "int AUTO_INCREMENT"
|
sqlType = "int AUTO_INCREMENT"
|
||||||
} else {
|
} else {
|
||||||
sqlType = "int"
|
sqlType = "int"
|
||||||
}
|
}
|
||||||
case reflect.Uint8:
|
case reflect.Uint8:
|
||||||
if s.fieldCanAutoIncrement(field) {
|
if s.fieldCanAutoIncrement(field) {
|
||||||
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
field.TagSettingsSet("AUTO_INCREMENT", "AUTO_INCREMENT")
|
||||||
sqlType = "tinyint unsigned AUTO_INCREMENT"
|
sqlType = "tinyint unsigned AUTO_INCREMENT"
|
||||||
} else {
|
} else {
|
||||||
sqlType = "tinyint unsigned"
|
sqlType = "tinyint unsigned"
|
||||||
}
|
}
|
||||||
case reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
|
case reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
|
||||||
if s.fieldCanAutoIncrement(field) {
|
if s.fieldCanAutoIncrement(field) {
|
||||||
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
field.TagSettingsSet("AUTO_INCREMENT", "AUTO_INCREMENT")
|
||||||
sqlType = "int unsigned AUTO_INCREMENT"
|
sqlType = "int unsigned AUTO_INCREMENT"
|
||||||
} else {
|
} else {
|
||||||
sqlType = "int unsigned"
|
sqlType = "int unsigned"
|
||||||
}
|
}
|
||||||
case reflect.Int64:
|
case reflect.Int64:
|
||||||
if s.fieldCanAutoIncrement(field) {
|
if s.fieldCanAutoIncrement(field) {
|
||||||
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
field.TagSettingsSet("AUTO_INCREMENT", "AUTO_INCREMENT")
|
||||||
sqlType = "bigint AUTO_INCREMENT"
|
sqlType = "bigint AUTO_INCREMENT"
|
||||||
} else {
|
} else {
|
||||||
sqlType = "bigint"
|
sqlType = "bigint"
|
||||||
}
|
}
|
||||||
case reflect.Uint64:
|
case reflect.Uint64:
|
||||||
if s.fieldCanAutoIncrement(field) {
|
if s.fieldCanAutoIncrement(field) {
|
||||||
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
field.TagSettingsSet("AUTO_INCREMENT", "AUTO_INCREMENT")
|
||||||
sqlType = "bigint unsigned AUTO_INCREMENT"
|
sqlType = "bigint unsigned AUTO_INCREMENT"
|
||||||
} else {
|
} else {
|
||||||
sqlType = "bigint unsigned"
|
sqlType = "bigint unsigned"
|
||||||
@ -96,11 +96,11 @@ func (s *mysql) DataTypeOf(field *StructField) string {
|
|||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
if _, ok := dataValue.Interface().(time.Time); ok {
|
if _, ok := dataValue.Interface().(time.Time); ok {
|
||||||
precision := ""
|
precision := ""
|
||||||
if p, ok := field.TagSettings["PRECISION"]; ok {
|
if p, ok := field.TagSettingsGet("PRECISION"); ok {
|
||||||
precision = fmt.Sprintf("(%s)", p)
|
precision = fmt.Sprintf("(%s)", p)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := field.TagSettings["NOT NULL"]; ok {
|
if _, ok := field.TagSettingsGet("NOT NULL"); ok {
|
||||||
sqlType = fmt.Sprintf("timestamp%v", precision)
|
sqlType = fmt.Sprintf("timestamp%v", precision)
|
||||||
} else {
|
} else {
|
||||||
sqlType = fmt.Sprintf("timestamp%v NULL", precision)
|
sqlType = fmt.Sprintf("timestamp%v NULL", precision)
|
||||||
|
@ -34,14 +34,14 @@ func (s *postgres) DataTypeOf(field *StructField) string {
|
|||||||
sqlType = "boolean"
|
sqlType = "boolean"
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uintptr:
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uintptr:
|
||||||
if s.fieldCanAutoIncrement(field) {
|
if s.fieldCanAutoIncrement(field) {
|
||||||
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
field.TagSettingsSet("AUTO_INCREMENT", "AUTO_INCREMENT")
|
||||||
sqlType = "serial"
|
sqlType = "serial"
|
||||||
} else {
|
} else {
|
||||||
sqlType = "integer"
|
sqlType = "integer"
|
||||||
}
|
}
|
||||||
case reflect.Int64, reflect.Uint32, reflect.Uint64:
|
case reflect.Int64, reflect.Uint32, reflect.Uint64:
|
||||||
if s.fieldCanAutoIncrement(field) {
|
if s.fieldCanAutoIncrement(field) {
|
||||||
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
field.TagSettingsSet("AUTO_INCREMENT", "AUTO_INCREMENT")
|
||||||
sqlType = "bigserial"
|
sqlType = "bigserial"
|
||||||
} else {
|
} else {
|
||||||
sqlType = "bigint"
|
sqlType = "bigint"
|
||||||
@ -49,7 +49,7 @@ func (s *postgres) DataTypeOf(field *StructField) string {
|
|||||||
case reflect.Float32, reflect.Float64:
|
case reflect.Float32, reflect.Float64:
|
||||||
sqlType = "numeric"
|
sqlType = "numeric"
|
||||||
case reflect.String:
|
case reflect.String:
|
||||||
if _, ok := field.TagSettings["SIZE"]; !ok {
|
if _, ok := field.TagSettingsGet("SIZE"); !ok {
|
||||||
size = 0 // if SIZE haven't been set, use `text` as the default type, as there are no performance different
|
size = 0 // if SIZE haven't been set, use `text` as the default type, as there are no performance different
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,14 +29,14 @@ func (s *sqlite3) DataTypeOf(field *StructField) string {
|
|||||||
sqlType = "bool"
|
sqlType = "bool"
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
|
||||||
if s.fieldCanAutoIncrement(field) {
|
if s.fieldCanAutoIncrement(field) {
|
||||||
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
field.TagSettingsSet("AUTO_INCREMENT", "AUTO_INCREMENT")
|
||||||
sqlType = "integer primary key autoincrement"
|
sqlType = "integer primary key autoincrement"
|
||||||
} else {
|
} else {
|
||||||
sqlType = "integer"
|
sqlType = "integer"
|
||||||
}
|
}
|
||||||
case reflect.Int64, reflect.Uint64:
|
case reflect.Int64, reflect.Uint64:
|
||||||
if s.fieldCanAutoIncrement(field) {
|
if s.fieldCanAutoIncrement(field) {
|
||||||
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
field.TagSettingsSet("AUTO_INCREMENT", "AUTO_INCREMENT")
|
||||||
sqlType = "integer primary key autoincrement"
|
sqlType = "integer primary key autoincrement"
|
||||||
} else {
|
} else {
|
||||||
sqlType = "bigint"
|
sqlType = "bigint"
|
||||||
|
@ -18,7 +18,7 @@ import (
|
|||||||
func setIdentityInsert(scope *gorm.Scope) {
|
func setIdentityInsert(scope *gorm.Scope) {
|
||||||
if scope.Dialect().GetName() == "mssql" {
|
if scope.Dialect().GetName() == "mssql" {
|
||||||
for _, field := range scope.PrimaryFields() {
|
for _, field := range scope.PrimaryFields() {
|
||||||
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok && !field.IsBlank {
|
if _, ok := field.TagSettingsGet("AUTO_INCREMENT"); ok && !field.IsBlank {
|
||||||
scope.NewDB().Exec(fmt.Sprintf("SET IDENTITY_INSERT %v ON", scope.TableName()))
|
scope.NewDB().Exec(fmt.Sprintf("SET IDENTITY_INSERT %v ON", scope.TableName()))
|
||||||
scope.InstanceSet("mssql:identity_insert_on", true)
|
scope.InstanceSet("mssql:identity_insert_on", true)
|
||||||
}
|
}
|
||||||
@ -70,14 +70,14 @@ func (s *mssql) DataTypeOf(field *gorm.StructField) string {
|
|||||||
sqlType = "bit"
|
sqlType = "bit"
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
|
||||||
if s.fieldCanAutoIncrement(field) {
|
if s.fieldCanAutoIncrement(field) {
|
||||||
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
field.TagSettingsSet("AUTO_INCREMENT", "AUTO_INCREMENT")
|
||||||
sqlType = "int IDENTITY(1,1)"
|
sqlType = "int IDENTITY(1,1)"
|
||||||
} else {
|
} else {
|
||||||
sqlType = "int"
|
sqlType = "int"
|
||||||
}
|
}
|
||||||
case reflect.Int64, reflect.Uint64:
|
case reflect.Int64, reflect.Uint64:
|
||||||
if s.fieldCanAutoIncrement(field) {
|
if s.fieldCanAutoIncrement(field) {
|
||||||
field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
|
field.TagSettingsSet("AUTO_INCREMENT", "AUTO_INCREMENT")
|
||||||
sqlType = "bigint IDENTITY(1,1)"
|
sqlType = "bigint IDENTITY(1,1)"
|
||||||
} else {
|
} else {
|
||||||
sqlType = "bigint"
|
sqlType = "bigint"
|
||||||
@ -116,7 +116,7 @@ func (s *mssql) DataTypeOf(field *gorm.StructField) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s mssql) fieldCanAutoIncrement(field *gorm.StructField) bool {
|
func (s mssql) fieldCanAutoIncrement(field *gorm.StructField) bool {
|
||||||
if value, ok := field.TagSettings["AUTO_INCREMENT"]; ok {
|
if value, ok := field.TagSettingsGet("AUTO_INCREMENT"); ok {
|
||||||
return value != "FALSE"
|
return value != "FALSE"
|
||||||
}
|
}
|
||||||
return field.IsPrimaryKey
|
return field.IsPrimaryKey
|
||||||
|
10
field.go
10
field.go
@ -2,6 +2,7 @@ package gorm
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"database/sql/driver"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
@ -44,7 +45,14 @@ func (field *Field) Set(value interface{}) (err error) {
|
|||||||
if reflectValue.Type().ConvertibleTo(fieldValue.Type()) {
|
if reflectValue.Type().ConvertibleTo(fieldValue.Type()) {
|
||||||
fieldValue.Set(reflectValue.Convert(fieldValue.Type()))
|
fieldValue.Set(reflectValue.Convert(fieldValue.Type()))
|
||||||
} else if scanner, ok := fieldValue.Addr().Interface().(sql.Scanner); ok {
|
} else if scanner, ok := fieldValue.Addr().Interface().(sql.Scanner); ok {
|
||||||
err = scanner.Scan(reflectValue.Interface())
|
v := reflectValue.Interface()
|
||||||
|
if valuer, ok := v.(driver.Valuer); ok {
|
||||||
|
if v, err = valuer.Value(); err == nil {
|
||||||
|
err = scanner.Scan(v)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err = scanner.Scan(v)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf("could not convert argument of field %s from %s to %s", field.Name, reflectValue.Type(), fieldValue.Type())
|
err = fmt.Errorf("could not convert argument of field %s from %s to %s", field.Name, reflectValue.Type(), fieldValue.Type())
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package gorm_test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gofrs/uuid"
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -43,7 +44,24 @@ func TestCalculateField(t *testing.T) {
|
|||||||
|
|
||||||
if field, ok := scope.FieldByName("embedded_name"); !ok {
|
if field, ok := scope.FieldByName("embedded_name"); !ok {
|
||||||
t.Errorf("should find embedded field")
|
t.Errorf("should find embedded field")
|
||||||
} else if _, ok := field.TagSettings["NOT NULL"]; !ok {
|
} else if _, ok := field.TagSettingsGet("NOT NULL"); !ok {
|
||||||
t.Errorf("should find embedded field's tag settings")
|
t.Errorf("should find embedded field's tag settings")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFieldSet(t *testing.T) {
|
||||||
|
type TestFieldSetNullUUID struct {
|
||||||
|
NullUUID uuid.NullUUID
|
||||||
|
}
|
||||||
|
scope := DB.NewScope(&TestFieldSetNullUUID{})
|
||||||
|
field := scope.Fields()[0]
|
||||||
|
err := field.Set(uuid.FromStringOrNil("3034d44a-da03-11e8-b366-4a00070b9f00"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if id, ok := field.Field.Addr().Interface().(*uuid.NullUUID); !ok {
|
||||||
|
t.Fatal()
|
||||||
|
} else if !id.Valid || id.UUID.String() != "3034d44a-da03-11e8-b366-4a00070b9f00" {
|
||||||
|
t.Fatal(id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
22
main.go
22
main.go
@ -22,7 +22,7 @@ type DB struct {
|
|||||||
logMode int
|
logMode int
|
||||||
logger logger
|
logger logger
|
||||||
search *search
|
search *search
|
||||||
values map[string]interface{}
|
values sync.Map
|
||||||
|
|
||||||
// global db
|
// global db
|
||||||
parent *DB
|
parent *DB
|
||||||
@ -72,7 +72,6 @@ func Open(dialect string, args ...interface{}) (db *DB, err error) {
|
|||||||
db = &DB{
|
db = &DB{
|
||||||
db: dbSQL,
|
db: dbSQL,
|
||||||
logger: defaultLogger,
|
logger: defaultLogger,
|
||||||
values: map[string]interface{}{},
|
|
||||||
callbacks: DefaultCallback,
|
callbacks: DefaultCallback,
|
||||||
dialect: newDialect(dialect, dbSQL),
|
dialect: newDialect(dialect, dbSQL),
|
||||||
}
|
}
|
||||||
@ -315,6 +314,11 @@ func (s *DB) Find(out interface{}, where ...interface{}) *DB {
|
|||||||
return s.NewScope(out).inlineCondition(where...).callCallbacks(s.parent.callbacks.queries).db
|
return s.NewScope(out).inlineCondition(where...).callCallbacks(s.parent.callbacks.queries).db
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Preloads preloads relations, don`t touch out
|
||||||
|
func (s *DB) Preloads(out interface{}) *DB {
|
||||||
|
return s.NewScope(out).InstanceSet("gorm:only_preload", 1).callCallbacks(s.parent.callbacks.queries).db
|
||||||
|
}
|
||||||
|
|
||||||
// Scan scan value to a struct
|
// Scan scan value to a struct
|
||||||
func (s *DB) Scan(dest interface{}) *DB {
|
func (s *DB) Scan(dest interface{}) *DB {
|
||||||
return s.NewScope(s.Value).Set("gorm:query_destination", dest).callCallbacks(s.parent.callbacks.queries).db
|
return s.NewScope(s.Value).Set("gorm:query_destination", dest).callCallbacks(s.parent.callbacks.queries).db
|
||||||
@ -680,13 +684,13 @@ func (s *DB) Set(name string, value interface{}) *DB {
|
|||||||
|
|
||||||
// InstantSet instant set setting, will affect current db
|
// InstantSet instant set setting, will affect current db
|
||||||
func (s *DB) InstantSet(name string, value interface{}) *DB {
|
func (s *DB) InstantSet(name string, value interface{}) *DB {
|
||||||
s.values[name] = value
|
s.values.Store(name, value)
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get get setting by name
|
// Get get setting by name
|
||||||
func (s *DB) Get(name string) (value interface{}, ok bool) {
|
func (s *DB) Get(name string) (value interface{}, ok bool) {
|
||||||
value, ok = s.values[name]
|
value, ok = s.values.Load(name)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -695,7 +699,7 @@ func (s *DB) SetJoinTableHandler(source interface{}, column string, handler Join
|
|||||||
scope := s.NewScope(source)
|
scope := s.NewScope(source)
|
||||||
for _, field := range scope.GetModelStruct().StructFields {
|
for _, field := range scope.GetModelStruct().StructFields {
|
||||||
if field.Name == column || field.DBName == column {
|
if field.Name == column || field.DBName == column {
|
||||||
if many2many := field.TagSettings["MANY2MANY"]; many2many != "" {
|
if many2many, _ := field.TagSettingsGet("MANY2MANY"); many2many != "" {
|
||||||
source := (&Scope{Value: source}).GetModelStruct().ModelType
|
source := (&Scope{Value: source}).GetModelStruct().ModelType
|
||||||
destination := (&Scope{Value: reflect.New(field.Struct.Type).Interface()}).GetModelStruct().ModelType
|
destination := (&Scope{Value: reflect.New(field.Struct.Type).Interface()}).GetModelStruct().ModelType
|
||||||
handler.Setup(field.Relationship, many2many, source, destination)
|
handler.Setup(field.Relationship, many2many, source, destination)
|
||||||
@ -750,16 +754,16 @@ func (s *DB) clone() *DB {
|
|||||||
parent: s.parent,
|
parent: s.parent,
|
||||||
logger: s.logger,
|
logger: s.logger,
|
||||||
logMode: s.logMode,
|
logMode: s.logMode,
|
||||||
values: map[string]interface{}{},
|
|
||||||
Value: s.Value,
|
Value: s.Value,
|
||||||
Error: s.Error,
|
Error: s.Error,
|
||||||
blockGlobalUpdate: s.blockGlobalUpdate,
|
blockGlobalUpdate: s.blockGlobalUpdate,
|
||||||
dialect: newDialect(s.dialect.GetName(), s.db),
|
dialect: newDialect(s.dialect.GetName(), s.db),
|
||||||
}
|
}
|
||||||
|
|
||||||
for key, value := range s.values {
|
s.values.Range(func(k, v interface{}) bool {
|
||||||
db.values[key] = value
|
db.values.Store(k, v)
|
||||||
}
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
if s.search == nil {
|
if s.search == nil {
|
||||||
db.search = &search{limit: -1, offset: -1}
|
db.search = &search{limit: -1, offset: -1}
|
||||||
|
142
main_test.go
142
main_test.go
@ -581,6 +581,60 @@ func TestJoins(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type JoinedIds struct {
|
||||||
|
UserID int64 `gorm:"column:id"`
|
||||||
|
BillingAddressID int64 `gorm:"column:id"`
|
||||||
|
EmailID int64 `gorm:"column:id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScanIdenticalColumnNames(t *testing.T) {
|
||||||
|
var user = User{
|
||||||
|
Name: "joinsIds",
|
||||||
|
Email: "joinIds@example.com",
|
||||||
|
BillingAddress: Address{
|
||||||
|
Address1: "One Park Place",
|
||||||
|
},
|
||||||
|
Emails: []Email{{Email: "join1@example.com"}, {Email: "join2@example.com"}},
|
||||||
|
}
|
||||||
|
DB.Save(&user)
|
||||||
|
|
||||||
|
var users []JoinedIds
|
||||||
|
DB.Select("users.id, addresses.id, emails.id").Table("users").
|
||||||
|
Joins("left join addresses on users.billing_address_id = addresses.id").
|
||||||
|
Joins("left join emails on emails.user_id = users.id").
|
||||||
|
Where("name = ?", "joinsIds").Scan(&users)
|
||||||
|
|
||||||
|
if len(users) != 2 {
|
||||||
|
t.Fatal("should find two rows using left join")
|
||||||
|
}
|
||||||
|
|
||||||
|
if user.Id != users[0].UserID {
|
||||||
|
t.Errorf("Expected result row to contain UserID %d, but got %d", user.Id, users[0].UserID)
|
||||||
|
}
|
||||||
|
if user.Id != users[1].UserID {
|
||||||
|
t.Errorf("Expected result row to contain UserID %d, but got %d", user.Id, users[1].UserID)
|
||||||
|
}
|
||||||
|
|
||||||
|
if user.BillingAddressID.Int64 != users[0].BillingAddressID {
|
||||||
|
t.Errorf("Expected result row to contain BillingAddressID %d, but got %d", user.BillingAddressID.Int64, users[0].BillingAddressID)
|
||||||
|
}
|
||||||
|
if user.BillingAddressID.Int64 != users[1].BillingAddressID {
|
||||||
|
t.Errorf("Expected result row to contain BillingAddressID %d, but got %d", user.BillingAddressID.Int64, users[0].BillingAddressID)
|
||||||
|
}
|
||||||
|
|
||||||
|
if users[0].EmailID == users[1].EmailID {
|
||||||
|
t.Errorf("Email ids should be unique. Got %d and %d", users[0].EmailID, users[1].EmailID)
|
||||||
|
}
|
||||||
|
|
||||||
|
if int64(user.Emails[0].Id) != users[0].EmailID && int64(user.Emails[1].Id) != users[0].EmailID {
|
||||||
|
t.Errorf("Expected result row ID to be either %d or %d, but was %d", user.Emails[0].Id, user.Emails[1].Id, users[0].EmailID)
|
||||||
|
}
|
||||||
|
|
||||||
|
if int64(user.Emails[0].Id) != users[1].EmailID && int64(user.Emails[1].Id) != users[1].EmailID {
|
||||||
|
t.Errorf("Expected result row ID to be either %d or %d, but was %d", user.Emails[0].Id, user.Emails[1].Id, users[1].EmailID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestJoinsWithSelect(t *testing.T) {
|
func TestJoinsWithSelect(t *testing.T) {
|
||||||
type result struct {
|
type result struct {
|
||||||
Name string
|
Name string
|
||||||
@ -879,6 +933,94 @@ func TestOpenWithOneParameter(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSaveAssociations(t *testing.T) {
|
||||||
|
db := DB.New()
|
||||||
|
deltaAddressCount := 0
|
||||||
|
if err := db.Model(&Address{}).Count(&deltaAddressCount).Error; err != nil {
|
||||||
|
t.Errorf("failed to fetch address count")
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
|
||||||
|
placeAddress := &Address{
|
||||||
|
Address1: "somewhere on earth",
|
||||||
|
}
|
||||||
|
ownerAddress1 := &Address{
|
||||||
|
Address1: "near place address",
|
||||||
|
}
|
||||||
|
ownerAddress2 := &Address{
|
||||||
|
Address1: "address2",
|
||||||
|
}
|
||||||
|
db.Create(placeAddress)
|
||||||
|
|
||||||
|
addressCountShouldBe := func(t *testing.T, expectedCount int) {
|
||||||
|
countFromDB := 0
|
||||||
|
t.Helper()
|
||||||
|
err := db.Model(&Address{}).Count(&countFromDB).Error
|
||||||
|
if err != nil {
|
||||||
|
t.Error("failed to fetch address count")
|
||||||
|
}
|
||||||
|
if countFromDB != expectedCount {
|
||||||
|
t.Errorf("address count mismatch: %d", countFromDB)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
addressCountShouldBe(t, deltaAddressCount+1)
|
||||||
|
|
||||||
|
// owner address should be created, place address should be reused
|
||||||
|
place1 := &Place{
|
||||||
|
PlaceAddressID: placeAddress.ID,
|
||||||
|
PlaceAddress: placeAddress,
|
||||||
|
OwnerAddress: ownerAddress1,
|
||||||
|
}
|
||||||
|
err := db.Create(place1).Error
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to store place: %s", err.Error())
|
||||||
|
}
|
||||||
|
addressCountShouldBe(t, deltaAddressCount+2)
|
||||||
|
|
||||||
|
// owner address should be created again, place address should be reused
|
||||||
|
place2 := &Place{
|
||||||
|
PlaceAddressID: placeAddress.ID,
|
||||||
|
PlaceAddress: &Address{
|
||||||
|
ID: 777,
|
||||||
|
Address1: "address1",
|
||||||
|
},
|
||||||
|
OwnerAddress: ownerAddress2,
|
||||||
|
OwnerAddressID: 778,
|
||||||
|
}
|
||||||
|
err = db.Create(place2).Error
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to store place: %s", err.Error())
|
||||||
|
}
|
||||||
|
addressCountShouldBe(t, deltaAddressCount+3)
|
||||||
|
|
||||||
|
count := 0
|
||||||
|
db.Model(&Place{}).Where(&Place{
|
||||||
|
PlaceAddressID: placeAddress.ID,
|
||||||
|
OwnerAddressID: ownerAddress1.ID,
|
||||||
|
}).Count(&count)
|
||||||
|
if count != 1 {
|
||||||
|
t.Errorf("only one instance of (%d, %d) should be available, found: %d",
|
||||||
|
placeAddress.ID, ownerAddress1.ID, count)
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Model(&Place{}).Where(&Place{
|
||||||
|
PlaceAddressID: placeAddress.ID,
|
||||||
|
OwnerAddressID: ownerAddress2.ID,
|
||||||
|
}).Count(&count)
|
||||||
|
if count != 1 {
|
||||||
|
t.Errorf("only one instance of (%d, %d) should be available, found: %d",
|
||||||
|
placeAddress.ID, ownerAddress2.ID, count)
|
||||||
|
}
|
||||||
|
|
||||||
|
db.Model(&Place{}).Where(&Place{
|
||||||
|
PlaceAddressID: placeAddress.ID,
|
||||||
|
}).Count(&count)
|
||||||
|
if count != 2 {
|
||||||
|
t.Errorf("two instances of (%d) should be available, found: %d",
|
||||||
|
placeAddress.ID, count)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestBlockGlobalUpdate(t *testing.T) {
|
func TestBlockGlobalUpdate(t *testing.T) {
|
||||||
db := DB.New()
|
db := DB.New()
|
||||||
db.Create(&Toy{Name: "Stuffed Animal", OwnerType: "Nobody"})
|
db.Create(&Toy{Name: "Stuffed Animal", OwnerType: "Nobody"})
|
||||||
|
@ -118,6 +118,14 @@ type Company struct {
|
|||||||
Owner *User `sql:"-"`
|
Owner *User `sql:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Place struct {
|
||||||
|
Id int64
|
||||||
|
PlaceAddressID int
|
||||||
|
PlaceAddress *Address `gorm:"save_associations:false"`
|
||||||
|
OwnerAddressID int
|
||||||
|
OwnerAddress *Address `gorm:"save_associations:true"`
|
||||||
|
}
|
||||||
|
|
||||||
type EncryptedData []byte
|
type EncryptedData []byte
|
||||||
|
|
||||||
func (data *EncryptedData) Scan(value interface{}) error {
|
func (data *EncryptedData) Scan(value interface{}) error {
|
||||||
@ -284,7 +292,7 @@ func runMigration() {
|
|||||||
DB.Exec(fmt.Sprintf("drop table %v;", table))
|
DB.Exec(fmt.Sprintf("drop table %v;", table))
|
||||||
}
|
}
|
||||||
|
|
||||||
values := []interface{}{&Short{}, &ReallyLongThingThatReferencesShort{}, &ReallyLongTableNameToTestMySQLNameLengthLimit{}, &NotSoLongTableName{}, &Product{}, &Email{}, &Address{}, &CreditCard{}, &Company{}, &Role{}, &Language{}, &HNPost{}, &EngadgetPost{}, &Animal{}, &User{}, &JoinTable{}, &Post{}, &Category{}, &Comment{}, &Cat{}, &Dog{}, &Hamster{}, &Toy{}, &ElementWithIgnoredField{}}
|
values := []interface{}{&Short{}, &ReallyLongThingThatReferencesShort{}, &ReallyLongTableNameToTestMySQLNameLengthLimit{}, &NotSoLongTableName{}, &Product{}, &Email{}, &Address{}, &CreditCard{}, &Company{}, &Role{}, &Language{}, &HNPost{}, &EngadgetPost{}, &Animal{}, &User{}, &JoinTable{}, &Post{}, &Category{}, &Comment{}, &Cat{}, &Dog{}, &Hamster{}, &Toy{}, &ElementWithIgnoredField{}, &Place{}}
|
||||||
for _, value := range values {
|
for _, value := range values {
|
||||||
DB.DropTable(value)
|
DB.DropTable(value)
|
||||||
}
|
}
|
||||||
|
@ -24,17 +24,22 @@ type ModelStruct struct {
|
|||||||
PrimaryFields []*StructField
|
PrimaryFields []*StructField
|
||||||
StructFields []*StructField
|
StructFields []*StructField
|
||||||
ModelType reflect.Type
|
ModelType reflect.Type
|
||||||
|
|
||||||
defaultTableName string
|
defaultTableName string
|
||||||
|
l sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// TableName returns model's table name
|
// TableName returns model's table name
|
||||||
func (s *ModelStruct) TableName(db *DB) string {
|
func (s *ModelStruct) TableName(db *DB) string {
|
||||||
|
s.l.Lock()
|
||||||
|
defer s.l.Unlock()
|
||||||
|
|
||||||
if s.defaultTableName == "" && db != nil && s.ModelType != nil {
|
if s.defaultTableName == "" && db != nil && s.ModelType != nil {
|
||||||
// Set default table name
|
// Set default table name
|
||||||
if tabler, ok := reflect.New(s.ModelType).Interface().(tabler); ok {
|
if tabler, ok := reflect.New(s.ModelType).Interface().(tabler); ok {
|
||||||
s.defaultTableName = tabler.TableName()
|
s.defaultTableName = tabler.TableName()
|
||||||
} else {
|
} else {
|
||||||
tableName := ToDBName(s.ModelType.Name())
|
tableName := ToTableName(s.ModelType.Name())
|
||||||
if db == nil || !db.parent.singularTable {
|
if db == nil || !db.parent.singularTable {
|
||||||
tableName = inflection.Plural(tableName)
|
tableName = inflection.Plural(tableName)
|
||||||
}
|
}
|
||||||
@ -60,6 +65,30 @@ type StructField struct {
|
|||||||
Struct reflect.StructField
|
Struct reflect.StructField
|
||||||
IsForeignKey bool
|
IsForeignKey bool
|
||||||
Relationship *Relationship
|
Relationship *Relationship
|
||||||
|
|
||||||
|
tagSettingsLock sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// TagSettingsSet Sets a tag in the tag settings map
|
||||||
|
func (s *StructField) TagSettingsSet(key, val string) {
|
||||||
|
s.tagSettingsLock.Lock()
|
||||||
|
defer s.tagSettingsLock.Unlock()
|
||||||
|
s.TagSettings[key] = val
|
||||||
|
}
|
||||||
|
|
||||||
|
// TagSettingsGet returns a tag from the tag settings
|
||||||
|
func (s *StructField) TagSettingsGet(key string) (string, bool) {
|
||||||
|
s.tagSettingsLock.RLock()
|
||||||
|
defer s.tagSettingsLock.RUnlock()
|
||||||
|
val, ok := s.TagSettings[key]
|
||||||
|
return val, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// TagSettingsDelete deletes a tag
|
||||||
|
func (s *StructField) TagSettingsDelete(key string) {
|
||||||
|
s.tagSettingsLock.Lock()
|
||||||
|
defer s.tagSettingsLock.Unlock()
|
||||||
|
delete(s.TagSettings, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (structField *StructField) clone() *StructField {
|
func (structField *StructField) clone() *StructField {
|
||||||
@ -83,6 +112,9 @@ func (structField *StructField) clone() *StructField {
|
|||||||
clone.Relationship = &relationship
|
clone.Relationship = &relationship
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// copy the struct field tagSettings, they should be read-locked while they are copied
|
||||||
|
structField.tagSettingsLock.Lock()
|
||||||
|
defer structField.tagSettingsLock.Unlock()
|
||||||
for key, value := range structField.TagSettings {
|
for key, value := range structField.TagSettings {
|
||||||
clone.TagSettings[key] = value
|
clone.TagSettings[key] = value
|
||||||
}
|
}
|
||||||
@ -105,7 +137,7 @@ type Relationship struct {
|
|||||||
|
|
||||||
func getForeignField(column string, fields []*StructField) *StructField {
|
func getForeignField(column string, fields []*StructField) *StructField {
|
||||||
for _, field := range fields {
|
for _, field := range fields {
|
||||||
if field.Name == column || field.DBName == column || field.DBName == ToDBName(column) {
|
if field.Name == column || field.DBName == column || field.DBName == ToColumnName(column) {
|
||||||
return field
|
return field
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -149,19 +181,19 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// is ignored field
|
// is ignored field
|
||||||
if _, ok := field.TagSettings["-"]; ok {
|
if _, ok := field.TagSettingsGet("-"); ok {
|
||||||
field.IsIgnored = true
|
field.IsIgnored = true
|
||||||
} else {
|
} else {
|
||||||
if _, ok := field.TagSettings["PRIMARY_KEY"]; ok {
|
if _, ok := field.TagSettingsGet("PRIMARY_KEY"); ok {
|
||||||
field.IsPrimaryKey = true
|
field.IsPrimaryKey = true
|
||||||
modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, field)
|
modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, field)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := field.TagSettings["DEFAULT"]; ok {
|
if _, ok := field.TagSettingsGet("DEFAULT"); ok {
|
||||||
field.HasDefaultValue = true
|
field.HasDefaultValue = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok && !field.IsPrimaryKey {
|
if _, ok := field.TagSettingsGet("AUTO_INCREMENT"); ok && !field.IsPrimaryKey {
|
||||||
field.HasDefaultValue = true
|
field.HasDefaultValue = true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -177,8 +209,8 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||||||
if indirectType.Kind() == reflect.Struct {
|
if indirectType.Kind() == reflect.Struct {
|
||||||
for i := 0; i < indirectType.NumField(); i++ {
|
for i := 0; i < indirectType.NumField(); i++ {
|
||||||
for key, value := range parseTagSetting(indirectType.Field(i).Tag) {
|
for key, value := range parseTagSetting(indirectType.Field(i).Tag) {
|
||||||
if _, ok := field.TagSettings[key]; !ok {
|
if _, ok := field.TagSettingsGet(key); !ok {
|
||||||
field.TagSettings[key] = value
|
field.TagSettingsSet(key, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -186,17 +218,17 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||||||
} else if _, isTime := fieldValue.(*time.Time); isTime {
|
} else if _, isTime := fieldValue.(*time.Time); isTime {
|
||||||
// is time
|
// is time
|
||||||
field.IsNormal = true
|
field.IsNormal = true
|
||||||
} else if _, ok := field.TagSettings["EMBEDDED"]; ok || fieldStruct.Anonymous {
|
} else if _, ok := field.TagSettingsGet("EMBEDDED"); ok || fieldStruct.Anonymous {
|
||||||
// is embedded struct
|
// is embedded struct
|
||||||
for _, subField := range scope.New(fieldValue).GetModelStruct().StructFields {
|
for _, subField := range scope.New(fieldValue).GetModelStruct().StructFields {
|
||||||
subField = subField.clone()
|
subField = subField.clone()
|
||||||
subField.Names = append([]string{fieldStruct.Name}, subField.Names...)
|
subField.Names = append([]string{fieldStruct.Name}, subField.Names...)
|
||||||
if prefix, ok := field.TagSettings["EMBEDDED_PREFIX"]; ok {
|
if prefix, ok := field.TagSettingsGet("EMBEDDED_PREFIX"); ok {
|
||||||
subField.DBName = prefix + subField.DBName
|
subField.DBName = prefix + subField.DBName
|
||||||
}
|
}
|
||||||
|
|
||||||
if subField.IsPrimaryKey {
|
if subField.IsPrimaryKey {
|
||||||
if _, ok := subField.TagSettings["PRIMARY_KEY"]; ok {
|
if _, ok := subField.TagSettingsGet("PRIMARY_KEY"); ok {
|
||||||
modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, subField)
|
modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, subField)
|
||||||
} else {
|
} else {
|
||||||
subField.IsPrimaryKey = false
|
subField.IsPrimaryKey = false
|
||||||
@ -227,13 +259,13 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||||||
elemType = field.Struct.Type
|
elemType = field.Struct.Type
|
||||||
)
|
)
|
||||||
|
|
||||||
if foreignKey := field.TagSettings["FOREIGNKEY"]; foreignKey != "" {
|
if foreignKey, _ := field.TagSettingsGet("FOREIGNKEY"); foreignKey != "" {
|
||||||
foreignKeys = strings.Split(foreignKey, ",")
|
foreignKeys = strings.Split(foreignKey, ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
if foreignKey := field.TagSettings["ASSOCIATION_FOREIGNKEY"]; foreignKey != "" {
|
if foreignKey, _ := field.TagSettingsGet("ASSOCIATION_FOREIGNKEY"); foreignKey != "" {
|
||||||
associationForeignKeys = strings.Split(foreignKey, ",")
|
associationForeignKeys = strings.Split(foreignKey, ",")
|
||||||
} else if foreignKey := field.TagSettings["ASSOCIATIONFOREIGNKEY"]; foreignKey != "" {
|
} else if foreignKey, _ := field.TagSettingsGet("ASSOCIATIONFOREIGNKEY"); foreignKey != "" {
|
||||||
associationForeignKeys = strings.Split(foreignKey, ",")
|
associationForeignKeys = strings.Split(foreignKey, ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -242,13 +274,13 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if elemType.Kind() == reflect.Struct {
|
if elemType.Kind() == reflect.Struct {
|
||||||
if many2many := field.TagSettings["MANY2MANY"]; many2many != "" {
|
if many2many, _ := field.TagSettingsGet("MANY2MANY"); many2many != "" {
|
||||||
relationship.Kind = "many_to_many"
|
relationship.Kind = "many_to_many"
|
||||||
|
|
||||||
{ // Foreign Keys for Source
|
{ // Foreign Keys for Source
|
||||||
joinTableDBNames := []string{}
|
joinTableDBNames := []string{}
|
||||||
|
|
||||||
if foreignKey := field.TagSettings["JOINTABLE_FOREIGNKEY"]; foreignKey != "" {
|
if foreignKey, _ := field.TagSettingsGet("JOINTABLE_FOREIGNKEY"); foreignKey != "" {
|
||||||
joinTableDBNames = strings.Split(foreignKey, ",")
|
joinTableDBNames = strings.Split(foreignKey, ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -269,7 +301,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||||||
// if defined join table's foreign key
|
// if defined join table's foreign key
|
||||||
relationship.ForeignDBNames = append(relationship.ForeignDBNames, joinTableDBNames[idx])
|
relationship.ForeignDBNames = append(relationship.ForeignDBNames, joinTableDBNames[idx])
|
||||||
} else {
|
} else {
|
||||||
defaultJointableForeignKey := ToDBName(reflectType.Name()) + "_" + foreignField.DBName
|
defaultJointableForeignKey := ToColumnName(reflectType.Name()) + "_" + foreignField.DBName
|
||||||
relationship.ForeignDBNames = append(relationship.ForeignDBNames, defaultJointableForeignKey)
|
relationship.ForeignDBNames = append(relationship.ForeignDBNames, defaultJointableForeignKey)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -279,7 +311,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||||||
{ // Foreign Keys for Association (Destination)
|
{ // Foreign Keys for Association (Destination)
|
||||||
associationJoinTableDBNames := []string{}
|
associationJoinTableDBNames := []string{}
|
||||||
|
|
||||||
if foreignKey := field.TagSettings["ASSOCIATION_JOINTABLE_FOREIGNKEY"]; foreignKey != "" {
|
if foreignKey, _ := field.TagSettingsGet("ASSOCIATION_JOINTABLE_FOREIGNKEY"); foreignKey != "" {
|
||||||
associationJoinTableDBNames = strings.Split(foreignKey, ",")
|
associationJoinTableDBNames = strings.Split(foreignKey, ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -300,7 +332,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||||||
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, associationJoinTableDBNames[idx])
|
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, associationJoinTableDBNames[idx])
|
||||||
} else {
|
} else {
|
||||||
// join table foreign keys for association
|
// join table foreign keys for association
|
||||||
joinTableDBName := ToDBName(elemType.Name()) + "_" + field.DBName
|
joinTableDBName := ToColumnName(elemType.Name()) + "_" + field.DBName
|
||||||
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, joinTableDBName)
|
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, joinTableDBName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -308,7 +340,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
joinTableHandler := JoinTableHandler{}
|
joinTableHandler := JoinTableHandler{}
|
||||||
joinTableHandler.Setup(relationship, many2many, reflectType, elemType)
|
joinTableHandler.Setup(relationship, ToTableName(many2many), reflectType, elemType)
|
||||||
relationship.JoinTableHandler = &joinTableHandler
|
relationship.JoinTableHandler = &joinTableHandler
|
||||||
field.Relationship = relationship
|
field.Relationship = relationship
|
||||||
} else {
|
} else {
|
||||||
@ -317,7 +349,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||||||
var toFields = toScope.GetStructFields()
|
var toFields = toScope.GetStructFields()
|
||||||
relationship.Kind = "has_many"
|
relationship.Kind = "has_many"
|
||||||
|
|
||||||
if polymorphic := field.TagSettings["POLYMORPHIC"]; polymorphic != "" {
|
if polymorphic, _ := field.TagSettingsGet("POLYMORPHIC"); polymorphic != "" {
|
||||||
// Dog has many toys, tag polymorphic is Owner, then associationType is Owner
|
// Dog has many toys, tag polymorphic is Owner, then associationType is Owner
|
||||||
// Toy use OwnerID, OwnerType ('dogs') as foreign key
|
// Toy use OwnerID, OwnerType ('dogs') as foreign key
|
||||||
if polymorphicType := getForeignField(polymorphic+"Type", toFields); polymorphicType != nil {
|
if polymorphicType := getForeignField(polymorphic+"Type", toFields); polymorphicType != nil {
|
||||||
@ -325,7 +357,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||||||
relationship.PolymorphicType = polymorphicType.Name
|
relationship.PolymorphicType = polymorphicType.Name
|
||||||
relationship.PolymorphicDBName = polymorphicType.DBName
|
relationship.PolymorphicDBName = polymorphicType.DBName
|
||||||
// if Dog has multiple set of toys set name of the set (instead of default 'dogs')
|
// if Dog has multiple set of toys set name of the set (instead of default 'dogs')
|
||||||
if value, ok := field.TagSettings["POLYMORPHIC_VALUE"]; ok {
|
if value, ok := field.TagSettingsGet("POLYMORPHIC_VALUE"); ok {
|
||||||
relationship.PolymorphicValue = value
|
relationship.PolymorphicValue = value
|
||||||
} else {
|
} else {
|
||||||
relationship.PolymorphicValue = scope.TableName()
|
relationship.PolymorphicValue = scope.TableName()
|
||||||
@ -407,17 +439,17 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||||||
tagAssociationForeignKeys []string
|
tagAssociationForeignKeys []string
|
||||||
)
|
)
|
||||||
|
|
||||||
if foreignKey := field.TagSettings["FOREIGNKEY"]; foreignKey != "" {
|
if foreignKey, _ := field.TagSettingsGet("FOREIGNKEY"); foreignKey != "" {
|
||||||
tagForeignKeys = strings.Split(foreignKey, ",")
|
tagForeignKeys = strings.Split(foreignKey, ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
if foreignKey := field.TagSettings["ASSOCIATION_FOREIGNKEY"]; foreignKey != "" {
|
if foreignKey, _ := field.TagSettingsGet("ASSOCIATION_FOREIGNKEY"); foreignKey != "" {
|
||||||
tagAssociationForeignKeys = strings.Split(foreignKey, ",")
|
tagAssociationForeignKeys = strings.Split(foreignKey, ",")
|
||||||
} else if foreignKey := field.TagSettings["ASSOCIATIONFOREIGNKEY"]; foreignKey != "" {
|
} else if foreignKey, _ := field.TagSettingsGet("ASSOCIATIONFOREIGNKEY"); foreignKey != "" {
|
||||||
tagAssociationForeignKeys = strings.Split(foreignKey, ",")
|
tagAssociationForeignKeys = strings.Split(foreignKey, ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
if polymorphic := field.TagSettings["POLYMORPHIC"]; polymorphic != "" {
|
if polymorphic, _ := field.TagSettingsGet("POLYMORPHIC"); polymorphic != "" {
|
||||||
// Cat has one toy, tag polymorphic is Owner, then associationType is Owner
|
// Cat has one toy, tag polymorphic is Owner, then associationType is Owner
|
||||||
// Toy use OwnerID, OwnerType ('cats') as foreign key
|
// Toy use OwnerID, OwnerType ('cats') as foreign key
|
||||||
if polymorphicType := getForeignField(polymorphic+"Type", toFields); polymorphicType != nil {
|
if polymorphicType := getForeignField(polymorphic+"Type", toFields); polymorphicType != nil {
|
||||||
@ -425,7 +457,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||||||
relationship.PolymorphicType = polymorphicType.Name
|
relationship.PolymorphicType = polymorphicType.Name
|
||||||
relationship.PolymorphicDBName = polymorphicType.DBName
|
relationship.PolymorphicDBName = polymorphicType.DBName
|
||||||
// if Cat has several different types of toys set name for each (instead of default 'cats')
|
// if Cat has several different types of toys set name for each (instead of default 'cats')
|
||||||
if value, ok := field.TagSettings["POLYMORPHIC_VALUE"]; ok {
|
if value, ok := field.TagSettingsGet("POLYMORPHIC_VALUE"); ok {
|
||||||
relationship.PolymorphicValue = value
|
relationship.PolymorphicValue = value
|
||||||
} else {
|
} else {
|
||||||
relationship.PolymorphicValue = scope.TableName()
|
relationship.PolymorphicValue = scope.TableName()
|
||||||
@ -563,10 +595,10 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Even it is ignored, also possible to decode db value into the field
|
// Even it is ignored, also possible to decode db value into the field
|
||||||
if value, ok := field.TagSettings["COLUMN"]; ok {
|
if value, ok := field.TagSettingsGet("COLUMN"); ok {
|
||||||
field.DBName = value
|
field.DBName = value
|
||||||
} else {
|
} else {
|
||||||
field.DBName = ToDBName(fieldStruct.Name)
|
field.DBName = ToColumnName(fieldStruct.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
modelStruct.StructFields = append(modelStruct.StructFields, field)
|
modelStruct.StructFields = append(modelStruct.StructFields, field)
|
||||||
|
124
naming.go
Normal file
124
naming.go
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
package gorm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Namer is a function type which is given a string and return a string
|
||||||
|
type Namer func(string) string
|
||||||
|
|
||||||
|
// NamingStrategy represents naming strategies
|
||||||
|
type NamingStrategy struct {
|
||||||
|
DB Namer
|
||||||
|
Table Namer
|
||||||
|
Column Namer
|
||||||
|
}
|
||||||
|
|
||||||
|
// TheNamingStrategy is being initialized with defaultNamingStrategy
|
||||||
|
var TheNamingStrategy = &NamingStrategy{
|
||||||
|
DB: defaultNamer,
|
||||||
|
Table: defaultNamer,
|
||||||
|
Column: defaultNamer,
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddNamingStrategy sets the naming strategy
|
||||||
|
func AddNamingStrategy(ns *NamingStrategy) {
|
||||||
|
if ns.DB == nil {
|
||||||
|
ns.DB = defaultNamer
|
||||||
|
}
|
||||||
|
if ns.Table == nil {
|
||||||
|
ns.Table = defaultNamer
|
||||||
|
}
|
||||||
|
if ns.Column == nil {
|
||||||
|
ns.Column = defaultNamer
|
||||||
|
}
|
||||||
|
TheNamingStrategy = ns
|
||||||
|
}
|
||||||
|
|
||||||
|
// DBName alters the given name by DB
|
||||||
|
func (ns *NamingStrategy) DBName(name string) string {
|
||||||
|
return ns.DB(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TableName alters the given name by Table
|
||||||
|
func (ns *NamingStrategy) TableName(name string) string {
|
||||||
|
return ns.Table(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ColumnName alters the given name by Column
|
||||||
|
func (ns *NamingStrategy) ColumnName(name string) string {
|
||||||
|
return ns.Column(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToDBName convert string to db name
|
||||||
|
func ToDBName(name string) string {
|
||||||
|
return TheNamingStrategy.DBName(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToTableName convert string to table name
|
||||||
|
func ToTableName(name string) string {
|
||||||
|
return TheNamingStrategy.TableName(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToColumnName convert string to db name
|
||||||
|
func ToColumnName(name string) string {
|
||||||
|
return TheNamingStrategy.ColumnName(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
var smap = newSafeMap()
|
||||||
|
|
||||||
|
func defaultNamer(name string) string {
|
||||||
|
const (
|
||||||
|
lower = false
|
||||||
|
upper = true
|
||||||
|
)
|
||||||
|
|
||||||
|
if v := smap.Get(name); v != "" {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
if name == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
value = commonInitialismsReplacer.Replace(name)
|
||||||
|
buf = bytes.NewBufferString("")
|
||||||
|
lastCase, currCase, nextCase, nextNumber bool
|
||||||
|
)
|
||||||
|
|
||||||
|
for i, v := range value[:len(value)-1] {
|
||||||
|
nextCase = bool(value[i+1] >= 'A' && value[i+1] <= 'Z')
|
||||||
|
nextNumber = bool(value[i+1] >= '0' && value[i+1] <= '9')
|
||||||
|
|
||||||
|
if i > 0 {
|
||||||
|
if currCase == upper {
|
||||||
|
if lastCase == upper && (nextCase == upper || nextNumber == upper) {
|
||||||
|
buf.WriteRune(v)
|
||||||
|
} else {
|
||||||
|
if value[i-1] != '_' && value[i+1] != '_' {
|
||||||
|
buf.WriteRune('_')
|
||||||
|
}
|
||||||
|
buf.WriteRune(v)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
buf.WriteRune(v)
|
||||||
|
if i == len(value)-2 && (nextCase == upper && nextNumber == lower) {
|
||||||
|
buf.WriteRune('_')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
currCase = upper
|
||||||
|
buf.WriteRune(v)
|
||||||
|
}
|
||||||
|
lastCase = currCase
|
||||||
|
currCase = nextCase
|
||||||
|
}
|
||||||
|
|
||||||
|
buf.WriteByte(value[len(value)-1])
|
||||||
|
|
||||||
|
s := strings.ToLower(buf.String())
|
||||||
|
smap.Set(name, s)
|
||||||
|
return s
|
||||||
|
}
|
69
naming_test.go
Normal file
69
naming_test.go
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
package gorm_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/jinzhu/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTheNamingStrategy(t *testing.T) {
|
||||||
|
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
namer gorm.Namer
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{name: "auth", expected: "auth", namer: gorm.TheNamingStrategy.DB},
|
||||||
|
{name: "userRestrictions", expected: "user_restrictions", namer: gorm.TheNamingStrategy.Table},
|
||||||
|
{name: "clientID", expected: "client_id", namer: gorm.TheNamingStrategy.Column},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cases {
|
||||||
|
t.Run(c.name, func(t *testing.T) {
|
||||||
|
result := c.namer(c.name)
|
||||||
|
if result != c.expected {
|
||||||
|
t.Errorf("error in naming strategy. expected: %v got :%v\n", c.expected, result)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNamingStrategy(t *testing.T) {
|
||||||
|
|
||||||
|
dbNameNS := func(name string) string {
|
||||||
|
return "db_" + name
|
||||||
|
}
|
||||||
|
tableNameNS := func(name string) string {
|
||||||
|
return "tbl_" + name
|
||||||
|
}
|
||||||
|
columnNameNS := func(name string) string {
|
||||||
|
return "col_" + name
|
||||||
|
}
|
||||||
|
|
||||||
|
ns := &gorm.NamingStrategy{
|
||||||
|
DB: dbNameNS,
|
||||||
|
Table: tableNameNS,
|
||||||
|
Column: columnNameNS,
|
||||||
|
}
|
||||||
|
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
namer gorm.Namer
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{name: "auth", expected: "db_auth", namer: ns.DB},
|
||||||
|
{name: "user", expected: "tbl_user", namer: ns.Table},
|
||||||
|
{name: "password", expected: "col_password", namer: ns.Column},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cases {
|
||||||
|
t.Run(c.name, func(t *testing.T) {
|
||||||
|
result := c.namer(c.name)
|
||||||
|
if result != c.expected {
|
||||||
|
t.Errorf("error in naming strategy. expected: %v got :%v\n", c.expected, result)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
36
scope.go
36
scope.go
@ -68,7 +68,7 @@ func (scope *Scope) Dialect() Dialect {
|
|||||||
|
|
||||||
// Quote used to quote string to escape them for database
|
// Quote used to quote string to escape them for database
|
||||||
func (scope *Scope) Quote(str string) string {
|
func (scope *Scope) Quote(str string) string {
|
||||||
if strings.Index(str, ".") != -1 {
|
if strings.Contains(str, ".") {
|
||||||
newStrs := []string{}
|
newStrs := []string{}
|
||||||
for _, str := range strings.Split(str, ".") {
|
for _, str := range strings.Split(str, ".") {
|
||||||
newStrs = append(newStrs, scope.Dialect().Quote(str))
|
newStrs = append(newStrs, scope.Dialect().Quote(str))
|
||||||
@ -134,7 +134,7 @@ func (scope *Scope) Fields() []*Field {
|
|||||||
// FieldByName find `gorm.Field` with field name or db name
|
// FieldByName find `gorm.Field` with field name or db name
|
||||||
func (scope *Scope) FieldByName(name string) (field *Field, ok bool) {
|
func (scope *Scope) FieldByName(name string) (field *Field, ok bool) {
|
||||||
var (
|
var (
|
||||||
dbName = ToDBName(name)
|
dbName = ToColumnName(name)
|
||||||
mostMatchedField *Field
|
mostMatchedField *Field
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -330,7 +330,7 @@ func (scope *Scope) TableName() string {
|
|||||||
// QuotedTableName return quoted table name
|
// QuotedTableName return quoted table name
|
||||||
func (scope *Scope) QuotedTableName() (name string) {
|
func (scope *Scope) QuotedTableName() (name string) {
|
||||||
if scope.Search != nil && len(scope.Search.tableName) > 0 {
|
if scope.Search != nil && len(scope.Search.tableName) > 0 {
|
||||||
if strings.Index(scope.Search.tableName, " ") != -1 {
|
if strings.Contains(scope.Search.tableName, " ") {
|
||||||
return scope.Search.tableName
|
return scope.Search.tableName
|
||||||
}
|
}
|
||||||
return scope.Quote(scope.Search.tableName)
|
return scope.Quote(scope.Search.tableName)
|
||||||
@ -486,8 +486,10 @@ func (scope *Scope) scan(rows *sql.Rows, columns []string, fields []*Field) {
|
|||||||
values[index] = &ignored
|
values[index] = &ignored
|
||||||
|
|
||||||
selectFields = fields
|
selectFields = fields
|
||||||
|
offset := 0
|
||||||
if idx, ok := selectedColumnsMap[column]; ok {
|
if idx, ok := selectedColumnsMap[column]; ok {
|
||||||
selectFields = selectFields[idx+1:]
|
offset = idx + 1
|
||||||
|
selectFields = selectFields[offset:]
|
||||||
}
|
}
|
||||||
|
|
||||||
for fieldIndex, field := range selectFields {
|
for fieldIndex, field := range selectFields {
|
||||||
@ -501,7 +503,7 @@ func (scope *Scope) scan(rows *sql.Rows, columns []string, fields []*Field) {
|
|||||||
resetFields[index] = field
|
resetFields[index] = field
|
||||||
}
|
}
|
||||||
|
|
||||||
selectedColumnsMap[column] = fieldIndex
|
selectedColumnsMap[column] = offset + fieldIndex
|
||||||
|
|
||||||
if field.IsNormal {
|
if field.IsNormal {
|
||||||
break
|
break
|
||||||
@ -853,6 +855,14 @@ func (scope *Scope) inlineCondition(values ...interface{}) *Scope {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) callCallbacks(funcs []*func(s *Scope)) *Scope {
|
func (scope *Scope) callCallbacks(funcs []*func(s *Scope)) *Scope {
|
||||||
|
defer func() {
|
||||||
|
if err := recover(); err != nil {
|
||||||
|
if db, ok := scope.db.db.(sqlTx); ok {
|
||||||
|
db.Rollback()
|
||||||
|
}
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
for _, f := range funcs {
|
for _, f := range funcs {
|
||||||
(*f)(scope)
|
(*f)(scope)
|
||||||
if scope.skipLeft {
|
if scope.skipLeft {
|
||||||
@ -880,7 +890,7 @@ func convertInterfaceToMap(values interface{}, withIgnoredField bool) map[string
|
|||||||
switch reflectValue.Kind() {
|
switch reflectValue.Kind() {
|
||||||
case reflect.Map:
|
case reflect.Map:
|
||||||
for _, key := range reflectValue.MapKeys() {
|
for _, key := range reflectValue.MapKeys() {
|
||||||
attrs[ToDBName(key.Interface().(string))] = reflectValue.MapIndex(key).Interface()
|
attrs[ToColumnName(key.Interface().(string))] = reflectValue.MapIndex(key).Interface()
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
for _, field := range (&Scope{Value: values}).Fields() {
|
for _, field := range (&Scope{Value: values}).Fields() {
|
||||||
@ -907,7 +917,7 @@ func (scope *Scope) updatedAttrsWithValues(value interface{}) (results map[strin
|
|||||||
results[field.DBName] = value
|
results[field.DBName] = value
|
||||||
} else {
|
} else {
|
||||||
err := field.Set(value)
|
err := field.Set(value)
|
||||||
if field.IsNormal {
|
if field.IsNormal && !field.IsIgnored {
|
||||||
hasUpdate = true
|
hasUpdate = true
|
||||||
if err == ErrUnaddressable {
|
if err == ErrUnaddressable {
|
||||||
results[field.DBName] = value
|
results[field.DBName] = value
|
||||||
@ -1113,8 +1123,8 @@ func (scope *Scope) createJoinTable(field *StructField) {
|
|||||||
if field, ok := scope.FieldByName(fieldName); ok {
|
if field, ok := scope.FieldByName(fieldName); ok {
|
||||||
foreignKeyStruct := field.clone()
|
foreignKeyStruct := field.clone()
|
||||||
foreignKeyStruct.IsPrimaryKey = false
|
foreignKeyStruct.IsPrimaryKey = false
|
||||||
foreignKeyStruct.TagSettings["IS_JOINTABLE_FOREIGNKEY"] = "true"
|
foreignKeyStruct.TagSettingsSet("IS_JOINTABLE_FOREIGNKEY", "true")
|
||||||
delete(foreignKeyStruct.TagSettings, "AUTO_INCREMENT")
|
foreignKeyStruct.TagSettingsDelete("AUTO_INCREMENT")
|
||||||
sqlTypes = append(sqlTypes, scope.Quote(relationship.ForeignDBNames[idx])+" "+scope.Dialect().DataTypeOf(foreignKeyStruct))
|
sqlTypes = append(sqlTypes, scope.Quote(relationship.ForeignDBNames[idx])+" "+scope.Dialect().DataTypeOf(foreignKeyStruct))
|
||||||
primaryKeys = append(primaryKeys, scope.Quote(relationship.ForeignDBNames[idx]))
|
primaryKeys = append(primaryKeys, scope.Quote(relationship.ForeignDBNames[idx]))
|
||||||
}
|
}
|
||||||
@ -1124,8 +1134,8 @@ func (scope *Scope) createJoinTable(field *StructField) {
|
|||||||
if field, ok := toScope.FieldByName(fieldName); ok {
|
if field, ok := toScope.FieldByName(fieldName); ok {
|
||||||
foreignKeyStruct := field.clone()
|
foreignKeyStruct := field.clone()
|
||||||
foreignKeyStruct.IsPrimaryKey = false
|
foreignKeyStruct.IsPrimaryKey = false
|
||||||
foreignKeyStruct.TagSettings["IS_JOINTABLE_FOREIGNKEY"] = "true"
|
foreignKeyStruct.TagSettingsSet("IS_JOINTABLE_FOREIGNKEY", "true")
|
||||||
delete(foreignKeyStruct.TagSettings, "AUTO_INCREMENT")
|
foreignKeyStruct.TagSettingsDelete("AUTO_INCREMENT")
|
||||||
sqlTypes = append(sqlTypes, scope.Quote(relationship.AssociationForeignDBNames[idx])+" "+scope.Dialect().DataTypeOf(foreignKeyStruct))
|
sqlTypes = append(sqlTypes, scope.Quote(relationship.AssociationForeignDBNames[idx])+" "+scope.Dialect().DataTypeOf(foreignKeyStruct))
|
||||||
primaryKeys = append(primaryKeys, scope.Quote(relationship.AssociationForeignDBNames[idx]))
|
primaryKeys = append(primaryKeys, scope.Quote(relationship.AssociationForeignDBNames[idx]))
|
||||||
}
|
}
|
||||||
@ -1260,7 +1270,7 @@ func (scope *Scope) autoIndex() *Scope {
|
|||||||
var uniqueIndexes = map[string][]string{}
|
var uniqueIndexes = map[string][]string{}
|
||||||
|
|
||||||
for _, field := range scope.GetStructFields() {
|
for _, field := range scope.GetStructFields() {
|
||||||
if name, ok := field.TagSettings["INDEX"]; ok {
|
if name, ok := field.TagSettingsGet("INDEX"); ok {
|
||||||
names := strings.Split(name, ",")
|
names := strings.Split(name, ",")
|
||||||
|
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
@ -1271,7 +1281,7 @@ func (scope *Scope) autoIndex() *Scope {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if name, ok := field.TagSettings["UNIQUE_INDEX"]; ok {
|
if name, ok := field.TagSettingsGet("UNIQUE_INDEX"); ok {
|
||||||
names := strings.Split(name, ",")
|
names := strings.Split(name, ",")
|
||||||
|
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
|
63
utils.go
63
utils.go
@ -1,7 +1,6 @@
|
|||||||
package gorm
|
package gorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
@ -58,66 +57,6 @@ func newSafeMap() *safeMap {
|
|||||||
return &safeMap{l: new(sync.RWMutex), m: make(map[string]string)}
|
return &safeMap{l: new(sync.RWMutex), m: make(map[string]string)}
|
||||||
}
|
}
|
||||||
|
|
||||||
var smap = newSafeMap()
|
|
||||||
|
|
||||||
type strCase bool
|
|
||||||
|
|
||||||
const (
|
|
||||||
lower strCase = false
|
|
||||||
upper strCase = true
|
|
||||||
)
|
|
||||||
|
|
||||||
// ToDBName convert string to db name
|
|
||||||
func ToDBName(name string) string {
|
|
||||||
if v := smap.Get(name); v != "" {
|
|
||||||
return v
|
|
||||||
}
|
|
||||||
|
|
||||||
if name == "" {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
value = commonInitialismsReplacer.Replace(name)
|
|
||||||
buf = bytes.NewBufferString("")
|
|
||||||
lastCase, currCase, nextCase, nextNumber strCase
|
|
||||||
)
|
|
||||||
|
|
||||||
for i, v := range value[:len(value)-1] {
|
|
||||||
nextCase = strCase(value[i+1] >= 'A' && value[i+1] <= 'Z')
|
|
||||||
nextNumber = strCase(value[i+1] >= '0' && value[i+1] <= '9')
|
|
||||||
|
|
||||||
if i > 0 {
|
|
||||||
if currCase == upper {
|
|
||||||
if lastCase == upper && (nextCase == upper || nextNumber == upper) {
|
|
||||||
buf.WriteRune(v)
|
|
||||||
} else {
|
|
||||||
if value[i-1] != '_' && value[i+1] != '_' {
|
|
||||||
buf.WriteRune('_')
|
|
||||||
}
|
|
||||||
buf.WriteRune(v)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
buf.WriteRune(v)
|
|
||||||
if i == len(value)-2 && (nextCase == upper && nextNumber == lower) {
|
|
||||||
buf.WriteRune('_')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
currCase = upper
|
|
||||||
buf.WriteRune(v)
|
|
||||||
}
|
|
||||||
lastCase = currCase
|
|
||||||
currCase = nextCase
|
|
||||||
}
|
|
||||||
|
|
||||||
buf.WriteByte(value[len(value)-1])
|
|
||||||
|
|
||||||
s := strings.ToLower(buf.String())
|
|
||||||
smap.Set(name, s)
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
// SQL expression
|
// SQL expression
|
||||||
type expr struct {
|
type expr struct {
|
||||||
expr string
|
expr string
|
||||||
@ -267,7 +206,7 @@ func getValueFromFields(value reflect.Value, fieldNames []string) (results []int
|
|||||||
// as FieldByName could panic
|
// as FieldByName could panic
|
||||||
if indirectValue := reflect.Indirect(value); indirectValue.IsValid() {
|
if indirectValue := reflect.Indirect(value); indirectValue.IsValid() {
|
||||||
for _, fieldName := range fieldNames {
|
for _, fieldName := range fieldNames {
|
||||||
if fieldValue := indirectValue.FieldByName(fieldName); fieldValue.IsValid() {
|
if fieldValue := reflect.Indirect(indirectValue.FieldByName(fieldName)); fieldValue.IsValid() {
|
||||||
result := fieldValue.Interface()
|
result := fieldValue.Interface()
|
||||||
if r, ok := result.(driver.Valuer); ok {
|
if r, ok := result.(driver.Valuer); ok {
|
||||||
result, _ = r.Value()
|
result, _ = r.Value()
|
||||||
|
@ -1,35 +0,0 @@
|
|||||||
package gorm_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/jinzhu/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestToDBNameGenerateFriendlyName(t *testing.T) {
|
|
||||||
var maps = map[string]string{
|
|
||||||
"": "",
|
|
||||||
"X": "x",
|
|
||||||
"ThisIsATest": "this_is_a_test",
|
|
||||||
"PFAndESI": "pf_and_esi",
|
|
||||||
"AbcAndJkl": "abc_and_jkl",
|
|
||||||
"EmployeeID": "employee_id",
|
|
||||||
"SKU_ID": "sku_id",
|
|
||||||
"UTF8": "utf8",
|
|
||||||
"Level1": "level1",
|
|
||||||
"SHA256Hash": "sha256_hash",
|
|
||||||
"FieldX": "field_x",
|
|
||||||
"HTTPAndSMTP": "http_and_smtp",
|
|
||||||
"HTTPServerHandlerForURLID": "http_server_handler_for_url_id",
|
|
||||||
"UUID": "uuid",
|
|
||||||
"HTTPURL": "http_url",
|
|
||||||
"HTTP_URL": "http_url",
|
|
||||||
"ThisIsActuallyATestSoWeMayBeAbleToUseThisCodeInGormPackageAlsoIdCanBeUsedAtTheEndAsID": "this_is_actually_a_test_so_we_may_be_able_to_use_this_code_in_gorm_package_also_id_can_be_used_at_the_end_as_id",
|
|
||||||
}
|
|
||||||
|
|
||||||
for key, value := range maps {
|
|
||||||
if gorm.ToDBName(key) != value {
|
|
||||||
t.Errorf("%v ToDBName should equal %v, but got %v", key, value, gorm.ToDBName(key))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user