diff --git a/src/honey/sql/helpers.cljc b/src/honey/sql/helpers.cljc index c06b541..072f4ef 100644 --- a/src/honey/sql/helpers.cljc +++ b/src/honey/sql/helpers.cljc @@ -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.helpers "Helper functions for the built-in clauses in honey.sql. @@ -58,7 +58,7 @@ bulk-collect-info [& args] (as they are for all helper functions)." - (:refer-clojure :exclude [distinct filter for group-by into partition-by set update]) + (:refer-clojure :exclude [assert distinct filter for group-by into partition-by set update]) (:require [clojure.core :as c] [honey.sql :as h])) @@ -452,6 +452,14 @@ [& clauses] (generic :except-all (cons {} clauses))) +(defn assert + "Accepts an expression (predicate). + + Produces: ASSERT expression" + {:arglists '([expr])} + [& args] + (generic-1 :assert args)) + (defn select "Accepts any number of column names, or column/alias pairs, or SQL expressions (optionally aliased): diff --git a/test/honey/sql/xtdb_test.cljc b/test/honey/sql/xtdb_test.cljc index fb759db..bb85cf5 100644 --- a/test/honey/sql/xtdb_test.cljc +++ b/test/honey/sql/xtdb_test.cljc @@ -131,9 +131,18 @@ (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)))) + (testing "quoted sql" + (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)))) + (testing "helper" + (is (= ["ASSERT NOT EXISTS (SELECT 1 FROM users WHERE email = 'james @example.com')"] + (-> (h/assert [:not-exists {:select 1 :from :users :where [:= :email "james @example.com"]}]) + (sql/format {:inline true})))) + (is (= ["ASSERT NOT EXISTS (SELECT 1 FROM users WHERE email = 'james @example.com')"] + (-> {} + (h/assert [:not-exists {:select 1 :from :users :where [:= :email "james @example.com"]}]) + (sql/format {:inline true}))))))