Skip to content

Commit

Permalink
fix some symbol/keyword resolution bugs
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Corfield <[email protected]>
  • Loading branch information
seancorfield committed Sep 29, 2024
1 parent de75ace commit e2dc330
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions src/honey/sql.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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!)]))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)]
Expand Down
11 changes: 11 additions & 0 deletions test/honey/sql_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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]})
Expand Down

0 comments on commit e2dc330

Please sign in to comment.