提交 c2209b31 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Reduce number of Column() constructors

上级 847ab2c2
......@@ -216,7 +216,6 @@ import org.h2.table.TableFilter;
import org.h2.table.TableFilter.TableFilterVisitor;
import org.h2.table.TableView;
import org.h2.util.IntervalUtils;
import org.h2.util.MathUtils;
import org.h2.util.ParserUtil;
import org.h2.util.StatementBuilder;
import org.h2.util.StringUtils;
......@@ -5351,7 +5350,6 @@ public class Parser {
regular = true;
}
long precision = -1;
int displaySize = -1;
ExtTypeInfo extTypeInfo = null;
int scale = -1;
String comment = null;
......@@ -5368,7 +5366,6 @@ public class Parser {
comment = templateColumn.getComment();
original = forTable ? domain.getSQL() : templateColumn.getOriginalSQL();
precision = type.getPrecision();
displaySize = type.getDisplaySize();
scale = type.getScale();
extTypeInfo = type.getExtTypeInfo();
} else {
......@@ -5388,8 +5385,6 @@ public class Parser {
read();
}
precision = precision == -1 ? dataType.defaultPrecision : precision;
displaySize = displaySize == -1 ? dataType.defaultDisplaySize
: displaySize;
scale = scale == -1 ? dataType.defaultScale : scale;
if (dataType.supportsPrecision || dataType.supportsScale) {
int t = dataType.type;
......@@ -5403,7 +5398,6 @@ public class Parser {
} else {
original = original + '(' + originalScale + ')';
}
precision = displaySize = ValueTime.getDisplaySize(originalScale);
break;
case Value.TIMESTAMP:
if (original.equals("TIMESTAMP WITHOUT TIME ZONE")) {
......@@ -5411,11 +5405,9 @@ public class Parser {
} else {
original = original + '(' + originalScale + ')';
}
precision = displaySize = ValueTimestamp.getDisplaySize(originalScale);
break;
case Value.TIMESTAMP_TZ:
original = "TIMESTAMP(" + originalScale + ") WITH TIME ZONE";
precision = displaySize = ValueTimestampTimeZone.getDisplaySize(originalScale);
break;
}
} else if (original.equals("DATETIME") || original.equals("DATETIME2")) {
......@@ -5428,11 +5420,9 @@ public class Parser {
read(CLOSE_PAREN);
scale = originalScale;
original = original + '(' + originalScale + ')';
precision = displaySize = ValueTimestamp.getDisplaySize(originalScale);
}
} else if (original.equals("SMALLDATETIME")) {
scale = 0;
precision = displaySize = ValueTimestamp.getDisplaySize(0);
}
} else if (DataType.isIntervalType(t)) {
if (originalPrecision >= 0 || originalScale >= 0) {
......@@ -5466,7 +5456,6 @@ public class Parser {
}
}
precision = p;
displaySize = MathUtils.convertLongToInt(precision);
original += ")";
}
read(CLOSE_PAREN);
......@@ -5547,13 +5536,13 @@ public class Parser {
// MySQL compatibility
readIf("UNSIGNED");
int type = dataType.type;
if (scale > precision && !DataType.isIntervalType(type)) {
if (scale > precision && dataType.supportsPrecision && dataType.supportsScale
&& !DataType.isIntervalType(type)) {
throw DbException.get(ErrorCode.INVALID_VALUE_SCALE_PRECISION,
Integer.toString(scale), Long.toString(precision));
}
Column column = new Column(columnName, type, precision, scale,
displaySize, extTypeInfo);
Column column = new Column(columnName, TypeInfo.getTypeInfo(type, precision, scale, extTypeInfo));
if (templateColumn != null) {
column.setNullable(templateColumn.isNullable());
column.setDefaultExpression(session,
......@@ -5883,7 +5872,7 @@ public class Parser {
for (int i = 0; i < columnCount; i++) {
Column c = columns.get(i);
if (c.getType().getValueType() == Value.UNKNOWN) {
c = new Column(c.getName(), Value.STRING, 0, 0, 0);
c = new Column(c.getName(), Value.STRING);
columns.set(i, c);
}
Expression[] array = new Expression[rowCount];
......
......@@ -15,17 +15,12 @@ import org.h2.engine.Database;
import org.h2.engine.DbObject;
import org.h2.engine.Session;
import org.h2.expression.Expression;
import org.h2.expression.ExpressionColumn;
import org.h2.message.DbException;
import org.h2.schema.Schema;
import org.h2.schema.Sequence;
import org.h2.table.Column;
import org.h2.table.Table;
import org.h2.util.ColumnNamer;
import org.h2.value.DataType;
import org.h2.value.ExtTypeInfo;
import org.h2.value.TypeInfo;
import org.h2.value.Value;
/**
* This class represents the statement
......@@ -181,39 +176,8 @@ public class CreateTable extends CommandWithColumns {
ColumnNamer columnNamer= new ColumnNamer(session);
for (int i = 0; i < columnCount; i++) {
Expression expr = expressions.get(i);
TypeInfo type = expr.getType();
int valueType = type.getValueType();
String name = columnNamer.getColumnName(expr,i,expr.getAlias());
long precision = type.getPrecision();
int displaySize = type.getDisplaySize();
DataType dt = DataType.getDataType(valueType);
if (precision > 0 && (dt.defaultPrecision == 0 ||
(dt.defaultPrecision > precision && dt.defaultPrecision < Byte.MAX_VALUE))) {
// dont' set precision to MAX_VALUE if this is the default
precision = dt.defaultPrecision;
}
int scale = type.getScale();
if (scale > 0 && (dt.defaultScale == 0 ||
(dt.defaultScale > scale && dt.defaultScale < precision))) {
scale = dt.defaultScale;
}
if (scale > precision) {
precision = scale;
}
ExtTypeInfo extTypeInfo = null;
int t = dt.type;
if (DataType.isExtInfoType(t)) {
if (expr instanceof ExpressionColumn) {
extTypeInfo = ((ExpressionColumn) expr).getColumn().getType().getExtTypeInfo();
} else if (t == Value.ENUM) {
/*
* Only columns of tables may be enumerated.
*/
throw DbException.get(ErrorCode.GENERAL_ERROR_1,
"Unable to resolve enumerators of expression");
}
}
Column col = new Column(name, valueType, precision, scale, displaySize, extTypeInfo);
String name = columnNamer.getColumnName(expr, i, expr.getAlias());
Column col = new Column(name, expr.getType());
addColumn(col);
}
}
......
......@@ -18,6 +18,7 @@ import org.h2.table.Column;
import org.h2.table.Table;
import org.h2.table.TableType;
import org.h2.table.TableView;
import org.h2.value.TypeInfo;
import org.h2.value.Value;
/**
......@@ -110,7 +111,7 @@ public class CreateView extends SchemaCommand {
columnTemplatesAsStrings = new Column[columnNames.length];
for (int i = 0; i < columnNames.length; ++i) {
// non table expressions are fine to use unknown column type
columnTemplatesAsUnknowns[i] = new Column(columnNames[i], Value.UNKNOWN);
columnTemplatesAsUnknowns[i] = new Column(columnNames[i], TypeInfo.TYPE_UNKNOWN);
// table expressions can't have unknown types - so we use string instead
columnTemplatesAsStrings[i] = new Column(columnNames[i], Value.STRING);
}
......
......@@ -60,6 +60,7 @@ import org.h2.util.JdbcUtils;
import org.h2.util.MathUtils;
import org.h2.util.StringUtils;
import org.h2.util.Utils;
import org.h2.value.DataType;
import org.h2.value.TypeInfo;
import org.h2.value.Value;
import org.h2.value.ValueArray;
......@@ -2546,8 +2547,14 @@ public class Function extends Expression implements FunctionCall {
type = typeInfo;
if (allConst) {
Value v = getValue(session);
if (v == ValueNull.INSTANCE) {
if (info.type == CAST || info.type == CONVERT) {
if (v == ValueNull.INSTANCE) {
return this;
}
DataType dt = DataType.getDataType(type.getValueType());
TypeInfo vt = v.getType();
if (dt.supportsPrecision && type.getPrecision() != vt.getPrecision()
|| dt.supportsScale && type.getScale() != vt.getScale()) {
return this;
}
}
......
......@@ -27,7 +27,6 @@ import org.h2.schema.Sequence;
import org.h2.util.MathUtils;
import org.h2.util.StringUtils;
import org.h2.value.DataType;
import org.h2.value.ExtTypeInfo;
import org.h2.value.TypeInfo;
import org.h2.value.Value;
import org.h2.value.ValueInt;
......@@ -88,22 +87,7 @@ public class Column {
private Domain domain;
public Column(String name, int valueType) {
this(name, valueType, -1, -1, -1, null);
}
public Column(String name, int valueType, long precision, int scale, int displaySize) {
this(name, valueType, precision, scale, displaySize, null);
}
public Column(String name, int valueType, long precision, int scale, int displaySize, ExtTypeInfo extTypeInfo) {
this.name = name;
if (precision == -1 && scale == -1 && displaySize == -1 && valueType != Value.UNKNOWN) {
DataType dt = DataType.getDataType(valueType);
precision = dt.defaultPrecision;
scale = dt.defaultScale;
displaySize = dt.defaultDisplaySize;
}
this.type = new TypeInfo(valueType, precision, scale, displaySize, extTypeInfo);
this(name, TypeInfo.getTypeInfo(valueType));
}
public Column(String name, TypeInfo type) {
......
......@@ -28,11 +28,11 @@ import org.h2.message.DbException;
import org.h2.result.Row;
import org.h2.result.RowList;
import org.h2.schema.Schema;
import org.h2.util.MathUtils;
import org.h2.util.StatementBuilder;
import org.h2.util.StringUtils;
import org.h2.util.Utils;
import org.h2.value.DataType;
import org.h2.value.TypeInfo;
import org.h2.value.Value;
import org.h2.value.ValueDate;
import org.h2.value.ValueTime;
......@@ -156,9 +156,8 @@ public class TableLink extends Table {
precision = convertPrecision(sqlType, precision);
int scale = rs.getInt("DECIMAL_DIGITS");
scale = convertScale(sqlType, scale);
int displaySize = MathUtils.convertLongToInt(precision);
int type = DataType.convertSQLTypeToValueType(sqlType, sqlTypeName);
Column col = new Column(n, type, precision, scale, displaySize);
Column col = new Column(n, TypeInfo.getTypeInfo(type, precision, scale, null));
col.setTable(this, i++);
columnList.add(col);
columnMap.put(n, col);
......@@ -185,9 +184,8 @@ public class TableLink extends Table {
precision = convertPrecision(sqlType, precision);
int scale = rsMeta.getScale(i + 1);
scale = convertScale(sqlType, scale);
int displaySize = rsMeta.getColumnDisplaySize(i + 1);
int type = DataType.getValueTypeFromResultSet(rsMeta, i + 1);
Column col = new Column(n, type, precision, scale, displaySize);
Column col = new Column(n, TypeInfo.getTypeInfo(type, precision, scale, null));
col.setTable(this, i++);
columnList.add(col);
columnMap.put(n, col);
......
......@@ -38,8 +38,6 @@ import org.h2.util.ColumnNamer;
import org.h2.util.StatementBuilder;
import org.h2.util.StringUtils;
import org.h2.util.Utils;
import org.h2.value.DataType;
import org.h2.value.ExtTypeInfo;
import org.h2.value.TypeInfo;
import org.h2.value.Value;
......@@ -182,29 +180,19 @@ public class TableView extends Table {
for (int i = 0; i < count; i++) {
Expression expr = expressions.get(i);
String name = null;
int valueType = Value.UNKNOWN;
TypeInfo type = TypeInfo.TYPE_UNKNOWN;
if (columnTemplates != null && columnTemplates.length > i) {
name = columnTemplates[i].getName();
valueType = columnTemplates[i].getType().getValueType();
type = columnTemplates[i].getType();
}
if (name == null) {
name = expr.getAlias();
}
name = columnNamer.getColumnName(expr, i, name);
if (valueType == Value.UNKNOWN) {
valueType = expr.getType().getValueType();
}
TypeInfo type = expr.getType();
long precision = type.getPrecision();
int scale = type.getScale();
int displaySize = type.getDisplaySize();
ExtTypeInfo extTypeInfo = null;
if (DataType.isExtInfoType(valueType)) {
if (expr instanceof ExpressionColumn) {
extTypeInfo = ((ExpressionColumn) expr).getColumn().getType().getExtTypeInfo();
}
if (type.getValueType() == Value.UNKNOWN) {
type = expr.getType();
}
Column col = new Column(name, valueType, precision, scale, displaySize, extTypeInfo);
Column col = new Column(name, type);
col.setTable(this, i);
// Fetch check constraint from view column source
ExpressionColumn fromColumn = null;
......
......@@ -257,29 +257,36 @@ public class TypeInfo {
if (scale < 0) {
scale = ValueDecimal.DEFAULT_SCALE;
}
if (precision < scale) {
precision = scale;
}
return new TypeInfo(Value.DECIMAL, precision, scale, MathUtils.convertLongToInt(precision + 2), null);
case Value.TIME:
case Value.TIME: {
if (scale < 0 || scale >= ValueTime.MAXIMUM_SCALE) {
return TYPE_TIME;
}
return new TypeInfo(Value.TIME, ValueTime.MAXIMUM_PRECISION, scale, ValueTime.DEFAULT_PRECISION, null);
case Value.TIMESTAMP:
int d = scale == 0 ? 8 : 9 + scale;
return new TypeInfo(Value.TIME, d, scale, d, null);
}
case Value.TIMESTAMP: {
if (scale < 0 || scale >= ValueTimestamp.MAXIMUM_SCALE) {
return TYPE_TIMESTAMP;
}
return new TypeInfo(Value.TIMESTAMP, ValueTimestamp.MAXIMUM_PRECISION, scale,
ValueTimestamp.MAXIMUM_PRECISION, null);
case Value.TIMESTAMP_TZ:
int d = scale == 0 ? 19 : 20 + scale;
return new TypeInfo(Value.TIMESTAMP, d, scale, d, null);
}
case Value.TIMESTAMP_TZ: {
if (scale < 0 || scale >= ValueTimestampTimeZone.MAXIMUM_SCALE) {
return TYPE_TIMESTAMP_TZ;
}
return new TypeInfo(Value.TIMESTAMP_TZ, ValueTimestampTimeZone.MAXIMUM_PRECISION, scale,
ValueTimestampTimeZone.MAXIMUM_PRECISION, null);
int d = scale == 0 ? 25 : 26 + scale;
return new TypeInfo(Value.TIMESTAMP_TZ, d, scale, d, null);
}
case Value.BYTES:
if (precision < 0) {
precision = Integer.MAX_VALUE;
}
return new TypeInfo(Value.BYTES, precision, scale, MathUtils.convertLongToInt(precision) * 2, null);
return new TypeInfo(Value.BYTES, precision, 0, MathUtils.convertLongToInt(precision) * 2, null);
case Value.STRING:
if (precision < 0) {
return TYPE_STRING_DEFAULT;
......
......@@ -39,16 +39,6 @@ public class ValueTime extends Value {
*/
public static final int MAXIMUM_SCALE = 9;
/**
* Get display size for the specified scale.
*
* @param scale scale
* @return display size
*/
public static int getDisplaySize(int scale) {
return scale == 0 ? 8 : 9 + scale;
}
/**
* Nanoseconds since midnight
*/
......
......@@ -40,16 +40,6 @@ public class ValueTimestamp extends Value {
*/
public static final int MAXIMUM_SCALE = 9;
/**
* Get display size for the specified scale.
*
* @param scale scale
* @return display size
*/
public static int getDisplaySize(int scale) {
return scale == 0 ? 19 : 20 + scale;
}
/**
* A bit field with bits for the year, month, and day (see DateTimeUtils for
* encoding)
......
......@@ -45,16 +45,6 @@ public class ValueTimestampTimeZone extends Value {
*/
static final int MAXIMUM_SCALE = ValueTimestamp.MAXIMUM_SCALE;
/**
* Get display size for the specified scale.
*
* @param scale scale
* @return display size
*/
public static int getDisplaySize(int scale) {
return scale == 0 ? 25 : 26 + scale;
}
/**
* A bit field with bits for the year, month, and day (see DateTimeUtils for
* encoding)
......
......@@ -356,7 +356,7 @@ public class TestCases extends TestDb {
Statement stat = conn.createStatement();
stat.execute("create table test as select cast(0 as dec(10, 2)) x");
ResultSetMetaData meta = stat.executeQuery("select * from test").getMetaData();
assertEquals(2, meta.getPrecision(1));
assertEquals(10, meta.getPrecision(1));
assertEquals(2, meta.getScale(1));
stat.execute("alter table test add column y int");
stat.execute("drop table test");
......
......@@ -253,8 +253,8 @@ SELECT * FROM V3;
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'E' ORDER BY TABLE_NAME;
> TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME ORDINAL_POSITION DOMAIN_CATALOG DOMAIN_SCHEMA DOMAIN_NAME COLUMN_DEFAULT IS_NULLABLE DATA_TYPE CHARACTER_MAXIMUM_LENGTH CHARACTER_OCTET_LENGTH NUMERIC_PRECISION NUMERIC_PRECISION_RADIX NUMERIC_SCALE DATETIME_PRECISION INTERVAL_TYPE INTERVAL_PRECISION CHARACTER_SET_NAME COLLATION_NAME TYPE_NAME NULLABLE IS_COMPUTED SELECTIVITY CHECK_CONSTRAINT SEQUENCE_NAME REMARKS SOURCE_DATA_TYPE COLUMN_TYPE COLUMN_ON_UPDATE IS_VISIBLE
> ------------- ------------ ---------- ----------- ---------------- -------------- ------------- ----------- -------------- ----------- --------- ------------------------ ---------------------- ----------------- ----------------------- ------------- ------------------ ------------- ------------------ ------------------ -------------- --------- -------- ----------- ----------- ---------------- ------------- ------- ---------------- -------------- ---------------- ----------
> SCRIPT PUBLIC TEST E 1 null null null null YES 1111 2147483647 2147483647 2147483647 10 0 null null null Unicode OFF ENUM 1 FALSE 50 null null ENUM('A', 'B') null TRUE
> SCRIPT PUBLIC V E 1 null null null null YES 1111 2147483647 2147483647 2147483647 10 0 null null null Unicode OFF ENUM 1 FALSE 50 null null ENUM('A', 'B') null TRUE
> SCRIPT PUBLIC TEST E 1 null null null null YES 1111 10 10 10 10 0 null null null Unicode OFF ENUM 1 FALSE 50 null null ENUM('A', 'B') null TRUE
> SCRIPT PUBLIC V E 1 null null null null YES 1111 10 10 10 10 0 null null null Unicode OFF ENUM 1 FALSE 50 null null ENUM('A', 'B') null TRUE
> SCRIPT PUBLIC V1 E 1 null null null null YES 4 10 10 10 10 0 null null null Unicode OFF INTEGER 1 FALSE 50 null null INTEGER null TRUE
> SCRIPT PUBLIC V2 E 1 null null null null YES 4 10 10 10 10 0 null null null Unicode OFF INTEGER 1 FALSE 50 null null INTEGER null TRUE
> SCRIPT PUBLIC V3 E 1 null null null null YES 4 10 10 10 10 0 null null null Unicode OFF INTEGER 1 FALSE 50 null null INTEGER null TRUE
......
......@@ -6084,17 +6084,17 @@ SELECT ID FROM TEST WHERE XVI LIKE 'abc%';
> 3
> rows: 1
SELECT 'abc', 'Papa Joe''s', CAST(-1 AS SMALLINT), CAST(2 AS BIGINT), CAST(0 AS DOUBLE), CAST('0a0f' AS BINARY), CAST(125 AS TINYINT), TRUE, FALSE FROM TEST WHERE ID=1;
> 'abc' 'Papa Joe''s' -1 2 0.0 X'0a0f' 125 TRUE FALSE
> ----- ------------- -- - --- ------- --- ---- -----
SELECT 'abc', 'Papa Joe''s', CAST(-1 AS SMALLINT), CAST(2 AS BIGINT), CAST(0 AS DOUBLE), CAST('0a0f' AS BINARY) B, CAST(125 AS TINYINT), TRUE, FALSE FROM TEST WHERE ID=1;
> 'abc' 'Papa Joe''s' -1 2 0.0 B 125 TRUE FALSE
> ----- ------------- -- - --- ---- --- ---- -----
> abc Papa Joe's -1 2 0.0 0a0f 125 TRUE FALSE
> rows: 1
-- ' This apostrophe is here to fix syntax highlighting in the text editors.
SELECT CAST('abcd' AS VARCHAR(255)), CAST('ef_gh' AS VARCHAR(3));
> 'abcd' 'ef_'
> ------ -----
SELECT CAST('abcd' AS VARCHAR(255)) C1, CAST('ef_gh' AS VARCHAR(3)) C2;
> C1 C2
> ---- ---
> abcd ef_
> rows: 1
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论