Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #553 by adding :not-between #554

Merged
merged 1 commit into from
Nov 22, 2024
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
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
22 changes: 13 additions & 9 deletions src/honey/sql.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1877,6 +1877,17 @@
(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})]
(-> [(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]]
(let [[sql & params] (format-expr x)]
(into [(str sql " " (sql-kw k))] params)))
Expand Down Expand Up @@ -1938,15 +1949,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