From 49d8365bfb06c9e4813e9feb3eb0a847e9a01603 Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Wed, 10 Feb 2021 22:32:29 -0800 Subject: [PATCH] Addresses #293 partial with-columns implementation --- src/honey/sql.cljc | 16 +++++++++++++++- test/honey/sql/helpers_test.cljc | 5 +++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index ca768ab..118274c 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -485,7 +485,21 @@ (when if-exists (str (sql-kw :if-exists) " ")) (str/join ", " (map #'format-entity tables)))])) -(defn- format-table-columns [k [x]] ["()"]) +(defn- format-table-columns [k xs] + (let [simple-expr (fn [e] + (let [[x & y] (format-expr e)] + (when (seq y) + (throw (ex-info "column elements must be simple expressions" + {:expr e :params y}))) + x))] + (binding [*inline* true] + [(str "(\n " + (str/join ",\n " + (map #(str/join " " + (let [[id & spec] (map simple-expr %)] + (cons id (map upper-case spec)))) + xs)) + "\n)")]))) (def ^:private base-clause-order "The (base) order for known clauses. Can have items added and removed. diff --git a/test/honey/sql/helpers_test.cljc b/test/honey/sql/helpers_test.cljc index cf4ddba..27a9f78 100644 --- a/test/honey/sql/helpers_test.cljc +++ b/test/honey/sql/helpers_test.cljc @@ -335,8 +335,9 @@ (is (= (sql/format {:create-view :metro :select [:*] :from [:cities] :where [:= :metroflag "y"]}) ["CREATE VIEW metro AS SELECT * FROM cities WHERE metroflag = ?" "y"])) (is (= (sql/format {:create-table :films - :with-columns []}) - ["CREATE TABLE films ()"])) + :with-columns [[:id :int :unsigned :auto-increment] + [:name [:varchar 50] [:not nil]]]}) + ["CREATE TABLE films (\n id INT UNSIGNED AUTO_INCREMENT,\n name VARCHAR(50) NOT NULL\n)"])) (is (= (sql/format {:drop-table :foo}) ["DROP TABLE foo"])) (is (= (sql/format {:drop-table [:if-exists :foo]})