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