From 3f1677bff26e36636975e8c93ba12c6c4698ea9b Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Sun, 24 Nov 2024 22:40:20 -0800 Subject: [PATCH] fixes #555 by implementing SETTING clause Signed-off-by: Sean Corfield --- CHANGELOG.md | 1 + src/honey/sql.cljc | 21 +++++++++++++++++++++ src/honey/sql/helpers.cljc | 5 +++++ test/honey/sql_test.cljc | 17 +++++++++++++++++ 4 files changed, 44 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1283af8..946fd6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changes * 2.6.next in progress + * Address [#555](https://github.com/seancorfield/honeysql/issues/555) by supporting `SETTING` clause for XTDB. * Experimental `:xtdb` dialect removed (since XTDB no longer supports qualified column names). * 2.6.1230 -- 2024-11-23 diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index 27b1bdf..1acdd82 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -55,6 +55,7 @@ :refresh-materialized-view :create-index ;; then SQL clauses in priority order: + :setting :raw :nest :with :with-recursive :intersect :union :union-all :except :except-all :table :select :select-distinct :select-distinct-on :select-top :select-distinct-top @@ -1585,6 +1586,25 @@ (into [(str (sql-kw k) " " (join ", " sqls))] params)) (format-records k [args]))) +(defn- format-setting + [k args] + (if (and (sequential? args) (ident? (first args))) + (format-setting k [args]) + (let [[sqls params] + (reduce-sql + (map (fn [arg] + (let [[sqls params] + (reduce-sql (map (fn [x] + (if (ident? x) + [(if (str/ends-with? (name x) "-time") + (format-fn-name x) + (sql-kw x))] + (format-expr x))) + arg))] + (into [(join " " sqls)] params))) + args))] + (into [(str (sql-kw k) " " (join ", " sqls))] params)))) + (defn- check-where "Given a formatter function, performs a pre-flight check that there is a non-empty where clause if at least basic checking is enabled." @@ -1638,6 +1658,7 @@ :drop-materialized-view #'format-drop-items :refresh-materialized-view (fn [_ x] (format-create :refresh :materialized-view x nil)) :create-index #'format-create-index + :setting #'format-setting :raw (fn [_ x] (raw-render x)) :nest (fn [_ x] (let [[sql & params] (format-dsl x {:nested true})] diff --git a/src/honey/sql/helpers.cljc b/src/honey/sql/helpers.cljc index 214a433..bddd974 100644 --- a/src/honey/sql/helpers.cljc +++ b/src/honey/sql/helpers.cljc @@ -399,6 +399,11 @@ [& args] (generic :create-index args)) +(defn setting + "Accepts one or more time settings for a query." + [& args] + (generic :setting args)) + (defn with "Accepts one or more CTE definitions. diff --git a/test/honey/sql_test.cljc b/test/honey/sql_test.cljc index 13eb030..575e7a2 100644 --- a/test/honey/sql_test.cljc +++ b/test/honey/sql_test.cljc @@ -1399,6 +1399,23 @@ ORDER BY id = ? DESC (is (= [(str "CREATE TABLE \"" table "\"")] (sut/format {:create-table table})))))) +(deftest issue-555-setting + (testing "setting default time" + (is (= ["SETTING DEFAULT SYSTEM_TIME AS OF DATE '2024-11-24'"] + (sut/format {:setting [:default :system-time :as-of [:inline :DATE "2024-11-24"]]}))) + (is (= ["SETTING BASIS TO DATE '2024-11-24', DEFAULT VALID_TIME TO BETWEEN DATE '2022' AND DATE '2023'"] + (sut/format {:setting [[:basis-to [:inline :DATE "2024-11-24"]] + [:default :valid-time :to :between [:inline :DATE "2022"] :and [:inline :DATE "2023"]]]}))) + (is (= ["SETTING DEFAULT SYSTEM_TIME AS OF DATE '2024-11-24' SELECT * FROM table"] + (sut/format (-> (h/setting :default :system-time :as-of [:inline :DATE "2024-11-24"]) + (h/select :*) + (h/from :table))))) + (is (= ["SETTING BASIS TO DATE '2024-11-24', DEFAULT VALID_TIME TO BETWEEN DATE '2022' AND DATE '2023' SELECT * FROM table"] + (sut/format (-> (h/setting [:basis-to [:inline :DATE "2024-11-24"]] + [:default :valid-time :to :between [:inline :DATE "2022"] :and [:inline :DATE "2023"]]) + (h/select :*) + (h/from :table))))))) + (comment ;; partial (incorrect!) workaround for #407: (sut/format {:select :f.* :from [[:foo [:f :for :system-time]]] :where [:= :f.id 1]})