Skip to content

Commit f80e738

Browse files
committed
extend README.md
1 parent 1992d0e commit f80e738

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

README.md

+59
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,36 @@ The `ilike` and `not-ilike` operators can be used to query data using a pattern
207207
=> ["SELECT name FROM products WHERE name NOT ILIKE ?" "%name%"]
208208
```
209209

210+
### case when then else
211+
212+
The case condition expression can be used to `select` or `where` clauses.
213+
- case-when
214+
```clojure
215+
(-> (select (case-when [:= :name "Bob"] "Mr. B."
216+
[:= :name "Alisa"] "Strange Lady"))
217+
(from :guests)
218+
sql/format)
219+
=> ["SELECT FROM guests CASE WHEN name = ? THEN ? WHEN name = ? THEN ? END"
220+
"Bob"
221+
"Mr. B."
222+
"Alisa"
223+
"Strange Lady"]
224+
```
225+
- case-when-else
226+
```clojure
227+
(-> (select (case-when-else [:= :name "Bob"] "Mr. B."
228+
[:= :name "Alisa"] "Strange Lady"
229+
"Unknown"))
230+
(from :guests)
231+
sql/format)
232+
=> ["SELECT FROM guests CASE WHEN name = ? THEN ? WHEN name = ? THEN ? ELSE ? END"
233+
"Bob"
234+
"Mr. B."
235+
"Alisa"
236+
"Strange Lady"
237+
"Unknown"]
238+
```
239+
210240
### except
211241

212242
```clojure
@@ -240,6 +270,35 @@ The `ilike` and `not-ilike` operators can be used to query data using a pattern
240270
0.25 0.50 0.75]
241271
```
242272

273+
### join lateral
274+
275+
Sometimes we need some pivot functionality to solve some analytical tasks or just improve the performance of our queries. In PostgreSQL we have `JOIN LATERAL` clauses to do that.
276+
- join-lateral
277+
```clojure
278+
(-> (h/select [:%count.* :total-n]
279+
[:%count.gr-coef :good-n]
280+
[:%count.br-coef :bad-n]
281+
[:%avg.gr-length :good-length]
282+
[:%avg.gr-coef :good-coef])
283+
(h/from [:stats-open-results :r])
284+
(h/join [:stats-positions :p] [:= :p.id :r.position-id])
285+
(e/join-lateral [(h/select
286+
[(e/case-when [:>= :strength-coef (sql/inline 3)]
287+
:best-length) :gr-length]
288+
[(e/case-when [:>= :strength-coef (sql/inline 3)]
289+
:strength-coef) :gr-coef]
290+
[(e/case-when [:< :strength-coef (sql/inline 3)]
291+
:strength-coef) :br-coef])
292+
:z0] :true)
293+
(h/where [:= :p.direction "UP"])
294+
(h/group :r.class-id)
295+
sql/format)
296+
=> ["SELECT count(*) AS total_n, count(gr_coef) AS good_n, count(br_coef) AS bad_n, avg(gr_length) AS good_length, avg(gr_coef) AS good_coef FROM stats_open_results r INNER JOIN stats_positions p ON p.id = r.position_id JOIN LATERAL (SELECT (CASE WHEN strength_coef >= 3 THEN best_length END) AS gr_length, (CASE WHEN strength_coef >= 3 THEN strength_coef END) AS gr_coef, (CASE WHEN strength_coef < 3 THEN strength_coef END) AS br_coef) z0 ON true WHERE (p.direction = ?) GROUP BY r.class_id"
297+
"UP"]
298+
```
299+
- left-join-lateral
300+
The same functionality but for the case when the existing of a pivot result for each row is not mandatory.
301+
243302
### SQL functions
244303

245304
The following are the SQL functions added in `honeysql-postgres`

src/honeysql_postgres/format.cljc

+1-1
Original file line numberDiff line numberDiff line change
@@ -304,5 +304,5 @@
304304
(defmethod format-clause :case-when-else
305305
[[_ pred-thens] _]
306306
(let [else (last pred-thens)]
307-
(format-branches (concat (format-case-preds (drop-last pred-thens))
307+
(format-branches (concat (vec (format-case-preds (drop-last pred-thens)))
308308
[(str "ELSE " (sqlf/to-sql else))]))))

0 commit comments

Comments
 (0)