Skip to content

Commit

Permalink
Fix JDBC-132 by providing better argument checking
Browse files Browse the repository at this point in the history
  • Loading branch information
seancorfield committed Jun 1, 2016
1 parent b224a3e commit c46688d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/main/clojure/clojure/java/jdbc.clj
Expand Up @@ -1156,8 +1156,12 @@ http://clojure-doc.org/articles/ecosystem/java_jdbc/home.html" }
entities (:entities opts identity)
table-spec-str (or (and table-spec (str " " table-spec)) "")
spec-to-string (fn [spec]
(str/join " " (cons (as-sql-name entities (first spec))
(map name (rest spec)))))]
(try
(str/join " " (cons (as-sql-name entities (first spec))
(map name (rest spec))))
(catch Exception _
(throw (IllegalArgumentException.
"column spec is not a sequence of keywords / strings")))))]
(format "CREATE TABLE %s (%s)%s"
(as-sql-name entities table)
(str/join ", " (map spec-to-string specs))
Expand Down
25 changes: 24 additions & 1 deletion src/test/clojure/clojure/java/test_utilities.clj
@@ -1,4 +1,5 @@
;; Copyright (c) Stephen C. Gilardi. All rights reserved. The use and
;; Copyright (c) 2008-2016 Sean Corfield, Stephen C. Gilardi.
;; All rights reserved. The use and
;; distribution terms for this software are covered by the Eclipse Public
;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can
;; be found in the file epl-v10.html at the root of this distribution. By
Expand Down Expand Up @@ -64,3 +65,25 @@
(sql/create-table-ddl :thing [[:col1 "int"] ["col2" :int]]
{:table-spec "ENGINE=MyISAM"
:entities clojure.string/upper-case}))))

;; since we have clojure.spec instrumentation enabled for Clojure 1.9.0
;; we need to account for the fact that we'll get different exceptions
;; for Clojure < 1.9.0 since the spec will trigger first and obscure
;; our own argument checking on 1.9.0+

(defn argument-exception?
"Given a thunk, try to execute it and return true if it throws
either an IllegalArgumentException or a Clojure exception from
clojure.spec."
[thunk]
(try
(thunk)
false
(catch IllegalArgumentException _ true)
(catch clojure.lang.ExceptionInfo e
(re-find #"did not conform to spec" (.getMessage e)))))

(deftest test-invalid-create-table-ddl
(is (argument-exception? (fn [] (sql/create-table-ddl :thing [[]]))))
(is (argument-exception? (fn [] (sql/create-table-ddl :thing [[:col1 "int"] []]))))
(is (argument-exception? (fn [] (sql/create-table-ddl :thing [:col1 "int"])))))

0 comments on commit c46688d

Please sign in to comment.