forked from mirror/gorm
change the method of initializing slice (#4097)
* change the method of initializing slice and fixed the length to be specified as 0 * keep the association.go code in the var group * keep the association.go code in the var group * change to initializing in var group
This commit is contained in:
parent
664755270d
commit
adf85d5b82
|
@ -39,7 +39,7 @@ func SaveBeforeAssociations(create bool) func(db *gorm.DB) {
|
||||||
switch db.Statement.ReflectValue.Kind() {
|
switch db.Statement.ReflectValue.Kind() {
|
||||||
case reflect.Slice, reflect.Array:
|
case reflect.Slice, reflect.Array:
|
||||||
var (
|
var (
|
||||||
objs []reflect.Value
|
objs = make([]reflect.Value, 0, db.Statement.ReflectValue.Len())
|
||||||
fieldType = rel.Field.FieldType
|
fieldType = rel.Field.FieldType
|
||||||
isPtr = fieldType.Kind() == reflect.Ptr
|
isPtr = fieldType.Kind() == reflect.Ptr
|
||||||
)
|
)
|
||||||
|
@ -140,7 +140,7 @@ func SaveAfterAssociations(create bool) func(db *gorm.DB) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if elems.Len() > 0 {
|
if elems.Len() > 0 {
|
||||||
assignmentColumns := []string{}
|
assignmentColumns := make([]string, 0, len(rel.References))
|
||||||
for _, ref := range rel.References {
|
for _, ref := range rel.References {
|
||||||
assignmentColumns = append(assignmentColumns, ref.ForeignKey.DBName)
|
assignmentColumns = append(assignmentColumns, ref.ForeignKey.DBName)
|
||||||
}
|
}
|
||||||
|
@ -154,7 +154,7 @@ func SaveAfterAssociations(create bool) func(db *gorm.DB) {
|
||||||
f = f.Addr()
|
f = f.Addr()
|
||||||
}
|
}
|
||||||
|
|
||||||
assignmentColumns := []string{}
|
assignmentColumns := make([]string, 0, len(rel.References))
|
||||||
for _, ref := range rel.References {
|
for _, ref := range rel.References {
|
||||||
if ref.OwnPrimaryKey {
|
if ref.OwnPrimaryKey {
|
||||||
fv, _ := ref.PrimaryKey.ValueOf(db.Statement.ReflectValue)
|
fv, _ := ref.PrimaryKey.ValueOf(db.Statement.ReflectValue)
|
||||||
|
@ -219,7 +219,7 @@ func SaveAfterAssociations(create bool) func(db *gorm.DB) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if elems.Len() > 0 {
|
if elems.Len() > 0 {
|
||||||
assignmentColumns := []string{}
|
assignmentColumns := make([]string, 0, len(rel.References))
|
||||||
for _, ref := range rel.References {
|
for _, ref := range rel.References {
|
||||||
assignmentColumns = append(assignmentColumns, ref.ForeignKey.DBName)
|
assignmentColumns = append(assignmentColumns, ref.ForeignKey.DBName)
|
||||||
}
|
}
|
||||||
|
@ -324,7 +324,7 @@ func onConflictOption(stmt *gorm.Statement, s *schema.Schema, selectColumns map[
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(defaultUpdatingColumns) > 0 {
|
if len(defaultUpdatingColumns) > 0 {
|
||||||
var columns []clause.Column
|
columns := make([]clause.Column, 0, len(s.PrimaryFieldDBNames))
|
||||||
for _, dbName := range s.PrimaryFieldDBNames {
|
for _, dbName := range s.PrimaryFieldDBNames {
|
||||||
columns = append(columns, clause.Column{Name: dbName})
|
columns = append(columns, clause.Column{Name: dbName})
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,7 @@ func DeleteBeforeAssociations(db *gorm.DB) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(db.Statement.Selects) > 0 {
|
if len(db.Statement.Selects) > 0 {
|
||||||
var selects []string
|
selects := make([]string, 0, len(db.Statement.Selects))
|
||||||
for _, s := range db.Statement.Selects {
|
for _, s := range db.Statement.Selects {
|
||||||
if s == clause.Associations {
|
if s == clause.Associations {
|
||||||
selects = append(selects, s)
|
selects = append(selects, s)
|
||||||
|
@ -69,9 +69,9 @@ func DeleteBeforeAssociations(db *gorm.DB) {
|
||||||
}
|
}
|
||||||
case schema.Many2Many:
|
case schema.Many2Many:
|
||||||
var (
|
var (
|
||||||
queryConds []clause.Expression
|
queryConds = make([]clause.Expression, 0, len(rel.References))
|
||||||
foreignFields []*schema.Field
|
foreignFields = make([]*schema.Field, 0, len(rel.References))
|
||||||
relForeignKeys []string
|
relForeignKeys = make([]string, 0, len(rel.References))
|
||||||
modelValue = reflect.New(rel.JoinTable.ModelType).Interface()
|
modelValue = reflect.New(rel.JoinTable.ModelType).Interface()
|
||||||
table = rel.JoinTable.Table
|
table = rel.JoinTable.Table
|
||||||
tx = db.Session(&gorm.Session{NewDB: true}).Model(modelValue).Table(table)
|
tx = db.Session(&gorm.Session{NewDB: true}).Model(modelValue).Table(table)
|
||||||
|
|
|
@ -27,8 +27,13 @@ func preload(db *gorm.DB, rel *schema.Relationship, conds []interface{}, preload
|
||||||
})
|
})
|
||||||
|
|
||||||
if rel.JoinTable != nil {
|
if rel.JoinTable != nil {
|
||||||
var joinForeignFields, joinRelForeignFields []*schema.Field
|
|
||||||
var joinForeignKeys []string
|
var (
|
||||||
|
joinForeignFields = make([]*schema.Field, 0, len(rel.References))
|
||||||
|
joinRelForeignFields = make([]*schema.Field, 0, len(rel.References))
|
||||||
|
joinForeignKeys = make([]string, 0, len(rel.References))
|
||||||
|
)
|
||||||
|
|
||||||
for _, ref := range rel.References {
|
for _, ref := range rel.References {
|
||||||
if ref.OwnPrimaryKey {
|
if ref.OwnPrimaryKey {
|
||||||
joinForeignKeys = append(joinForeignKeys, ref.ForeignKey.DBName)
|
joinForeignKeys = append(joinForeignKeys, ref.ForeignKey.DBName)
|
||||||
|
|
|
@ -92,7 +92,7 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
var commonInitialismsForReplacer []string
|
commonInitialismsForReplacer := make([]string, 0, len(commonInitialisms))
|
||||||
for _, initialism := range commonInitialisms {
|
for _, initialism := range commonInitialisms {
|
||||||
commonInitialismsForReplacer = append(commonInitialismsForReplacer, initialism, strings.Title(strings.ToLower(initialism)))
|
commonInitialismsForReplacer = append(commonInitialismsForReplacer, initialism, strings.Title(strings.ToLower(initialism)))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue