diff --git a/CHANGES.md b/CHANGES.md index f9130e6..8c6fef1 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,7 @@ +## 0.9.4 (in progress) + +* `#sql/inline nil` should produce `NULL`. Fix #221. (@seancorfield) + ## 0.9.3 * Support parameters in `#sql/raw`. Fix #219. (@seancorfield) diff --git a/src/honeysql/format.cljc b/src/honeysql/format.cljc index 7f1d9b8..01a2246 100644 --- a/src/honeysql/format.cljc +++ b/src/honeysql/format.cljc @@ -411,7 +411,10 @@ (str "ARRAY[" (comma-join (map to-sql (.-values x))) "]")) SqlInline (to-sql [x] - (str (.-value x))) + (let [v (.-value x)] + (if (some? v) + (str v) + "NULL"))) #?(:clj Object :cljs default) (to-sql [x] #?(:clj (add-anon-param x) diff --git a/test/honeysql/core_test.cljc b/test/honeysql/core_test.cljc index c7fc6af..2c3ceb6 100644 --- a/test/honeysql/core_test.cljc +++ b/test/honeysql/core_test.cljc @@ -205,6 +205,14 @@ (-> (select :*) (from :foo) (where [:= :id (sql/inline 5)]) + sql/format))) + ;; testing for = NULL always fails in SQL -- this test is just to show + ;; that an #inline nil should render as NULL (so make sure you only use + ;; it in contexts where a literal NULL is acceptable!) + (is (= ["SELECT * FROM foo WHERE id = NULL"] + (-> (select :*) + (from :foo) + (where [:= :id (sql/inline nil)]) sql/format)))) #?(:cljs (cljs.test/run-all-tests))