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

fix(finisher_api): compose sqls and vars statement #6567

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions finisher_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func (db *DB) CreateInBatches(value interface{}, batchSize int) (tx *DB) {
// the reflection length judgment of the optimized value
reflectLen := reflectValue.Len()

statementSql := strings.Builder{}
statementVars := make([]interface{}, 0, reflectLen)

callFc := func(tx *DB) error {
for i := 0; i < reflectLen; i += batchSize {
ends := i + batchSize
Expand All @@ -49,6 +52,11 @@ func (db *DB) CreateInBatches(value interface{}, batchSize int) (tx *DB) {
if subtx.Error != nil {
return subtx.Error
}

statementSql.WriteString(subtx.Statement.SQL.String())
statementSql.WriteString(";")
statementVars = append(statementVars, subtx.Statement.Vars...)

rowsAffected += subtx.RowsAffected
}
return nil
Expand All @@ -59,6 +67,8 @@ func (db *DB) CreateInBatches(value interface{}, batchSize int) (tx *DB) {
} else {
tx.AddError(tx.Transaction(callFc))
}
tx.Statement.SQL = statementSql
tx.Statement.Vars = statementVars

tx.RowsAffected = rowsAffected
default:
Expand Down
24 changes: 23 additions & 1 deletion tests/sql_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,28 @@ func TestToSQL(t *testing.T) {
})
})
assertEqualSQL(t, `SELECT * FROM users ORDER BY id DESC`, sql)

// create batch, models are equal batch size
bs := 2
m := gorm.Model{
CreatedAt: date,
UpdatedAt: date,
}
sql = DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
return tx.Model(&User{}).
CreateInBatches(
[]User{
{
Name: "foo", Age: 20, Model: m,
},
{
Name: "bar", Age: 30, Model: m,
},
},
bs,
)
})
assertEqualSQL(t, `INSERT INTO "users" ("created_at","updated_at","deleted_at","name","age","birthday","company_id","manager_id","active") VALUES ('2021-10-18 00:00:00','2021-10-18 00:00:00',NULL,'foo',20,NULL,NULL,NULL,false),('2021-10-18 00:00:00','2021-10-18 00:00:00',NULL,'bar',30,NULL,NULL,NULL,false) RETURNING "id";`, sql)
}

// assertEqualSQL for assert that the sql is equal, this method will ignore quote, and dialect specials.
Expand All @@ -469,7 +491,7 @@ func assertEqualSQL(t *testing.T, expected string, actually string) {
expected = updatedAtRe.ReplaceAllString(expected, `"updated_at"=?`)

// ignore RETURNING "id" (only in PostgreSQL)
returningRe := regexp.MustCompile(`(?i)RETURNING "id"`)
returningRe := regexp.MustCompile(`\s?(?i)RETURNING "id"`)
actually = returningRe.ReplaceAllString(actually, ``)
expected = returningRe.ReplaceAllString(expected, ``)

Expand Down
Loading