Skip to content

Commit c7aafb4

Browse files
authored
Merge pull request #117 from ulule/fix/panic-if-condition-is-nil
fix: panic if condition is nil on WHERE / AND clauses
2 parents 4790f61 + 96805b0 commit c7aafb4

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

builder/select.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ func (b Select) With(args ...stmt.WithQuery) Select {
144144

145145
// Where adds WHERE clauses.
146146
func (b Select) Where(condition stmt.Expression) Select {
147+
if condition == nil {
148+
panic("loukoum: condition must be not nil")
149+
}
147150
if b.query.Where.IsEmpty() {
148151
b.query.Where = stmt.NewWhere(condition)
149152
return b
@@ -154,6 +157,9 @@ func (b Select) Where(condition stmt.Expression) Select {
154157

155158
// And adds AND WHERE conditions.
156159
func (b Select) And(condition stmt.Expression) Select {
160+
if condition == nil {
161+
panic("loukoum: condition must be not nil")
162+
}
157163
b.query.Where = b.query.Where.And(condition)
158164
return b
159165
}

builder/select_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"testing"
66

7+
"github.com/stretchr/testify/require"
78
loukoum "github.com/ulule/loukoum/v3"
89
"github.com/ulule/loukoum/v3/builder"
910
"github.com/ulule/loukoum/v3/stmt"
@@ -1630,3 +1631,21 @@ func TestSelect_Extra(t *testing.T) {
16301631
},
16311632
})
16321633
}
1634+
1635+
func TestSelect_NilCondition(t *testing.T) {
1636+
is := require.New(t)
1637+
is.Panics(func() {
1638+
var nilcond stmt.Expression
1639+
loukoum.Select("col").
1640+
From("table").
1641+
Where(loukoum.Condition("col").Equal("value")).
1642+
And(nilcond)
1643+
})
1644+
1645+
is.Panics(func() {
1646+
var nilcond stmt.Expression
1647+
loukoum.Select("col").
1648+
From("table").
1649+
Where(nilcond)
1650+
})
1651+
}

0 commit comments

Comments
 (0)