提交 5ab7b5ae authored 作者: Owner's avatar Owner

Added default naming rules

上级 8565a071
...@@ -23,6 +23,7 @@ import org.h2.result.RowFactory; ...@@ -23,6 +23,7 @@ import org.h2.result.RowFactory;
import org.h2.schema.Schema; import org.h2.schema.Schema;
import org.h2.table.Table; import org.h2.table.Table;
import org.h2.tools.CompressTool; import org.h2.tools.CompressTool;
import org.h2.util.ColumnNamer;
import org.h2.util.JdbcUtils; import org.h2.util.JdbcUtils;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
import org.h2.value.CompareMode; import org.h2.value.CompareMode;
...@@ -534,6 +535,16 @@ public class Set extends Prepared { ...@@ -534,6 +535,16 @@ public class Set extends Prepared {
database.setAllowBuiltinAliasOverride(value == 1); database.setAllowBuiltinAliasOverride(value == 1);
break; break;
} }
case SetTypes.COLUMN_NAME_RULES: {
session.getUser().checkAdmin();
System.out.println("1"+stringValue);
System.out.println("2"+expression.getColumnName());
System.out.println("3"+expression.getSQL());
ColumnNamer.configure(expression.getColumnName());
break;
}
default: default:
DbException.throwInternalError("type="+type); DbException.throwInternalError("type="+type);
} }
......
...@@ -247,6 +247,11 @@ public class SetTypes { ...@@ -247,6 +247,11 @@ public class SetTypes {
* The type of SET BUILTIN_ALIAS_OVERRIDE statement. * The type of SET BUILTIN_ALIAS_OVERRIDE statement.
*/ */
public static final int BUILTIN_ALIAS_OVERRIDE = 47; public static final int BUILTIN_ALIAS_OVERRIDE = 47;
/**
* The type of a SET COLUMN_NAME_RULES statement.
*/
public static final int COLUMN_NAME_RULES = 48;
private static final ArrayList<String> TYPES = New.arrayList(); private static final ArrayList<String> TYPES = New.arrayList();
...@@ -304,6 +309,8 @@ public class SetTypes { ...@@ -304,6 +309,8 @@ public class SetTypes {
list.add(FORCE_JOIN_ORDER, "FORCE_JOIN_ORDER"); list.add(FORCE_JOIN_ORDER, "FORCE_JOIN_ORDER");
list.add(LAZY_QUERY_EXECUTION, "LAZY_QUERY_EXECUTION"); list.add(LAZY_QUERY_EXECUTION, "LAZY_QUERY_EXECUTION");
list.add(BUILTIN_ALIAS_OVERRIDE, "BUILTIN_ALIAS_OVERRIDE"); list.add(BUILTIN_ALIAS_OVERRIDE, "BUILTIN_ALIAS_OVERRIDE");
list.add(COLUMN_NAME_RULES, "COLUMN_NAME_RULES");
} }
/** /**
......
...@@ -374,7 +374,7 @@ public class Session extends SessionWithState { ...@@ -374,7 +374,7 @@ public class Session extends SessionWithState {
*/ */
public void removeLocalTempTable(Table table) { public void removeLocalTempTable(Table table) {
// Exception thrown in org.h2.engine.Database.removeMeta if line below is missing with TestGeneralCommonTableQueries // Exception thrown in org.h2.engine.Database.removeMeta if line below is missing with TestGeneralCommonTableQueries
//database.lockMeta(this); database.lockMeta(this);
modificationId++; modificationId++;
localTempTables.remove(table.getName()); localTempTables.remove(table.getName());
synchronized (database) { synchronized (database) {
...@@ -977,7 +977,7 @@ public class Session extends SessionWithState { ...@@ -977,7 +977,7 @@ public class Session extends SessionWithState {
table.setModified(); table.setModified();
it.remove(); it.remove();
// Exception thrown in org.h2.engine.Database.removeMeta if line below is missing with TestDeadlock // Exception thrown in org.h2.engine.Database.removeMeta if line below is missing with TestDeadlock
//database.lockMeta(this); database.lockMeta(this);
table.removeChildrenAndResources(this); table.removeChildrenAndResources(this);
if (closeSession) { if (closeSession) {
// need to commit, otherwise recovery might // need to commit, otherwise recovery might
......
package org.h2.util; package org.h2.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.h2.expression.Expression; import org.h2.expression.Expression;
import org.h2.message.DbException;
public class ColumnNamer { public class ColumnNamer {
private static final String DEFAULT_COLUMN_NAME = "DEFAULT";
private static final String DEFAULT_COMMAND = "DEFAULT";
private static final String REGULAR_EXPRESSION_MATCH_DISALLOWED = "REGULAREXPRESSIONMATCHDISALLOWED = ";
private static final String REGULAR_EXPRESSION_MATCH_ALLOWED = "REGULAREXPRESSIONMATCHALLOWED = ";
private static final String DEFAULT_COLUMN_NAME_PATTERN = "DEFAULTCOLUMNNAMEPATTERN = ";
private static final String MAX_IDENTIFIER_LENGTH = "MAXIDENTIFIERLENGTH = ";
/** /**
* Create a standardized column name that isn't null and doesn't have a CR/LF in it. * Create a standardized column name that isn't null and doesn't have a CR/LF in it.
* @param columnExp the column expression * @param columnExp the column expression
...@@ -40,39 +51,39 @@ public class ColumnNamer { ...@@ -40,39 +51,39 @@ public class ColumnNamer {
String columnName = null; String columnName = null;
if (columnNameOverride != null){ if (columnNameOverride != null){
columnName = columnNameOverride; columnName = columnNameOverride;
//if(!isAllowableColumnName(columnName)){ if(!isAllowableColumnName(columnName)){
// columnName = columnName.replace("\n", " ").replace("\r", " "); columnName = fixColumnName(columnName);
//} }
//if(!isAllowableColumnName(columnName)){ if(!isAllowableColumnName(columnName)){
// columnName = null; columnName = null;
//} }
return columnName; return columnName;
} }
// try a name from the column alias // try a name from the column alias
if (columnName==null && columnExp.getAlias()!=null){ if (columnName==null && columnExp.getAlias()!=null && !DEFAULT_COLUMN_NAME.equals(columnExp.getAlias())){
columnName = columnExp.getAlias(); columnName = columnExp.getAlias();
if(!isAllowableColumnName(columnName)){ if(!isAllowableColumnName(columnName)){
columnName = columnName.replace("\n", " ").replace("\r", " "); columnName = fixColumnName(columnName);
} }
if(!isAllowableColumnName(columnName)){ if(!isAllowableColumnName(columnName)){
columnName = null; columnName = null;
} }
} }
// try a name derived from the column expression SQL // try a name derived from the column expression SQL
if (columnName==null && columnExp.getColumnName()!=null){ if (columnName==null && columnExp.getColumnName()!=null && !DEFAULT_COLUMN_NAME.equals(columnExp.getColumnName())){
columnName = columnExp.getColumnName(); columnName = columnExp.getColumnName();
if(!isAllowableColumnName(columnName)){ if(!isAllowableColumnName(columnName)){
columnName = columnName.replace("\n", " ").replace("\r", " "); columnName = fixColumnName(columnName);
} }
if(!isAllowableColumnName(columnName)){ if(!isAllowableColumnName(columnName)){
columnName = null; columnName = null;
} }
} }
// try a name derived from the column expression plan SQL // try a name derived from the column expression plan SQL
if (columnName==null && columnExp.getSQL()!=null){ if (columnName==null && columnExp.getSQL()!=null && !DEFAULT_COLUMN_NAME.equals(columnExp.getSQL())){
columnName = columnExp.getSQL(); columnName = columnExp.getSQL();
if(!isAllowableColumnName(columnName)){ if(!isAllowableColumnName(columnName)){
columnName = columnName.replace("\n", " ").replace("\r", " "); columnName = fixColumnName(columnName);
} }
if(!isAllowableColumnName(columnName)){ if(!isAllowableColumnName(columnName)){
columnName = null; columnName = null;
...@@ -80,19 +91,98 @@ public class ColumnNamer { ...@@ -80,19 +91,98 @@ public class ColumnNamer {
} }
// go with a innocuous default name pattern // go with a innocuous default name pattern
if (columnName==null){ if (columnName==null){
columnName = "_unnamed_column_"+(indexOfColumn+1)+"_"; columnName = defaultColumnNamePattern.replace("$$", ""+(indexOfColumn+1));
} }
return columnName; return columnName;
} }
public static boolean isAllowableColumnName(String proposedName){ public static boolean isAllowableColumnName(String proposedName){
// check null
if (proposedName == null){ if (proposedName == null){
return false; return false;
} }
//if(proposedName.contains("\n") || proposedName.contains("\r")){ // check size limits
// return false; if (proposedName.length() > maxIdentiferLength || proposedName.length()==0){
//} return false;
}
Matcher match = compiledRegularExpressionMatchAllowed.matcher(proposedName);
if(!match.matches()){
return false;
}
return true; return true;
} }
private static String fixColumnName(String proposedName) {
Matcher match = compiledRegularExpressionMatchDisallowed.matcher(proposedName);
proposedName = match.replaceAll("");
// check size limits - then truncate
if (proposedName.length() > maxIdentiferLength){
proposedName=proposedName.substring(0, maxIdentiferLength);
}
return proposedName;
}
static int maxIdentiferLength = Integer.MAX_VALUE;
static String regularExpressionMatchAllowed = "(?m)(?s).+";
static String regularExpressionMatchDisallowed = "(?m)(?s)[\\x00]";
static String defaultColumnNamePattern = "_unnamed_column_$$_";
static Pattern compiledRegularExpressionMatchAllowed = Pattern.compile(regularExpressionMatchAllowed);
static Pattern compiledRegularExpressionMatchDisallowed = Pattern.compile(regularExpressionMatchDisallowed);
public static void configure(String stringValue) {
try{
if(stringValue.equalsIgnoreCase(DEFAULT_COMMAND)){
maxIdentiferLength = Integer.MAX_VALUE;
regularExpressionMatchAllowed = "(?m)(?s).+";
regularExpressionMatchDisallowed = "(?m)(?s)[\\x00]";
defaultColumnNamePattern = "_UNNAMED_$$";
} else if(stringValue.equalsIgnoreCase("ORACLE128")){
maxIdentiferLength = 128;
regularExpressionMatchAllowed = "(?m)(?s)\"?[A-Za-z0-9_]+\"?";
regularExpressionMatchDisallowed = "(?m)(?s)[^A-Za-z0-9_\"]";
defaultColumnNamePattern = "_UNNAMED_$$";
} else if(stringValue.equalsIgnoreCase("ORACLE30")){
maxIdentiferLength = 30;
regularExpressionMatchAllowed = "(?m)(?s)\"?[A-Za-z0-9_]+\"?";
regularExpressionMatchDisallowed = "(?m)(?s)[^A-Za-z0-9_\"]";
defaultColumnNamePattern = "_UNNAMED_$$";
}else if(stringValue.equalsIgnoreCase("POSTGRES")){
maxIdentiferLength = 31;
regularExpressionMatchAllowed = "(?m)(?s)\"?[A-Za-z0-9_\"]+\"?";
regularExpressionMatchDisallowed = "(?m)(?s)[^A-Za-z0-9_\"]";
defaultColumnNamePattern = "_UNNAMED_$$";
} else if(stringValue.startsWith(MAX_IDENTIFIER_LENGTH)){
maxIdentiferLength = Math.max(0,Integer.parseInt(stringValue.substring(MAX_IDENTIFIER_LENGTH.length())));
} else if(stringValue.startsWith(DEFAULT_COLUMN_NAME_PATTERN)){
defaultColumnNamePattern =stringValue.substring(DEFAULT_COLUMN_NAME_PATTERN.length());
} else if(stringValue.startsWith(REGULAR_EXPRESSION_MATCH_ALLOWED)){
regularExpressionMatchAllowed=stringValue.substring(REGULAR_EXPRESSION_MATCH_ALLOWED.length());
} else if(stringValue.startsWith(REGULAR_EXPRESSION_MATCH_DISALLOWED)){
regularExpressionMatchDisallowed =stringValue.substring(REGULAR_EXPRESSION_MATCH_DISALLOWED.length());
}
else
{
throw DbException.getInvalidValueException("SET COLUMN_NAME_RULES: unknown id:"+stringValue,
stringValue);
}
// recompile RE patterns
compiledRegularExpressionMatchAllowed = Pattern.compile(regularExpressionMatchAllowed);
compiledRegularExpressionMatchDisallowed = Pattern.compile(regularExpressionMatchDisallowed);
}
//Including NumberFormatException|PatternSyntaxException
catch(RuntimeException e){
throw DbException.getInvalidValueException("SET COLUMN_NAME_RULES:"+e.getMessage(),
stringValue);
}
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论