fix #435 by documenting CREATE TEMP TABLE etc

This commit is contained in:
Sean Corfield 2022-10-01 00:31:20 -07:00
parent e553f4f169
commit 6c107b7cf0
3 changed files with 33 additions and 6 deletions

View file

@ -1,6 +1,7 @@
# Changes # Changes
* 2.3.next in progress * 2.3.next in progress
* Address [#435](https://github.com/seancorfield/honeysql/issues/435) by showing `CREATE TEMP TABLE` etc.
* Fix [#431](https://github.com/seancorfield/honeysql/issues/431). * Fix [#431](https://github.com/seancorfield/honeysql/issues/431).
* Address [#430](https://github.com/seancorfield/honeysql/issues/430) by treating `:'` as introducing a name that should be treating literally and not formatted as a SQL entity (which respects quoting, dot-splitting, etc); this effectively expands the "escape hatch" introduced via [#352](https://github.com/seancorfield/honeysql/issues/352) in 2.2.868. _Note that the function context behavior formats as a SQL entity, rather than the usual SQL "keyword", whereas this new context is a literal transcription rather than as a SQL entity!_ * Address [#430](https://github.com/seancorfield/honeysql/issues/430) by treating `:'` as introducing a name that should be treating literally and not formatted as a SQL entity (which respects quoting, dot-splitting, etc); this effectively expands the "escape hatch" introduced via [#352](https://github.com/seancorfield/honeysql/issues/352) in 2.2.868. _Note that the function context behavior formats as a SQL entity, rather than the usual SQL "keyword", whereas this new context is a literal transcription rather than as a SQL entity!_
* Address [#427](https://github.com/seancorfield/honeysql/issues/427) by adding `set-options!`. * Address [#427](https://github.com/seancorfield/honeysql/issues/427) by adding `set-options!`.

View file

@ -133,10 +133,9 @@ user=> (sql/format {:alter-table :fruit :rename-table :vegetable})
## create-table, with-columns ## create-table, with-columns
`:create-table` can accept a single table name or a pair `:create-table` can accept a single table name or a sequence
containing a table name and a flag indicating the creation containing a table name and a flag indicating the creation
should be conditional (`:if-not-exists` or the symbol `if-not-exists`, should be conditional (`:if-not-exists` or the symbol `if-not-exists`). `:create-table` should
although any truthy value will work). `:create-table` should
be used with `:with-columns` to specify the actual columns be used with `:with-columns` to specify the actual columns
in the table: in the table:
@ -149,6 +148,21 @@ user=> (sql/format {:create-table :fruit
["CREATE TABLE fruit (id INT NOT NULL, name VARCHAR(32) NOT NULL, cost FLOAT NULL)"] ["CREATE TABLE fruit (id INT NOT NULL, name VARCHAR(32) NOT NULL, cost FLOAT NULL)"]
``` ```
Any keywords (or symbols) preceding the table name will be
turned into SQL keywords (this is true for all of the `create-*`
DSL identifiers):
```clojure
user=> (sql/format {:create-table [:my :fancy :fruit :if-not-exists]
:with-columns
[[:id :int [:not nil]]
[:name [:varchar 32] [:not nil]]
[:cost :float :null]]})
["CREATE MY FANCY TABLE IF NOT EXISTS fruit (id INT NOT NULL, name VARCHAR(32) NOT NULL, cost FLOAT NULL)"]
```
This lets you write SQL like `CREATE TEMP TABLE foo ...` etc.
The `:with-columns` clause is formatted as if `{:inline true}` The `:with-columns` clause is formatted as if `{:inline true}`
was specified so nothing is parameterized. In addition, was specified so nothing is parameterized. In addition,
everything except the first element of a column description everything except the first element of a column description
@ -216,10 +230,20 @@ WITH NO DATA
" "y"] " "y"]
``` ```
As above, any keywords (or symbols) preceding the table name
will be turned into SQL keywords (this is true for all of the
`create-*` DSL identifiers) so you can write:
```
{:create-table-as [:temp :metro :if-not-exists [..]] ..}
```
to produce `CREATE TEMP TABLE IF NOT EXISTS metro ..`.
## create-extension ## create-extension
`:create-extension` can accept a single extension name or a pair `:create-extension` can accept a single extension name or a
of the extension name, followed by sequence of the extension name, followed by
a flag indicating the creation should be conditional a flag indicating the creation should be conditional
(`:if-not-exists` or the symbol `if-not-exists`). (`:if-not-exists` or the symbol `if-not-exists`).
See the [PostgreSQL](postgresql.md) section for examples. See the [PostgreSQL](postgresql.md) section for examples.

View file

@ -891,4 +891,6 @@
(deftest issue-431 (deftest issue-431
(testing "where false should not be ignored" (testing "where false should not be ignored"
(is (= {:where false} (is (= {:where false}
(where false))))) (where false)))
(is (= ["SELECT * FROM table WHERE FALSE"]
(sql/format {:select [:*] :from [:table] :where false})))))