fixes #555 by implementing SETTING clause

Signed-off-by: Sean Corfield <sean@corfield.org>
This commit is contained in:
Sean Corfield 2024-11-24 22:40:20 -08:00
parent 782bc4b78a
commit 3f1677bff2
No known key found for this signature in database
4 changed files with 44 additions and 0 deletions

View file

@ -1,6 +1,7 @@
# Changes # Changes
* 2.6.next in progress * 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). * Experimental `:xtdb` dialect removed (since XTDB no longer supports qualified column names).
* 2.6.1230 -- 2024-11-23 * 2.6.1230 -- 2024-11-23

View file

@ -55,6 +55,7 @@
:refresh-materialized-view :refresh-materialized-view
:create-index :create-index
;; then SQL clauses in priority order: ;; then SQL clauses in priority order:
: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
:select :select-distinct :select-distinct-on :select-top :select-distinct-top :select :select-distinct :select-distinct-on :select-top :select-distinct-top
@ -1585,6 +1586,25 @@
(into [(str (sql-kw k) " " (join ", " sqls))] params)) (into [(str (sql-kw k) " " (join ", " sqls))] params))
(format-records k [args]))) (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 (defn- check-where
"Given a formatter function, performs a pre-flight check that there is "Given a formatter function, performs a pre-flight check that there is
a non-empty where clause if at least basic checking is enabled." a non-empty where clause if at least basic checking is enabled."
@ -1638,6 +1658,7 @@
:drop-materialized-view #'format-drop-items :drop-materialized-view #'format-drop-items
:refresh-materialized-view (fn [_ x] (format-create :refresh :materialized-view x nil)) :refresh-materialized-view (fn [_ x] (format-create :refresh :materialized-view x nil))
:create-index #'format-create-index :create-index #'format-create-index
:setting #'format-setting
:raw (fn [_ x] (raw-render x)) :raw (fn [_ x] (raw-render x))
:nest (fn [_ x] :nest (fn [_ x]
(let [[sql & params] (format-dsl x {:nested true})] (let [[sql & params] (format-dsl x {:nested true})]

View file

@ -399,6 +399,11 @@
[& args] [& args]
(generic :create-index args)) (generic :create-index args))
(defn setting
"Accepts one or more time settings for a query."
[& args]
(generic :setting args))
(defn with (defn with
"Accepts one or more CTE definitions. "Accepts one or more CTE definitions.

View file

@ -1399,6 +1399,23 @@ ORDER BY id = ? DESC
(is (= [(str "CREATE TABLE \"" table "\"")] (is (= [(str "CREATE TABLE \"" table "\"")]
(sut/format {: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 (comment
;; partial (incorrect!) workaround for #407: ;; partial (incorrect!) workaround for #407:
(sut/format {:select :f.* :from [[:foo [:f :for :system-time]]] :where [:= :f.id 1]}) (sut/format {:select :f.* :from [[:foo [:f :for :system-time]]] :where [:= :f.id 1]})