Signed-off-by: Sean Corfield <sean@corfield.org>
This commit is contained in:
Sean Corfield 2025-02-20 17:03:44 -08:00
parent c2990597f1
commit 4d1f5f83b7
No known key found for this signature in database
4 changed files with 25 additions and 2 deletions

View file

@ -1,6 +1,7 @@
# Changes # Changes
* 2.6.next in progress * 2.6.next in progress
* Address [#567](https://github.com/seancorfield/honeysql/issues/567) by adding support for `ASSERT` clause.
* Address [#566](https://github.com/seancorfield/honeysql/issues/566) by adding `IS [NOT] DISTINCT FROM` operators. * Address [#566](https://github.com/seancorfield/honeysql/issues/566) by adding `IS [NOT] DISTINCT FROM` operators.
* Add examples of `:alias` with `:group-by` (syntax is slightly different to existing examples for `:order-by`). * Add examples of `:alias` with `:group-by` (syntax is slightly different to existing examples for `:order-by`).

View file

@ -207,3 +207,14 @@ user=> (sql/format {:patch-into :foo
:records [{:_id 1 :status "active"}]}) :records [{:_id 1 :status "active"}]})
["PATCH INTO foo RECORDS ?" {:_id 1, :status "active"}] ["PATCH INTO foo RECORDS ?" {:_id 1, :status "active"}]
``` ```
## `assert`
XTDB supports an `ASSERT` operation that will throw an exception if the
asserted predicate is not true:
```clojure
user=> (sql/format '{assert (not-exists {select 1 from users where (= email "james @example.com")})}
:inline true)
["ASSERT NOT EXISTS (SELECT 1 FROM users WHERE email = 'james @example.com')"]
```

View file

@ -59,7 +59,7 @@
;; then SQL clauses in priority order: ;; then SQL clauses in priority order:
:setting :setting
:raw :nest :with :with-recursive :intersect :union :union-all :except :except-all :raw :nest :with :with-recursive :intersect :union :union-all :except :except-all
:table :table :assert ; #567 XTDB
:select :select-distinct :select-distinct-on :select-top :select-distinct-top :select :select-distinct :select-distinct-on :select-top :select-distinct-top
:distinct :expr :exclude :rename :distinct :expr :exclude :rename
:into :bulk-collect-into :into :bulk-collect-into
@ -1654,6 +1654,9 @@
:except #'format-on-set-op :except #'format-on-set-op
:except-all #'format-on-set-op :except-all #'format-on-set-op
:table #'format-selector :table #'format-selector
:assert (fn [k xs]
(let [[sql & params] (format-expr xs)]
(into [(str (sql-kw k) " " sql)] params)))
:select #'format-selects :select #'format-selects
:select-distinct #'format-selects :select-distinct #'format-selects
:select-distinct-on #'format-selects-on :select-distinct-on #'format-selects-on

View file

@ -1,4 +1,4 @@
;; copyright (c) 2020-2024 sean corfield, all rights reserved ;; copyright (c) 2020-2025 sean corfield, all rights reserved
(ns honey.sql.xtdb-test (ns honey.sql.xtdb-test
(:require [clojure.test :refer [deftest is testing]] (:require [clojure.test :refer [deftest is testing]]
@ -129,3 +129,11 @@
(sql/format '{select (((get-in (. a b) c (lift 1) d)))}))) (sql/format '{select (((get-in (. a b) c (lift 1) d)))})))
(is (= ["SELECT (OBJECT (_id: 1, b: 'thing').b).c[?].d" 1] (is (= ["SELECT (OBJECT (_id: 1, b: 'thing').b).c[?].d" 1]
(sql/format '{select (((get-in (. (object {_id 1 b "thing"}) b) c (lift 1) d)))})))) (sql/format '{select (((get-in (. (object {_id 1 b "thing"}) b) c (lift 1) d)))}))))
(deftest assert-statement
(is (= ["ASSERT NOT EXISTS (SELECT 1 FROM users WHERE email = 'james @example.com')"]
(sql/format '{assert (not-exists {select 1 from users where (= email "james @example.com")})}
:inline true)))
(is (= ["ASSERT TRUE"]
(sql/format '{assert true}
:inline true))))