From c46688d1dcffb352f848ba5f46358f7b6ec874bc Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Wed, 1 Jun 2016 10:37:38 -0700 Subject: [PATCH] Fix JDBC-132 by providing better argument checking --- src/main/clojure/clojure/java/jdbc.clj | 8 ++++-- .../clojure/clojure/java/test_utilities.clj | 25 ++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/main/clojure/clojure/java/jdbc.clj b/src/main/clojure/clojure/java/jdbc.clj index e26b9dc9..42455c3c 100644 --- a/src/main/clojure/clojure/java/jdbc.clj +++ b/src/main/clojure/clojure/java/jdbc.clj @@ -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)) diff --git a/src/test/clojure/clojure/java/test_utilities.clj b/src/test/clojure/clojure/java/test_utilities.clj index b4a0d557..a534e477 100644 --- a/src/test/clojure/clojure/java/test_utilities.clj +++ b/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 @@ -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"])))))