fixes #440 by supporting multiple tables in truncate

Signed-off-by: Sean Corfield <sean@corfield.org>
This commit is contained in:
Sean Corfield 2025-04-23 17:45:59 -04:00
parent df753e8635
commit f5dbc274be
No known key found for this signature in database
4 changed files with 31 additions and 12 deletions

View file

@ -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.

View file

@ -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

View file

@ -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]

View file

@ -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