fixes #555 by implementing SETTING clause
Signed-off-by: Sean Corfield <sean@corfield.org>
This commit is contained in:
parent
782bc4b78a
commit
3f1677bff2
4 changed files with 44 additions and 0 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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})]
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
||||
|
|
|
|||
|
|
@ -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]})
|
||||
|
|
|
|||
Loading…
Reference in a new issue