fix batch insert interface slice be panic #1598

Merged
lunny merged 1 commits from joelBai/xorm:fix_batch_insert_interface_slice into master 2 years ago
  1. 8
      session_insert.go
  2. 31
      session_insert_test.go

8
session_insert.go

@ -122,7 +122,13 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error @@ -122,7 +122,13 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error
for i := 0; i < size; i++ {
v := sliceValue.Index(i)
vv := reflect.Indirect(v)
var vv reflect.Value
switch v.Kind() {
case reflect.Interface:
vv = reflect.Indirect(v.Elem())
default:
vv = reflect.Indirect(v)
}
elemValue := v.Interface()
var colPlaces []string

31
session_insert_test.go

@ -433,6 +433,37 @@ func TestInsertMulti2(t *testing.T) { @@ -433,6 +433,37 @@ func TestInsertMulti2(t *testing.T) {
assert.EqualValues(t, len(users2), cnt)
}
func TestInsertMulti2Interface(t *testing.T) {
assert.NoError(t, prepareEngine())
assertSync(t, new(Userinfo))
users := []interface{}{
Userinfo{Username: "xlw", Departname: "dev", Alias: "lunny2", Created: time.Now()},
Userinfo{Username: "xlw2", Departname: "dev", Alias: "lunny3", Created: time.Now()},
Userinfo{Username: "xlw11", Departname: "dev", Alias: "lunny2", Created: time.Now()},
Userinfo{Username: "xlw22", Departname: "dev", Alias: "lunny3", Created: time.Now()},
}
cnt, err := testEngine.Insert(&users)
if err != nil {
t.Error(err)
panic(err)
}
assert.EqualValues(t, len(users), cnt)
users2 := []interface{}{
&Userinfo{Username: "1xlw", Departname: "dev", Alias: "lunny2", Created: time.Now()},
&Userinfo{Username: "1xlw2", Departname: "dev", Alias: "lunny3", Created: time.Now()},
&Userinfo{Username: "1xlw11", Departname: "dev", Alias: "lunny2", Created: time.Now()},
&Userinfo{Username: "1xlw22", Departname: "dev", Alias: "lunny3", Created: time.Now()},
}
cnt, err = testEngine.Insert(&users2)
assert.NoError(t, err)
assert.EqualValues(t, len(users2), cnt)
}
func TestInsertTwoTable(t *testing.T) {
assert.NoError(t, prepareEngine())

Loading…
Cancel
Save