Clojure functions for creating parameterized SQL statements.
jdbc-format is intended to be used with clojure.java.jdbc. It provides a mechanism of turning declarative templates into map-based parmeterized sql statements, eliminating the complexity of keeping track of parameter ordering. This is its one and only goal.
jdbc-format is tested on Clojure 1.5.1 only. It may work on other Clojure versions.
This is alpha quality software.
jdbc-format is available as a Maven artifact from Clojars:
[jdbc-format "0.2.0"]
jdbc-format follows Semantic Versioning. Please note that this means the public API for this library is not considered stable.
The library exports three functions: sql
, formatter
and sql-params-fn
. If you are the 99% use-case, you should only ever use sql-params-fn
.
(ns jdbc-format-usage
(:require (jdbc-format (core :refer [sql-params-fn]))))
(def template
"SELECT fruit FROM fruits WHERE color = :color AND citrus = :citrus?")
(def formatter
"A function that turns maps into a parameterized SQL vector according
to my template."
(sql-params-fn template))
(formatter {:color "orange" :citrus? true})
;; ["SELECT fruit FROM fruits WHERE color = ? AND citrus = ?" "orange" true]
(formatter {:color "black"})
;; ["SELECT fruit FROM fruits WHERE color = ? AND citrus = ?" "black" nil]
(formatter {:color "yellow" :citrus? false :peel? true})
;; ["SELECT fruit FROM fruits WHERE color = ? AND citrus = ?" "yellow" false]
- Remove the
formatting
function in favor ofsql-params-fn
, which covers the most common use case more effectively.
- Initial Release
Please post any comments, concerns, or issues to the Github issues page.
Copyright © 2013 ToBeReplaced
Distributed under the Eclipse Public License, the same as Clojure. The license can be found at epl-v10.html in the root of this distribution.