Skip to content

Commit

Permalink
fixes #555 by implementing SETTING clause
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Corfield <sean@corfield.org>
  • Loading branch information
seancorfield committed Nov 25, 2024
1 parent 782bc4b commit 3f1677b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
21 changes: 21 additions & 0 deletions src/honey/sql.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."
Expand Down Expand Up @@ -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})]
Expand Down
5 changes: 5 additions & 0 deletions src/honey/sql/helpers.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions test/honey/sql_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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]})
Expand Down

0 comments on commit 3f1677b

Please sign in to comment.