提交 cc172420 authored 作者: Thomas Mueller's avatar Thomas Mueller

For alias columns, ResultSetMetaData.getTableName() and getColumnName() now…

For alias columns, ResultSetMetaData.getTableName() and getColumnName() now return the real table and column name.
上级 0a211f76
...@@ -1555,7 +1555,7 @@ public class Parser { ...@@ -1555,7 +1555,7 @@ public class Parser {
Expression expr = readExpression(); Expression expr = readExpression();
if (readIf("AS") || currentTokenType == IDENTIFIER) { if (readIf("AS") || currentTokenType == IDENTIFIER) {
String alias = readAliasIdentifier(); String alias = readAliasIdentifier();
expr = new Alias(expr, alias); expr = new Alias(expr, alias, database.getMode().aliasColumnName);
} }
expressions.add(expr); expressions.add(expr);
} }
......
...@@ -115,7 +115,7 @@ public class CreateTable extends SchemaCommand { ...@@ -115,7 +115,7 @@ public class CreateTable extends SchemaCommand {
if (asQuery != null) { if (asQuery != null) {
asQuery.prepare(); asQuery.prepare();
if (columns.size() == 0) { if (columns.size() == 0) {
generateColumnFromQuery(); generateColumnsFromQuery();
} else if (columns.size() != asQuery.getColumnCount()) { } else if (columns.size() != asQuery.getColumnCount()) {
throw Message.getSQLException(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH); throw Message.getSQLException(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
} }
...@@ -194,13 +194,13 @@ public class CreateTable extends SchemaCommand { ...@@ -194,13 +194,13 @@ public class CreateTable extends SchemaCommand {
return 0; return 0;
} }
private void generateColumnFromQuery() { private void generateColumnsFromQuery() {
int columnCount = asQuery.getColumnCount(); int columnCount = asQuery.getColumnCount();
ObjectArray expressions = asQuery.getExpressions(); ObjectArray expressions = asQuery.getExpressions();
for (int i = 0; i < columnCount; i++) { for (int i = 0; i < columnCount; i++) {
Expression expr = (Expression) expressions.get(i); Expression expr = (Expression) expressions.get(i);
int type = expr.getType(); int type = expr.getType();
String name = expr.getColumnName(); String name = expr.getAlias();
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);
......
...@@ -13,6 +13,7 @@ import java.util.HashSet; ...@@ -13,6 +13,7 @@ import java.util.HashSet;
import org.h2.constant.ErrorCode; import org.h2.constant.ErrorCode;
import org.h2.constant.SysProperties; import org.h2.constant.SysProperties;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.engine.Mode;
import org.h2.engine.Session; import org.h2.engine.Session;
import org.h2.expression.Alias; import org.h2.expression.Alias;
import org.h2.expression.Comparison; import org.h2.expression.Comparison;
...@@ -1089,7 +1090,8 @@ public class Select extends Query { ...@@ -1089,7 +1090,8 @@ public class Select extends Query {
if (expr instanceof Alias) { if (expr instanceof Alias) {
return expr.getAlias(); return expr.getAlias();
} }
expr = new Alias(expr, session.getNextTempViewName() + "_X"); Mode mode = session.getDatabase().getMode();
expr = new Alias(expr, session.getNextTempViewName() + "_X", mode.aliasColumnName);
expressions.set(0, expr); expressions.set(0, expr);
return expr.getAlias(); return expr.getAlias();
} }
......
...@@ -72,16 +72,23 @@ public class SysProperties { ...@@ -72,16 +72,23 @@ public class SysProperties {
public static final String USER_HOME = getStringSetting("user.home", ""); public static final String USER_HOME = getStringSetting("user.home", "");
/** /**
* System property <code>h2.aliasColumnName</code> (default: false).<br /> * System property <code>h2.aliasColumnName</code>.<br />
* When enabled, aliased columns (as in SELECT ID AS I FROM TEST) return the * When enabled, aliased columns (as in SELECT ID AS I FROM TEST) return the
* real table and column name in ResultSetMetaData.getTableName() and * alias (I in this case) in ResultSetMetaData.getColumnName() and 'null' in
* getColumnName(). * getTableName(). If disabled, the real column name (ID in this case) and
* table name is returned. This setting only affects the default mode.
* <p> * <p>
* This feature is disabled by default for compatibility with other * When using different modes, this feature is disabled for compatibility
* databases (HSQLDB, Apache Derby, PostgreSQL, some version of MySQL). * for all databases except MySQL. For MySQL, it is always enabled.
* </p>
* <p>
* In version 1.0, it is enabled by default.
* </p>
* <p>
* In version 1.1, it is disabled by default.
* </p> * </p>
*/ */
public static final boolean ALIAS_COLUMN_NAME = getBooleanSetting("h2.aliasColumnName", false); public static final boolean ALIAS_COLUMN_NAME = getBooleanSetting("h2.aliasColumnName", Constants.VERSION <= 1.0);
/** /**
* System property <code>h2.allowBigDecimalExtensions</code> (default: * System property <code>h2.allowBigDecimalExtensions</code> (default:
...@@ -164,10 +171,10 @@ public class SysProperties { ...@@ -164,10 +171,10 @@ public class SysProperties {
public static final int DATASOURCE_TRACE_LEVEL = getIntSetting("h2.dataSourceTraceLevel", TraceSystem.ERROR); public static final int DATASOURCE_TRACE_LEVEL = getIntSetting("h2.dataSourceTraceLevel", TraceSystem.ERROR);
/** /**
* System property <code>h2.defaultMaxMemoryUndo</code> (default: 100000).<br /> * System property <code>h2.defaultMaxMemoryUndo</code> (default: 50000).<br />
* The default value for the MAX_MEMORY_UNDO setting. * The default value for the MAX_MEMORY_UNDO setting.
*/ */
public static final int DEFAULT_MAX_MEMORY_UNDO = getIntSetting("h2.defaultMaxMemoryUndo", 100000); public static final int DEFAULT_MAX_MEMORY_UNDO = getIntSetting("h2.defaultMaxMemoryUndo", 50000);
/** /**
* System property <code>h2.defaultLockMode</code> (default: 3).<br /> * System property <code>h2.defaultLockMode</code> (default: 3).<br />
......
...@@ -9,7 +9,6 @@ package org.h2.expression; ...@@ -9,7 +9,6 @@ package org.h2.expression;
import java.sql.SQLException; import java.sql.SQLException;
import org.h2.command.Parser; import org.h2.command.Parser;
import org.h2.constant.SysProperties;
import org.h2.engine.Session; import org.h2.engine.Session;
import org.h2.table.ColumnResolver; import org.h2.table.ColumnResolver;
import org.h2.table.TableFilter; import org.h2.table.TableFilter;
...@@ -22,10 +21,12 @@ public class Alias extends Expression { ...@@ -22,10 +21,12 @@ public class Alias extends Expression {
private final String alias; private final String alias;
private Expression expr; private Expression expr;
private boolean aliasColumnName;
public Alias(Expression expression, String alias) { public Alias(Expression expression, String alias, boolean aliasColumnName) {
this.expr = expression; this.expr = expression;
this.alias = alias; this.alias = alias;
this.aliasColumnName = aliasColumnName;
} }
public Expression getNonAliasExpression() { public Expression getNonAliasExpression() {
...@@ -94,17 +95,17 @@ public class Alias extends Expression { ...@@ -94,17 +95,17 @@ public class Alias extends Expression {
} }
public String getTableName() { public String getTableName() {
if (SysProperties.ALIAS_COLUMN_NAME) { if (aliasColumnName) {
return expr.getTableName();
}
return super.getTableName(); return super.getTableName();
} }
return expr.getTableName();
}
public String getColumnName() { public String getColumnName() {
if (SysProperties.ALIAS_COLUMN_NAME) { if (aliasColumnName) {
return expr.getColumnName();
}
return super.getColumnName(); return super.getColumnName();
} }
return expr.getColumnName();
}
} }
...@@ -922,13 +922,13 @@ public class Function extends Expression implements FunctionCall { ...@@ -922,13 +922,13 @@ public class Function extends Expression implements FunctionCall {
result = ValueString.get(right(v0.getString(), v1.getInt())); result = ValueString.get(right(v0.getString(), v1.getInt()));
break; break;
case LTRIM: case LTRIM:
result = ValueString.get(trim(v0.getString(), true, false, v1 == null ? " " : v1.getString())); result = ValueString.get(StringUtils.trim(v0.getString(), true, false, v1 == null ? " " : v1.getString()));
break; break;
case TRIM: case TRIM:
result = ValueString.get(trim(v0.getString(), true, true, v1 == null ? " " : v1.getString())); result = ValueString.get(StringUtils.trim(v0.getString(), true, true, v1 == null ? " " : v1.getString()));
break; break;
case RTRIM: case RTRIM:
result = ValueString.get(trim(v0.getString(), false, true, v1 == null ? " " : v1.getString())); result = ValueString.get(StringUtils.trim(v0.getString(), false, true, v1 == null ? " " : v1.getString()));
break; break;
case SUBSTR: case SUBSTR:
case SUBSTRING: { case SUBSTRING: {
...@@ -1242,28 +1242,6 @@ public class Function extends Expression implements FunctionCall { ...@@ -1242,28 +1242,6 @@ public class Function extends Expression implements FunctionCall {
return s.substring(start, start + length); return s.substring(start, start + length);
} }
private static String trim(String s, boolean leading, boolean trailing, String sp) {
char space = (sp == null || sp.length() < 1) ? ' ' : sp.charAt(0);
// TODO function trim: HSQLDB says 'tabs are not removed', but they are.
// check what other databases do
if (leading) {
int len = s.length(), i = 0;
while (i < len && s.charAt(i) == space) {
i++;
}
s = (i == 0) ? s : s.substring(i);
}
if (trailing) {
int endIndex = s.length() - 1;
int i = endIndex;
while (i >= 0 && s.charAt(i) == space) {
i--;
}
s = i == endIndex ? s : s.substring(0, i + 1);
}
return s;
}
private static String replace(String s, String replace, String with) { private static String replace(String s, String replace, String with) {
if (replace == null || replace.length() == 0) { if (replace == null || replace.length() == 0) {
// avoid out of memory // avoid out of memory
......
...@@ -862,4 +862,36 @@ public class StringUtils { ...@@ -862,4 +862,36 @@ public class StringUtils {
return copy; return copy;
} }
/**
* Trim a character from a string.
*
* @param s the string
* @param leading if leading characters should be removed
* @param trailing if trailing characters should be removed
* @param sp what to remove (only the first character is used)
* or null for a space
* @return the trimmed string
*/
public static String trim(String s, boolean leading, boolean trailing, String sp) {
char space = (sp == null || sp.length() < 1) ? ' ' : sp.charAt(0);
// TODO function trim: HSQLDB says 'tabs are not removed', but they are.
// check what other databases do
if (leading) {
int len = s.length(), i = 0;
while (i < len && s.charAt(i) == space) {
i++;
}
s = (i == 0) ? s : s.substring(i);
}
if (trailing) {
int endIndex = s.length() - 1;
int i = endIndex;
while (i >= 0 && s.charAt(i) == space) {
i--;
}
s = i == endIndex ? s : s.substring(0, i + 1);
}
return s;
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论