change the method of initializing slice and fixed the length to be specified as 0
This commit is contained in:
parent
73d44a4f97
commit
16bda61a03
@ -39,10 +39,10 @@ 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
|
|
||||||
fieldType = rel.Field.FieldType
|
fieldType = rel.Field.FieldType
|
||||||
isPtr = fieldType.Kind() == reflect.Ptr
|
isPtr = fieldType.Kind() == reflect.Ptr
|
||||||
)
|
)
|
||||||
|
objs := make([]reflect.Value, 0, db.Statement.ReflectValue.Len())
|
||||||
|
|
||||||
if !isPtr {
|
if !isPtr {
|
||||||
fieldType = reflect.PtrTo(fieldType)
|
fieldType = reflect.PtrTo(fieldType)
|
||||||
@ -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})
|
||||||
}
|
}
|
||||||
@ -340,10 +340,11 @@ func onConflictOption(stmt *gorm.Statement, s *schema.Schema, selectColumns map[
|
|||||||
|
|
||||||
func saveAssociations(db *gorm.DB, rel *schema.Relationship, values interface{}, selectColumns map[string]bool, restricted bool, defaultUpdatingColumns []string) error {
|
func saveAssociations(db *gorm.DB, rel *schema.Relationship, values interface{}, selectColumns map[string]bool, restricted bool, defaultUpdatingColumns []string) error {
|
||||||
var (
|
var (
|
||||||
selects, omits []string
|
onConflict = onConflictOption(db.Statement, rel.FieldSchema, selectColumns, restricted, defaultUpdatingColumns)
|
||||||
onConflict = onConflictOption(db.Statement, rel.FieldSchema, selectColumns, restricted, defaultUpdatingColumns)
|
refName = rel.Name + "."
|
||||||
refName = rel.Name + "."
|
|
||||||
)
|
)
|
||||||
|
selects := make([]string, 0, len(selectColumns))
|
||||||
|
omits := make([]string, 0, len(selectColumns))
|
||||||
|
|
||||||
for name, ok := range selectColumns {
|
for name, ok := range selectColumns {
|
||||||
columnName := ""
|
columnName := ""
|
||||||
|
@ -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,14 +69,15 @@ func DeleteBeforeAssociations(db *gorm.DB) {
|
|||||||
}
|
}
|
||||||
case schema.Many2Many:
|
case schema.Many2Many:
|
||||||
var (
|
var (
|
||||||
queryConds []clause.Expression
|
modelValue = reflect.New(rel.JoinTable.ModelType).Interface()
|
||||||
foreignFields []*schema.Field
|
table = rel.JoinTable.Table
|
||||||
relForeignKeys []string
|
tx = db.Session(&gorm.Session{NewDB: true}).Model(modelValue).Table(table)
|
||||||
modelValue = reflect.New(rel.JoinTable.ModelType).Interface()
|
|
||||||
table = rel.JoinTable.Table
|
|
||||||
tx = db.Session(&gorm.Session{NewDB: true}).Model(modelValue).Table(table)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
queryConds := make([]clause.Expression, 0, len(rel.References))
|
||||||
|
foreignFields := make([]*schema.Field, 0, len(rel.References))
|
||||||
|
relForeignKeys := make([]string, 0, len(rel.References))
|
||||||
|
|
||||||
for _, ref := range rel.References {
|
for _, ref := range rel.References {
|
||||||
if ref.OwnPrimaryKey {
|
if ref.OwnPrimaryKey {
|
||||||
foreignFields = append(foreignFields, ref.PrimaryKey)
|
foreignFields = append(foreignFields, ref.PrimaryKey)
|
||||||
|
@ -27,8 +27,10 @@ func preload(db *gorm.DB, rel *schema.Relationship, conds []interface{}, preload
|
|||||||
})
|
})
|
||||||
|
|
||||||
if rel.JoinTable != nil {
|
if rel.JoinTable != nil {
|
||||||
var joinForeignFields, joinRelForeignFields []*schema.Field
|
joinForeignFields := make([]*schema.Field, 0, len(rel.References))
|
||||||
var joinForeignKeys []string
|
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…
x
Reference in New Issue
Block a user