Skip to content

Instantly share code, notes, and snippets.

@martinklepsch
Forked from alandipert/deterpolate.clj
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinklepsch/83abda8c0b2da13d5dd4 to your computer and use it in GitHub Desktop.
Save martinklepsch/83abda8c0b2da13d5dd4 to your computer and use it in GitHub Desktop.
(defn deterpolate [s]
(loop [tokens (interpolate s)
out-str (StringBuffer.)
out-args []]
(if (seq tokens)
(let [token (first tokens)]
(cond (string? token)
(recur (rest tokens)
(doto out-str (.append token))
out-args)
(vector? token)
(recur (rest tokens)
(doto out-str (.append (str/join "," (map (constantly "?") token))))
(into out-args token))
:else
(recur (rest tokens)
(doto out-str (.append "?"))
(conj out-args token))))
(into [(str out-str)] out-args))))
@martinklepsch
Copy link
Author

user=> (deterpolate "select * from foo where x < ~{x} and y in (~{[a,b,3]})")
["select * from foo where x < ? and y in (?,?,?)" x a b 3]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment