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

Added standard column namer

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