123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- package utils
- import (
- "errors"
- "github.com/go-xorm/xorm"
- "reflect"
- )
- // 分页插入数据库
- func Insert(db *xorm.Engine, beans ...interface{}) error {
- return InsertWithSize(db, 500, beans...)
- }
- func InsertWithSize(db *xorm.Engine, size int, beans ...interface{}) error {
- if nil == db {
- return errors.New(`db can not be nil`)
- }
- if nil == beans || len(beans) == 0 {
- return nil
- }
- trans := db.NewSession()
- defer trans.Close()
- _ = trans.Begin()
- err := TransInsertWithSize(trans, size, beans...)
- if nil != err {
- _ = trans.Rollback()
- return err
- }
- return trans.Commit()
- }
- func TransInsert(trans *xorm.Session, beans ...interface{}) error {
- return TransInsertWithSize(trans, 500, beans...)
- }
- func TransInsertWithSize(trans *xorm.Session, size int, beans ...interface{}) error {
- if nil == trans {
- return errors.New(`trans can not be nil`)
- }
- if nil == beans || len(beans) == 0 {
- return nil
- }
- if size < 100 {
- size = 500
- }
- var objects []interface{}
- for _, bean := range beans {
- sliceValue := reflect.Indirect(reflect.ValueOf(bean))
- if sliceValue.Kind() == reflect.Slice {
- for i := 0; i < sliceValue.Len(); i++ {
- v := sliceValue.Index(i)
- elemValue := v.Interface()
- objects = append(objects, elemValue)
- }
- } else {
- objects = append(objects, bean)
- }
- }
- total := len(objects)
- var err error
- if total <= size {
- _, err = trans.Insert(&objects)
- return err
- }
- for i := 0; i < total; i += size {
- end := i + size
- if end > total {
- end = total
- }
- arr := objects[i:end]
- _, err = trans.Insert(&arr)
- if nil != err {
- return err
- }
- }
- return err
- }
|