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

support bulk insert #20

Merged
merged 5 commits into from
Nov 22, 2021
Merged
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
83 changes: 79 additions & 4 deletions _example/user.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions _example/user_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ func (u User) DefaultInsertHook(q userInsertSQL) (userInsertSQL, error) {
}

func (u User) DefaultInsertOnDuplicateKeyUpdateHook(q userInsertOnDuplicateKeyUpdateSQL) (userInsertOnDuplicateKeyUpdateSQL, error) {
now := time.Now()
q.insertSQL = q.insertSQL.ValueUpdatedAt(mysql.NullTime{Time: now, Valid: true})

return q.SameOnUpdateUpdatedAt(), nil
}

Expand Down
83 changes: 79 additions & 4 deletions _example/user_item.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 63 additions & 10 deletions _example/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"reflect"
"regexp"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -190,24 +191,24 @@ func TestInsert(t *testing.T) {
if err != nil {
t.Error("unexpected error:", err)
}
switch query {
case "INSERT INTO user (`name`,`created_at`) VALUES(?,?);":
if !reflect.DeepEqual(args[0], "hogehoge") {
t.Error("unexpected args:", args)
}
case "INSERT INTO user (`created_at`,`name`) VALUES(?,?);":
if !reflect.DeepEqual(args[1], "hogehoge") {
t.Error("unexpected args:", args)
}
default:
expected := "INSERT INTO user (`created_at`,`name`) VALUES(?,?);"
if query != expected {
t.Error("unexpected query:", query)
}
if !reflect.DeepEqual(args[1], "hogehoge") {
t.Error("unexpected args:", args)
}
}

func TestInsertOnDuplicateKeyUpdate(t *testing.T) {
now := time.Now()
q := NewUserSQL().Insert().
ValueID(1).
ValueName("hogehoge").
ValueUpdatedAt(mysql.NullTime{
Valid: true,
Time: now,
}).
OnDuplicateKeyUpdate().
ValueOnUpdateAge(sql.NullInt64{
Valid: true,
Expand All @@ -229,6 +230,58 @@ func TestInsertOnDuplicateKeyUpdate(t *testing.T) {
}
}

func TestBulkInsert(t *testing.T) {
items := NewUserItemSQL().BulkInsert()
for i := 1; i <= 10; i++ {
q := NewUserItemSQL().Insert().
ValueUserID(42).
ValueItemID(strconv.Itoa(i))
items.Append(q)
}
query, vs, err := items.ToSql()
if err != nil {
t.Error("unexpected error:", err)
}
expected := "INSERT INTO `user_item` (`item_id`,`user_id`) VALUES (?,?),(?,?),(?,?),(?,?),(?,?),(?,?),(?,?),(?,?),(?,?),(?,?);"
if query != expected {
t.Error("query is not match:", query)
}
if !reflect.DeepEqual(vs, []interface{}{"1", uint64(42), "2", uint64(42), "3", uint64(42), "4", uint64(42), "5", uint64(42), "6", uint64(42), "7", uint64(42), "8", uint64(42), "9", uint64(42), "10", uint64(42)}) {
t.Errorf("vs is not valid: %+v", vs)
}
}

func TestBulkInsertWithOnDuplicateKeyUpdate(t *testing.T) {
items := NewUserItemSQL().BulkInsert()
items.Append(
NewUserItemSQL().Insert().ValueUserID(42).ValueItemID("1").ValueIsUsed(true),
NewUserItemSQL().Insert().ValueUserID(42).ValueItemID("2").ValueIsUsed(true),
)

now := mysql.NullTime{
Valid: true,
Time: time.Now(),
}
query, vs, err := items.
OnDuplicateKeyUpdate().
SameOnUpdateIsUsed().
ValueOnUpdateUsedAt(now).
ToSql()
if err != nil {
t.Error("unexpected error:", err)
}
bulkInsertQuery := "INSERT INTO `user_item` (`is_used`,`item_id`,`user_id`) VALUES (?,?,?),(?,?,?) "
expected1 := bulkInsertQuery + "ON DUPLICATE KEY UPDATE `is_used` = VALUES(`is_used`), `used_at` = ?;"
expected2 := bulkInsertQuery + "ON DUPLICATE KEY UPDATE `used_at` = ?, `is_used` = VALUES(`is_used`);"
if query != expected1 && query != expected2 {
t.Error("query is not match:", query)
}
if !reflect.DeepEqual(vs, []interface{}{true, "1", uint64(42), true, "2", uint64(42), now}) {
t.Errorf("vs is not valid: %+v", vs)
}

}

func TestDelete(t *testing.T) {
q := NewUserSQL().Delete().Name("hogehoge")
query, args, err := q.ToSql()
Expand Down
Loading