Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify interface and implementation of BulkInsertWithAssigningIDs #53

Open
wants to merge 12 commits into
base: epic-assigning-ids
Choose a base branch
from
152 changes: 51 additions & 101 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,118 +46,68 @@ In the above pattern `Name` and `Email` fields are excluded.

## Example

### BulkInsert

```go
package main

import (
"fmt"
"log"
"time"

"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
gormbulk "github.com/t-tiger/gorm-bulk-insert/v2"
)

type fakeTable struct {
ID int `gorm:"AUTO_INCREMENT"`
Name string
Email string
CreatedAt time.Time
UpdatedAt time.Time
}

func main() {
db, err := gorm.Open("mysql", "mydb")
if err != nil {
log.Fatal(err)
}

var insertRecords []interface{}
for i := 0; i < 10; i++ {
insertRecords = append(insertRecords,
fakeTable{
Name: fmt.Sprintf("name%d", i),
Email: fmt.Sprintf("test%[email protected]", i),
// you don't need to set CreatedAt, UpdatedAt
},
)
}

err = gormbulk.BulkInsert(db, insertRecords, 3000)
if err != nil {
// do something
}

// columns you want to exclude from Insert, specify as an argument
err = gormbulk.BulkInsert(db, insertRecords, 3000, "Email")
if err != nil {
// do something
}
}
```

### BulkInsertWithAssigningIDs

```go
package main

import (
"fmt"
"time"
"fmt"
"log"
"time"

"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
gormbulk "github.com/t-tiger/gorm-bulk-insert/v2"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
gormbulk "github.com/t-tiger/gorm-bulk-insert/v2"
)

type fakeTable struct {
IdPK uint `gorm:"primary_key"`
CreatedAt time.Time `gorm:"default:now()"`
Data string
ID int `gorm:"AUTO_INCREMENT"`
Name string
Email string
CreatedAt time.Time
UpdatedAt time.Time
}

func main() {
db, err := gorm.Open("postgres", "host=localhost port=5432 user=cloudwalker dbname=cloudwalker password=cloudwalker sslmode=disable")
if err != nil {
panic(err)
}
defer db.Close()
db.SingularTable(true)

if err := db.AutoMigrate(fakeTable{}).Error; err != nil {
panic(err)
}

models := []interface{}{
fakeTable{Data: "aaa"},
fakeTable{Data: "bbb"},
fakeTable{Data: "ccc"},
}

// if you want to scan * back
var returnModel []fakeTable
if err := gormbulk.BulkInsertWithAssigningIDs(
db.Set("gorm:insert_option", "returning *"), &returnModel, models, 1000); err != nil {
panic(err)
}
fmt.Printf("success to insert with returning: %+v\n", returnModel)
// success to insert with returning: [
// {IdPK:1 CreatedAt:2021-10-31 16:21:48.019947 +0000 UTC Data:aaa}
// {IdPK:2 CreatedAt:2021-10-31 16:21:48.019959 +0000 UTC Data:bbb}
// {IdPK:3 CreatedAt:2021-10-31 16:21:48.019965 +0000 UTC Data:ccc}
// ]

// if you want to scan primary key
var returnId []uint
if err := gormbulk.BulkInsertWithAssigningIDs(
db.Set("gorm:insert_option", "returning id"), &returnId, models, 1000); err != nil {
panic(err)
}
fmt.Printf("success to insert with returning: %+v\n", returnId)
// `success to insert with returning: [4 5 6]`
db, err := gorm.Open("mysql", "mydb")
if err != nil {
log.Fatal(err)
}

var insertRecords []interface{}
for i := 0; i < 10; i++ {
insertRecords = append(insertRecords,
fakeTable{
Name: fmt.Sprintf("name%d", i),
Email: fmt.Sprintf("test%[email protected]", i),
// you don't need to set CreatedAt, UpdatedAt
},
)
}

err = gormbulk.BulkInsert(db, insertRecords, 3000)
if err != nil {
// do something
}

// Columns you want to exclude from Insert, specify as an argument
err = gormbulk.BulkInsert(db, insertRecords, 3000, "Email")
if err != nil {
// do something
}

// Fetch returning values
dbForReturning := db.Set("gorm:insert_option", "RETURNING id, name, created_at")
var returned []struct {
ID int
Name string
CreatedAt time.Time
}
err = gormbulk.BulkInsertWithReturningValues(dbForReturning, insertRecords, &returned, 3000)
if err != nil {
// do something
}
// Values of `returned` will be as follows
// {{ID: 1, Name: "name0", CreatedAt: 2021-10-31 16:21:48.019947 +0000 UTC}, ...}
}
```

Expand Down
83 changes: 27 additions & 56 deletions bulk_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,79 +25,58 @@ import (
func BulkInsert(db *gorm.DB, objects []interface{}, chunkSize int, excludeColumns ...string) error {
// Split records with specified size not to exceed Database parameter limit
for _, objSet := range splitObjects(objects, chunkSize) {
if err := insertObjSet(db, objSet, excludeColumns...); err != nil {
_, err := insertObjSet(db, objSet, excludeColumns...)
if err != nil {
return err
}
}
return nil
}

// BulkInsertWithAssigningIDs executes the query to insert multiple records at once.
// it will scan the result of `returning id` or `returning *` to [returnedValue] after every insert.
// it's necessary to set "gorm:insert_option"="returning id" in *gorm.DB
//
// [returnedValue] slice of primary_key or model, must be a *[]uint(for integer), *[]string(for uuid), *[]struct(for `returning *`)
// BulkInsertWithReturningValues executes the query to insert multiple records at once.
// This will scan the returned value into `dstValues`.
// It's necessary to set "gorm:insert_option" to execute "returning" query.
//
// [objects] must be a slice of struct.
//
// [returnedVals] must be a point to a slice of struct. Values returned from `RETURNING` clause will be assigned.
//
// [chunkSize] is a number of variables embedded in query. To prevent the error which occurs embedding a large number of variables at once
// and exceeds the limit of prepared statement. Larger size normally leads to better performance, in most cases 2000 to 3000 is reasonable.
//
// [excludeColumns] is column names to exclude from insert.
func BulkInsertWithAssigningIDs(db *gorm.DB, returnedValue interface{}, objects []interface{}, chunkSize int, excludeColumns ...string) error {
typ := reflect.TypeOf(returnedValue)
if typ.Kind() != reflect.Ptr || typ.Elem().Kind() != reflect.Slice {
return errors.New("returningId must be a slice ptr")
func BulkInsertWithReturningValues(db *gorm.DB, objects []interface{}, returnedVals interface{}, chunkSize int, excludeColumns ...string) error {
bombsimon marked this conversation as resolved.
Show resolved Hide resolved
typ := reflect.TypeOf(returnedVals)
if typ.Kind() != reflect.Ptr || typ.Elem().Kind() != reflect.Slice || typ.Elem().Elem().Kind() != reflect.Struct {
return errors.New("returnedVals must be a pointer to a slice of struct")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can also accept *[]uint i think, it will be more useful.

Copy link
Owner Author

@t-tiger t-tiger Nov 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of accepting a different type in a single method, rather I'd like to have a dedicated method if we accept a slice.

The reason why I changed not to accept slice is, returning statement is not only for PK, we can get values with multiple fields. Therefore, I thought we should support a wide range of use cases. However, I guess one of the major use cases is returning id that you proposed in the previous PR, so I can re-consider having a dedicated method like this.

func BulkInsertWithReturningIDs(db *gorm.DB, objects []interface{}, ids interface{}, chunkSize int, excludeColumns ...string) error

// Or we should rename to use "PK" instead of "ID"? But PK is not always a single column🤔
func BulkInsertWithReturningPKs(db *gorm.DB, objects []interface{}, pks interface{}, chunkSize int, excludeColumns ...string) error

What do you think?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about

func BulkInsertPluckingReturning(db *gorm.DB, objects []interface{}, column string, columnData interface{}, chunkSize int, excludeColumns ...string) error

Copy link
Owner Author

@t-tiger t-tiger Nov 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I feel BulkInsertWithReturningIDs seems better. If the caller wants to get values except for id column, it's still able to use BulkInsertWithReturningValues. If you agree to this, I'll implement as soon as possible and merge it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea

}

allIds := reflect.Indirect(reflect.ValueOf(returnedValue))
typ = allIds.Type()

// Deference value of slice
valueTyp := typ.Elem()
for valueTyp.Kind() == reflect.Ptr {
valueTyp = valueTyp.Elem()
}
refDst := reflect.Indirect(reflect.ValueOf(returnedVals))
typ = refDst.Type()

// Split records with specified size not to exceed Database parameter limit
for _, objSet := range splitObjects(objects, chunkSize) {
returnValueSlice := reflect.New(typ)
var scanReturningId func(*gorm.DB) error
switch valueTyp.Kind() {
case reflect.Struct:
// If user want to scan `returning *` with returnedValue=[]struct{...}
scanReturningId = func(db *gorm.DB) error {
return db.Scan(returnValueSlice.Interface()).Error
}
default:
// If user want to scan primary key `returning pk` with returnedValue=[]struct{...}
pk := db.NewScope(objects[0]).PrimaryKey()
scanReturningId = func(db *gorm.DB) error {
return db.Pluck(pk, returnValueSlice.Interface()).Error
}
db, err := insertObjSet(db, objSet, excludeColumns...)
if err != nil {
return err
}

if err := insertObjSetWithCallback(db, objSet, scanReturningId, excludeColumns...); err != nil {
scanned := reflect.New(typ)
if err := db.Scan(scanned.Interface()).Error; err != nil {
return err
}

allIds.Set(reflect.AppendSlice(allIds, returnValueSlice.Elem()))
refDst.Set(reflect.AppendSlice(refDst, scanned.Elem()))
}
return nil
}

func insertObjSet(db *gorm.DB, objects []interface{}, excludeColumns ...string) error {
return insertObjSetWithCallback(db, objects, nil, excludeColumns...)
}

func insertObjSetWithCallback(db *gorm.DB, objects []interface{}, postInsert func(*gorm.DB) error, excludeColumns ...string) error {
func insertObjSet(db *gorm.DB, objects []interface{}, excludeColumns ...string) (*gorm.DB, error) {
bombsimon marked this conversation as resolved.
Show resolved Hide resolved
if len(objects) == 0 {
return nil
return db, nil
}

firstAttrs, err := extractMapValue(objects[0], excludeColumns)
if err != nil {
return err
return db, err
}

attrSize := len(firstAttrs)
Expand All @@ -116,12 +95,12 @@ func insertObjSetWithCallback(db *gorm.DB, objects []interface{}, postInsert fun
for _, obj := range objects {
objAttrs, err := extractMapValue(obj, excludeColumns)
if err != nil {
return err
return db, err
}

// If object sizes are different, SQL statement loses consistency
if len(objAttrs) != attrSize {
return errors.New("attribute sizes are inconsistent")
return db, errors.New("attribute sizes are inconsistent")
}

scope := db.NewScope(obj)
Expand All @@ -144,7 +123,7 @@ func insertObjSetWithCallback(db *gorm.DB, objects []interface{}, postInsert fun
if val, ok := db.Get("gorm:insert_option"); ok {
strVal, ok := val.(string)
if !ok {
return errors.New("gorm:insert_option should be a string")
return db, errors.New("gorm:insert_option should be a string")
}
insertOption = strVal
}
Expand All @@ -157,18 +136,10 @@ func insertObjSetWithCallback(db *gorm.DB, objects []interface{}, postInsert fun
))

db = db.Raw(mainScope.SQL, mainScope.SQLVars...)

if err := db.Error; err != nil {
return err
}

if postInsert != nil {
if err := postInsert(db); err != nil {
return err
}
return db, err
}

return nil
return db, nil
}

// Obtain columns and values required for insert from interface
Expand Down
Loading