mirror of
https://gitee.com/johng/gf
synced 2026-06-27 17:57:21 +08:00
fix issue of OmitEmptyWhere in Builder for package gdb (#2195)
This commit is contained in:
@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
Reference in New Issue
Block a user