psql命令行工具与可视化pgAdmin 如何格式化代码
作者: 张文升
日期: 2016-11-17
工欲善其事,必先利其器。对开发者来说,有个好的IDE可以事半功倍。
今天开发有个同学问我pgAdmin III有没有办法?先给出答案:pgAdmin III是可以格式化代码的!
下载安装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 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;
谢谢分享