xorm: Move for loop that finds inserted columns outside of iteration over all values. (#102700)
* Move for loop that finds inserted columns outside of iteration over all values.
This commit is contained in:
@@ -120,13 +120,42 @@ func (session *Session) innerInsertMulti(rowsSlicePtr any) (int64, error) {
|
||||
}
|
||||
|
||||
table := session.statement.RefTable
|
||||
size := sliceValue.Len()
|
||||
firstElement := sliceValue.Index(0)
|
||||
firstValue := reflect.Indirect(firstElement)
|
||||
|
||||
var colNames []string
|
||||
var colMultiPlaces []string
|
||||
var args []any
|
||||
var cols []*core.Column
|
||||
|
||||
// Find columns that will be in the INSERT statement.
|
||||
for _, col := range table.Columns() {
|
||||
ptrFieldValue, err := col.ValueOfV(&firstValue)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
fieldValue := *ptrFieldValue
|
||||
if col.IsAutoIncrement && isZero(fieldValue.Interface()) {
|
||||
continue
|
||||
}
|
||||
if col.MapType == core.ONLYFROMDB {
|
||||
continue
|
||||
}
|
||||
if col.IsDeleted {
|
||||
continue
|
||||
}
|
||||
if session.statement.omitColumnMap.contain(col.Name) {
|
||||
continue
|
||||
}
|
||||
if len(session.statement.columnMap) > 0 && !session.statement.columnMap.contain(col.Name) {
|
||||
continue
|
||||
}
|
||||
|
||||
colNames = append(colNames, col.Name)
|
||||
cols = append(cols, col)
|
||||
}
|
||||
|
||||
var colMultiPlaces []string
|
||||
var args []any
|
||||
size := sliceValue.Len()
|
||||
for i := 0; i < size; i++ {
|
||||
v := sliceValue.Index(i)
|
||||
vv := reflect.Indirect(v)
|
||||
@@ -143,105 +172,38 @@ func (session *Session) innerInsertMulti(rowsSlicePtr any) (int64, error) {
|
||||
processor.BeforeInsert()
|
||||
}
|
||||
|
||||
if i == 0 {
|
||||
for _, col := range table.Columns() {
|
||||
ptrFieldValue, err := col.ValueOfV(&vv)
|
||||
for _, col := range cols {
|
||||
ptrFieldValue, err := col.ValueOfV(&vv)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
fieldValue := *ptrFieldValue
|
||||
|
||||
if (col.IsCreated || col.IsUpdated) && session.statement.UseAutoTime {
|
||||
val, t := session.engine.nowTime(col)
|
||||
args = append(args, val)
|
||||
|
||||
var colName = col.Name
|
||||
session.afterClosures = append(session.afterClosures, func(bean any) {
|
||||
col := table.GetColumn(colName)
|
||||
setColumnTime(bean, col, t)
|
||||
})
|
||||
} else if col.IsVersion && session.statement.checkVersion {
|
||||
args = append(args, 1)
|
||||
var colName = col.Name
|
||||
session.afterClosures = append(session.afterClosures, func(bean any) {
|
||||
col := table.GetColumn(colName)
|
||||
setColumnInt(bean, col, 1)
|
||||
})
|
||||
} else {
|
||||
arg, err := session.value2Interface(col, fieldValue)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
fieldValue := *ptrFieldValue
|
||||
if col.IsAutoIncrement && isZero(fieldValue.Interface()) {
|
||||
continue
|
||||
}
|
||||
if col.MapType == core.ONLYFROMDB {
|
||||
continue
|
||||
}
|
||||
if col.IsDeleted {
|
||||
continue
|
||||
}
|
||||
if session.statement.omitColumnMap.contain(col.Name) {
|
||||
continue
|
||||
}
|
||||
if len(session.statement.columnMap) > 0 && !session.statement.columnMap.contain(col.Name) {
|
||||
continue
|
||||
}
|
||||
if (col.IsCreated || col.IsUpdated) && session.statement.UseAutoTime {
|
||||
val, t := session.engine.nowTime(col)
|
||||
args = append(args, val)
|
||||
|
||||
var colName = col.Name
|
||||
session.afterClosures = append(session.afterClosures, func(bean any) {
|
||||
col := table.GetColumn(colName)
|
||||
setColumnTime(bean, col, t)
|
||||
})
|
||||
} else if col.IsVersion && session.statement.checkVersion {
|
||||
args = append(args, 1)
|
||||
var colName = col.Name
|
||||
session.afterClosures = append(session.afterClosures, func(bean any) {
|
||||
col := table.GetColumn(colName)
|
||||
setColumnInt(bean, col, 1)
|
||||
})
|
||||
} else {
|
||||
arg, err := session.value2Interface(col, fieldValue)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
args = append(args, arg)
|
||||
}
|
||||
|
||||
colNames = append(colNames, col.Name)
|
||||
cols = append(cols, col)
|
||||
colPlaces = append(colPlaces, "?")
|
||||
args = append(args, arg)
|
||||
}
|
||||
} else {
|
||||
for _, col := range cols {
|
||||
ptrFieldValue, err := col.ValueOfV(&vv)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
fieldValue := *ptrFieldValue
|
||||
|
||||
if col.IsAutoIncrement && isZero(fieldValue.Interface()) {
|
||||
continue
|
||||
}
|
||||
if col.MapType == core.ONLYFROMDB {
|
||||
continue
|
||||
}
|
||||
if col.IsDeleted {
|
||||
continue
|
||||
}
|
||||
if session.statement.omitColumnMap.contain(col.Name) {
|
||||
continue
|
||||
}
|
||||
if len(session.statement.columnMap) > 0 && !session.statement.columnMap.contain(col.Name) {
|
||||
continue
|
||||
}
|
||||
if (col.IsCreated || col.IsUpdated) && session.statement.UseAutoTime {
|
||||
val, t := session.engine.nowTime(col)
|
||||
args = append(args, val)
|
||||
|
||||
var colName = col.Name
|
||||
session.afterClosures = append(session.afterClosures, func(bean any) {
|
||||
col := table.GetColumn(colName)
|
||||
setColumnTime(bean, col, t)
|
||||
})
|
||||
} else if col.IsVersion && session.statement.checkVersion {
|
||||
args = append(args, 1)
|
||||
var colName = col.Name
|
||||
session.afterClosures = append(session.afterClosures, func(bean any) {
|
||||
col := table.GetColumn(colName)
|
||||
setColumnInt(bean, col, 1)
|
||||
})
|
||||
} else {
|
||||
arg, err := session.value2Interface(col, fieldValue)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
args = append(args, arg)
|
||||
}
|
||||
|
||||
colPlaces = append(colPlaces, "?")
|
||||
}
|
||||
colPlaces = append(colPlaces, "?")
|
||||
}
|
||||
colMultiPlaces = append(colMultiPlaces, strings.Join(colPlaces, ", "))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user