Skip to content

Commit

Permalink
fix: add UpdateQuery.OmitZero
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Oct 5, 2021
1 parent 479e526 commit 2294db6
Show file tree
Hide file tree
Showing 12 changed files with 19 additions and 1 deletion.
3 changes: 3 additions & 0 deletions internal/dbtest/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})?'`)
Expand Down
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-87
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE `models` AS `model` SET WHERE (`model`.`id` = 42)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-88
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE `models` AS `model` SET WHERE (`model`.`id` = 42)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-87
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE `models` AS `model` SET WHERE (`model`.`id` = 42)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-88
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE `models` AS `model` SET WHERE (`model`.`id` = 42)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-87
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE "models" AS "model" SET WHERE ("model"."id" = 42)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-88
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE "models" AS "model" SET WHERE ("model"."id" = 42)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-87
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE "models" AS "model" SET WHERE ("model"."id" = 42)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-88
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE "models" AS "model" SET WHERE ("model"."id" = 42)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-87
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE "models" AS "model" SET WHERE ("model"."id" = 42)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-88
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE "models" AS "model" SET WHERE ("model"."id" = 42)
7 changes: 6 additions & 1 deletion query_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 2294db6

Please sign in to comment.