diff --git a/CHANGELOG.md b/CHANGELOG.md index d30bc75..ed5f018 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changes * 2.7.next in progress + * Address [#440](https://github.com/seancorfield/honeysql/issues/440) by supporting multiple tables in `:truncate`. * Support `USING HASH` as well as `USING GIN`. * Fix [#571](https://github.com/seancorfield/honeysql/issues/571) by allowing `:order-by` to take an empty sequence of columns (and be omitted). * Update dev/build deps. diff --git a/doc/clause-reference.md b/doc/clause-reference.md index 399be17..a7c70e2 100644 --- a/doc/clause-reference.md +++ b/doc/clause-reference.md @@ -830,13 +830,23 @@ is a "hard" delete as opposed to a temporal delete. ## truncate `:truncate` accepts a simple SQL entity (table name) -or a table name followed by various options: +or a table name followed by various options, or a +sequence that starts with a sequence of one or more table names, +optionally followed by various options: ```clojure user=> (sql/format '{truncate transport}) ["TRUNCATE TABLE transport"] +user=> (sql/format '{truncate (transport)}) +["TRUNCATE TABLE transport"] user=> (sql/format '{truncate (transport restart identity)}) ["TRUNCATE TABLE transport RESTART IDENTITY"] +user=> (sql/format '{truncate ((transport))}) +["TRUNCATE TABLE transport"] +user=> (sql/format '{truncate ((transport other))}) +["TRUNCATE TABLE transport, other"] +user=> (sql/format '{truncate ((transport other) restart identity)}) +["TRUNCATE TABLE transport, other RESTART IDENTITY"] ``` ## columns diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index 3fc43af..feb6d37 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -459,8 +459,8 @@ (if (str/starts-with? c "'") (do (reset! *formatted-column* true) - [(subs c 1)]) - [(format-entity x opts)]))) + (subs c 1)) + (format-entity x opts)))) (defn- format-var ([x] (format-var x {})) @@ -487,7 +487,7 @@ :else ["?" (->param k)])) :else - (format-simple-var x c opts))))) + [(format-simple-var x c opts)])))) (defn- format-entity-alias [x] (cond (sequential? x) @@ -1388,20 +1388,17 @@ [(butlast coll) (last coll) nil]))] (into [(join " " (map sql-kw) prequel) (when table - (let [[v & more] (format-simple-var table)] - (when (seq more) - (throw (ex-info (str "DDL syntax error at: " - (pr-str table) - " - expected table name") - {:unexpected more}))) - v)) + (format-simple-var table)) (when ine (sql-kw ine))] (when opts (format-ddl-options opts context))))) (defn- format-truncate [_ xs] (let [[table & options] (ensure-sequential xs) - [pre table ine options] (destructure-ddl-item [table options] "truncate")] + table (if (or (ident? table) (string? table)) + (format-simple-var table) + (join ", " (map format-simple-var table))) + [pre _ ine options] (destructure-ddl-item [nil options] "truncate")] (when (seq pre) (throw (ex-info "TRUNCATE syntax error" {:unexpected pre}))) (when (seq ine) (throw (ex-info "TRUNCATE syntax error" {:unexpected ine}))) [(join " " (cond-> ["TRUNCATE TABLE" table] @@ -1414,6 +1411,11 @@ (destructure-ddl-item [:id [:int :unsigned :auto-increment]] "test") (destructure-ddl-item [[[:foreign-key :bar]] :quux [[:wibble :wobble]]] "test") (format-truncate :truncate [:foo]) + (format-truncate :truncate ["foo, bar"]) + (format-truncate :truncate "foo, bar") + (format-truncate :truncate [[:foo :bar]]) + (format-truncate :truncate :foo) + (format {:truncate [[:foo] :x]}) ) (defn- format-create [q k item as] diff --git a/test/honey/sql_test.cljc b/test/honey/sql_test.cljc index bb44f22..5a5707a 100644 --- a/test/honey/sql_test.cljc +++ b/test/honey/sql_test.cljc @@ -614,6 +614,12 @@ (format {:dialect :mysql})))) (is (= ["TRUNCATE TABLE `foo` CONTINUE IDENTITY"] (-> {:truncate [:foo :continue :identity]} + (format {:dialect :mysql})))) + (is (= ["TRUNCATE TABLE `t1`, `t2`"] + (-> {:truncate [[:t1 :t2]]} + (format {:dialect :mysql})))) + (is (= ["TRUNCATE TABLE `t1`, `t2` CONTINUE IDENTITY"] + (-> {:truncate [[:t1 :t2] :continue :identity]} (format {:dialect :mysql}))))) (deftest inlined-values-are-stringified-correctly