Skip to content

Instantly share code, notes, and snippets.

@seancorfield
Created July 5, 2011 03:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seancorfield/1064231 to your computer and use it in GitHub Desktop.
Save seancorfield/1064231 to your computer and use it in GitHub Desktop.
find-by-keys, built on clojure.java.jdbc
(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