clarkyu2016 / sql-beautify

VS Code extension that beautifies SQL(HQL).
MIT License
60 stars 20 forks source link

WITH 关键字格式化的问题 #11

Open DestroyLee opened 3 years ago

DestroyLee commented 3 years ago

有with子句的SQL,在格式化的时候,with关键字不能识别,格式也有一定的问题,有空优化一下呗

clarkyu2016 commented 3 years ago

已经加入开发计划

clarkyu2016 commented 3 years ago

@DestroyLee 最新的0.2.0版本已经支持格式化,但是由于case比较少可能还会有问题,如果有badcase欢迎随时和我反馈!

ljfre commented 3 years ago

@DestroyLee最新的0.2.0版本已经支持格式化,但是由于情况比较少可能还会有问题,如果有badcase欢迎随时和我反馈!

WITH get AS 
(
    SELECT  account_id 
           ,COUNT(1)
    FROM table2
    WHERE account_id is not null 
    GROUP BY  account_id 
)
, expiry as(
       SELECT  a.account_id 
              ,COUNT(1) 
       FROM table1 a
       LEFT JOIN table2 b
       ON a.coupon_code = b.coupon_code AND b.status = '20'
       WHERE a.account_id is not null 
       AND b.coupon_code is not null 
       GROUP BY  a.account_id 
) 
,used as(
       SELECT  account_id 
              ,COUNT(1) 
       FROM table3
       WHERE account_id is not null 
       GROUP BY  account_id 
)

这段代码美化后会缩成一团,变成

WITH get AS 
(
    SELECT  account_id 
           ,COUNT(1)
    FROM table2
    WHERE account_id is not null 
    GROUP BY  account_id 
) , expiry as(
SELECT  a.account_id 
       ,COUNT(1)
FROM table1 a
LEFT JOIN table2 b
ON a.coupon_code = b.coupon_code AND b.status = '20'
WHERE a.account_id is not null 
AND b.coupon_code is not null 
GROUP BY  a.account_id ) 
         ,used as(
SELECT  account_id 
       ,COUNT(1)
FROM table3
WHERE account_id is not null 
GROUP BY  account_id )

插件版本v0.2.3,烦请优化一下

clarkyu2016 commented 3 years ago

as( 这样写法无法被识别,比较规范的格式应该是as (,不过会在下个版本把这个格式考虑进去。

DestroyLee commented 3 years ago

测试发现with子句中包含注释会造成把部分关键字一并注释掉

clarkyu2016 commented 3 years ago

测试发现with子句中包含注释会造成把部分关键字一并注释掉

可以提供下样例代码吗?

jiaochenglong commented 2 years ago

测试发现with子句中包含注释会造成把部分关键字一并注释掉

可以提供下样例代码吗?

应该是这样的。 with上一行有注释,就会被删掉,如果在同一行或者下一行就没这种情况。