Fixes #128 by adding :truncate / truncate

This commit is contained in:
Sean Corfield 2019-09-07 13:24:46 -07:00
parent 4fbf69e59e
commit 7831ebed38
5 changed files with 25 additions and 0 deletions

View file

@ -1,6 +1,7 @@
## Changes coming in 0.9.7 ## Changes coming in 0.9.7
* Fix #248 by treating alias as "not a subquery" when generating SQL for it. (@seancorfield) * Fix #248 by treating alias as "not a subquery" when generating SQL for it. (@seancorfield)
* Fix #128 by adding `truncate` support.
* Fix #99 by adding a note to the first use of `select` in the README that column names can be keywords or symbols but not strings. * Fix #99 by adding a note to the first use of `select` in the README that column names can be keywords or symbols but not strings.
## 0.9.6 ## 0.9.6

View file

@ -228,6 +228,14 @@ If your database supports it, you can also delete from multiple tables:
"musical"] "musical"]
``` ```
If you want to delete everything from a table, you can use `truncate`:
```clojure
(-> (truncate :films)
sql/format)
=> ["TRUNCATE films"]
```
Queries can be nested: Queries can be nested:
```clojure ```clojure

View file

@ -219,6 +219,7 @@
:update 70 :update 70
:delete 75 :delete 75
:delete-from 80 :delete-from 80
:truncate 85
:columns 90 :columns 90
:from 110 :from 110
:join 120 :join 120
@ -629,6 +630,9 @@
(defmethod format-clause :delete [[_ tables] _] (defmethod format-clause :delete [[_ tables] _]
(str "DELETE " (comma-join (map to-sql tables)))) (str "DELETE " (comma-join (map to-sql tables))))
(defmethod format-clause :truncate [[_ table] _]
(str "TRUNCATE " (to-sql table)))
(defn cte->sql (defn cte->sql
[[cte-name query]] [[cte-name query]]
(str (binding [*subquery?* false] (str (binding [*subquery?* false]

View file

@ -279,6 +279,13 @@
([tables] (delete nil tables)) ([tables] (delete nil tables))
([m tables] (build-clause :delete m tables))) ([m tables] (build-clause :delete m tables)))
(defmethod build-clause :truncate [_ m table]
(assoc m :truncate table))
(defn truncate
([table] (truncate nil table))
([m table] (build-clause :truncate m table)))
(macros/usetime (macros/usetime
(defhelper with [m ctes] (defhelper with [m ctes]
(assoc m :with ctes))) (assoc m :with ctes)))

View file

@ -228,6 +228,11 @@
:where [:= :t1.bar 42]} :where [:= :t1.bar 42]}
(format :quoting :mysql))))) (format :quoting :mysql)))))
(deftest truncate-test
(is (= ["TRUNCATE `foo`"]
(-> {:truncate :foo}
(format :quoting :mysql)))))
(deftest inlined-values-are-stringified-correctly (deftest inlined-values-are-stringified-correctly
(is (= ["SELECT foo, bar, NULL"] (is (= ["SELECT foo, bar, NULL"]
(format {:select [(honeysql.core/inline "foo") (format {:select [(honeysql.core/inline "foo")