Skip to content

Commit

Permalink
MemoryTableMeta ddl 处理注释2 (alibaba#2694)
Browse files Browse the repository at this point in the history
* MemoryTableMeta ddl 处理注释

* MemoryTableMeta ddl 处理注释2
  • Loading branch information
zhimin711 authored Aug 20, 2020
1 parent e472697 commit 182163c
Showing 1 changed file with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public boolean apply(EntryPosition position, String schema, String ddl, String e
&& !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);
Expand All @@ -97,6 +98,38 @@ public boolean apply(EntryPosition position, String schema, String ddl, String e
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);
Expand Down

0 comments on commit 182163c

Please sign in to comment.