From 2294db61d228711435fff1075409a30086b37555 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Tue, 5 Oct 2021 09:18:50 +0300 Subject: [PATCH] fix: add UpdateQuery.OmitZero --- internal/dbtest/query_test.go | 3 +++ internal/dbtest/testdata/snapshots/TestQuery-mysql5-87 | 1 + internal/dbtest/testdata/snapshots/TestQuery-mysql5-88 | 1 + internal/dbtest/testdata/snapshots/TestQuery-mysql8-87 | 1 + internal/dbtest/testdata/snapshots/TestQuery-mysql8-88 | 1 + internal/dbtest/testdata/snapshots/TestQuery-pg-87 | 1 + internal/dbtest/testdata/snapshots/TestQuery-pg-88 | 1 + internal/dbtest/testdata/snapshots/TestQuery-pgx-87 | 1 + internal/dbtest/testdata/snapshots/TestQuery-pgx-88 | 1 + internal/dbtest/testdata/snapshots/TestQuery-sqlite-87 | 1 + internal/dbtest/testdata/snapshots/TestQuery-sqlite-88 | 1 + query_update.go | 7 ++++++- 12 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-mysql5-87 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-mysql5-88 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-mysql8-87 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-mysql8-88 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-pg-87 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-pg-88 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-pgx-87 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-pgx-88 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-sqlite-87 create mode 100644 internal/dbtest/testdata/snapshots/TestQuery-sqlite-88 diff --git a/internal/dbtest/query_test.go b/internal/dbtest/query_test.go index 0d105cd2b..efe0cd9b6 100644 --- a/internal/dbtest/query_test.go +++ b/internal/dbtest/query_test.go @@ -554,6 +554,9 @@ func TestQuery(t *testing.T) { func(db *bun.DB) schema.QueryAppender { return db.NewInsert().Model(new(SoftDelete)).On("CONFLICT DO NOTHING") }, + func(db *bun.DB) schema.QueryAppender { + return db.NewUpdate().Model(&Model{ID: 42}).OmitZero().WherePK() + }, } timeRE := regexp.MustCompile(`'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+(\+\d{2}:\d{2})?'`) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-87 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-87 new file mode 100644 index 000000000..de87b9bb6 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-87 @@ -0,0 +1 @@ +UPDATE `models` AS `model` SET WHERE (`model`.`id` = 42) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-88 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-88 new file mode 100644 index 000000000..de87b9bb6 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-88 @@ -0,0 +1 @@ +UPDATE `models` AS `model` SET WHERE (`model`.`id` = 42) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-87 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-87 new file mode 100644 index 000000000..de87b9bb6 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-87 @@ -0,0 +1 @@ +UPDATE `models` AS `model` SET WHERE (`model`.`id` = 42) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-88 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-88 new file mode 100644 index 000000000..de87b9bb6 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-88 @@ -0,0 +1 @@ +UPDATE `models` AS `model` SET WHERE (`model`.`id` = 42) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-87 b/internal/dbtest/testdata/snapshots/TestQuery-pg-87 new file mode 100644 index 000000000..a451b7ea7 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-87 @@ -0,0 +1 @@ +UPDATE "models" AS "model" SET WHERE ("model"."id" = 42) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-88 b/internal/dbtest/testdata/snapshots/TestQuery-pg-88 new file mode 100644 index 000000000..a451b7ea7 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-88 @@ -0,0 +1 @@ +UPDATE "models" AS "model" SET WHERE ("model"."id" = 42) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-87 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-87 new file mode 100644 index 000000000..a451b7ea7 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-87 @@ -0,0 +1 @@ +UPDATE "models" AS "model" SET WHERE ("model"."id" = 42) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-88 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-88 new file mode 100644 index 000000000..a451b7ea7 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-88 @@ -0,0 +1 @@ +UPDATE "models" AS "model" SET WHERE ("model"."id" = 42) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-87 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-87 new file mode 100644 index 000000000..a451b7ea7 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-87 @@ -0,0 +1 @@ +UPDATE "models" AS "model" SET WHERE ("model"."id" = 42) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-88 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-88 new file mode 100644 index 000000000..a451b7ea7 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-88 @@ -0,0 +1 @@ +UPDATE "models" AS "model" SET WHERE ("model"."id" = 42) diff --git a/query_update.go b/query_update.go index a5d9a17f2..ae9c0f44c 100644 --- a/query_update.go +++ b/query_update.go @@ -100,6 +100,11 @@ func (q *UpdateQuery) Value(column string, expr string, args ...interface{}) *Up return q } +func (q *UpdateQuery) OmitZero() *UpdateQuery { + q.omitZero = true + return q +} + //------------------------------------------------------------------------------ func (q *UpdateQuery) WherePK() *UpdateQuery { @@ -254,7 +259,7 @@ func (q *UpdateQuery) appendSetStruct( isTemplate := fmter.IsNop() pos := len(b) for _, f := range fields { - if q.omitZero && f.NullZero && f.HasZeroValue(model.strct) { + if q.omitZero && f.HasZeroValue(model.strct) { continue }