From 2f62872d5605e80633f613b8871bc3c1ab6e7389 Mon Sep 17 00:00:00 2001 From: "Kevin W. van Rooijen" Date: Fri, 15 Jan 2021 21:50:58 +0100 Subject: [PATCH 1/4] Add create / drop extension --- src/honeysql_postgres/format.cljc | 15 +++++++++++++++ src/honeysql_postgres/helpers.cljc | 6 ++++++ 2 files changed, 21 insertions(+) diff --git a/src/honeysql_postgres/format.cljc b/src/honeysql_postgres/format.cljc index 6182e0f..f423690 100644 --- a/src/honeysql_postgres/format.cljc +++ b/src/honeysql_postgres/format.cljc @@ -7,6 +7,8 @@ (def ^:private custom-additions {:create-table 10 :drop-table 10 + :create-extension 10 + :drop-extension 10 :alter-table 20 :add-column 30 :drop-column 40 @@ -223,3 +225,16 @@ (defmethod format-modifiers :distinct-on [[_ & fields]] (str "DISTINCT ON(" (sqlf/comma-join (map sqlf/to-sql fields)) ")")) + +(defmethod sqlf/format-clause :drop-extension [[_ [extension-name]] _] + (str "DROP EXTENSION " + (-> extension-name + util/get-first + sqlf/to-sql))) + +(defmethod format-clause :create-extension [[_ [ extension-name if-not-exists]] _] + (str "CREATE EXTENSION " + (when if-not-exists "IF NOT EXISTS ") + (-> extension-name + util/get-first + sqlf/to-sql))) diff --git a/src/honeysql_postgres/helpers.cljc b/src/honeysql_postgres/helpers.cljc index 80fba56..3377f19 100644 --- a/src/honeysql_postgres/helpers.cljc +++ b/src/honeysql_postgres/helpers.cljc @@ -67,3 +67,9 @@ (defhelper insert-into-as [m fields] (assoc m :insert-into-as (sqlh/collify fields))) + +(defhelper create-extension [m extension-name] + (assoc m :create-extension (sqlh/collify extension-name))) + +(defhelper drop-extension [m extension-name] + (assoc m :drop-extension (sqlh/collify extension-name))) From 5531a24f28598f70a06f5dbb21ee4b705442733c Mon Sep 17 00:00:00 2001 From: "Kevin W. van Rooijen" Date: Fri, 15 Jan 2021 21:59:29 +0100 Subject: [PATCH 2/4] Add tests for create-extension / drop-extension --- test/honeysql_postgres/postgres_test.cljc | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/test/honeysql_postgres/postgres_test.cljc b/test/honeysql_postgres/postgres_test.cljc index 92802ca..5fd25c7 100644 --- a/test/honeysql_postgres/postgres_test.cljc +++ b/test/honeysql_postgres/postgres_test.cljc @@ -5,7 +5,8 @@ alter-table rename-column drop-column add-column partition-by insert-into-as create-table rename-table drop-table - window create-view over with-columns]] + window create-view over with-columns + create-extension drop-extension]] [honeysql.helpers :as sqlh :refer [insert-into values where select columns from order-by update sset query-values modifiers]] @@ -257,3 +258,22 @@ (from :products) (modifiers :distinct-on :a :b) (sql/format :quoting :ansi)))))) + +(deftest create-extension-test + (testing "create extension" + (is (= ["CREATE EXTENSION \"uuid-ossp\""] + (-> (create-extension :uuid-ossp) + (sql/format :allow-dashed-names? true + :quoting :ansi))))) + (testing "create extension if not exists" + (is (= ["CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\""] + (-> (create-extension :uuid-ossp :if-not-exists? true) + (sql/format :allow-dashed-names? true + :quoting :ansi)))))) + +(deftest drop-extension-test + (testing "create extension" + (is (= ["DROP EXTENSION \"uuid-ossp\""] + (-> (drop-extension :uuid-ossp) + (sql/format :allow-dashed-names? true + :quoting :ansi)))))) From 2ffb0b2b8c803b31814e4744f79da7c49dcd8be4 Mon Sep 17 00:00:00 2001 From: "Kevin W. van Rooijen" Date: Fri, 12 Feb 2021 12:14:50 +0100 Subject: [PATCH 3/4] Add create-extension / drop-extension to README --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index c900da8..0bbe87e 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,8 @@ This library aims to extend the features of honeysql to support postgres specifi - [create table](#create-table) - [drop table](#drop-table) - [alter table](#alter-table) + - [create extension](#create-extension) + - [drop extension](#drop-extension) - [pattern matching](#pattern-matching) - [except](#except) - [SQL functions](#sql-functions) @@ -143,6 +145,23 @@ use `alter-table` along with `add-column` & `drop-column` to modify table level => ["ALTER TABLE employees DROP COLUMN address"] ``` +### create-extension +`create-extension` can be used to create extensions with a given keyword. +```clj +(-> (create-extension :uuid-ossp :if-not-exists? true) + (sql/format :allow-dashed-names? true + :quoting :ansi)) + +=> ["CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\""] +``` + +### drop-extension +`drop-extension` is used to drop extensions. +```clj +(drop-extension "uuid-ossp") +=> ["DROP EXTENSION \"uuid-ossp\""] +``` + ### pattern matching The `ilike` and `not-ilike` operators can be used to query data using a pattern matching technique. - like From 6f3ac6bc8698bec2d310712201e162b411b600c9 Mon Sep 17 00:00:00 2001 From: "Kevin W. van Rooijen" Date: Fri, 12 Feb 2021 12:47:53 +0100 Subject: [PATCH 4/4] Fix formatting, update drop-extension example --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0bbe87e..072f58a 100644 --- a/README.md +++ b/README.md @@ -149,8 +149,8 @@ use `alter-table` along with `add-column` & `drop-column` to modify table level `create-extension` can be used to create extensions with a given keyword. ```clj (-> (create-extension :uuid-ossp :if-not-exists? true) - (sql/format :allow-dashed-names? true - :quoting :ansi)) + (sql/format :allow-dashed-names? true + :quoting :ansi)) => ["CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\""] ``` @@ -158,7 +158,9 @@ use `alter-table` along with `add-column` & `drop-column` to modify table level ### drop-extension `drop-extension` is used to drop extensions. ```clj -(drop-extension "uuid-ossp") +(-> (drop-extension :uuid-ossp) + (sql/format :allow-dashed-names? true + :quoting :ansi)) => ["DROP EXTENSION \"uuid-ossp\""] ```