From da150f03e335473efbaf4f2e8c6caba34d3829dc Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Wed, 1 Mar 2023 16:55:54 -0800 Subject: [PATCH] address #474 --- CHANGELOG.md | 1 + src/honey/sql.cljc | 3 +++ test/honey/sql_test.cljc | 11 +++++++++++ 3 files changed, 15 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5468f08..2be18ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changes * 2.4.next in progress + * Address [#474](https://github.com/seancorfield/honeysql/issues/474) by adding dot-selection special syntax. Documentation TBD! * Improve docstrings for PostgreSQL operators via PR [#473](https://github.com/seancorfield/honeysql/pull/473) [@holyjak](https://github.com/holyjak). * Address [#471](https://github.com/seancorfield/honeysql/issues/471) by supported interspersed SQL keywords in function calls. Documentation TBD! * Fix [#467](https://github.com/seancorfield/honeysql/issues/467) by allowing single keywords (symbols) as a short hand for a single-element sequence in more constructs via PR [#470](https://github.com/seancorfield/honeysql/pull/470) [@p-himik](https://github.com/p-himik). diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index 7986516..3321fcd 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -1472,6 +1472,9 @@ :primary-key #'function-0 :references #'function-1 :unique #'function-1-opt + :. (fn [_ [expr col]] + (let [[sql & params] (format-expr expr)] + (into [(str sql "." (format-entity col))] params))) ;; used in DDL to force rendering as a SQL entity instead ;; of a SQL keyword: :entity (fn [_ [e]] [(format-entity e)]) diff --git a/test/honey/sql_test.cljc b/test/honey/sql_test.cljc index e26c887..4690fcf 100644 --- a/test/honey/sql_test.cljc +++ b/test/honey/sql_test.cljc @@ -1130,3 +1130,14 @@ ORDER BY id = ? DESC (is (= ["SELECT XMLELEMENT(NAME \"foo$bar\", XMLATTRIBUTES('xyz' AS \"a&b\"))"] (sut/format {:select [[[:xmlelement :*name :foo$bar [:xmlattributes [:inline "xyz"] :*as :a&b]]]]}))))) + +(deftest issue-474-dot-selection + (testing "basic dot selection" + (is (= ["SELECT a.b, c.d, e.f"] + (let [t :a c :d] + (sut/format {:select [[[:. t :b]] [[:. :c c]] [[:. :e :f]]]}))))) + (testing "basic field selection from composite" + (is (= ["SELECT (v).*, (w).x, (Y(z)).*"] + (sut/format '{select (((. (nest v) *)) + ((. (nest w) x)) + ((. (nest (y z)) *)))})))))