Merge pull request #217 from jkk/MichaelBlume-delete

Add multi-table DELETE that takes a FROM clause
This commit is contained in:
Sean Corfield 2018-06-27 10:22:23 -07:00 committed by GitHub
commit 71d7ced0a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 0 deletions

View file

@ -204,6 +204,22 @@ Deletes look as you would expect:
=> ["DELETE FROM films WHERE kind <> ?" "musical"]
```
If your database supports it, you can also delete from multiple tables:
```clojure
(-> (delete [:films :directors])
(from :films)
(join :directors [:= :films.director_id :directors.id])
(where [:<> :kind "musical"])
sql/format)
=> [#sql/regularize
"DELETE films, directors
FROM films
INNER JOIN directors ON films.director_id = directors.id
WHERE kind <> ?"
"musical"]
```
Queries can be nested:
```clojure

View file

@ -205,6 +205,7 @@
:select 50
:insert-into 60
:update 70
:delete 75
:delete-from 80
:columns 90
:from 110
@ -588,6 +589,9 @@
(defmethod format-clause :delete-from [[_ table] _]
(str "DELETE FROM " (to-sql table)))
(defmethod format-clause :delete [[_ tables] _]
(str "DELETE " (comma-join (map to-sql tables))))
(defn cte->sql
[[cte-name query]]
(str (binding [*subquery?* false]

View file

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

View file

@ -200,3 +200,17 @@
:set {:a 1}
:where [:= :bar.b 42]}
(format :quoting :mysql)))))
(deftest delete-from-test
(is (= ["DELETE FROM `foo` WHERE `foo`.`id` = ?" 42]
(-> {:delete-from :foo
:where [:= :foo.id 42]}
(format :quoting :mysql)))))
(deftest delete-test
(is (= ["DELETE `t1`, `t2` FROM `table1` `t1` INNER JOIN `table2` `t2` ON `t1`.`fk` = `t2`.`id` WHERE `t1`.`bar` = ?" 42]
(-> {:delete [:t1 :t2]
:from [[:table1 :t1]]
:join [[:table2 :t2] [:= :t1.fk :t2.id]]
:where [:= :t1.bar 42]}
(format :quoting :mysql)))))