Merge branch 'master' of github.com:go-xorm/xorm
This commit is contained in:
commit
e01b55f035
57
helpers.go
57
helpers.go
|
@ -12,7 +12,6 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-xorm/core"
|
"github.com/go-xorm/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -196,26 +195,44 @@ func isArrayValueZero(v reflect.Value) bool {
|
||||||
|
|
||||||
func int64ToIntValue(id int64, tp reflect.Type) reflect.Value {
|
func int64ToIntValue(id int64, tp reflect.Type) reflect.Value {
|
||||||
var v interface{}
|
var v interface{}
|
||||||
switch tp.Kind() {
|
kind := tp.Kind()
|
||||||
case reflect.Int16:
|
|
||||||
v = int16(id)
|
if kind == reflect.Ptr {
|
||||||
case reflect.Int32:
|
kind = tp.Elem().Kind()
|
||||||
v = int32(id)
|
|
||||||
case reflect.Int:
|
|
||||||
v = int(id)
|
|
||||||
case reflect.Int64:
|
|
||||||
v = id
|
|
||||||
case reflect.Uint16:
|
|
||||||
v = uint16(id)
|
|
||||||
case reflect.Uint32:
|
|
||||||
v = uint32(id)
|
|
||||||
case reflect.Uint64:
|
|
||||||
v = uint64(id)
|
|
||||||
case reflect.Uint:
|
|
||||||
v = uint(id)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch kind {
|
||||||
|
case reflect.Int16:
|
||||||
|
temp := int16(id)
|
||||||
|
v = &temp
|
||||||
|
case reflect.Int32:
|
||||||
|
temp := int32(id)
|
||||||
|
v = &temp
|
||||||
|
case reflect.Int:
|
||||||
|
temp := int(id)
|
||||||
|
v = &temp
|
||||||
|
case reflect.Int64:
|
||||||
|
temp := id
|
||||||
|
v = &temp
|
||||||
|
case reflect.Uint16:
|
||||||
|
temp := uint16(id)
|
||||||
|
v = &temp
|
||||||
|
case reflect.Uint32:
|
||||||
|
temp := uint32(id)
|
||||||
|
v = &temp
|
||||||
|
case reflect.Uint64:
|
||||||
|
temp := uint64(id)
|
||||||
|
v = &temp
|
||||||
|
case reflect.Uint:
|
||||||
|
temp := uint(id)
|
||||||
|
v = &temp
|
||||||
|
}
|
||||||
|
|
||||||
|
if tp.Kind() == reflect.Ptr {
|
||||||
return reflect.ValueOf(v).Convert(tp)
|
return reflect.ValueOf(v).Convert(tp)
|
||||||
}
|
}
|
||||||
|
return reflect.ValueOf(v).Elem().Convert(tp)
|
||||||
|
}
|
||||||
|
|
||||||
func int64ToInt(id int64, tp reflect.Type) interface{} {
|
func int64ToInt(id int64, tp reflect.Type) interface{} {
|
||||||
return int64ToIntValue(id, tp).Interface()
|
return int64ToIntValue(id, tp).Interface()
|
||||||
|
@ -537,6 +554,10 @@ func genCols(table *core.Table, session *Session, bean interface{}, useCol bool,
|
||||||
if len(fieldValue.String()) == 0 {
|
if len(fieldValue.String()) == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
case reflect.Ptr:
|
||||||
|
if fieldValue.Pointer() == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -67,7 +67,7 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error
|
||||||
return 0, errors.New("could not insert a empty slice")
|
return 0, errors.New("could not insert a empty slice")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := session.Statement.setRefValue(sliceValue.Index(0)); err != nil {
|
if err := session.Statement.setRefValue(reflect.ValueOf(sliceValue.Index(0).Interface())); err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,8 @@
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -26,3 +28,111 @@ func TestInsertOne(t *testing.T) {
|
||||||
_, err := testEngine.InsertOne(data)
|
_, err := testEngine.InsertOne(data)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInsertMulti(t *testing.T) {
|
||||||
|
|
||||||
|
assert.NoError(t, prepareEngine())
|
||||||
|
type TestMulti struct {
|
||||||
|
Id int64 `xorm:"int(11) pk"`
|
||||||
|
Name string `xorm:"varchar(255)"`
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.NoError(t, testEngine.Sync2(new(TestMulti)))
|
||||||
|
|
||||||
|
num, err := insertMultiDatas(1,
|
||||||
|
append([]TestMulti{}, TestMulti{1, "test1"}, TestMulti{2, "test2"}, TestMulti{3, "test3"}))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, 3, num)
|
||||||
|
}
|
||||||
|
|
||||||
|
func insertMultiDatas(step int, datas interface{}) (num int64, err error) {
|
||||||
|
sliceValue := reflect.Indirect(reflect.ValueOf(datas))
|
||||||
|
var iLen int64
|
||||||
|
if sliceValue.Kind() != reflect.Slice {
|
||||||
|
return 0, fmt.Errorf("not silce")
|
||||||
|
}
|
||||||
|
iLen = int64(sliceValue.Len())
|
||||||
|
if iLen == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
session := testEngine.NewSession()
|
||||||
|
defer session.Close()
|
||||||
|
|
||||||
|
if err = callbackLooper(datas, step,
|
||||||
|
func(innerDatas interface{}) error {
|
||||||
|
n, e := session.InsertMulti(innerDatas)
|
||||||
|
if e != nil {
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
num += n
|
||||||
|
return nil
|
||||||
|
}); err != nil {
|
||||||
|
return 0, err
|
||||||
|
} else if num != iLen {
|
||||||
|
return 0, fmt.Errorf("num error: %d - %d", num, iLen)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func callbackLooper(datas interface{}, step int, actionFunc func(interface{}) error) (err error) {
|
||||||
|
|
||||||
|
sliceValue := reflect.Indirect(reflect.ValueOf(datas))
|
||||||
|
if sliceValue.Kind() != reflect.Slice {
|
||||||
|
return fmt.Errorf("not slice")
|
||||||
|
}
|
||||||
|
if sliceValue.Len() <= 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
tempLen := 0
|
||||||
|
processedLen := sliceValue.Len()
|
||||||
|
for i := 0; i < sliceValue.Len(); i += step {
|
||||||
|
if processedLen > step {
|
||||||
|
tempLen = i + step
|
||||||
|
} else {
|
||||||
|
tempLen = sliceValue.Len()
|
||||||
|
}
|
||||||
|
var tempInterface []interface{}
|
||||||
|
for j := i; j < tempLen; j++ {
|
||||||
|
tempInterface = append(tempInterface, sliceValue.Index(j).Interface())
|
||||||
|
}
|
||||||
|
if err = actionFunc(tempInterface); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
processedLen = processedLen - step
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInsertOneIfPkIsPoint(t *testing.T) {
|
||||||
|
assert.NoError(t, prepareEngine())
|
||||||
|
|
||||||
|
type TestPoint struct {
|
||||||
|
Id *int64 `xorm:"autoincr pk notnull 'id'"`
|
||||||
|
Msg *string `xorm:"varchar(255)"`
|
||||||
|
Created *time.Time `xorm:"created"`
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.NoError(t, testEngine.Sync2(new(TestPoint)))
|
||||||
|
msg := "hi"
|
||||||
|
data := TestPoint{Msg: &msg}
|
||||||
|
_, err := testEngine.InsertOne(&data)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInsertOneIfPkIsPointRename(t *testing.T) {
|
||||||
|
assert.NoError(t, prepareEngine())
|
||||||
|
type ID *int64
|
||||||
|
type TestPoint struct {
|
||||||
|
Id ID `xorm:"autoincr pk notnull 'id'"`
|
||||||
|
Msg *string `xorm:"varchar(255)"`
|
||||||
|
Created *time.Time `xorm:"created"`
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.NoError(t, testEngine.Sync2(new(TestPoint)))
|
||||||
|
msg := "hi"
|
||||||
|
data := TestPoint{Msg: &msg}
|
||||||
|
_, err := testEngine.InsertOne(&data)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue