File tree Expand file tree Collapse file tree 3 files changed +96
-0
lines changed Expand file tree Collapse file tree 3 files changed +96
-0
lines changed Original file line number Diff line number Diff line change 17
17
:within-group 55
18
18
:over 55
19
19
:insert-into-as 60
20
+ :join-lateral 153
21
+ :left-join-lateral 154
20
22
:partition-by 165
21
23
:window 195
22
24
:upsert 225
267
269
(-> extension-name
268
270
util/get-first
269
271
sqlf/to-sql)))
272
+
273
+ (defn- format-join [type table pred]
274
+ (str (when type
275
+ (str (sqlf/upper-case (name type)) " " ))
276
+ " JOIN LATERAL " (sqlf/to-sql table)
277
+ (when (some? pred)
278
+ (str " ON " (sqlf/format-predicate* pred)))))
279
+
280
+ (defn- make-join [type join-groups]
281
+ (sqlf/space-join (map #(apply format-join type %)
282
+ (partition 2 join-groups))))
283
+
284
+ (defmethod format-clause :join-lateral [[_ join-groups] _]
285
+ (make-join :inner join-groups))
286
+
287
+ (defmethod format-clause :left-join-lateral [[_ join-groups] _]
288
+ (make-join :left join-groups))
289
+
290
+ (defn- format-case-preds [pred-thens]
291
+ (map (fn [[pred then]]
292
+ (str " WHEN " (sqlf/format-predicate* pred)
293
+ " THEN " (sqlf/to-sql then)))
294
+ (partition 2 pred-thens)))
295
+
296
+ (defn- format-branches
297
+ [branches]
298
+ (str " CASE " (sqlf/space-join branches) " END" ))
299
+
300
+ (defmethod format-clause :case-when
301
+ [[_ pred-thens] _]
302
+ (format-branches (format-case-preds pred-thens)))
303
+
304
+ (defmethod format-clause :case-when-else
305
+ [[_ pred-thens] _]
306
+ (let [else (last pred-thens)]
307
+ (format-branches (concat (format-case-preds (drop-last pred-thens))
308
+ [(str " ELSE " (sqlf/to-sql else))]))))
Original file line number Diff line number Diff line change 80
80
81
81
(defhelper drop-extension [m extension-name]
82
82
(assoc m :drop-extension (sqlh/collify extension-name)))
83
+
84
+ (defhelper join-lateral
85
+ [m clauses]
86
+ (assoc m :join-lateral clauses))
87
+
88
+ (defhelper left-join-lateral
89
+ [m clauses]
90
+ (assoc m :join-lateral clauses))
91
+
92
+ (defhelper case-when
93
+ [m clauses]
94
+ (assoc m :case-when clauses))
95
+
96
+ (defhelper case-when-else
97
+ [m clauses]
98
+ (assoc m :case-when-else clauses))
Original file line number Diff line number Diff line change 8
8
:refer
9
9
[add-column
10
10
alter-table
11
+ case-when
12
+ case-when-else
11
13
create-extension
12
14
create-table
13
15
create-view
19
21
drop-table
20
22
filter
21
23
insert-into-as
24
+ join-lateral
25
+ left-join-lateral
22
26
on-conflict
23
27
on-conflict-constraint
24
28
over
334
338
(-> (drop-extension :uuid-ossp )
335
339
(sql/format :allow-dashed-names? true
336
340
:quoting :ansi ))))))
341
+
342
+ (deftest case-when-test
343
+ (is (= [" CASE WHEN x = 1 THEN x WHEN x > 1 THEN y END" ]
344
+ (-> (case-when [:= :x (sql/inline 1 )] :x
345
+ [:> :x (sql/inline 1 )] :y )
346
+ sql/format))))
347
+
348
+ (deftest case-when-else-test
349
+ (is (= [" CASE WHEN x = 1 THEN x WHEN x > 1 THEN y ELSE z END" ]
350
+ (-> (case-when-else [:= :x (sql/inline 1 )] :x
351
+ [:> :x (sql/inline 1 )] :y
352
+ :z )
353
+ sql/format))))
354
+
355
+ (deftest join-lateral-test
356
+ (is (= [(str " SELECT COUNT(x3), COUNT(x0) FROM x_values "
357
+ " INNER JOIN LATERAL (SELECT (CASE WHEN x > 3 THEN x END) AS x3, (CASE WHEN x > 0 THEN x END) AS x0) z ON true" )]
358
+ (-> (select :%count.x3
359
+ :%count.x0)
360
+ (from :x-values )
361
+ (join-lateral [(select
362
+ [(case-when [:> :x (sql/inline 3 )] :x ) :x3 ]
363
+ [(case-when [:> :x (sql/inline 0 )] :x ) :x0 ]) :z ] :true )
364
+ sql/format))))
365
+
366
+ (deftest left-join-lateral-test
367
+ (is (= [(str " SELECT COUNT(x3), COUNT(x0) FROM x_values "
368
+ " LEFT JOIN LATERAL (SELECT "
369
+ " (CASE WHEN x > 3 THEN x END) AS x3, "
370
+ " (CASE WHEN x > 0 THEN x END) AS x0) z ON true" )]
371
+ (-> (select :%count.x3
372
+ :%count.x0)
373
+ (from :x-values )
374
+ (left-join-lateral [(select
375
+ [(case-when [:> :x (sql/inline 3 )] :x ) :x3 ]
376
+ [(case-when [:> :x (sql/inline 0 )] :x ) :x0 ]) :z ] :true )
377
+ sql/format))))
You can’t perform that action at this time.
0 commit comments