Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -143,6 +145,25 @@ 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)
(sql/format :allow-dashed-names? true
:quoting :ansi))
=> ["DROP EXTENSION \"uuid-ossp\""]
```

### pattern matching
The `ilike` and `not-ilike` operators can be used to query data using a pattern matching technique.
- like
Expand Down
15 changes: 15 additions & 0 deletions src/honeysql_postgres/format.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)))
6 changes: 6 additions & 0 deletions src/honeysql_postgres/helpers.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
22 changes: 21 additions & 1 deletion test/honeysql_postgres/postgres_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down Expand Up @@ -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))))))