Java SQL 格式化实践
SqlParseradmin 发布于:2023-07-29 21:28:12
阅读:loading
前一篇《Java SQL 格式化实践》介绍了 JSqlParser 项目解析SQL的实践,本篇介绍一下Java中格式化SQL语句的实现,类似这种的技术技能我个人还是比较喜欢的,所以就有了这篇。
JSQLFormatter是一个开源的Java库,用于格式化和美化SQL语句。它可以将复杂的SQL语句转换为易读且格式良好的形式,提高代码的可读性和可维护性。项目地址为:“https://github.com/manticore-projects/jsqlformatter”,在它的介绍信息里有一些在线示例和官网的更详细的介绍,有兴趣可以自行查看。
(1)SQL语句美化:JSQLFormatter可以对SQL语句进行自动缩进、换行和对齐,使其更易读。
(2)支持多种SQL方言:JSQLFormatter支持多种常见的SQL方言,包括MySQL、Oracle、SQL Server等,可以正确处理特定方言的语法和约定。
(3)保留注释和引号:JSQLFormatter可以保留SQL语句中的注释和引号,不会对它们进行修改或删除。
(4)可定制化:JSQLFormatter提供了一些选项和配置,可以根据需要进行定制,以满足不同的格式化需求。
(5)基于JSqlParser解析项目而实现。
(6)支持复杂的SELECT语句、INSERT INTO、MERGE、UPDATE、DELETE、CREATE、ALTER。
(1)提供独立可执行的jar的应用程序,也可以使用Maven坐标集成使用;
(2)适用于 Windows、Linux 或 MacOS 的本机静态二进制或动态库;
(3)Netbeans 插件(其他平台如 Eclipse、JEdit、Squirrel SQL、DBeaver 即将推出);
UPDATE risk.counterparty SET id_counterparty = :id_counterparty
, label = :label , description = :description , id_counterparty_group_type = :id_counterparty_group_type
, id_counterparty_type = :id_counterparty_type
, id_counterparty_sub_type = :id_counterparty_sub_type
, id_country_group = :id_country_group
, id_country = :id_country, id_country_state = :id_country_state , id_district = :id_district
, id_city = :id_city
, id_industrial_sector = :id_industrial_sector
, id_industrial_sub_sector = :id_industrial_sub_sector
, block_auto_update_flag = :block_auto_update_flag
, id_user_editor = :id_user_editor
, id_organization_unit = :id_organization_unit , id_status = :id_status
, update_timestamp = current_timestamp WHERE id_counterparty_ref = :id_counterparty_ref
(1)将原始SQL语句格式化,并转换为Java代码中的变量定义,参考代码:
"UPDATE risk.counterparty"
+ "SET id_counterparty = :id_counterparty"
+ " , label = :label"
+ " , description = :description"
+ " , id_counterparty_group_type = :id_counterparty_group_type"
+ " , id_counterparty_type = :id_counterparty_type"
+ " , id_counterparty_sub_type = :id_counterparty_sub_type"
+ " , id_country_group = :id_country_group"
+ " , id_country = :id_country"
+ " , id_country_state = :id_country_state"
+ " , id_district = :id_district"
+ " , id_city = :id_city"
+ " , id_industrial_sector = :id_industrial_sector"
+ " , id_industrial_sub_sector = :id_industrial_sub_sector"
+ " , block_auto_update_flag = :block_auto_update_flag"
+ " , id_user_editor = :id_user_editor"
+ " , id_organization_unit = :id_organization_unit"
+ " , id_status = :id_status"
+ " , update_timestamp = current_timestamp"
+ "WHERE id_counterparty_ref = :id_counterparty_ref"
+ ";"
(2)格式化简单SQL为tree结构的描述,参考代码:
/**
* 格式化SQL为tree结构的描述
* @throws Exception 异常处理
*/
@Test
public void formatToTree1() throws Exception {
String sql = "select '11' a , '22' b , c , to_date('20230729' , 'yyyymmdd') d from dual";
System.out.println(JSQLFormatter.formatToTree(sql));
}
(3)格式化SQL语句,参考代码:
(4)格式化SQL语句,转换为HTML代码高亮关键字,参考如下:
@Test
public void format2Html() {
String sql =
"UPDATE cfe.calendar SET year_offset = ? , settlement_shift = ? , friday_is_holiday = ? , " +
"saturday_is_holiday = ? , sunday_is_holiday = ? WHERE id_calendar = ? " +
"and UPDATE_TIME = 1";
String format = SqlFormatter.format(sql);
String[] keywords = {"UPDATE", "SET", "WHERE"};
// 对关键字进行高亮替换
for (String keyword : keywords) {
format = format.replaceAll("(?i)\\b" + keyword + "\\b", "<span class=\"highlight\">" + keyword + "</span>");
}
System.out.println("<style>" +
".highlight {" +
" font-weight: bold;" +
" color: BLUE;" +
"}" +
"</style>\n" +
"<pre>" + format + "</pre>");
}
(输出结果)
(HTML预览)
相关源码工程点击此处下载:源码工程.txt;
点赞