From 664e5e2644d6bd5afff84c16e5aaf923eb7380c9 Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Wed, 23 Aug 2023 12:45:19 -0700 Subject: [PATCH] add create or replace view for postgresql --- CHANGELOG.md | 1 + doc/clause-reference.md | 12 ++++++++++++ src/honey/sql.cljc | 12 +++++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 77ecc40..19f07c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/doc/clause-reference.md b/doc/clause-reference.md index 22f7db4..ff2d179 100644 --- a/doc/clause-reference.md +++ b/doc/clause-reference.md @@ -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 diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index fce02b2..05b2c02 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -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]]]}) )