Created
July 5, 2011 03:57
-
-
Save seancorfield/1064231 to your computer and use it in GitHub Desktop.
find-by-keys, built on clojure.java.jdbc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def q | |
"Convert keyword to MySQL quoted entity name." | |
(partial sql/as-quoted-identifier \`)) | |
(def qi | |
"Naming strategy for MySQL quoting and no lowercasing in results." | |
{ :entity (partial sql/as-quoted-str \`) :keyword identity }) | |
(defn find-by-keys | |
"Given a table name (string) and a map of key/value pairs, | |
return a sequence of records processed under the function..." | |
[f table record] | |
(let [ks (map (fn [k] (str (q k) " = ? ")) (keys record)) | |
vs (vals record)] | |
(sql/with-naming-strategy | |
qi | |
(sql/with-connection | |
(ws/worldsingles-db-readonly) | |
(sql/with-query-results | |
rows | |
(apply vector | |
(apply str "SELECT * FROM " (q table) " WHERE " | |
(interpose "AND " ks)) vs) | |
(f rows)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment