diff --git a/CHANGELOG.md b/CHANGELOG.md index 38b3350..5d55dd3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,6 @@ * 2.0.next in progress * The documentation continues to be expanded and clarified in response to feedback! - * Tentative fix for #315 by expanding `:in` handling to deal with `nil` values. * Fix #310 by adding support for `FILTER`, `WITHIN GROUP`, and `ORDER BY` (as an expression), from [nilenso/honeysql-postgres](https://github.com/nilenso/honeysql-postgres) 0.4.112. These are [Special Syntax](doc/special-syntax.md) and there are also helpers for `filter` and `within-group` -- so **be careful about referring in all of `honey.sql.helpers`** since it will now shadow `clojure.core/filter` (it already shadows `for`, `group-by`, `into`, `partition-by`, `set`, and `update`). * Fix #308 by supporting join clauses in `join-by` (and correcting the helper docstring). diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index 148db70..337d02c 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -927,34 +927,17 @@ x)) (defn- format-in [in [x y]] - (let [nil-check (and (sequential? y) (not (ident? (first y)))) - y' (if nil-check (remove nil? y) y) - [sql-x & params-x] (format-expr x {:nested true}) - [sql-y & params-y] (format-expr y' {:nested true}) + (let [[sql-x & params-x] (format-expr x {:nested true}) + [sql-y & params-y] (format-expr y {:nested true}) values (unwrap (first params-y) {})] (if (and (= "?" sql-y) (= 1 (count params-y)) (coll? values)) - (let [values' (remove nil? values) - sql (str "(" (str/join ", " (repeat (count values') "?")) ")") - in (str sql-x " " (sql-kw in) " " sql)] - (-> [(if (not= (count values) (count values')) - (if (zero? (count values')) - (str sql-x " IS NULL") - (str "(" in " OR " sql-x " IS NULL)")) - (if (zero? (count values)) - "FALSE" - in))] + (let [sql (str "(" (str/join ", " (repeat (count values) "?")) ")")] + (-> [(str sql-x " " (sql-kw in) " " sql)] (into params-x) - (into values'))) - (let [in (str sql-x " " (sql-kw in) " " sql-y)] - (-> [(if (and nil-check (not= (count y) (count y'))) - (if (zero? (count y')) - (str sql-x " IS NULL") - (str "(" in " OR " sql-x " IS NULL)")) - (if (zero? (count y)) - "FALSE" - in))] - (into params-x) - (into params-y)))))) + (into values))) + (-> [(str sql-x " " (sql-kw in) " " sql-y)] + (into params-x) + (into params-y))))) (defn- function-0 [k xs] [(str (sql-kw k) diff --git a/test/honey/sql/helpers_test.cljc b/test/honey/sql/helpers_test.cljc index c92591e..14d1ce3 100644 --- a/test/honey/sql/helpers_test.cljc +++ b/test/honey/sql/helpers_test.cljc @@ -290,39 +290,6 @@ :from [:customers] :where [:in :id values]}))) (is (= ["SELECT * FROM customers WHERE id IN (?)" 1] - (sql/format {:select [:*] - :from [:customers] - :where [:in :id :?ids]} - {:params {:ids values}}))))) - (testing (str "with just a nil value from a " (name cname)) - (let [values (conj coll nil)] - (is (= ["SELECT * FROM customers WHERE id IS NULL"] - (sql/format {:select [:*] - :from [:customers] - :where [:in :id values]}))) - (is (= ["SELECT * FROM customers WHERE id IS NULL"] - (sql/format {:select [:*] - :from [:customers] - :where [:in :id :?ids]} - {:params {:ids values}}))))) - (testing (str "with nil values from a " (name cname)) - (let [values (conj coll 1 nil)] - (is (= ["SELECT * FROM customers WHERE (id IN (?) OR id IS NULL)" 1] - (sql/format {:select [:*] - :from [:customers] - :where [:in :id values]}))) - (is (= ["SELECT * FROM customers WHERE (id IN (?) OR id IS NULL)" 1] - (sql/format {:select [:*] - :from [:customers] - :where [:in :id :?ids]} - {:params {:ids values}}))))) - (testing (str "with no values from a " (name cname)) - (let [values coll] - (is (= ["SELECT * FROM customers WHERE FALSE"] - (sql/format {:select [:*] - :from [:customers] - :where [:in :id values]}))) - (is (= ["SELECT * FROM customers WHERE FALSE"] (sql/format {:select [:*] :from [:customers] :where [:in :id :?ids]} @@ -334,16 +301,6 @@ :from [:customers] :where [:in :id values]}))) (is (= ["SELECT * FROM customers WHERE id IN (?, ?)" 1 2] - (sql/format {:select [:*] - :from [:customers] - :where [:in :id :?ids]} - {:params {:ids values}})))) - (let [values [1 nil 2]] - (is (= ["SELECT * FROM customers WHERE (id IN (?, ?) OR id IS NULL)" 1 2] - (sql/format {:select [:*] - :from [:customers] - :where [:in :id values]}))) - (is (= ["SELECT * FROM customers WHERE (id IN (?, ?) OR id IS NULL)" 1 2] (sql/format {:select [:*] :from [:customers] :where [:in :id :?ids]}