提交 3ccdfaff authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Do not use regular expressions in Regular and some other modes in ColumnNamer

上级 5c4719de
......@@ -6,7 +6,7 @@ package org.h2.util;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.h2.engine.Session;
import org.h2.expression.Expression;
......@@ -124,26 +124,31 @@ public class ColumnNamer {
}
private boolean isAllowableColumnName(String proposedName) {
// check null
if (proposedName == null) {
return false;
}
// check size limits
if (proposedName.length() > configuration.getMaxIdentiferLength() || proposedName.length() == 0) {
int length = proposedName.length();
if (length > configuration.getMaxIdentiferLength() || length == 0) {
return false;
}
Matcher match = configuration.getCompiledRegularExpressionMatchAllowed().matcher(proposedName);
return match.matches();
Pattern allowed = configuration.getCompiledRegularExpressionMatchAllowed();
return allowed == null || allowed.matcher(proposedName).matches();
}
private String fixColumnName(String proposedName) {
Matcher match = configuration.getCompiledRegularExpressionMatchDisallowed().matcher(proposedName);
proposedName = match.replaceAll("");
Pattern disallowed = configuration.getCompiledRegularExpressionMatchDisallowed();
if (disallowed == null) {
proposedName = StringUtils.replaceAll(proposedName, "\u0000", "");
} else {
proposedName = disallowed.matcher(proposedName).replaceAll("");
}
// check size limits - then truncate
if (proposedName.length() > configuration.getMaxIdentiferLength()) {
proposedName = proposedName.substring(0, configuration.getMaxIdentiferLength());
int length = proposedName.length(), maxLength = configuration.getMaxIdentiferLength();
if (length > maxLength) {
proposedName = proposedName.substring(0, maxLength);
}
return proposedName;
......
......@@ -40,8 +40,7 @@ public class ColumnNamerConfiguration {
this.defaultColumnNamePattern = defaultColumnNamePattern;
this.generateUniqueColumnNames = generateUniqueColumnNames;
compiledRegularExpressionMatchAllowed = Pattern.compile(regularExpressionMatchAllowed);
compiledRegularExpressionMatchDisallowed = Pattern.compile(regularExpressionMatchDisallowed);
recompilePatterns();
}
public int getMaxIdentiferLength() {
......@@ -80,6 +79,11 @@ public class ColumnNamerConfiguration {
this.defaultColumnNamePattern = defaultColumnNamePattern;
}
/**
* Returns compiled pattern for allowed names.
*
* @return compiled pattern, or null for default
*/
public Pattern getCompiledRegularExpressionMatchAllowed() {
return compiledRegularExpressionMatchAllowed;
}
......@@ -88,6 +92,11 @@ public class ColumnNamerConfiguration {
this.compiledRegularExpressionMatchAllowed = compiledRegularExpressionMatchAllowed;
}
/**
* Returns compiled pattern for disallowed names.
*
* @return compiled pattern, or null for default
*/
public Pattern getCompiledRegularExpressionMatchDisallowed() {
return compiledRegularExpressionMatchDisallowed;
}
......@@ -138,8 +147,11 @@ public class ColumnNamerConfiguration {
private void recompilePatterns() {
try {
// recompile RE patterns
setCompiledRegularExpressionMatchAllowed(Pattern.compile(getRegularExpressionMatchAllowed()));
setCompiledRegularExpressionMatchDisallowed(Pattern.compile(getRegularExpressionMatchDisallowed()));
setCompiledRegularExpressionMatchAllowed(
regularExpressionMatchAllowed != null ? Pattern.compile(regularExpressionMatchAllowed) : null);
setCompiledRegularExpressionMatchDisallowed(
regularExpressionMatchDisallowed != null ? Pattern.compile(regularExpressionMatchDisallowed)
: null);
} catch (Exception e) {
configure(REGULAR);
throw e;
......@@ -147,7 +159,7 @@ public class ColumnNamerConfiguration {
}
public static ColumnNamerConfiguration getDefault() {
return new ColumnNamerConfiguration(Integer.MAX_VALUE, "(?m)(?s).+", "(?m)(?s)[\\x00]", "_UNNAMED_$$", false);
return new ColumnNamerConfiguration(Integer.MAX_VALUE, null, null, "_UNNAMED_$$", false);
}
private static String unquoteString(String s) {
......@@ -220,8 +232,8 @@ public class ColumnNamerConfiguration {
case Ignite:
default:
setMaxIdentiferLength(Integer.MAX_VALUE);
setRegularExpressionMatchAllowed("(?m)(?s).+");
setRegularExpressionMatchDisallowed("(?m)(?s)[\\x00]");
setRegularExpressionMatchAllowed(null);
setRegularExpressionMatchDisallowed(null);
setDefaultColumnNamePattern("_UNNAMED_$$");
setGenerateUniqueColumnNames(false);
break;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论