Skip to content

Commit 7487ecd

Browse files
feat: support while loop in rt.postgres grammar
- Registered `:while` op in `rt.postgres.grammar/+features+`. - Added `pg-while-block` in `rt.postgres.grammar.form-let` to handle AST transformation. - Added `block-while-block` in `rt.postgres.grammar.common` to emit PL/pgSQL `WHILE ... LOOP ... END LOOP;`. - Added unit test in `rt.postgres.grammar.form-let-test`.
1 parent efcaedd commit 7487ecd

4 files changed

Lines changed: 25 additions & 0 deletions

File tree

src/rt/postgres/grammar.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888

8989
:letblk {:op :letblk :symbol #{'let:block} :macro #'form-let/pg-tf-let-block :emit :macro :type :block}
9090
:let {:op :let :symbol #{'let} :macro #'form-let/pg-tf-let :emit :macro :type :block}
91+
:while {:op :while :symbol #{'while} :emit #'form-let/pg-while-block :type :block}
9192
:loop {:op :loop :symbol #{'loop} :emit #'form-let/pg-loop-block :type :block}
9293
:case {:op :case :symbol #{'case} :emit #'form-let/pg-case-block :type :block}
9394
:forech {:op :forech :symbol #{'for:each} :macro #'tf/pg-tf-foreach :emit :macro :type :block}

src/rt/postgres/grammar/common.clj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,14 @@
315315
\\ (\| (~'do ~@forms))
316316
\\ :end-loop]))
317317

318+
(defn block-while-block
319+
"emits while block"
320+
{:added "4.0"}
321+
([condition & forms]
322+
`[:while ~condition :loop
323+
\\ (\| (~'do ~@forms))
324+
\\ :end-loop \;]))
325+
318326
(defn block-case-block
319327
"emits case block"
320328
{:added "4.0"}

src/rt/postgres/grammar/form_let.clj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,16 @@
213213
grammar
214214
mopts))))
215215

216+
(defn pg-while-block
217+
"creates a while block"
218+
{:added "4.0"}
219+
([[_ condition & forms] grammar mopts]
220+
(binding [*input-syms* (or *input-syms* (volatile! #{}))]
221+
(emit-common/*emit-fn*
222+
(apply common/block-while-block condition forms)
223+
grammar
224+
mopts))))
225+
216226
(defn pg-case-block
217227
"creates a case block"
218228
{:added "4.0"}

test/rt/postgres/grammar/form_let_test.clj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@
9595
(pg-loop-block '(loop [] (let [a 1] (return a))) nil nil))
9696
=> "block")
9797

98+
^{:refer rt.postgres.grammar.form-let/pg-while-block :added "4.0"}
99+
(fact "creates a while block"
100+
(with-redefs [emit-common/*emit-fn* (fn [& _] "block")]
101+
(pg-while-block '(while (true) (let [a 1] (return a))) nil nil))
102+
=> "block")
103+
98104
^{:refer rt.postgres.grammar.form-let/pg-case-block :added "4.0"}
99105
(fact "creates a case block"
100106
(with-redefs [emit-common/*emit-fn* (fn [& _] "block")]

0 commit comments

Comments
 (0)