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 1717 :within-group 55
1818 :over 55
1919 :insert-into-as 60
20+ :join-lateral 153
21+ :left-join-lateral 154
2022 :partition-by 165
2123 :window 195
2224 :upsert 225
267269 (-> extension-name
268270 util/get-first
269271 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 8080
8181(defhelper drop-extension [m extension-name]
8282 (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 88 :refer
99 [add-column
1010 alter-table
11+ case-when
12+ case-when-else
1113 create-extension
1214 create-table
1315 create-view
1921 drop-table
2022 filter
2123 insert-into-as
24+ join-lateral
25+ left-join-lateral
2226 on-conflict
2327 on-conflict-constraint
2428 over
334338 (-> (drop-extension :uuid-ossp )
335339 (sql/format :allow-dashed-names? true
336340 :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