add method
This commit is contained in:
parent
b444011d09
commit
f7b5934161
@ -4,6 +4,7 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -26,6 +27,23 @@ func (db *DB) Create(value interface{}) (tx *DB) {
|
|||||||
return tx.callbacks.Create().Execute(tx)
|
return tx.callbacks.Create().Execute(tx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *DB) CreateInAutoBatches(value interface{}, maxParameter int) (tx *DB) {
|
||||||
|
reflectValue := reflect.Indirect(reflect.ValueOf(value))
|
||||||
|
|
||||||
|
switch reflectValue.Kind() {
|
||||||
|
case reflect.Slice, reflect.Array:
|
||||||
|
nField := reflect.Indirect(reflectValue.Index(0)).NumField()
|
||||||
|
length := reflectValue.Len()
|
||||||
|
batchSize := int(math.Min(float64(maxParameter/nField), float64(length)))
|
||||||
|
tx = db.CreateInBatches(value, batchSize)
|
||||||
|
default:
|
||||||
|
tx = db.getInstance()
|
||||||
|
tx.Statement.Dest = value
|
||||||
|
tx = tx.callbacks.Create().Execute(tx)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// CreateInBatches inserts value in batches of batchSize
|
// CreateInBatches inserts value in batches of batchSize
|
||||||
func (db *DB) CreateInBatches(value interface{}, batchSize int) (tx *DB) {
|
func (db *DB) CreateInBatches(value interface{}, batchSize int) (tx *DB) {
|
||||||
reflectValue := reflect.Indirect(reflect.ValueOf(value))
|
reflectValue := reflect.Indirect(reflect.ValueOf(value))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user