Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pgstmt/cond.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Cond interface {
IsNull(field string)
IsNotNull(field string)
Raw(sql string)
Not(f func(b Cond))
And(f func(b Cond))
Or(f func(b Cond))
Mode() CondMode
Expand Down Expand Up @@ -188,6 +189,17 @@ func (st *cond) Raw(sql string) {
st.ops.push(sql)
}

func (st *cond) Not(b func(b Cond)) {
var x cond
x.ops.sep = " and "
x.nested = true
b(&x)

if !x.empty() {
st.ops.push(withParen(" ", "not", &x))
}
}

func (st *cond) And(f func(b Cond)) {
var x cond
x.ops.sep = " and "
Expand Down
19 changes: 19 additions & 0 deletions pgstmt/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,25 @@ func TestSelect(t *testing.T) {
`,
nil,
},
{
"select where not",
pgstmt.Select(func(b pgstmt.SelectStatement) {
b.Columns("*")
b.From("table1")
b.Where(func(b pgstmt.Cond) {
b.Eq("id", 1)
b.Not(func(b pgstmt.Cond) {
b.Op("tags", "@>", pq.Array([]string{"a", "b"}))
})
})
}),
`
select *
from table1
where (id = $1 and (not (tags @> $2)))
`,
[]any{1, pq.Array([]string{"a", "b"})},
},
}

for _, tC := range cases {
Expand Down