提交 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 java.util.regex.Pattern;
import org.h2.expression.Expression; 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.
* @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){ public static String getColumnName(Expression columnExp, int indexOfColumn, String[] columnNameOverides){
String columnName = null; String columnNameOverride = null;
if (columnNameOverides != null && columnNameOverides.length > indexOfColumn){ if (columnNameOverides != null && columnNameOverides.length > indexOfColumn){
columnName = columnNameOverides[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){ if (columnName==null && columnExp.getAlias()!=null){
columnName = columnExp.getAlias(); columnName = columnExp.getAlias();
...@@ -32,8 +61,6 @@ public class ColumnNamer { ...@@ -32,8 +61,6 @@ public class ColumnNamer {
return columnName; return columnName;
} }
//private static final Pattern reasonableNameCharactersPatternRE = Pattern.compile("[a-zA-Z0-9_'\\(\\)\\*,\\.\\+\\-\\*/:=\\<\\>!\\|@ \\t\\?\"\\$]*");
public static boolean isAllowableColumnName(String proposedName){ public static boolean isAllowableColumnName(String proposedName){
if (proposedName == null){ if (proposedName == null){
return false; return false;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论