diff --git a/CHANGELOG.md b/CHANGELOG.md index b0c9aaa..829f361 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changes * 2.5.next in progress + * Fix [#518](https://github.com/seancorfield/honeysql/issues/518) by moving temporal clause before alias. * Implemented `CREATE INDEX` [#348](https://github.com/seancorfield/honeysql/issues/348) via PR [#517](https://github.com/seancorfield/honeysql/pull/517) [@dancek](https://github.com/dancek). * 2.5.1103 -- 2023-12-03 diff --git a/doc/clause-reference.md b/doc/clause-reference.md index 44d7155..6becd01 100644 --- a/doc/clause-reference.md +++ b/doc/clause-reference.md @@ -825,7 +825,7 @@ user=> (sql/format {:select [:username] user=> (sql/format {:select [:u.username] :from [[:user :u :for :system-time :from [:inline "2019-08-01 15:23:00"] :to [:inline "2019-08-01 15:24:00"]]] :where [:= :u.id 9]}) -["SELECT u.username FROM user AS u FOR SYSTEM_TIME FROM '2019-08-01 15:23:00' TO '2019-08-01 15:24:00' WHERE u.id = ?" 9] +["SELECT u.username FROM user FOR SYSTEM_TIME FROM '2019-08-01 15:23:00' TO '2019-08-01 15:24:00' AS u WHERE u.id = ?" 9] ``` > Note: the actual formatting of a `:from` clause is currently identical to the formatting of a `:select` clause. diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index 67abc90..806246e 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -568,6 +568,8 @@ (format-temporal temporal))] (-> [(str sql + (when sql'' + (str " " sql'')) (when sql' (str (if as (if (and (contains? *dialect* :as) @@ -575,9 +577,7 @@ " " " AS ") " ") - sql')) - (when sql'' - (str " " sql'')))] + sql')))] (into params) (into params') (into params''))))) @@ -2383,7 +2383,13 @@ (sql/format {:select [[:column-name :'some-alias]] :from :b :order-by [[[:alias :'some-alias]]]}) + ;; this was an earlier (and incorrect!) workaround for temporal queries: (sql/format {:select :f.* :from [[:foo [:f :FOR :SYSTEM-TIME]]] :where [:= :f.id 1]}) + ;; this is the correct way to do it: + (sql/format {:select :f.* :from [[:foo :f :for :system-time]] :where [:= :f.id 1]}) + (sql/format {:select [:u.username] + :from [[:user :u :for :system-time :from [:inline "2019-08-01 15:23:00"] :to [:inline "2019-08-01 15:24:00"]]] + :where [:= :u.id 9]}) (sql/format {:using [[:source [:= :table.id :source.id]]]}) ;; #389 -- ONLY for from/join etc: @@ -2402,6 +2408,12 @@ (sql/register-clause! :overriding-system-value (fn [_ _] ["OVERRIDING SYSTEM VALUE"]) :values) + (sql/format-expr-list [:foo/id] {:drop-ns false}) + (sql/format-expr-list [:foo/id] {:drop-ns true}) + (sql/format-expr-list [:'foo/id] {:drop-ns true}) + (sql/format-expr-list [(keyword "foo/foo/id")] {:drop-ns false}) + (sql/format-expr-list [(keyword "foo/foo/id")] {:drop-ns true}) + (sql/format {:insert-into :foo :values [{:foo/id 1}]}) (sql/format {:insert-into :foo :values [{:id 1}] :overriding-system-value true}) (sql/format {:insert-into [{:overriding-value :system} [:transport :t] [:id :name]] diff --git a/test/honey/sql_test.cljc b/test/honey/sql_test.cljc index 7386fa0..81a221d 100644 --- a/test/honey/sql_test.cljc +++ b/test/honey/sql_test.cljc @@ -1055,7 +1055,7 @@ ORDER BY id = ? DESC (is (= ["SELECT \"A\"\"B\""] (sut/format {:select (keyword "A\"B")} {:dialect :oracle})))) (deftest issue-407-temporal - (is (= ["SELECT f.* FROM foo AS f FOR SYSTEM_TIME ALL WHERE f.id = ?" 1] + (is (= ["SELECT f.* FROM foo FOR SYSTEM_TIME ALL AS f WHERE f.id = ?" 1] (sut/format {:select :f.* :from [[:foo :f :for :system-time :all]] :where [:= :f.id 1]}))) (is (= ["SELECT * FROM foo FOR SYSTEM_TIME ALL WHERE id = ?" 1] (sut/format {:select :* :from [[:foo :for :system-time :all]] :where [:= :id 1]})))) @@ -1310,5 +1310,8 @@ ORDER BY id = ? DESC :quoted false}))))) (comment - ;; partial workaround for #407: - (sut/format {:select :f.* :from [[:foo [:f :for :system-time]]] :where [:= :f.id 1]})) + ;; partial (incorrect!) workaround for #407: + (sut/format {:select :f.* :from [[:foo [:f :for :system-time]]] :where [:= :f.id 1]}) + ;; correct version: + (sut/format {:select :f.* :from [[:foo :f :for :system-time]] :where [:= :f.id 1]}) + )