Skip to content

Commit 2374ea2

Browse files
committed
JDBC-177 allow numbers in column specs in create-table-ddl
1 parent b2b7c70 commit 2374ea2

File tree

5 files changed

+51
-25
lines changed

5 files changed

+51
-25
lines changed

.joker

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{:known-macros [clojure.java.jdbc/with-db-connection
2+
clojure.java.jdbc/with-db-metadata
3+
clojure.java.jdbc/with-db-transaction]}

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Changes coming in 0.7.10
2+
3+
* Relax restriction on `create-table-ddl` column specs to allow numbers (as well as keywords and strings) [JDBC-177](https://dev.clojure.org/jira/browse/JDBC-177).
4+
15
Changes in 0.7.9
26

37
* Fix behavior of multi-inserts when database does not support generated keys [JDBC-176](https://dev.clojure.org/jira/browse/JDBC-176).

src/main/clojure/clojure/java/jdbc.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1692,10 +1692,11 @@ http://clojure-doc.org/articles/ecosystem/java_jdbc/home.html"}
16921692
entities (:entities opts identity)
16931693
table-name (as-sql-name entities table)
16941694
table-spec-str (or (and table-spec (str " " table-spec)) "")
1695+
stringify (fn [x] (if (keyword? x) (name x) (str x)))
16951696
spec-to-string (fn [spec]
16961697
(try
16971698
(str/join " " (cons (as-sql-name entities (first spec))
1698-
(map name (rest spec))))
1699+
(map stringify (rest spec))))
16991700
(catch Exception _
17001701
(throw (IllegalArgumentException.
17011702
"column spec is not a sequence of keywords / strings")))))]

src/main/clojure/clojure/java/jdbc/spec.clj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
;; Copyright (c) 2016-2017 Sean Corfield. All rights reserved.
1+
;; Copyright (c) 2016-2019 Sean Corfield. All rights reserved.
22
;; The use and distribution terms for this software are covered by
33
;; the Eclipse Public License 1.0
44
;; (http://opensource.org/licenses/eclipse-1.0.php) which can be
@@ -407,7 +407,9 @@
407407
:opts (s/? ::exec-sql-options))
408408
:ret ::execute-result)
409409

410-
(s/def ::column-spec (s/cat :col ::identifier :spec (s/* (s/or :kw keyword? :str string?))))
410+
(s/def ::column-spec (s/cat :col ::identifier :spec (s/* (s/or :kw keyword?
411+
:str string?
412+
:num number?))))
411413

412414
(s/fdef sql/create-table-ddl
413415
:args (s/cat :table ::identifier

src/test/clojure/clojure/java/jdbc_test.clj

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
;; Migrated from clojure.contrib.test-sql 17 April 2011
2222

2323
(ns clojure.java.jdbc-test
24-
(:require [clojure.test :refer :all]
24+
(:require [clojure.test :refer [deftest is use-fixtures]]
2525
[clojure.java.jdbc :as sql]
2626
[clojure.string :as str]))
2727

@@ -887,27 +887,27 @@
887887
(is (= "fruit" (-> table-info
888888
first
889889
:table_name
890-
clojure.string/lower-case)))))
891-
(sql/with-db-connection [conn db]
892-
(sql/with-db-metadata [metadata conn {}]
893-
(let [table-info (sql/metadata-query (.getTables metadata
894-
nil nil nil
895-
(into-array ["TABLE" "VIEW"])))]
896-
(is (not= [] table-info))
897-
(is (= "fruit" (-> table-info
898-
first
899-
:table_name
900-
clojure.string/lower-case)))))
901-
;; JDBC-171 this used to blow up because the connnection is closed
902-
(sql/with-db-metadata [metadata conn {}]
903-
(let [table-info (sql/metadata-query (.getTables metadata
904-
nil nil nil
905-
(into-array ["TABLE" "VIEW"])))]
906-
(is (not= [] table-info))
907-
(is (= "fruit" (-> table-info
908-
first
909-
:table_name
910-
clojure.string/lower-case))))))))
890+
clojure.string/lower-case))))
891+
(sql/with-db-connection [conn db]
892+
(sql/with-db-metadata [metadata conn {}]
893+
(let [table-info (sql/metadata-query (.getTables metadata
894+
nil nil nil
895+
(into-array ["TABLE" "VIEW"])))]
896+
(is (not= [] table-info))
897+
(is (= "fruit" (-> table-info
898+
first
899+
:table_name
900+
clojure.string/lower-case)))))
901+
;; JDBC-171 this used to blow up because the connnection is closed
902+
(sql/with-db-metadata [metadata conn {}]
903+
(let [table-info (sql/metadata-query (.getTables metadata
904+
nil nil nil
905+
(into-array ["TABLE" "VIEW"])))]
906+
(is (not= [] table-info))
907+
(is (= "fruit" (-> table-info
908+
first
909+
:table_name
910+
clojure.string/lower-case)))))))))
911911

912912
(deftest test-metadata-managed-computed
913913
(doseq [db (test-specs)]
@@ -1254,6 +1254,22 @@
12541254
{:id (generated-key db 3) :name "Orange" :appearance "round" :cost nil :grade nil}
12551255
{:id (generated-key db 2) :name "Pear" :appearance "yellow" :cost nil :grade nil}] rows)))))
12561256

1257+
(deftest test-create-table-ddl
1258+
(is (re-find #"`foo` int default 0"
1259+
(sql/create-table-ddl :table
1260+
[[:foo :int :default 0]]
1261+
{:entities (sql/quoted :mysql)}))))
1262+
1263+
(comment
1264+
db (sql/create-table-ddl
1265+
table
1266+
[[:id :int "PRIMARY KEY AUTO_INCREMENT"]
1267+
[:name "VARCHAR(32)"]
1268+
[:appearance "VARCHAR(32)"]
1269+
[:cost :int]
1270+
[:grade :real]]
1271+
{:table-spec "ENGINE=InnoDB"}))
1272+
12571273
(deftest test-resultset-read-column
12581274
(extend-protocol sql/IResultSetReadColumn
12591275
String

0 commit comments

Comments
 (0)