diff --git a/CHANGELOG.md b/CHANGELOG.md index 6293e4e..3a1d2bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changes * 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). * 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!`. diff --git a/doc/clause-reference.md b/doc/clause-reference.md index 686bdfb..3896503 100644 --- a/doc/clause-reference.md +++ b/doc/clause-reference.md @@ -133,10 +133,9 @@ user=> (sql/format {:alter-table :fruit :rename-table :vegetable}) ## 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 -should be conditional (`:if-not-exists` or the symbol `if-not-exists`, -although any truthy value will work). `:create-table` should +should be conditional (`:if-not-exists` or the symbol `if-not-exists`). `:create-table` should be used with `:with-columns` to specify the actual columns 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)"] ``` +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}` was specified so nothing is parameterized. In addition, everything except the first element of a column description @@ -216,10 +230,20 @@ WITH NO DATA " "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` can accept a single extension name or a pair -of the extension name, followed by +`:create-extension` can accept a single extension name or a +sequence of the extension name, followed by a flag indicating the creation should be conditional (`:if-not-exists` or the symbol `if-not-exists`). See the [PostgreSQL](postgresql.md) section for examples. diff --git a/test/honey/sql/helpers_test.cljc b/test/honey/sql/helpers_test.cljc index f6c7303..a512556 100644 --- a/test/honey/sql/helpers_test.cljc +++ b/test/honey/sql/helpers_test.cljc @@ -891,4 +891,6 @@ (deftest issue-431 (testing "where false should not be ignored" (is (= {:where false} - (where false))))) + (where false))) + (is (= ["SELECT * FROM table WHERE FALSE"] + (sql/format {:select [:*] :from [:table] :where false})))))