fix issue of OmitEmptyWhere in Builder for package gdb (#2195)

This commit is contained in:
John Guo
2022-10-12 20:30:47 +08:00
committed by GitHub
parent 35623b5abe
commit 4ebe4233fc
2 changed files with 24 additions and 1 deletions

View File

@ -4665,3 +4665,26 @@ func TestResult_Structs1(t *testing.T) {
t.Assert(array[1].Name, "smith")
})
}
func Test_Builder_OmitEmptyWhere(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
count, err := db.Model(table).Where("id", 1).Count()
t.AssertNil(err)
t.Assert(count, 1)
})
gtest.C(t, func(t *gtest.T) {
count, err := db.Model(table).Where("id", 0).OmitEmptyWhere().Count()
t.AssertNil(err)
t.Assert(count, TableSize)
})
gtest.C(t, func(t *gtest.T) {
builder := db.Model(table).OmitEmptyWhere().Builder()
count, err := db.Model(table).Where(
builder.Where("id", 0),
).Count()
t.AssertNil(err)
t.Assert(count, TableSize)
})
}

View File

@ -116,7 +116,7 @@ func (b *WhereBuilder) convertWhereBuilder(where interface{}, args []interface{}
}
if builder != nil {
conditionWhere, conditionArgs := builder.Build()
if len(b.whereHolder) == 0 {
if conditionWhere != "" && len(b.whereHolder) == 0 {
conditionWhere = "(" + conditionWhere + ")"
}
return conditionWhere, conditionArgs