Fixes #553 by adding :not-between

This commit is contained in:
Pete Looney 2024-11-22 12:39:59 -06:00
parent 21ce3a2242
commit b2c1ae0068
2 changed files with 17 additions and 10 deletions

View file

@ -93,7 +93,7 @@ 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 The time zone name or identifier will be inlined (as a string) and therefore
cannot be an expression. cannot be an expression.
## between ## between and not-between
Accepts three arguments: an expression, a lower bound, and Accepts three arguments: an expression, a lower bound, and
an upper bound: an upper bound:
@ -101,6 +101,9 @@ an upper bound:
```clojure ```clojure
(sql/format-expr [:between :id 1 100]) (sql/format-expr [:between :id 1 100])
;;=> ["id BETWEEN ? AND ?" 1 100] ;;=> ["id BETWEEN ? AND ?" 1 100]
(sql/format-expr [:not-between :id 1 100])
;;=> ["id NOT BETWEEN ? AND ?" 1 100]
``` ```
## case ## case

View file

@ -1877,6 +1877,17 @@
(into paramsx) (into paramsx)
(into params)))) (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})]
(-> [(str sql-x " " (sql-kw k) " " sql-a " AND " sql-b)]
(into params-x)
(into params-a)
(into params-b))))
(defn ignore-respect-nulls [k [x]] (defn ignore-respect-nulls [k [x]]
(let [[sql & params] (format-expr x)] (let [[sql & params] (format-expr x)]
(into [(str sql " " (sql-kw k))] params))) (into [(str sql " " (sql-kw k))] params)))
@ -1938,15 +1949,8 @@
(binding [*inline* true] (binding [*inline* true]
(format-expr (if (ident? tz) (name tz) tz)))] (format-expr (if (ident? tz) (name tz) tz)))]
(into [(str sql " AT TIME ZONE " tz-sql)] params))) (into [(str sql " AT TIME ZONE " tz-sql)] params)))
:between :between #'between-fn
(fn [_ [x a b]] :not-between #'between-fn
(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))))
:case #'case-clauses :case #'case-clauses
:case-expr #'case-clauses :case-expr #'case-clauses
:cast :cast