2025-12-07 zhengkai 修复COMMENT、COMMENT ON的处理

This commit is contained in:
Moshow郑锴
2025-12-07 21:17:54 +08:00
parent fc44cd89c2
commit 19982259c4

View File

@@ -233,11 +233,11 @@ public class SqlParserServiceImpl implements SqlParserService {
String classComment = null; String classComment = null;
//mysql是comment=,pgsql/oracle是comment on table, //mysql是comment=,pgsql/oracle是comment on table,
//2020-05-25 优化表备注的获取逻辑 //2020-05-25 优化表备注的获取逻辑
if (tableSql.contains("comment=") || tableSql.contains("comment on table")) { if (tableSql.toLowerCase().contains("comment=") || tableSql.toLowerCase().contains("comment on table")) {
int ix = tableSql.lastIndexOf("comment="); int ix = tableSql.toLowerCase().lastIndexOf("comment=");
String classCommentTmp = (ix > -1) ? String classCommentTmp = (ix > -1) ?
tableSql.substring(ix + 8).trim() : tableSql.substring(ix + 8).trim() :
tableSql.substring(tableSql.lastIndexOf("comment on table") + 17).trim(); tableSql.substring(tableSql.toLowerCase().lastIndexOf("comment on table") + 17).trim();
if (classCommentTmp.contains("`")) { if (classCommentTmp.contains("`")) {
classCommentTmp = classCommentTmp.substring(classCommentTmp.indexOf("`") + 1); classCommentTmp = classCommentTmp.substring(classCommentTmp.indexOf("`") + 1);
classCommentTmp = classCommentTmp.substring(0, classCommentTmp.indexOf("`")); classCommentTmp = classCommentTmp.substring(0, classCommentTmp.indexOf("`"));
@@ -256,11 +256,11 @@ public class SqlParserServiceImpl implements SqlParserService {
List<FieldInfo> fieldList = new ArrayList<FieldInfo>(); List<FieldInfo> fieldList = new ArrayList<FieldInfo>();
// 正常( ) 内的一定是字段相关的定义。 // 正常( ) 内的一定是字段相关的定义。
String fieldListTmp = tableSql.substring(tableSql.indexOf("(") + 1, tableSql.lastIndexOf(")")); String fieldListTmp = tableSql.substring(tableSql.indexOf("(") + 1, tableSql.lastIndexOf(")")).trim();
// 匹配 comment替换备注里的小逗号, 防止不小心被当成切割符号切割 // 匹配 comment替换备注里的小逗号, 防止不小心被当成切割符号切割
String commentPattenStr1 = "comment `(.*?)\\`"; String commentPattenStr1 = "comment `(.*?)\\`";
Matcher matcher1 = Pattern.compile(commentPattenStr1).matcher(fieldListTmp); Matcher matcher1 = Pattern.compile(commentPattenStr1).matcher(fieldListTmp.toLowerCase());
while (matcher1.find()) { while (matcher1.find()) {
String commentTmp = matcher1.group(); String commentTmp = matcher1.group();
@@ -308,15 +308,15 @@ public class SqlParserServiceImpl implements SqlParserService {
// 2025-12-07 zhengkai 修复对primary key的处理 // 2025-12-07 zhengkai 修复对primary key的处理
boolean notSpecialFlag = ( boolean notSpecialFlag = (
!columnLine.contains("key ") !columnLine.contains("key ")
&& !columnLine.contains("constraint") && !columnLine.toLowerCase().contains("constraint")
&& !columnLine.contains(" using ") && !columnLine.toLowerCase().contains(" using ")
&& !columnLine.contains("unique ") && !columnLine.toLowerCase().contains("unique ")
&& !columnLine.contains("fulltext ") && !columnLine.toLowerCase().contains("fulltext ")
&& !columnLine.contains("index ") && !columnLine.toLowerCase().contains("index ")
&& !columnLine.contains("pctincrease") && !columnLine.toLowerCase().contains("pctincrease")
&& !columnLine.contains("buffer_pool") && !columnLine.toLowerCase().contains("buffer_pool")
&& !columnLine.contains("tablespace") && !columnLine.toLowerCase().contains("tablespace")
&& !(columnLine.contains("primary ") && columnLine.indexOf("storage") + 3 > columnLine.indexOf("(")) && !(columnLine.toLowerCase().contains("primary ") && columnLine.indexOf("storage") + 3 > columnLine.indexOf("("))
&& !(columnLine.toLowerCase().contains("primary ") && i > 3) && !(columnLine.toLowerCase().contains("primary ") && i > 3)
&& !columnLine.toLowerCase().contains("primary key") && !columnLine.toLowerCase().contains("primary key")
); );
@@ -374,13 +374,13 @@ public class SqlParserServiceImpl implements SqlParserService {
} }
// field commentMySQL的一般位于field行而pgsql和oralce多位于后面。 // field commentMySQL的一般位于field行而pgsql和oralce多位于后面。
String fieldComment = null; String fieldComment = null;
if (tableSql.contains("comment on column") && (tableSql.contains("." + columnName + " is ") || tableSql.contains(".`" + columnName + "` is"))) { if (tableSql.toLowerCase().contains("comment on column") && (tableSql.toLowerCase().contains("." + columnName + " is ") || tableSql.toLowerCase().contains(".`" + columnName + "` is"))) {
//新增对pgsql/oracle的字段备注支持 //新增对pgsql/oracle的字段备注支持
//COMMENT ON COLUMN public.check_info.check_name IS '检查者名称'; //COMMENT ON COLUMN public.check_info.check_name IS '检查者名称';
//2018-11-22 lshz0088 正则表达式的点号前面应该加上两个反斜杠,否则会认为是任意字符 //2018-11-22 lshz0088 正则表达式的点号前面应该加上两个反斜杠,否则会认为是任意字符
//2019-4-29 zhengkai 优化对oracle注释comment on column的支持@liukex //2019-4-29 zhengkai 优化对oracle注释comment on column的支持@liukex
tableSql = tableSql.replaceAll(".`" + columnName + "` is", "." + columnName + " is"); tableSql = tableSql.toLowerCase().replaceAll(".`" + columnName + "` is", "." + columnName + " is");
Matcher columnCommentMatcher = Pattern.compile("\\." + columnName + " is `").matcher(tableSql); Matcher columnCommentMatcher = Pattern.compile("\\." + columnName + " is `").matcher(tableSql.toLowerCase());
fieldComment = columnName; fieldComment = columnName;
while (columnCommentMatcher.find()) { while (columnCommentMatcher.find()) {
String columnCommentTmp = columnCommentMatcher.group(); String columnCommentTmp = columnCommentMatcher.group();
@@ -388,9 +388,9 @@ public class SqlParserServiceImpl implements SqlParserService {
fieldComment = tableSql.substring(tableSql.indexOf(columnCommentTmp) + columnCommentTmp.length()).trim(); fieldComment = tableSql.substring(tableSql.indexOf(columnCommentTmp) + columnCommentTmp.length()).trim();
fieldComment = fieldComment.substring(0, fieldComment.indexOf("`")).trim(); fieldComment = fieldComment.substring(0, fieldComment.indexOf("`")).trim();
} }
} else if (columnLine.contains(" comment")) { } else if (columnLine.toLowerCase().contains(" comment")) {
//20200518 zhengkai 修复包含comment关键字的问题 //20200518 zhengkai 修复包含comment关键字的问题
String commentTmp = columnLine.substring(columnLine.lastIndexOf("comment") + 7).trim(); String commentTmp = columnLine.toLowerCase().substring(columnLine.toLowerCase().lastIndexOf("comment") + 7).trim();
// '用户ID', // '用户ID',
if (commentTmp.contains("`") || commentTmp.indexOf("`") != commentTmp.lastIndexOf("`")) { if (commentTmp.contains("`") || commentTmp.indexOf("`") != commentTmp.lastIndexOf("`")) {
commentTmp = commentTmp.substring(commentTmp.indexOf("`") + 1, commentTmp.lastIndexOf("`")); commentTmp = commentTmp.substring(commentTmp.indexOf("`") + 1, commentTmp.lastIndexOf("`"));