mirror of
https://github.com/alibaba/canal.git
synced 2026-11-04 00:30:15 +08:00
MemoryTableMeta ddl 处理注释2 (#2694)
* MemoryTableMeta ddl 处理注释 * MemoryTableMeta ddl 处理注释2
This commit is contained in:
@@ -81,7 +81,8 @@ public class MemoryTableMeta implements TableMetaTSDB {
|
||||
&& !StringUtils.startsWithIgnoreCase(StringUtils.trim(ddl), "alter user")
|
||||
&& !StringUtils.startsWithIgnoreCase(StringUtils.trim(ddl), "drop user")
|
||||
&& !StringUtils.startsWithIgnoreCase(StringUtils.trim(ddl), "create database")) {
|
||||
repository.console(ddl);
|
||||
String sql = trimSimpleComment(ddl);
|
||||
repository.console(sql);
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
logger.warn("parse faield : " + ddl, e);
|
||||
@@ -97,6 +98,38 @@ public class MemoryTableMeta implements TableMetaTSDB {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 过滤简单的行与块注释(--或# 开始的行 /*开始与*\/结束的块)
|
||||
*
|
||||
* @param sql
|
||||
* @return
|
||||
*/
|
||||
private String trimSimpleComment(String sql) {
|
||||
String[] sqls = sql.split("\n");
|
||||
if (sqls.length == 1) return sql;
|
||||
|
||||
boolean isCommentBlock = false;
|
||||
StringBuilder sb = new StringBuilder(sql.length());
|
||||
for (String psql : sqls) {
|
||||
if (psql.startsWith("--") || psql.startsWith("#")) {
|
||||
continue;
|
||||
} else if (psql.trim().length() == 0) {
|
||||
sb.append("\n");
|
||||
}
|
||||
if (!isCommentBlock && !psql.trim().startsWith("/*") && !psql.trim().endsWith("*/")) {
|
||||
sb.append(psql).append("\n");
|
||||
continue;
|
||||
}
|
||||
if (!isCommentBlock && psql.trim().startsWith("/*")) {
|
||||
isCommentBlock = true;
|
||||
}
|
||||
if (isCommentBlock && psql.trim().endsWith("*/")) {
|
||||
isCommentBlock = false;
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TableMeta find(String schema, String table) {
|
||||
List<String> keys = Arrays.asList(schema, table);
|
||||
|
||||
Reference in New Issue
Block a user