diff --git a/CHANGELOG.md b/CHANGELOG.md index d25a6a3..ea5ebf6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # Changes * 2.4.next in progress - * Support BigQuery `CREATE OR REPLACE` (work-in-progress). + * Address [#511](https://github.com/seancorfield/honeysql/issues/511) by adding support for BigQuery `CREATE OR REPLACE`. * Address [#510](https://github.com/seancorfield/honeysql/issues/510) by adding some NRQL support (work-in-progress). * Fix [#509](https://github.com/seancorfield/honeysql/issues/509) by checking for `ident?` before checking keyword/symbol. diff --git a/doc/clause-reference.md b/doc/clause-reference.md index f5fafea..e45c86f 100644 --- a/doc/clause-reference.md +++ b/doc/clause-reference.md @@ -229,7 +229,8 @@ CREATE TABLE quux `:create-table-as` can accept a single table name or a sequence that starts with a table name, optionally followed by 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` or, +for BigQuery `:or-replace` or the symbol `or-replace`), optionally followed by a `{:columns ..}` clause to specify the columns to use in the created table, optionally followed by special syntax to specify `TABLESPACE` etc. @@ -261,7 +262,7 @@ A more concise version of the above can use the `TABLE` clause: ```clojure -user=> (sql/format {:create-table-as [:metro :if-not-exists +user=> (sql/format {:create-table-as [:metro :or-replace {:columns [:foo :bar :baz]} [:tablespace [:entity :quux]]], :table :cities, @@ -269,7 +270,7 @@ user=> (sql/format {:create-table-as [:metro :if-not-exists :with-data false} {:pretty true}) [" -CREATE TABLE IF NOT EXISTS metro (foo, bar, baz) TABLESPACE quux AS +CREATE OR REPLACE TABLE metro (foo, bar, baz) TABLESPACE quux AS TABLE cities WHERE metroflag = ? WITH NO DATA @@ -284,7 +285,13 @@ will be turned into SQL keywords (this is true for all of the {:create-table-as [:temp :metro :if-not-exists [..]] ..} ``` -to produce `CREATE TEMP TABLE IF NOT EXISTS metro ..`. +to produce `CREATE TEMP TABLE IF NOT EXISTS metro ..`, or: + +``` +{:create-table-as [:temp :metro :or-replace [..]] ..} +``` + +to produce `CREATE OR REPLACE TEMP TABLE metro ..`. ## create-extension diff --git a/test/honey/sql/helpers_test.cljc b/test/honey/sql/helpers_test.cljc index 534b978..245f486 100644 --- a/test/honey/sql/helpers_test.cljc +++ b/test/honey/sql/helpers_test.cljc @@ -559,6 +559,18 @@ (where [:= :metroflag "y"]) (with-data false))) ["CREATE OR REPLACE TABLE metro AS SELECT * FROM cities WHERE metroflag = ? WITH NO DATA" "y"])) + (is (= (sql/format (-> (create-table-as :temp :metro :if-not-exists) + (select :*) + (from :cities) + (where [:= :metroflag "y"]) + (with-data false))) + ["CREATE TEMP TABLE IF NOT EXISTS metro AS SELECT * FROM cities WHERE metroflag = ? WITH NO DATA" "y"])) + (is (= (sql/format (-> (create-table-as :temp :metro :or-replace) + (select :*) + (from :cities) + (where [:= :metroflag "y"]) + (with-data false))) + ["CREATE OR REPLACE TEMP TABLE metro AS SELECT * FROM cities WHERE metroflag = ? WITH NO DATA" "y"])) (is (= (sql/format (-> (create-materialized-view :metro :if-not-exists) (select :*) (from :cities)