fix some symbol/keyword resolution bugs

Signed-off-by: Sean Corfield <sean@corfield.org>
This commit is contained in:
Sean Corfield 2024-09-29 16:12:32 -07:00
parent de75ace988
commit e2dc330cf9
No known key found for this signature in database
3 changed files with 19 additions and 4 deletions

View file

@ -7,6 +7,7 @@
* Address [#541](https://github.com/seancorfield/honeysql/issues/541) by specifying the expected result of a formatter function passed to `register-clause!` and adding the example from the README to **Extending HoneySQL**.
* Getting Started updated based on feedback from Los Angeles Clojure meetup walkthrough [#539](https://github.com/seancorfield/honeysql/issues/539).
* Fix [#538](https://github.com/seancorfield/honeysql/issues/538) by removing `mod` from list of infix operators.
* Fixed a few symbol/keyword resolution bugs in the formatter. Thanks to [@irigarae](https://github.com/irigarae).
* Update Clojure version to 1.12.0; update dev/test/ci deps.
* 2.6.1161 -- 2024-08-29

View file

@ -516,14 +516,15 @@
"Given a general selectable item, split it into the subject selectable,
an optional alias, and any temporal clauses present."
[[selectable alias-for for-part & more]]
(let [no-alias? (and (= :for (sym->kw alias-for)) for-part)]
(let [no-alias? (and (contains? #{:for 'for} alias-for)
for-part)]
[selectable
(if no-alias?
nil
alias-for)
(cond no-alias?
(into [alias-for for-part] more)
(= :for (sym->kw for-part))
(contains? #{:for 'for} for-part)
(cons for-part more)
(or for-part (seq more))
::too-many!)]))
@ -946,7 +947,8 @@
true
[j])
sqls (conj sqls sql-j)]
(if (and (sequential? e) (= :using (first e)))
(if (and (sequential? e)
(contains? #{:using 'using} (first e)))
(let [[u-sqls u-params]
(reduce-sql (map #'format-entity-alias) (rest e))]
[(conj sqls
@ -1319,7 +1321,8 @@
(defn- format-create-index [k clauses]
(let [[index-spec [table & exprs]] clauses
[pre entity ine & more] (destructure-ddl-item index-spec (str (sql-kw k) " options"))
[using & exprs] (if (= :using-gin (first exprs))
[using & exprs] (if (contains? #{:using-gin 'using-gin}
(first exprs))
exprs
(cons nil exprs))
[sqls params] (format-expr-list exprs)]

View file

@ -1376,6 +1376,17 @@ ORDER BY id = ? DESC
(sut/format {:select :a :from :table :where [:= :x [:param p1]]}
{:params {p2 42}}))))))
(deftest issue-n-using
(testing "all keywords"
(is (= ["SELECT * FROM `t1` INNER JOIN `t2` USING (`id`) WHERE `t1`.`id` = ?" 1]
(sut/format {:select :* :from :t1 :join [:t2 [:using :id]] :where [:= :t1/id 1]} {:dialect :mysql}))))
(testing "all symbols"
(is (= ["SELECT * FROM `t1` INNER JOIN `t2` USING (`id`) WHERE `t1`.`id` = ?" 1]
(sut/format '{select * from t1 join (t2 (using id)) where (= t1/id 1)} {:dialect :mysql}))))
(testing "mixed keywords and symbols"
(is (= ["SELECT * FROM `t1` INNER JOIN `t2` USING (`id`) WHERE `t1`.`id` = ?" 1]
(sut/format '{select * from t1 join (t2 (:using id)) where (= t1/id 1)} {:dialect :mysql})))))
(comment
;; partial (incorrect!) workaround for #407:
(sut/format {:select :f.* :from [[:foo [:f :for :system-time]]] :where [:= :f.id 1]})