Skip to content

Commit

Permalink
fix #503 by adding at time zone special syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
seancorfield committed Sep 16, 2023
1 parent ac09fc1 commit 756ed95
Show file tree
Hide file tree
Showing 4 changed files with 30 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.4.next in progress
* Address [#503](https://github.com/seancorfield/honeysql/issues/503) by adding `:at-time-zone` special syntax.
* Address [#504](https://github.com/seancorfield/honeysql/issues/504) for BigQuery support, by adding special syntax for ignore/respect nulls, as well as new `:distinct` and `:expr` clauses to allow expressions to be qualified with SQL clauses. The latter will probably be useful for other dialects too.

* 2.4.1066 -- 2023-08-27
Expand Down
13 changes: 13 additions & 0 deletions doc/special-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ In addition, the argument to `:array` is treated as a literal sequence of Clojur
;;=> ["SELECT ARRAY[inline, (?, ?, ?)] AS arr" 1 2 3]
```

## at time zone

Accepts two arguments: an expression (assumed to be a date/time of some sort)
and a time zone name or identifier (can be a string, a symbol, or a keyword):

```clojure
(sql/format-expr [:at-time-zone [:now] :utc])
;;=> ["NOW() AT TIME ZONE 'UTC'"]
```

The time zone name or identifier will be inlined (as a string) and therefore
cannot be an expression.

## between

Accepts three arguments: an expression, a lower bound, and
Expand Down
8 changes: 8 additions & 0 deletions src/honey/sql.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1632,6 +1632,7 @@
:primary-key #'function-0
:references #'function-1
:unique #'function-1-opt
;; dynamic dotted name creation:
:. (fn [_ [expr col subcol]]
(let [[sql & params] (format-expr expr)]
(into [(str sql "." (format-entity col)
Expand All @@ -1658,6 +1659,13 @@
(let [[sqls params] (format-expr-list arr)
type-str (when type (str "::" (sql-kw type) "[]"))]
(into [(str "ARRAY[" (str/join ", " sqls) "]" type-str)] params)))
:at-time-zone
(fn [_ [expr tz]]
(let [[sql & params] (format-expr expr {:nested true})
[tz-sql & _]
(binding [*inline* true]
(format-expr (if (ident? tz) (name tz) tz)))]
(into [(str sql " AT TIME ZONE " tz-sql)] params)))
:between
(fn [_ [x a b]]
(let [[sql-x & params-x] (format-expr x {:nested true})
Expand Down
8 changes: 8 additions & 0 deletions test/honey/sql_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,14 @@ ORDER BY id = ? DESC
(is (= ["INSERT INTO foo (bar) OUTPUT inserted.* VALUES (?)" 1]
(sut/format {:insert-into :foo :columns [:bar] :output [:inserted.*] :values [[1]]}))))

(deftest at-time-zone-503
(is (= ["SELECT foo AT TIME ZONE 'UTC'"]
(sut/format {:select [[[:at-time-zone :foo "UTC"]]]})))
(is (= ["SELECT foo AT TIME ZONE 'UTC'"]
(sut/format {:select [[[:at-time-zone :foo :UTC]]]})))
(is (= ["SELECT FOO(bar) AT TIME ZONE 'UTC'"]
(sut/format {:select [[[:at-time-zone [:foo :bar] :UTC]]]}))))

(comment
;; partial workaround for #407:
(sut/format {:select :f.* :from [[:foo [:f :for :system-time]]] :where [:= :f.id 1]})
Expand Down

0 comments on commit 756ed95

Please # to comment.