marijnh / Postmodern

A Common Lisp PostgreSQL programming interface
http://marijnhaverbeke.nl/postmodern
Other
392 stars 90 forks source link

Updated :with & :with-recursive to be wrapped in parens. #336

Closed peoplestom closed 5 months ago

peoplestom commented 5 months ago

When doing a union over queries that use a with clause the with clauses aren't inside parens when they need to be for valid syntax.

e.g.

(:union
   (:with (:as 'cte1 (:parens (:select '* :from 'demo)))
      (:select '*
        :from 'base
        :inner-join 'cte1
        :on t))
   (:with (:as 'cte2 (:parens (:select '* :from 'example)))
      (:select '*
        :from 'level
        :left-join 'cte2
        :on t))))

without this PR produces:

(WITH cte1 AS  ((SELECT * FROM demo))
    (SELECT *
     FROM base 
     INNER JOIN cte1 
     ON true) 
union 
WITH cte2 AS  ((SELECT * FROM example))
    (SELECT * 
     FROM level 
     LEFT JOIN cte2
     ON true))

which fails.

with this PR it produces:

(( WITH cte1 AS  ((SELECT * FROM demo))
    (SELECT *
     FROM base
     INNER JOIN cte1
     ON true) ) 
union
( WITH cte2 AS  ((SELECT * FROM example))
    (SELECT *
     FROM level
     LEFT JOIN cte2
     ON true) ))