Permalink
Browse files

Address JDBC-45 by supporting nil / empty column list in sql/insert

  • Loading branch information...
seancorfield committed Apr 6, 2013
1 parent 63aeed6 commit 5389202342617c435e59fc84096a358aca187882
Showing with 21 additions and 6 deletions.
  1. +8 −5 src/main/clojure/clojure/java/jdbc/sql.clj
  2. +13 −1 src/test/clojure/clojure/java/test_sql.clj
@@ -113,12 +113,15 @@ and update! high-level operations within clojure.java.jdbc directly." }
[table columns values entities]
(let [nc (count columns)
vcs (map count values)]
- (if (not (apply = nc vcs))
+ (if (not (and (or (zero? nc) (= nc (first vcs))) (apply = vcs)))
(throw (IllegalArgumentException. "insert called with inconsistent number of columns / values"))
- (into [(str "INSERT INTO " (table-str table entities) " ( "
- (str/join ", " (map (fn [col] (col-str col entities)) columns))
- " ) VALUES ( "
- (str/join ", " (repeat nc "?"))
+ (into [(str "INSERT INTO " (table-str table entities)
+ (when (seq columns)
+ (str " ( "
+ (str/join ", " (map (fn [col] (col-str col entities)) columns))
+ " )"))
+ " VALUES ( "
+ (str/join ", " (repeat (first vcs) "?"))
" )")]
values))))
@@ -104,15 +104,27 @@
(deftest insert-dsl
(is (= ["INSERT INTO a ( b ) VALUES ( ? )" [2]] (insert :a [:b] [2])))
+ (is (= ["INSERT INTO a VALUES ( ? )" [2]] (insert :a nil [2])))
+ (is (= ["INSERT INTO a VALUES ( ? )" [2]] (insert :a [] [2])))
(is (= [["INSERT INTO a ( b ) VALUES ( ? )" 2]] (insert :a {:b 2})))
(is (= ["INSERT INTO a ( b ) VALUES ( ? )" [2] [3]] (insert :a [:b] [2] [3])))
+ (is (= ["INSERT INTO a VALUES ( ? )" [2] [3]] (insert :a nil [2] [3])))
+ (is (= ["INSERT INTO a VALUES ( ? )" [2] [3]] (insert :a [] [2] [3])))
(is (= ["INSERT INTO a ( b, c, d ) VALUES ( ?, ?, ? )" [2 3 4] [3 4 5]]
(insert :a [:b :c :d] [2 3 4] [3 4 5])))
+ (is (= ["INSERT INTO a VALUES ( ?, ?, ? )" [2 3 4] [3 4 5]]
+ (insert :a nil [2 3 4] [3 4 5])))
+ (is (= ["INSERT INTO a VALUES ( ?, ?, ? )" [2 3 4] [3 4 5]]
+ (insert :a [] [2 3 4] [3 4 5])))
(is (= [["INSERT INTO a ( b ) VALUES ( ? )" 2]
["INSERT INTO a ( b ) VALUES ( ? )" 3]]
(insert :a {:b 2} {:b 3})))
(is (= ["INSERT INTO `a` ( `b` ) VALUES ( ? )" [2]]
- (entities (quoted \`) (insert :a [:b] [2])))))
+ (entities (quoted \`) (insert :a [:b] [2]))))
+ (is (= ["INSERT INTO `a` VALUES ( ? )" [2]]
+ (entities (quoted \`) (insert :a nil [2]))))
+ (is (= ["INSERT INTO `a` VALUES ( ? )" [2]]
+ (entities (quoted \`) (insert :a [] [2])))))
(deftest bad-insert-args
(is (thrown? IllegalArgumentException (insert)))

0 comments on commit 5389202

Please sign in to comment.