add create or replace view for postgresql

This commit is contained in:
Sean Corfield 2023-08-23 12:45:19 -07:00
parent fd98efc95c
commit 664e5e2644
3 changed files with 24 additions and 1 deletions

View file

@ -1,6 +1,7 @@
# Changes
* 2.4.next in progress
* Add `:create-or-replace-view` to support PostgreSQL's lack of `IF NOT EXISTS` for `CREATE VIEW`.
* Add `:select` with function call and alias example to README (PR [#502](https://github.com/seancorfield/honeysql/pull/502) [@markbastian](https://github.com/markbastian)).
* Address [#497](https://github.com/seancorfield/honeysql/issues/497) by adding `:alias` special syntax. Documentation TBD.
* Attempt to clarify the formatting behavior of the `:values` clause when used to produce column names.

View file

@ -318,6 +318,18 @@ user=> (sql/format {:refresh-materialized-view [:concurrently :products]
["REFRESH MATERIALIZED VIEW CONCURRENTLY products WITH NO DATA"]
```
PostgreSQL does not support `IF NOT EXISTS` on `CREATE VIEW` (it supports it on
`CREATE MATERIALIZED VIEW`!) so HoneySQL also has `:create-or-replace-view`
for this case:
```clojure
user=> (sql/format {:create-or-replace-view [:products]
:select [:*]
:from [:items]
:where [:= :category "product"]})
["CREATE OR REPLACE VIEW products AS SELECT * FROM items WHERE category = ?" "product"]
```
## drop-table, drop-extension, drop-view, drop-materialized-view
`:drop-table` et al can accept a single table (extension, view) name or a sequence of

View file

@ -29,7 +29,8 @@
it uppercase and replaces - with space). "
(:refer-clojure :exclude [format])
(:require [clojure.string :as str]
[honey.sql.protocols :as p]))
[honey.sql.protocols :as p]
[honey.sql :as sql]))
;; default formatting for known clauses
@ -410,6 +411,7 @@
[sql' & params'] (when (or pair? big?)
(cond (sequential? a)
(let [[sqls params] (format-expr-list a {:aliased true})]
(println "sequential alias expression:" a)
(into [(str/join " " sqls)] params))
big? ; BigQuery support #281
(reduce (fn [[sql & params] [k arg]]
@ -1196,6 +1198,8 @@
:create-extension (fn [_ x] (format-create :create :extension x nil))
:with-columns #'format-table-columns
:create-view (fn [_ x] (format-create :create :view x :as))
;; postgresql lacks if not exists:
:create-or-replace-view (fn [_ x] (format-create :create :or-replace-view x :as))
:create-materialized-view (fn [_ x] (format-create :create :materialized-view x :as))
:drop-table #'format-drop-items
:drop-extension #'format-drop-items
@ -2115,4 +2119,10 @@
:from :b
:order-by [[[:alias "some-alias"]]]}
{:quoted true})
(honey.sql/format {:select [[:column-name "some-alias"]]
:from :b
:order-by [[[:alias "some-alias"]]]}
{:dialect :mysql})
(sql/format {:select :f.* :from [[:foo [:f :FOR :SYSTEM-TIME]]] :where [:= :f.id 1]})
(sql/format {:using [[:source [:= :table.id :source.id]]]})
)