From 061288f1c06f22581c3caa30f1c851a565cc7ff3 Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Mon, 12 Sep 2022 18:17:26 -0700 Subject: [PATCH] address #415 by supporting multi-column add/alter/modify --- src/honey/sql.cljc | 10 +++++++--- src/honey/sql/helpers.cljc | 12 +++++++++--- test/honey/sql/helpers_test.cljc | 5 +++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index 95ef85e..3f4081a 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -959,10 +959,14 @@ (str/join ", " (map #'format-single-column xs)) ")")]) -(defn- format-add-item [k spec] +(defn- format-add-single-item [k spec] (if (contains? #{:if-not-exists 'if-not-exists} (last spec)) - [(str (sql-kw k) " " (sql-kw :if-not-exists) " " (format-single-column (butlast spec)))] - [(str (sql-kw k) " " (format-single-column spec))])) + (str (sql-kw k) " " (sql-kw :if-not-exists) " " (format-single-column (butlast spec))) + (str (sql-kw k) " " (format-single-column spec)))) + +(defn- format-add-item [k spec] + (let [items (if (and (sequential? spec) (sequential? (first spec))) spec [spec])] + [(str/join ", " (for [item items] (format-add-single-item k item)))])) (defn- format-rename-item [k [x y]] [(str (sql-kw k) " " (format-entity x) " TO " (format-entity y))]) diff --git a/src/honey/sql/helpers.cljc b/src/honey/sql/helpers.cljc index 0dd99a0..ff1b2c1 100644 --- a/src/honey/sql/helpers.cljc +++ b/src/honey/sql/helpers.cljc @@ -138,6 +138,12 @@ (helper-merge data k args)) (helper-merge {} k args))) +(defn- generic-grouped [k args] + (if (map? (first args)) + (let [[data & args] args] + (helper-merge data k [args])) + (helper-merge {} k [args]))) + (defn- generic-1 [k [data arg]] (if (map? data) (assoc data k arg) @@ -169,7 +175,7 @@ (add-column :name [:varchar 32] [:not nil])" [& col-elems] - (generic :add-column col-elems)) + (generic-grouped :add-column col-elems)) (defn drop-column "Takes one or more column names (use with `alter-table`). @@ -188,7 +194,7 @@ (alter-column :name [:varchar 64] [:not nil])" [& col-elems] - (generic :alter-column col-elems)) + (generic-grouped :alter-column col-elems)) (defn modify-column "Like add-column, accepts any number of SQL elements @@ -199,7 +205,7 @@ MySQL-specific, deprecated. Use `alter-column` and specify the MySQL dialect to get `MODIFY COLUMN`." [& col-elems] - (generic :modify-column col-elems)) + (generic-grouped :modify-column col-elems)) (defn rename-column "Accepts two column names: the original name and the diff --git a/test/honey/sql/helpers_test.cljc b/test/honey/sql/helpers_test.cljc index 37bf7f0..c32cdd8 100644 --- a/test/honey/sql/helpers_test.cljc +++ b/test/honey/sql/helpers_test.cljc @@ -631,6 +631,11 @@ (is (= (sql/format (-> (alter-table :fruit) (add-column :id :int [:not nil]))) ["ALTER TABLE fruit ADD COLUMN id INT NOT NULL"])) + (is (= (sql/format (-> (alter-table :fruit) + (add-column :id :int [:not nil]) + (add-column :a1 :int nil) + (add-column :be :text [:not nil]))) + ["ALTER TABLE fruit ADD COLUMN id INT NOT NULL, ADD COLUMN a1 INT NULL, ADD COLUMN be TEXT NOT NULL"])) (is (= (sql/format (alter-table :fruit (add-column :id :int [:not nil]) (drop-column :ident)