下载安装pgFormatter
pgFormatter开源免费,不需要安装即可使用
git clone https://github.com/darold/pgFormatter.git
移动到适当的位置就可以使用了,我是放在/opt目录中。
pgAdmin III 格式化SQL代码
配置pgAdmin III
打开Perference -> Query tool对话窗;
如下图所示,在Extenal formatting utlity输入pgformat可执行文件的位置,点OK确认;
1.快捷键:COMMAND + SHIFT + F
2.Edit -> Format -> Extenal Format
格式化效果
命令行格式化SQL代码
查看帮助获得详细的参数信息
/opt/pgformat/pg_format --help
-
读取标准输入;
-d
是否debug模式,缺省值否;
-m
设置query的最大长度,超过设定值截断,缺省值不截断;
-o
格式化后的结果输出到文件,缺省值输出到标准输出;
-s
设置空格数
-u
关键字大小写形式,值可以为0、1、2、3这四个值,0标示不改变,1转换为小写,2转换为大写,3转换为首字母大写;缺省值为0,不转换大小写。
这是未经过格式化的代码
[winston@bogon ~]$ cat /tmp/unformatted_sql.txt
-- comment line
WITH history AS (SELECT employee_id FROM job_history
WHERE job_id = 'ST_CLERK' AND department_id = 50)
SELECT * FROM employees WHERE employee_id IN (SELECT
employee_id FROM history) AND department_id = 30;
要求格式化为关键字小写,去掉注释,1个tabl等于4个空格
[winston@bogon ~]$ /opt/pgformat/pg_format -n -s 4 -u 1 /tmp/unformatted_sql.txt
with history as (
select
employee_id
job_history
where
job_id = 'ST_CLERK'
and department_id = 50 )
select
employees
where
employee_id in (
select
employee_id
history )
and department_id = 30;
http://sqlformat.darold.net
。