Skip to content

Commit

Permalink
feat(update): "skipupdate" while bulk
Browse files Browse the repository at this point in the history
  • Loading branch information
choyri committed Oct 9, 2022
1 parent 28915f8 commit 1a32b2f
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 11 deletions.
9 changes: 7 additions & 2 deletions internal/dbtest/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,14 @@ func TestQuery(t *testing.T) {
return db.NewSelect().Model(new(SoftDelete1)).WhereAllWithDeleted()
},
func(db *bun.DB) schema.QueryAppender {
type Model struct {
ID int64 `bun:",pk,autoincrement"`
Str1 string
Str2 string `bun:",skipupdate"`
}
models := []Model{
{42, "hello"},
{43, "world"},
{42, "hello", "skip"},
{43, "world", "skip"},
}
return db.NewUpdate().
Model(&models).
Expand Down
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-mariadb-69
Original file line number Diff line number Diff line change
@@ -1 +1 @@
WITH `_data` AS (SELECT * FROM (VALUES ROW(42, 'hello'), ROW(43, 'world')) AS t (`id`, `str`)) UPDATE `models` AS `model`, _data SET `model`.`str` = _data.`str` WHERE (`model`.`id` = _data.`id`)
WITH `_data` AS (SELECT * FROM (VALUES ROW(42, 'hello', 'skip'), ROW(43, 'world', 'skip')) AS t (`id`, `str1`, `str2`)) UPDATE `models` AS `model`, _data SET `model`.`str1` = _data.`str1` WHERE (`model`.`id` = _data.`id`)
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-mssql2019-69
Original file line number Diff line number Diff line change
@@ -1 +1 @@
WITH "_data" AS (SELECT * FROM (VALUES (42, 'hello'), (43, 'world')) AS t ("id", "str")) UPDATE "models" SET "str" = _data."str" FROM _data WHERE ("models"."id" = _data."id")
WITH "_data" AS (SELECT * FROM (VALUES (42, 'hello', 'skip'), (43, 'world', 'skip')) AS t ("id", "str1", "str2")) UPDATE "models" SET "str1" = _data."str1" FROM _data WHERE ("models"."id" = _data."id")
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-mysql5-69
Original file line number Diff line number Diff line change
@@ -1 +1 @@
WITH `_data` AS (SELECT * FROM (VALUES ROW(42, 'hello'), ROW(43, 'world')) AS t (`id`, `str`)) UPDATE `models` AS `model`, _data SET `model`.`str` = _data.`str` WHERE (`model`.`id` = _data.`id`)
WITH `_data` AS (SELECT * FROM (VALUES ROW(42, 'hello', 'skip'), ROW(43, 'world', 'skip')) AS t (`id`, `str1`, `str2`)) UPDATE `models` AS `model`, _data SET `model`.`str1` = _data.`str1` WHERE (`model`.`id` = _data.`id`)
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-mysql8-69
Original file line number Diff line number Diff line change
@@ -1 +1 @@
WITH `_data` (`id`, `str`) AS (VALUES ROW(42, 'hello'), ROW(43, 'world')) UPDATE `models` AS `model`, _data SET `model`.`str` = _data.`str` WHERE (`model`.`id` = _data.`id`)
WITH `_data` (`id`, `str1`, `str2`) AS (VALUES ROW(42, 'hello', 'skip'), ROW(43, 'world', 'skip')) UPDATE `models` AS `model`, _data SET `model`.`str1` = _data.`str1` WHERE (`model`.`id` = _data.`id`)
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pg-69
Original file line number Diff line number Diff line change
@@ -1 +1 @@
WITH "_data" ("id", "str") AS (VALUES (42::BIGINT, 'hello'::VARCHAR), (43::BIGINT, 'world'::VARCHAR)) UPDATE "models" AS "model" SET "str" = _data."str" FROM _data WHERE ("model"."id" = _data."id")
WITH "_data" ("id", "str1", "str2") AS (VALUES (42::BIGINT, 'hello'::VARCHAR, 'skip'::VARCHAR), (43::BIGINT, 'world'::VARCHAR, 'skip'::VARCHAR)) UPDATE "models" AS "model" SET "str1" = _data."str1" FROM _data WHERE ("model"."id" = _data."id")
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-pgx-69
Original file line number Diff line number Diff line change
@@ -1 +1 @@
WITH "_data" ("id", "str") AS (VALUES (42::BIGINT, 'hello'::VARCHAR), (43::BIGINT, 'world'::VARCHAR)) UPDATE "models" AS "model" SET "str" = _data."str" FROM _data WHERE ("model"."id" = _data."id")
WITH "_data" ("id", "str1", "str2") AS (VALUES (42::BIGINT, 'hello'::VARCHAR, 'skip'::VARCHAR), (43::BIGINT, 'world'::VARCHAR, 'skip'::VARCHAR)) UPDATE "models" AS "model" SET "str1" = _data."str1" FROM _data WHERE ("model"."id" = _data."id")
2 changes: 1 addition & 1 deletion internal/dbtest/testdata/snapshots/TestQuery-sqlite-69
Original file line number Diff line number Diff line change
@@ -1 +1 @@
WITH "_data" ("id", "str") AS (VALUES (42, 'hello'), (43, 'world')) UPDATE "models" AS "model" SET "str" = _data."str" FROM _data WHERE ("model"."id" = _data."id")
WITH "_data" ("id", "str1", "str2") AS (VALUES (42, 'hello', 'skip'), (43, 'world', 'skip')) UPDATE "models" AS "model" SET "str1" = _data."str1" FROM _data WHERE ("model"."id" = _data."id")
9 changes: 7 additions & 2 deletions query_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,14 @@ func (q *UpdateQuery) updateSliceSet(
}

var b []byte
for i, field := range fields {
if i > 0 {
pos := len(b)
for _, field := range fields {
if field.SkipUpdate() {
continue
}
if len(b) != pos {
b = append(b, ", "...)
pos = len(b)
}
if fmter.HasFeature(feature.UpdateMultiTable) {
b = append(b, model.table.SQLAlias...)
Expand Down

0 comments on commit 1a32b2f

Please sign in to comment.