sql-formatter-org / sql-formatter

A whitespace formatter for different query languages
https://sql-formatter-org.github.io/sql-formatter/
MIT License
2.31k stars 399 forks source link

Feature Request: Insert newline after WITH clause #501

Open 0417taehyun opened 1 year ago

0417taehyun commented 1 year ago

Describe the Feature

Are there ways to separate the line with using other character, not the semicolon?

I think it is much more readable if the CTE and the actual query separate with a blank line. However, if I use the formatter there are no ways add a blank line except for using the semicolon(;). Thus, the result query looks like the one below.

with
  cte (id, name) AS (
    select
      *
    from
      users
  )
select
  supplier_name,
  city
from
  (
    select
      *
    from
      suppliers
      join addresses on suppliers.address_id = addresses.id
  ) as suppliers
where
  supplier_id > 500
order by
  supplier_name asc,
  city desc
;

Why do you want this feature?

As you might think it is possible to separate with the semicolon, so why we need the other way. The one below raises the syntax error because SQL needs to execute the select ... part after the with ... because of the semicolon(;), which I use to separate a new line.

with
  cte (id, name) AS (
    select
      *
    from
      users
  )
;

select
  supplier_name,
  city
from
  (
    select
      *
    from
      suppliers
      join addresses on suppliers.address_id = addresses.id
  ) as suppliers
where
  supplier_id > 500
order by
  supplier_name asc,
  city desc
;

Hence, for the readability, I think there are some other ways to add a new line for my two cents.

nene commented 1 year ago

So, what you essentially want is for the formatter to either:

0417taehyun commented 1 year ago

@nene

Thanks for replying me.

I totally agree that it is inefficient to add extra configuration only for the WITH clause to maintain the general options. Also, I understand that it would be hard to preserve empty lines because it requires to change the major architectural.

I appreciate your wonderful works. Thanks again!