diff --git a/CHANGELOG.md b/CHANGELOG.md index bade664..3bb4988 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changes * 2.2.next in progress + * Address [#400](https://github.com/seancorfield/honeysql/issues/400) by adding `:table` clause. * Address [#399](https://github.com/seancorfield/honeysql/issues/399) by correcting multi-column `RETURNING` clauses in docs and tests. * Address [#398](https://github.com/seancorfield/honeysql/issues/398) by adding `honey.sql.pg-json` namespace that registers PostgreSQL JSON operators and provides symbolic names for "unwritable" operators (that contain `@`). * Fix [#387](https://github.com/seancorfield/honeysql/issues/387) again. diff --git a/doc/clause-reference.md b/doc/clause-reference.md index 845001e..bee45b9 100644 --- a/doc/clause-reference.md +++ b/doc/clause-reference.md @@ -190,6 +190,25 @@ WITH NO DATA Without the `{:columns ..}` clause, the table will be created based on the columns in the query that follows. +A more concise version of the above can use the `TABLE` clause: + + +```clojure +user=> (sql/format {:create-table-as [:metro :if-not-exists + {:columns [:foo :bar :baz]} + [:tablespace [:entity :quux]]], + :table :cities, + :where [:= :metroflag "y"], + :with-data false} + {:pretty true}) +[" +CREATE TABLE IF NOT EXISTS metro (foo, bar, baz) TABLESPACE quux AS +TABLE cities +WHERE metroflag = ? +WITH NO DATA +" "y"] +``` + ## create-extension `:create-extension` can accept a single extension name or a pair @@ -319,7 +338,7 @@ user=> (sql/format '{union [{select (id,status) from (table-a)} ["SELECT id, status FROM table_a UNION SELECT id, event AS status, from, table_b"] ``` -## select, select-distinct +## select, select-distinct, table `:select` and `:select-distinct` expect a sequence of SQL entities (column names or expressions). Any of the SQL entities can be a pair of entity and alias. If you are selecting an expression, you would most @@ -361,6 +380,9 @@ user=> (sql/format {:select [[:* :except [:a :b] :replace [[[:inline 2] :c]]]] : ["SELECT * EXCEPT (a, b) REPLACE (2 AS c) FROM table"] ``` +The `:table` clause is equivalent to `:select :* :from` and accepts just +a simple table name -- `:create-table-as` above for an example. + ## select-distinct-on Similar to `:select-distinct` above but the first element diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index 721fb34..1cbad1d 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -50,6 +50,7 @@ :refresh-materialized-view ;; then SQL clauses in priority order: :raw :nest :with :with-recursive :intersect :union :union-all :except :except-all + :table :select :select-distinct :select-distinct-on :select-top :select-distinct-top :into :bulk-collect-into :insert-into :update :delete :delete-from :truncate @@ -931,6 +932,7 @@ :union-all #'format-on-set-op :except #'format-on-set-op :except-all #'format-on-set-op + :table #'format-selector :select #'format-selects :select-distinct #'format-selects :select-distinct-on #'format-selects-on diff --git a/src/honey/sql/helpers.cljc b/src/honey/sql/helpers.cljc index f6a21e4..00dd098 100644 --- a/src/honey/sql/helpers.cljc +++ b/src/honey/sql/helpers.cljc @@ -876,6 +876,14 @@ [& cols] (generic :returning cols)) +(defn table + "Accepts a single table name and produces TABLE name + + This is equivalent to: SELECT * FROM name" + {:arglists '([name])} + [& args] + (generic-1 :table args)) + (defn with-data "Accepts a Boolean determining WITH DATA vs WITH NO DATA." {:arglists '([data?])}