Skip to content
This repository has been archived by the owner on Dec 7, 2018. It is now read-only.

Add support for WHERE ... IN (...) #1

Merged
merged 1 commit into from
Nov 27, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ Generate SQL and parameters that can be used in **query** and **execute** expres
(select * :person)
;;=> ("SELECT * FROM person")
(select [:id :name] :person
(where {:email "user@domain.com"}))
;;=> ("SELECT id,name FROM person WHERE email = ?" "user@domain.com")
(where {:email "user@domain.com" :sex ["M", "F"]}))
;;=> ("SELECT id,name FROM person WHERE email = ? AND sex IN (?, ?)" "user@domain.com" "M", "F")
(select [{:p.id :userid} :p.name :a.city] {:person :p}
(join {:address :a} {:p.id :a.personid})
(where {:p.email "user@domain.com"}))
Expand Down
6 changes: 4 additions & 2 deletions src/java_jdbc/sql.clj
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ and update! high-level operations within clojure.java.jdbc directly." }
" AND "
(map (fn [k v]
(str (as-str entities k)
(if (nil? v) " IS NULL" " = ?")))
(if (sequential? v)
(str " IN (" (str/join ", " (repeat (count v) "?")) ")")
(if (nil? v) " IS NULL" " = ?"))))
ks vs))
(remove nil? vs))))
(remove nil? (flatten vs)))))
4 changes: 3 additions & 1 deletion test/java_jdbc/sql_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@

(deftest where-dsl
(is (= ["id = ?" 42] (where {:id 42})))
(is (= ["id IS NULL"] (where {:id nil}))))
(is (= ["id IS NULL"] (where {:id nil})))
(is (= ["id IN (?, ?, ?, ?, ?, ?)" 4 8 15 16 23 42] (where {:id [4 8 15 16 23 42]})))
(is (= ["name = ? AND code IS NULL AND id IN (?, ?, ?)" "wow" 4 8 15] (where {:code nil :id [4 8 15] :name "wow"}))))

(deftest select-where-dsl
(is (#{["SELECT * FROM a WHERE c = ? AND b = ?" 3 2]
Expand Down