fixes #440 by supporting multiple tables in truncate
Signed-off-by: Sean Corfield <sean@corfield.org>
This commit is contained in:
parent
df753e8635
commit
f5dbc274be
4 changed files with 31 additions and 12 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
* 2.7.next in progress
|
* 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`.
|
* 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).
|
* 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.
|
* Update dev/build deps.
|
||||||
|
|
|
||||||
|
|
@ -830,13 +830,23 @@ is a "hard" delete as opposed to a temporal delete.
|
||||||
## truncate
|
## truncate
|
||||||
|
|
||||||
`:truncate` accepts a simple SQL entity (table name)
|
`: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
|
```clojure
|
||||||
user=> (sql/format '{truncate transport})
|
user=> (sql/format '{truncate transport})
|
||||||
["TRUNCATE TABLE transport"]
|
["TRUNCATE TABLE transport"]
|
||||||
|
user=> (sql/format '{truncate (transport)})
|
||||||
|
["TRUNCATE TABLE transport"]
|
||||||
user=> (sql/format '{truncate (transport restart identity)})
|
user=> (sql/format '{truncate (transport restart identity)})
|
||||||
["TRUNCATE TABLE 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
|
## columns
|
||||||
|
|
|
||||||
|
|
@ -459,8 +459,8 @@
|
||||||
(if (str/starts-with? c "'")
|
(if (str/starts-with? c "'")
|
||||||
(do
|
(do
|
||||||
(reset! *formatted-column* true)
|
(reset! *formatted-column* true)
|
||||||
[(subs c 1)])
|
(subs c 1))
|
||||||
[(format-entity x opts)])))
|
(format-entity x opts))))
|
||||||
|
|
||||||
(defn- format-var
|
(defn- format-var
|
||||||
([x] (format-var x {}))
|
([x] (format-var x {}))
|
||||||
|
|
@ -487,7 +487,7 @@
|
||||||
:else
|
:else
|
||||||
["?" (->param k)]))
|
["?" (->param k)]))
|
||||||
:else
|
:else
|
||||||
(format-simple-var x c opts)))))
|
[(format-simple-var x c opts)]))))
|
||||||
|
|
||||||
(defn- format-entity-alias [x]
|
(defn- format-entity-alias [x]
|
||||||
(cond (sequential? x)
|
(cond (sequential? x)
|
||||||
|
|
@ -1388,20 +1388,17 @@
|
||||||
[(butlast coll) (last coll) nil]))]
|
[(butlast coll) (last coll) nil]))]
|
||||||
(into [(join " " (map sql-kw) prequel)
|
(into [(join " " (map sql-kw) prequel)
|
||||||
(when table
|
(when table
|
||||||
(let [[v & more] (format-simple-var table)]
|
(format-simple-var table))
|
||||||
(when (seq more)
|
|
||||||
(throw (ex-info (str "DDL syntax error at: "
|
|
||||||
(pr-str table)
|
|
||||||
" - expected table name")
|
|
||||||
{:unexpected more})))
|
|
||||||
v))
|
|
||||||
(when ine (sql-kw ine))]
|
(when ine (sql-kw ine))]
|
||||||
(when opts
|
(when opts
|
||||||
(format-ddl-options opts context)))))
|
(format-ddl-options opts context)))))
|
||||||
|
|
||||||
(defn- format-truncate [_ xs]
|
(defn- format-truncate [_ xs]
|
||||||
(let [[table & options] (ensure-sequential 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 pre) (throw (ex-info "TRUNCATE syntax error" {:unexpected pre})))
|
||||||
(when (seq ine) (throw (ex-info "TRUNCATE syntax error" {:unexpected ine})))
|
(when (seq ine) (throw (ex-info "TRUNCATE syntax error" {:unexpected ine})))
|
||||||
[(join " " (cond-> ["TRUNCATE TABLE" table]
|
[(join " " (cond-> ["TRUNCATE TABLE" table]
|
||||||
|
|
@ -1414,6 +1411,11 @@
|
||||||
(destructure-ddl-item [:id [:int :unsigned :auto-increment]] "test")
|
(destructure-ddl-item [:id [:int :unsigned :auto-increment]] "test")
|
||||||
(destructure-ddl-item [[[:foreign-key :bar]] :quux [[:wibble :wobble]]] "test")
|
(destructure-ddl-item [[[:foreign-key :bar]] :quux [[:wibble :wobble]]] "test")
|
||||||
(format-truncate :truncate [:foo])
|
(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]
|
(defn- format-create [q k item as]
|
||||||
|
|
|
||||||
|
|
@ -614,6 +614,12 @@
|
||||||
(format {:dialect :mysql}))))
|
(format {:dialect :mysql}))))
|
||||||
(is (= ["TRUNCATE TABLE `foo` CONTINUE IDENTITY"]
|
(is (= ["TRUNCATE TABLE `foo` CONTINUE IDENTITY"]
|
||||||
(-> {:truncate [: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})))))
|
(format {:dialect :mysql})))))
|
||||||
|
|
||||||
(deftest inlined-values-are-stringified-correctly
|
(deftest inlined-values-are-stringified-correctly
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue