本文介紹了如何在DBeaver中使用pgFormatter、sqlprase、sqlformatter等外部格式化程序?qū)ql進(jìn)行格式化。
目錄
一、pgFormatter
1.準(zhǔn)備工作
2.DBeaver中進(jìn)行配置
二、sqlprase
1.準(zhǔn)備工作
2.在DBeaver中配置
三、sql-formatter
1.準(zhǔn)備工作
2.在DBeaver中配置
一、pgFormatter
pgFormatter本質(zhì)是perl腳本,所以需要perl運(yùn)行環(huán)境支持。
1.準(zhǔn)備工作
下載地址:https://github.com/darold/pgFormatter/releases/
perl下載地址:https://strawberryperl.com/下載Portable版本的zip壓縮包即可
pgFormatter和perl下載后解壓到任意目錄,此處以Program Files文件夾為例
?
設(shè)置pgFormatter的配置文件
將D:\Program Files\pgFormatter-5.5\doc下的pg_format.conf.sample文件復(fù)制到D:\Program Files\pgFormatter-5.5文件夾下,并重命名為.pg_format.conf,該文件定義了如何對sql進(jìn)行格式化,可參考官方文檔用法說明根據(jù)自己需要進(jìn)行修改。
2.DBeaver中進(jìn)行配置
窗口→首選項(xiàng)→編輯器→SQL編輯器→SQL格式化
?
格式選擇:外部格式化程序
命令行中輸入:
"D:\Program Files\perl-5.32.1.1_x64\perl\bin\perl.exe" "D:\Program Files\pgFormatter-5.5\pg_format" ?-c "D:\Program Files\pgFormatter-5.5\.pg_format.conf" -
注意不要漏掉最后的短杠-
點(diǎn)擊應(yīng)用,如果看到sql格式化成功說明即配置成功
?
點(diǎn)擊“應(yīng)用并關(guān)閉”即可,在SQL編輯器中使用Ctrl+Shift+F即可對sql進(jìn)行格式化
二、sqlparse
sqlparse作為python中一個(gè)常用的sql解析庫,經(jīng)常用來解析sql,同時(shí)也可以用來格式化sql。
1.準(zhǔn)備工作
下載安裝python:https://www.python.org/downloads/windows/,使用Embeddable版本的zip壓縮包即可,同樣解壓到Program Files文件夾
下載安裝pip
下載pip腳本:https://bootstrap.pypa.io/get-pip.py,注意python2.7和python3使用的get-pip.py文件不同,復(fù)制get-pip.py文件到D:\Program Files\python3.12.1文件夾下,在cmd中進(jìn)行安裝
python get-pip.py
安裝后會在D:\Program Files\python3.12.1\Scripts下出現(xiàn)pip.exe文件
使用pip安裝sqlparse
pip install sqlparse
在D:\Program Files\python3.12.1文件夾下創(chuàng)建sql_parse.py文件,sql_parse.py內(nèi)容如下:
#-*- coding: UTF-8 -*-
# sql_parse.py 注意文件名必須使用下劃線分割單詞,不然會執(zhí)行出錯(cuò)!
import sys
import sqlparse
def sql_formatter(record):
sql = sqlparse.format(
record,
keyword_case='upper',#關(guān)鍵字大小寫
identifier_case='lower',#標(biāo)識符大小寫
truncate_strings=70,#字符串超過指定長度截?cái)? reindent=True,#是否整體縮進(jìn)
comma_first=False,#是否逗號在前
wrap_after=100 #select字段列表超過多少字符換行,如果不指定每個(gè)字段單獨(dú)一行
).strip('\n')
sql='\n'.join([l for l in sql.split('\n')])
return sql
# append
file_path = sys.argv[1]
with open(file_path,"r") as f:
read_sql = f.read()
with open(file_path,"w") as f:
for sql in read_sql.split(';'):
_sql = sql_formatter(sql)
print(_sql)
print("\n")
sqlparse.format參數(shù)說明詳見:?https://sqlparse.readthedocs.io/en/latest/api/#formatting
2.在DBeaver中配置
窗口→首選項(xiàng)→編輯器→SQL編輯器→SQL格式化
命令行中輸入:
"D:\Program Files\python3.12.1\python.exe" "D:\Program Files\python3.12.1\sql_parse.py" ${file}
注意,這里需要勾選使用臨時(shí)文件
點(diǎn)擊“應(yīng)用”后如果sql格式化成功即配置成功文章來源地址http://www.zghlxwxcb.cn/news/detail-822935.html
點(diǎn)擊“應(yīng)用并關(guān)閉”即可,在SQL編輯器中使用Ctrl+Shift+F即可對sql進(jìn)行格式化
在dbeaver中使用sqlparse進(jìn)行格式化參考了該篇文章https://jakpentest.tistory.com/190
三、sql-formatter
sql-formatter是一個(gè)JavaScript類庫,倉庫地址:https://github.com/sql-formatter-org/sql-formatter
1.準(zhǔn)備工作
下載安裝Node.js:https://nodejs.org/en/download/?,這里我們下載免安裝的zip版本即可,同樣解壓到Program Files文件夾
安裝sqlformmatter:
npm install sql-formatter
這里沒有使用-g參數(shù),即在當(dāng)前文件夾進(jìn)行安裝,安裝完成以后可以看到D:\Program Files\node-v20.10.0-win-x64\node_modules文件夾下多了一個(gè)sql-formatter文件夾。
創(chuàng)建sqlformatter格式化的配置文件config.json,可以是任意位置,比如:D:\Program Files\node-v20.10.0-win-x64\node_modules\sql-formatter\bin下
config.json內(nèi)容如下:
{
"language": "spark",
"tabWidth": 2,
"keywordCase": "upper",
"linesBetweenQueries": 2
}
2.在DBeaver中配置
窗口→首選項(xiàng)→編輯器→SQL編輯器→SQL格式化
命令行中輸入:
node "D:\\Program Files\\node-v20.10.0-win-x64\\node_modules\\sql-formatter\\bin\\sql-formatter-cli.cjs" -c "D:\\Program Files\\node-v20.10.0-win-x64\\node_modules\\sql-formatter\\bin\\config.json" ${file}
?注意,node命令中路徑需要使用雙斜杠,需要勾選使用臨時(shí)文件文章來源:http://www.zghlxwxcb.cn/news/detail-822935.html
點(diǎn)擊“應(yīng)用”后如果sql格式化成功即配置成功
到了這里,關(guān)于DBeaver中使用外部格式化程序?qū)M(jìn)行sql格式化的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!