diff --git a/finisher_api.go b/finisher_api.go index 67423b23..c9e2a3b2 100644 --- a/finisher_api.go +++ b/finisher_api.go @@ -21,6 +21,29 @@ func (db *DB) Create(value interface{}) (tx *DB) { return } +// CreateInBatches insert the value in batches into database +func (db *DB) CreateInBatches(value interface{}, batchSize int) (tx *DB) { + reflectValue := reflect.Indirect(reflect.ValueOf(value)) + + switch reflectValue.Kind() { + case reflect.Slice, reflect.Array: + tx = db.getInstance() + for i := 0; i < reflectValue.Len(); i += batchSize { + tx.AddError(tx.Transaction(func(tx *DB) error { + ends := i + batchSize + if ends > reflectValue.Len() { + ends = reflectValue.Len() + } + + return tx.Create(reflectValue.Slice(i, ends).Interface()).Error + })) + } + default: + return db.Create(value) + } + return +} + // Save update value in database, if the value doesn't have primary key, will insert it func (db *DB) Save(value interface{}) (tx *DB) { tx = db.getInstance() diff --git a/tests/create_test.go b/tests/create_test.go index 00674eec..8d005d0b 100644 --- a/tests/create_test.go +++ b/tests/create_test.go @@ -40,6 +40,32 @@ func TestCreate(t *testing.T) { } } +func TestCreateInBatches(t *testing.T) { + users := []User{ + *GetUser("create_in_batches_1", Config{Account: true, Pets: 2, Toys: 3, Company: true, Manager: true, Team: 0, Languages: 1, Friends: 1}), + *GetUser("create_in_batches_2", Config{Account: false, Pets: 2, Toys: 4, Company: false, Manager: false, Team: 1, Languages: 3, Friends: 5}), + *GetUser("create_in_batches_3", Config{Account: true, Pets: 0, Toys: 3, Company: true, Manager: false, Team: 4, Languages: 0, Friends: 1}), + *GetUser("create_in_batches_4", Config{Account: true, Pets: 3, Toys: 0, Company: false, Manager: true, Team: 0, Languages: 3, Friends: 0}), + *GetUser("create_in_batches_5", Config{Account: false, Pets: 0, Toys: 3, Company: true, Manager: false, Team: 1, Languages: 3, Friends: 1}), + *GetUser("create_in_batches_6", Config{Account: true, Pets: 4, Toys: 3, Company: false, Manager: true, Team: 1, Languages: 3, Friends: 0}), + } + + DB.CreateInBatches(&users, 2) + + for _, user := range users { + if user.ID == 0 { + t.Fatalf("failed to fill user's ID, got %v", user.ID) + } else { + var newUser User + if err := DB.Where("id = ?", user.ID).Preload(clause.Associations).First(&newUser).Error; err != nil { + t.Fatalf("errors happened when query: %v", err) + } else { + CheckUser(t, newUser, user) + } + } + } +} + func TestCreateFromMap(t *testing.T) { if err := DB.Model(&User{}).Create(map[string]interface{}{"Name": "create_from_map", "Age": 18}).Error; err != nil { t.Fatalf("failed to create data from map, got error: %v", err)