Skip to content

Commit

Permalink
Merge pull request #168 from vallahaye/167-add-logical-not-and-not-il…
Browse files Browse the repository at this point in the history
…ike-comparison-operators

Add logical NOT and NOT ILIKE operators
  • Loading branch information
huandu committed Sep 10, 2024
2 parents d6f429c + fc176ca commit e6de572
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
17 changes: 17 additions & 0 deletions cond.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ func (c *Cond) NotLike(field string, value interface{}) string {
return buf.String()
}

// NotILike represents "field NOT ILIKE value".
func (c *Cond) NotILike(field string, value interface{}) string {
buf := newStringBuilder()
buf.WriteString(Escape(field))
buf.WriteString(" NOT ILIKE ")
buf.WriteString(c.Args.Add(value))
return buf.String()
}

// IsNull represents "field IS NULL".
func (c *Cond) IsNull(field string) string {
buf := newStringBuilder()
Expand Down Expand Up @@ -244,6 +253,14 @@ func (c *Cond) And(andExpr ...string) string {
return buf.String()
}

// Not represents "NOT expr".
func (c *Cond) Not(notExpr string) string {
buf := newStringBuilder()
buf.WriteString("NOT ")
buf.WriteString(notExpr)
return buf.String()
}

// Exists represents "EXISTS (subquery)".
func (c *Cond) Exists(subquery interface{}) string {
buf := newStringBuilder()
Expand Down
2 changes: 2 additions & 0 deletions cond_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ func TestCond(t *testing.T) {
"$$a LIKE $0": func() string { return newTestCond().Like("$a", "%Huan%") },
"$$a ILIKE $0": func() string { return newTestCond().ILike("$a", "%Huan%") },
"$$a NOT LIKE $0": func() string { return newTestCond().NotLike("$a", "%Huan%") },
"$$a NOT ILIKE $0": func() string { return newTestCond().NotILike("$a", "%Huan%") },
"$$a IS NULL": func() string { return newTestCond().IsNull("$a") },
"$$a IS NOT NULL": func() string { return newTestCond().IsNotNull("$a") },
"$$a BETWEEN $0 AND $1": func() string { return newTestCond().Between("$a", 123, 456) },
"$$a NOT BETWEEN $0 AND $1": func() string { return newTestCond().NotBetween("$a", 123, 456) },
"(1 = 1 OR 2 = 2 OR 3 = 3)": func() string { return newTestCond().Or("1 = 1", "2 = 2", "3 = 3") },
"(1 = 1 AND 2 = 2 AND 3 = 3)": func() string { return newTestCond().And("1 = 1", "2 = 2", "3 = 3") },
"NOT 1 = 1": func() string { return newTestCond().Not("1 = 1") },
"EXISTS ($0)": func() string { return newTestCond().Exists(1) },
"NOT EXISTS ($0)": func() string { return newTestCond().NotExists(1) },
"$$a > ANY ($0, $1)": func() string { return newTestCond().Any("$a", ">", 1, 2) },
Expand Down

0 comments on commit e6de572

Please sign in to comment.