psql命令行工具与可视化pgAdmin 如何格式化代码 原作者:张文升 创作时间:2016-11-17 13:57:46+08 |
doudou586 发布于2016-11-18 12:47:46 评论: 1 浏览: 12063 顶: 737 踩: 898 |
工欲善其事,必先利其器。对开发者来说,有个好的IDE可以事半功倍。
今天开发有个同学问我pgAdmin III有没有办法?先给出答案:pgAdmin III是可以格式化代码的!
pgFormatter开源免费,不需要安装即可使用
git clone https://github.com/darold/pgFormatter.git
移动到适当的位置就可以使用了,我是放在/opt目录中。
打开Perference -> Query tool对话窗;
如下图所示,在Extenal formatting utlity输入pgformat可执行文件的位置,点OK确认;
1.快捷键:COMMAND + SHIFT + F
2.Edit -> Format -> Extenal Format
/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;