diff options
author | scgilardi <scgilardi@gmail.com> | 2008-09-14 06:06:27 +0000 |
---|---|---|
committer | scgilardi <scgilardi@gmail.com> | 2008-09-14 06:06:27 +0000 |
commit | 8ea24c5aeed899f719319187686bf9433740febd (patch) | |
tree | 1e106736c4e50bdcc4332f978073251e662bea84 /src/clojure/contrib/sql/sql.clj | |
parent | bdb9ad843f8c831c08f08fcfbcb2fbfebf507bbd (diff) |
sql: add insert-values, insert-rows
Diffstat (limited to 'src/clojure/contrib/sql/sql.clj')
-rw-r--r-- | src/clojure/contrib/sql/sql.clj | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/clojure/contrib/sql/sql.clj b/src/clojure/contrib/sql/sql.clj index e1becf56..313df7de 100644 --- a/src/clojure/contrib/sql/sql.clj +++ b/src/clojure/contrib/sql/sql.clj @@ -70,6 +70,27 @@ (do-commands con (format "drop table %s" name))) +(defn insert-values + "Inserts values into columns of a table. Columns is a seq of column + names (as strings) and each value is a seq of values for those + columns. To insert complete rows (all columns), use insert-rows" + [con table columns & values] + (let [count (count (first values)) + template (apply str (interpose "," (replicate count "?"))) + cols (if (seq columns) + (format "(%s)" (apply str (interpose "," columns))) + "")] + (apply do-prepared + con + (format "insert into %s %s values (%s)" table cols template) + values))) + +(defn insert-rows + "Inserts complete rows into a table. Each row is a seq of values for + each of the table's columns in order." + [con table & rows] + (apply insert-values con table nil rows)) + (defmacro with-results "Executes a query and then evaluates body repeatedly with rec bound to each of the generated results in turn" |