Skip to content

Commit

Permalink
Fixes #553 by adding :not-between
Browse files Browse the repository at this point in the history
  • Loading branch information
plooney81 committed Nov 22, 2024
1 parent 21ce3a2 commit 6e43f77
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
5 changes: 4 additions & 1 deletion doc/special-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,17 @@ and a time zone name or identifier (can be a string, a symbol, or a keyword):
The time zone name or identifier will be inlined (as a string) and therefore
cannot be an expression.

## between
## between and not-between

Accepts three arguments: an expression, a lower bound, and
an upper bound:

```clojure
(sql/format-expr [:between :id 1 100])
;;=> ["id BETWEEN ? AND ?" 1 100]

(sql/format-expr [:not-between :id 1 100])
;;=> ["id NOT BETWEEN ? AND ?" 1 100]
```

## case
Expand Down
24 changes: 15 additions & 9 deletions src/honey/sql.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1877,6 +1877,19 @@
(into paramsx)
(into params))))

(defn- between-fn
"For both :between and :not-between"
[k [x a b]]
(let [[sql-x & params-x] (format-expr x {:nested true})
[sql-a & params-a] (format-expr a {:nested true})
[sql-b & params-b] (format-expr b {:nested true})
add-not? (str/starts-with? (name k) "not")
between-type (if add-not? " NOT BETWEEN " " BETWEEN ")]
(-> [(str sql-x between-type sql-a " AND " sql-b)]
(into params-x)
(into params-a)
(into params-b))))

(defn ignore-respect-nulls [k [x]]
(let [[sql & params] (format-expr x)]
(into [(str sql " " (sql-kw k))] params)))
Expand Down Expand Up @@ -1938,15 +1951,8 @@
(binding [*inline* true]
(format-expr (if (ident? tz) (name tz) tz)))]
(into [(str sql " AT TIME ZONE " tz-sql)] params)))
:between
(fn [_ [x a b]]
(let [[sql-x & params-x] (format-expr x {:nested true})
[sql-a & params-a] (format-expr a {:nested true})
[sql-b & params-b] (format-expr b {:nested true})]
(-> [(str sql-x " BETWEEN " sql-a " AND " sql-b)]
(into params-x)
(into params-a)
(into params-b))))
:between #'between-fn
:not-between #'between-fn
:case #'case-clauses
:case-expr #'case-clauses
:cast
Expand Down

0 comments on commit 6e43f77

Please sign in to comment.