Skip to content

Commit cd1c3a2

Browse files
authored
Test the examples in the readme (#49)
- Fix the examples in the readme to adhere to the format necessary for https://github.com/seancorfield/readme
1 parent ba2850c commit cd1c3a2

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

.github/workflows/ci.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ jobs:
2323
run: clojure -M:test:runner
2424
- name: Run tests (clojurescript)
2525
run: clojure -M:test:cljs-runner
26+
- name: Run tests (readme codeblocks)
27+
run: clojure -M:readme
2628
- name: Install clj-kondo
2729
run: |
2830
curl -sLO https://raw.githubusercontent.com/borkdude/clj-kondo/master/script/install-clj-kondo

README.md

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ This library aims to extend the features of honeysql to support postgres specifi
4141
```
4242
### REPL
4343
```clojure
44+
; Note that `honeysql-postgres.format` and `honeysql-postgres.helpers`
45+
; must be required into the project for the extended features to work.
4446
(require '[honeysql.core :as sql]
45-
'[honeysql.helpers :refer :all]
46-
'[honeysql-postgres.format]
47+
'[honeysql.helpers :refer :all :as sqlh]
48+
'[honeysql-postgres.format :refer :all]
4749
'[honeysql-postgres.helpers :as psqlh])
4850
```
4951

@@ -58,18 +60,20 @@ The query creation and usage is exactly the same as honeysql.
5860
(-> (insert-into :distributors)
5961
(values [{:did 5 :dname "Gizmo Transglobal"}
6062
{:did 6 :dname "Associated Computing, Inc"}])
61-
(psqlh/upsert (-> (on-conflict :did)
62-
(do-update-set :dname)))
63+
(psqlh/upsert (-> (psqlh/on-conflict :did)
64+
(psqlh/do-update-set :dname)))
6365
(psqlh/returning :*)
6466
sql/format)
65-
=> ["INSERT INTO distributors (did, dname) VALUES (5, ?), (6, ?) ON CONFLICT (did) DO UPDATE SET dname = EXCLUDED.dname RETURNING *" "Gizmo Transglobal" "Associated Computing, Inc"]
67+
=> ["INSERT INTO distributors (did, dname) VALUES (?, ?), (?, ?) ON CONFLICT (did) DO UPDATE SET dname = EXCLUDED.dname RETURNING *"
68+
5 "Gizmo Transglobal" 6 "Associated Computing, Inc"]
6669

6770
(-> (insert-into :distributors)
6871
(values [{:did 23 :dname "Foo Distributors"}])
6972
(psqlh/on-conflict :did)
7073
(psqlh/do-update-set! [:dname "EXCLUDED.dname || ' (formerly ' || distributors.dname || ')'"] [:downer "EXCLUDED.downer"])
7174
sql/format)
72-
=> ["INSERT INTO distributors (did, dname) VALUES (23, ?) ON CONFLICT (did) DO UPDATE SET dname = ?, downer = ?" "Foo Distributors" "EXCLUDED.dname || ' (formerly ' || d.dname || ')'" "EXCLUDED.downer"]
75+
=> ["INSERT INTO distributors (did, dname) VALUES (?, ?) ON CONFLICT (did) DO UPDATE SET dname = ?, downer = ?"
76+
23 "Foo Distributors" "EXCLUDED.dname || ' (formerly ' || distributors.dname || ')'" "EXCLUDED.downer"]
7377
```
7478

7579
### insert into with alias
@@ -83,15 +87,16 @@ The query creation and usage is exactly the same as honeysql.
8387
(where [:<> :d.zipcode "21201"])))
8488
(psqlh/returning :d.*)
8589
sql/format)
86-
=> ["INSERT INTO distributors AS d (did, dname) VALUES (5, ?), (6, ?) ON CONFLICT (did) DO UPDATE SET dname = EXCLUDED.dname WHERE d.zipcode <> ? RETURNING d.*" "Gizmo Transglobal" "Associated Computing, Inc" "21201"]
90+
=> ["INSERT INTO distributors AS d (did, dname) VALUES (?, ?), (?, ?) ON CONFLICT (did) DO UPDATE SET dname = EXCLUDED.dname WHERE d.zipcode <> ? RETURNING d.*"
91+
5 "Gizmo Transglobal" 6 "Associated Computing, Inc" "21201"]
8792
```
8893

8994
### over
9095
You can make use of `over` to write window functions where it takes in vectors with aggregator functions and window functions along with optional alias like `(over [aggregator-function window-function alias])`, the can be coupled with the `window` clause to write window-function functions with alias that is later defines the window-function, like `(-> (over [aggregator-function :w]) (window :w window-function))`.
9196
```clojure
9297
(-> (select :id)
9398
(psqlh/over
94-
[(sql/call :avg :salary) (-> (partition-by :department) (order-by [:designation])) :Average]
99+
[(sql/call :avg :salary) (-> (psqlh/partition-by :department) (order-by [:designation])) :Average]
95100
[(sql/call :max :salary) :w :MaxSalary])
96101
(from :employee)
97102
(psqlh/window :w (psqlh/partition-by :department))
@@ -120,7 +125,8 @@ You can make use of `over` to write window functions where it takes in vectors w
120125
[:date_prod :date]
121126
[:kind (sql/call :varchar 10)]])
122127
sql/format)
123-
=> ["CREATE TABLE films (code char(5) CONSTRAINT firstkey PRIMARY KEY, title varchar(40) NOT NULL, did integer NOT NULL, date_prod date, kind varchar(10))"]
128+
=> ["CREATE TABLE films (code char(?) CONSTRAINT firstkey PRIMARY KEY, title varchar(?) NOT NULL, did integer NOT NULL, date_prod date, kind varchar(?))"
129+
5 40 10]
124130
```
125131

126132
### drop table
@@ -147,17 +153,16 @@ use `alter-table` along with `add-column` & `drop-column` to modify table level
147153
### create-extension
148154
`create-extension` can be used to create extensions with a given keyword.
149155
```clojure
150-
(-> (create-extension :uuid-ossp :if-not-exists? true)
156+
(-> (psqlh/create-extension :uuid-ossp :if-not-exists? true)
151157
(sql/format :allow-dashed-names? true
152158
:quoting :ansi))
153-
154159
=> ["CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\""]
155160
```
156161

157162
### drop-extension
158163
`drop-extension` is used to drop extensions.
159164
```clojure
160-
(-> (drop-extension :uuid-ossp)
165+
(-> (psqlh/drop-extension :uuid-ossp)
161166
(sql/format :allow-dashed-names? true
162167
:quoting :ansi))
163168
=> ["DROP EXTENSION \"uuid-ossp\""]
@@ -171,20 +176,18 @@ The `ilike` and `not-ilike` operators can be used to query data using a pattern
171176
(from :products)
172177
(where [:ilike :name "%name%"])
173178
sql/format)
174-
=> ["SELECT * FROM products WHERE name ILIKE ?" "%name%"]
179+
=> ["SELECT name FROM products WHERE name ILIKE ?" "%name%"]
175180
```
176181
- not-ilike
177182
```clojure
178183
(-> (select :name)
179184
(from :products)
180185
(where [:not-ilike :name "%name%"])
181186
sql/format)
182-
=> ["SELECT * FROM products WHERE name NOT ILIKE ?" "%name%"]
187+
=> ["SELECT name FROM products WHERE name NOT ILIKE ?" "%name%"]
183188
```
184189
### except
185-
186190
```clojure
187-
188191
(sql/format
189192
{:except
190193
[{:select [:ip]}
@@ -205,24 +208,24 @@ The following are the SQL functions added in `honeysql-postgres`
205208
(sql/format (sql/call :primary-key))
206209
=> ["PRIMARY KEY"]
207210

208-
(sql/format (sql/call :primary-key :arg1 :arg2 ... ))
209-
=> ["PRIMARY KEY (arg1, arg2, ... )"]
211+
(sql/format (sql/call :primary-key :arg1 :arg2))
212+
=> ["PRIMARY KEY(arg1, arg2)"]
210213
```
211214
- unique
212215
```clojure
213216
(sql/format (sql/call :unique))
214217
=> ["UNIQUE"]
215218

216-
(sql/format (sql/call :unique :arg1 :arg2 ... ))
217-
=> ["UNIQUE (arg1, arg2, ... )"]
219+
(sql/format (sql/call :unique :arg1 :arg2))
220+
=> ["UNIQUE(arg1, arg2)"]
218221
```
219222
- foreign-key
220223
```clojure
221224
(sql/format (sql/call :foreign-key))
222225
=> ["FOREIGN KEY"]
223226

224-
(sql/format (sql/call :foreign-key :arg1 :arg2 ... ))
225-
=> ["FOREIGN KEY (arg1, arg2, ... )"]
227+
(sql/format (sql/call :foreign-key :arg1 :arg2))
228+
=> ["FOREIGN KEY(arg1, arg2)"]
226229
```
227230
- references
228231
```clojure
@@ -236,12 +239,12 @@ The following are the SQL functions added in `honeysql-postgres`
236239
```
237240
- default
238241
```clojure
239-
(sql/format (sql/call :default value))
242+
(sql/format (sql/call :default :value))
240243
=> ["DEFAULT value"]
241244
```
242245
- nextval
243246
```clojure
244-
(sql/format (sql/call :nextval value))
247+
(sql/format (sql/call :nextval :value))
245248
=> ["nextval('value')"]
246249
```
247250
- check

deps.edn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"-d" "test"]}
99
:cljs-runner {:extra-deps {olical/cljs-test-runner {:mvn/version "3.8.0"}}
1010
:main-opts ["-m" "cljs-test-runner.main"]}
11+
:readme {:extra-deps {seancorfield/readme {:mvn/version "1.0.16"}}
12+
:main-opts ["-m" "seancorfield.readme"]}
1113
:jar {:replace-deps {seancorfield/depstar {:mvn/version "2.0.165"}}
1214
:exec-fn hf.depstar/jar
1315
:exec-args {}}}}

0 commit comments

Comments
 (0)