提交 18ef6e09 authored 作者: Owner's avatar Owner

Added standard column namer

上级 1b67892f
...@@ -234,7 +234,7 @@ public class CreateTable extends SchemaCommand { ...@@ -234,7 +234,7 @@ public class CreateTable extends SchemaCommand {
for (int i = 0; i < columnCount; i++) { for (int i = 0; i < columnCount; i++) {
Expression expr = expressions.get(i); Expression expr = expressions.get(i);
int type = expr.getType(); int type = expr.getType();
String name = ColumnNamer.getColumnName(expr,i,null); String name = ColumnNamer.getColumnName(expr,i);
long precision = expr.getPrecision(); long precision = expr.getPrecision();
int displaySize = expr.getDisplaySize(); int displaySize = expr.getDisplaySize();
DataType dt = DataType.getDataType(type); DataType dt = DataType.getDataType(type);
......
...@@ -842,8 +842,8 @@ public class Select extends Query { ...@@ -842,8 +842,8 @@ public class Select extends Query {
} }
for (int i = 0; i < expressions.size(); i++) { for (int i = 0; i < expressions.size(); i++) {
Expression e = expressions.get(i); Expression e = expressions.get(i);
String columnName = ColumnNamer.getColumnName(e,i,null);
if(!ColumnNamer.isAllowableColumnName(e.getAlias())){ if(!ColumnNamer.isAllowableColumnName(e.getAlias())){
String columnName = ColumnNamer.getColumnName(e,i);
e = new Alias(e,columnName,true); e = new Alias(e,columnName,true);
} }
expressions.set(i, e.optimize(session)); expressions.set(i, e.optimize(session));
......
...@@ -11,6 +11,7 @@ import org.h2.expression.ExpressionColumn; ...@@ -11,6 +11,7 @@ import org.h2.expression.ExpressionColumn;
import org.h2.table.Column; import org.h2.table.Column;
import org.h2.table.ColumnResolver; import org.h2.table.ColumnResolver;
import org.h2.table.TableFilter; import org.h2.table.TableFilter;
import org.h2.util.ColumnNamer;
import org.h2.value.Value; import org.h2.value.Value;
/** /**
...@@ -37,11 +38,8 @@ public class SelectListColumnResolver implements ColumnResolver { ...@@ -37,11 +38,8 @@ public class SelectListColumnResolver implements ColumnResolver {
ArrayList<Expression> columnList = select.getExpressions(); ArrayList<Expression> columnList = select.getExpressions();
for (int i = 0; i < columnCount; i++) { for (int i = 0; i < columnCount; i++) {
Expression expr = columnList.get(i); Expression expr = columnList.get(i);
String columnName = expr.getAlias(); String columnName = ColumnNamer.getColumnName(expr, i);
if(columnName==null){ Column column = new Column(columnName, Value.NULL);
columnName = "_col_"+(i+1);
}
Column column = new Column(expr.getAlias(), Value.NULL);
column.setTable(null, i); column.setTable(null, i);
columns[i] = column; columns[i] = column;
expressions[i] = expr.getNonAliasExpression(); expressions[i] = expr.getNonAliasExpression();
......
...@@ -337,7 +337,7 @@ public class SelectUnion extends Query { ...@@ -337,7 +337,7 @@ public class SelectUnion extends Query {
long prec = Math.max(l.getPrecision(), r.getPrecision()); long prec = Math.max(l.getPrecision(), r.getPrecision());
int scale = Math.max(l.getScale(), r.getScale()); int scale = Math.max(l.getScale(), r.getScale());
int displaySize = Math.max(l.getDisplaySize(), r.getDisplaySize()); int displaySize = Math.max(l.getDisplaySize(), r.getDisplaySize());
String columnName = ColumnNamer.getColumnName(l,i,null); String columnName = ColumnNamer.getColumnName(l,i);
Column col = new Column(columnName, type, prec, scale, displaySize); Column col = new Column(columnName, type, prec, scale, displaySize);
Expression e = new ExpressionColumn(session.getDatabase(), col); Expression e = new ExpressionColumn(session.getDatabase(), col);
expressions.add(e); expressions.add(e);
......
...@@ -168,9 +168,7 @@ public class TableView extends Table { ...@@ -168,9 +168,7 @@ public class TableView extends Table {
name = columnTemplates[i].getName(); name = columnTemplates[i].getName();
type = columnTemplates[i].getType(); type = columnTemplates[i].getType();
} }
if (name == null) { name = ColumnNamer.getColumnName(expr,i,name);
name = ColumnNamer.getColumnName(expr,i,null);
}
if (type == Value.UNKNOWN) { if (type == Value.UNKNOWN) {
type = expr.getType(); type = expr.getType();
} }
......
package org.h2.util; package org.h2.util;
import java.util.regex.Matcher; import org.h2.expression.Expression;
import java.util.regex.Pattern;
import org.h2.expression.Expression; public class ColumnNamer {
public class ColumnNamer { /**
* Create a standardized column name that isn't null or and doesn't have a CR/LF in it.
public static String getColumnName(Expression columnExp, int indexOfColumn, String[] columnNameOverides){ * @param columnExp the column expression
String columnName = null; * @param indexOfColumn index of column in below array
if (columnNameOverides != null && columnNameOverides.length > indexOfColumn){ * @return
columnName = columnNameOverides[indexOfColumn]; */
} public static String getColumnName(Expression expr, int indexOfColumn) {
if (columnName==null && columnExp.getAlias()!=null){ return getColumnName(expr,indexOfColumn,(String) null);
columnName = columnExp.getAlias(); }
if(!isAllowableColumnName(columnName)){ /**
columnName = null; * Create a standardized column name that isn't null or and doesn't have a CR/LF in it.
} * @param columnExp the column expression
} * @param indexOfColumn index of column in below array
if (columnName==null && columnExp.getColumnName()!=null){ * @param columnNameOverides array of overriding column names
columnName = columnExp.getColumnName(); * @return the new column name
if(!isAllowableColumnName(columnName)){ */
columnName = columnName.replace('\n', ' ').replace('\r', ' '); public static String getColumnName(Expression columnExp, int indexOfColumn, String[] columnNameOverides){
} String columnNameOverride = null;
if(!isAllowableColumnName(columnName)){ if (columnNameOverides != null && columnNameOverides.length > indexOfColumn){
columnName = null; columnNameOverride = columnNameOverides[indexOfColumn];
} }
} return getColumnName(columnExp, indexOfColumn, columnNameOverride);
if (columnName==null){ }
columnName = "_unnamed_column_"+(indexOfColumn+1)+"_";
} /**
return columnName; * Create a standardized column name that isn't null or and doesn't have a CR/LF in it.
} * @param columnExp the column expression
* @param indexOfColumn index of column in below array
//private static final Pattern reasonableNameCharactersPatternRE = Pattern.compile("[a-zA-Z0-9_'\\(\\)\\*,\\.\\+\\-\\*/:=\\<\\>!\\|@ \\t\\?\"\\$]*"); * @param columnNameOverride single overriding column name
* @return the new column name
public static boolean isAllowableColumnName(String proposedName){ */
if (proposedName == null){ public static String getColumnName(Expression columnExp, int indexOfColumn, String columnNameOverride) {
return false; String columnName = null;
} if (columnNameOverride != null){
if(proposedName.contains("\n") || proposedName.contains("\r")){ columnName = columnNameOverride;
return false; }
} if (columnName==null && columnExp.getAlias()!=null){
return true; columnName = columnExp.getAlias();
} if(!isAllowableColumnName(columnName)){
columnName = null;
} }
}
if (columnName==null && columnExp.getColumnName()!=null){
columnName = columnExp.getColumnName();
if(!isAllowableColumnName(columnName)){
columnName = columnName.replace('\n', ' ').replace('\r', ' ');
}
if(!isAllowableColumnName(columnName)){
columnName = null;
}
}
if (columnName==null){
columnName = "_unnamed_column_"+(indexOfColumn+1)+"_";
}
return columnName;
}
public static boolean isAllowableColumnName(String proposedName){
if (proposedName == null){
return false;
}
if(proposedName.contains("\n") || proposedName.contains("\r")){
return false;
}
return true;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论