提交 41d238b1 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add TypeInfo and use it in Column

上级 734f8029
...@@ -226,6 +226,7 @@ import org.h2.value.DataType; ...@@ -226,6 +226,7 @@ import org.h2.value.DataType;
import org.h2.value.ExtTypeInfo; import org.h2.value.ExtTypeInfo;
import org.h2.value.ExtTypeInfoEnum; import org.h2.value.ExtTypeInfoEnum;
import org.h2.value.ExtTypeInfoGeometry; import org.h2.value.ExtTypeInfoGeometry;
import org.h2.value.TypeInfo;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueArray; import org.h2.value.ValueArray;
import org.h2.value.ValueBoolean; import org.h2.value.ValueBoolean;
...@@ -5355,13 +5356,14 @@ public class Parser { ...@@ -5355,13 +5356,14 @@ public class Parser {
Domain domain = database.findDomain(original); Domain domain = database.findDomain(original);
if (domain != null) { if (domain != null) {
templateColumn = domain.getColumn(); templateColumn = domain.getColumn();
dataType = DataType.getDataType(templateColumn.getType()); TypeInfo type = templateColumn.getType();
dataType = DataType.getDataType(type.getValueType());
comment = templateColumn.getComment(); comment = templateColumn.getComment();
original = forTable ? domain.getSQL() : templateColumn.getOriginalSQL(); original = forTable ? domain.getSQL() : templateColumn.getOriginalSQL();
precision = templateColumn.getPrecision(); precision = type.getPrecision();
displaySize = templateColumn.getDisplaySize(); displaySize = type.getDisplaySize();
scale = templateColumn.getScale(); scale = type.getScale();
extTypeInfo = templateColumn.getExtTypeInfo(); extTypeInfo = type.getExtTypeInfo();
} else { } else {
Mode mode = database.getMode(); Mode mode = database.getMode();
dataType = DataType.getTypeByName(original, mode); dataType = DataType.getTypeByName(original, mode);
...@@ -5863,10 +5865,11 @@ public class Parser { ...@@ -5863,10 +5865,11 @@ public class Parser {
.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH); .get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
} }
Column c = columns.get(i); Column c = columns.get(i);
type = Value.getHigherOrder(c.getType(), type); TypeInfo t = c.getType();
prec = Math.max(c.getPrecision(), prec); type = Value.getHigherOrder(t.getValueType(), type);
scale = Math.max(c.getScale(), scale); prec = Math.max(t.getPrecision(), prec);
displaySize = Math.max(c.getDisplaySize(), displaySize); scale = Math.max(t.getScale(), scale);
displaySize = Math.max(t.getDisplaySize(), displaySize);
column = new Column(columnName, type, prec, scale, displaySize); column = new Column(columnName, type, prec, scale, displaySize);
columns.set(i, column); columns.set(i, column);
row.add(expr); row.add(expr);
...@@ -5883,7 +5886,7 @@ public class Parser { ...@@ -5883,7 +5886,7 @@ public class Parser {
} }
for (int i = 0; i < columnCount; i++) { for (int i = 0; i < columnCount; i++) {
Column c = columns.get(i); Column c = columns.get(i);
if (c.getType() == Value.UNKNOWN) { if (c.getType().getValueType() == Value.UNKNOWN) {
c = new Column(c.getName(), Value.STRING, 0, 0, 0); c = new Column(c.getName(), Value.STRING, 0, 0, 0);
columns.set(i, c); columns.set(i, c);
} }
......
...@@ -105,7 +105,7 @@ public class Analyze extends DefineCommand { ...@@ -105,7 +105,7 @@ public class Analyze extends DefineCommand {
StatementBuilder buff = new StatementBuilder("SELECT "); StatementBuilder buff = new StatementBuilder("SELECT ");
for (Column col : columns) { for (Column col : columns) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
if (DataType.isLargeObject(col.getType())) { if (DataType.isLargeObject(col.getType().getValueType())) {
// can not index LOB columns, so calculating // can not index LOB columns, so calculating
// the selectivity is not required // the selectivity is not required
buff.append("MAX(NULL)"); buff.append("MAX(NULL)");
......
...@@ -202,7 +202,7 @@ public class CreateTable extends CommandWithColumns { ...@@ -202,7 +202,7 @@ public class CreateTable extends CommandWithColumns {
int t = dt.type; int t = dt.type;
if (DataType.isExtInfoType(t)) { if (DataType.isExtInfoType(t)) {
if (expr instanceof ExpressionColumn) { if (expr instanceof ExpressionColumn) {
extTypeInfo = ((ExpressionColumn) expr).getColumn().getExtTypeInfo(); extTypeInfo = ((ExpressionColumn) expr).getColumn().getType().getExtTypeInfo();
} else if (t == Value.ENUM) { } else if (t == Value.ENUM) {
/* /*
* Only columns of tables may be enumerated. * Only columns of tables may be enumerated.
......
...@@ -216,7 +216,7 @@ public class ExpressionColumn extends Expression { ...@@ -216,7 +216,7 @@ public class ExpressionColumn extends Expression {
} }
} }
if (value != ValueNull.INSTANCE) { if (value != ValueNull.INSTANCE) {
ExtTypeInfo extTypeInfo = column.getExtTypeInfo(); ExtTypeInfo extTypeInfo = column.getType().getExtTypeInfo();
if (extTypeInfo != null) { if (extTypeInfo != null) {
return extTypeInfo.cast(value); return extTypeInfo.cast(value);
} }
...@@ -226,7 +226,7 @@ public class ExpressionColumn extends Expression { ...@@ -226,7 +226,7 @@ public class ExpressionColumn extends Expression {
@Override @Override
public int getType() { public int getType() {
return column == null ? Value.UNKNOWN : column.getType(); return column == null ? Value.UNKNOWN : column.getType().getValueType();
} }
@Override @Override
...@@ -239,17 +239,17 @@ public class ExpressionColumn extends Expression { ...@@ -239,17 +239,17 @@ public class ExpressionColumn extends Expression {
@Override @Override
public int getScale() { public int getScale() {
return column.getScale(); return column.getType().getScale();
} }
@Override @Override
public long getPrecision() { public long getPrecision() {
return column.getPrecision(); return column.getType().getPrecision();
} }
@Override @Override
public int getDisplaySize() { public int getDisplaySize() {
return column.getDisplaySize(); return column.getType().getDisplaySize();
} }
public String getOriginalColumnName() { public String getOriginalColumnName() {
...@@ -360,7 +360,7 @@ public class ExpressionColumn extends Expression { ...@@ -360,7 +360,7 @@ public class ExpressionColumn extends Expression {
@Override @Override
public void createIndexConditions(Session session, TableFilter filter) { public void createIndexConditions(Session session, TableFilter filter) {
TableFilter tf = getTableFilter(); TableFilter tf = getTableFilter();
if (filter == tf && column.getType() == Value.BOOLEAN) { if (filter == tf && column.getType().getValueType() == Value.BOOLEAN) {
IndexCondition cond = IndexCondition.get( IndexCondition cond = IndexCondition.get(
Comparison.EQUAL, this, ValueExpression.get( Comparison.EQUAL, this, ValueExpression.get(
ValueBoolean.TRUE)); ValueBoolean.TRUE));
......
...@@ -66,7 +66,7 @@ public class Parameter extends Expression implements ParameterInterface { ...@@ -66,7 +66,7 @@ public class Parameter extends Expression implements ParameterInterface {
return value.getType(); return value.getType();
} }
if (column != null) { if (column != null) {
return column.getType(); return column.getType().getValueType();
} }
return Value.UNKNOWN; return Value.UNKNOWN;
} }
...@@ -114,7 +114,7 @@ public class Parameter extends Expression implements ParameterInterface { ...@@ -114,7 +114,7 @@ public class Parameter extends Expression implements ParameterInterface {
return value.getScale(); return value.getScale();
} }
if (column != null) { if (column != null) {
return column.getScale(); return column.getType().getScale();
} }
return 0; return 0;
} }
...@@ -125,7 +125,7 @@ public class Parameter extends Expression implements ParameterInterface { ...@@ -125,7 +125,7 @@ public class Parameter extends Expression implements ParameterInterface {
return value.getPrecision(); return value.getPrecision();
} }
if (column != null) { if (column != null) {
return column.getPrecision(); return column.getType().getPrecision();
} }
return 0; return 0;
} }
...@@ -136,7 +136,7 @@ public class Parameter extends Expression implements ParameterInterface { ...@@ -136,7 +136,7 @@ public class Parameter extends Expression implements ParameterInterface {
return value.getDisplaySize(); return value.getDisplaySize();
} }
if (column != null) { if (column != null) {
return column.getDisplaySize(); return column.getType().getDisplaySize();
} }
return 0; return 0;
} }
......
...@@ -38,7 +38,7 @@ class AggregateDataEnvelope extends AggregateData { ...@@ -38,7 +38,7 @@ class AggregateDataEnvelope extends AggregateData {
if (on instanceof ExpressionColumn) { if (on instanceof ExpressionColumn) {
ExpressionColumn col = (ExpressionColumn) on; ExpressionColumn col = (ExpressionColumn) on;
Column column = col.getColumn(); Column column = col.getColumn();
if (column.getType() == Value.GEOMETRY) { if (column.getType().getValueType() == Value.GEOMETRY) {
TableFilter filter = col.getTableFilter(); TableFilter filter = col.getTableFilter();
if (filter != null) { if (filter != null) {
ArrayList<Index> indexes = filter.getTable().getIndexes(); ArrayList<Index> indexes = filter.getTable().getIndexes();
......
...@@ -206,7 +206,7 @@ public class CompareLike extends Condition { ...@@ -206,7 +206,7 @@ public class CompareLike extends Condition {
// can't use an index // can't use an index
return; return;
} }
if (!DataType.isStringType(l.getColumn().getType())) { if (!DataType.isStringType(l.getColumn().getType().getValueType())) {
// column is not a varchar - can't use the index // column is not a varchar - can't use the index
return; return;
} }
......
...@@ -235,7 +235,7 @@ public class Comparison extends Condition { ...@@ -235,7 +235,7 @@ public class Comparison extends Condition {
Column column = ((ExpressionColumn) left).getColumn(); Column column = ((ExpressionColumn) left).getColumn();
right = ValueExpression.get(r.convertTo(resType, right = ValueExpression.get(r.convertTo(resType,
MathUtils.convertLongToInt(left.getPrecision()), MathUtils.convertLongToInt(left.getPrecision()),
session.getDatabase().getMode(), column, column.getExtTypeInfo())); session.getDatabase().getMode(), column, column.getType().getExtTypeInfo()));
} }
} else if (right instanceof Parameter) { } else if (right instanceof Parameter) {
((Parameter) right).setColumn( ((Parameter) right).setColumn(
......
...@@ -57,7 +57,7 @@ public class ConditionInConstantSet extends Condition { ...@@ -57,7 +57,7 @@ public class ConditionInConstantSet extends Condition {
type = left.getType(); type = left.getType();
Mode mode = database.getMode(); Mode mode = database.getMode();
if (type == Value.ENUM) { if (type == Value.ENUM) {
extTypeInfo = ((ExpressionColumn) left).getColumn().getExtTypeInfo(); extTypeInfo = ((ExpressionColumn) left).getColumn().getType().getExtTypeInfo();
for (Expression expression : valueList) { for (Expression expression : valueList) {
add(extTypeInfo.cast(expression.getValue(session))); add(extTypeInfo.cast(expression.getValue(session)));
} }
......
...@@ -62,6 +62,7 @@ import org.h2.util.StringUtils; ...@@ -62,6 +62,7 @@ import org.h2.util.StringUtils;
import org.h2.util.Utils; import org.h2.util.Utils;
import org.h2.value.DataType; import org.h2.value.DataType;
import org.h2.value.ExtTypeInfo; import org.h2.value.ExtTypeInfo;
import org.h2.value.TypeInfo;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueArray; import org.h2.value.ValueArray;
import org.h2.value.ValueBoolean; import org.h2.value.ValueBoolean;
...@@ -2293,11 +2294,12 @@ public class Function extends Expression implements FunctionCall { ...@@ -2293,11 +2294,12 @@ public class Function extends Expression implements FunctionCall {
} }
public void setDataType(Column col) { public void setDataType(Column col) {
dataType = col.getType(); TypeInfo type = col.getType();
precision = col.getPrecision(); dataType = type.getValueType();
displaySize = col.getDisplaySize(); precision = type.getPrecision();
scale = col.getScale(); displaySize = type.getDisplaySize();
extTypeInfo = col.getExtTypeInfo(); scale = type.getScale();
extTypeInfo = type.getExtTypeInfo();
} }
@Override @Override
......
...@@ -125,8 +125,8 @@ public class TableFunction extends Function { ...@@ -125,8 +125,8 @@ public class TableFunction extends Function {
Column c = columns[j]; Column c = columns[j];
v = l[row]; v = l[row];
if (!unnest) { if (!unnest) {
v = c.convert(v).convertPrecision(c.getPrecision(), false) v = c.convert(v).convertPrecision(c.getType().getPrecision(), false)
.convertScale(true, c.getScale()); .convertScale(true, c.getType().getScale());
} }
} }
r[j] = v; r[j] = v;
......
...@@ -73,7 +73,7 @@ public abstract class BaseIndex extends SchemaObjectBase implements Index { ...@@ -73,7 +73,7 @@ public abstract class BaseIndex extends SchemaObjectBase implements Index {
*/ */
protected static void checkIndexColumnTypes(IndexColumn[] columns) { protected static void checkIndexColumnTypes(IndexColumn[] columns) {
for (IndexColumn c : columns) { for (IndexColumn c : columns) {
if (DataType.isLargeObject(c.column.getType())) { if (DataType.isLargeObject(c.column.getType().getValueType())) {
throw DbException.getUnsupportedException( throw DbException.getUnsupportedException(
"Index on BLOB or CLOB column: " + c.column.getCreateSQL()); "Index on BLOB or CLOB column: " + c.column.getCreateSQL());
} }
......
...@@ -77,7 +77,7 @@ public class HashIndex extends BaseIndex { ...@@ -77,7 +77,7 @@ public class HashIndex extends BaseIndex {
* case we need to convert, otherwise the ValueHashMap will not find the * case we need to convert, otherwise the ValueHashMap will not find the
* result. * result.
*/ */
v = v.convertTo(tableData.getColumn(indexColumn).getType(), database.getMode()); v = v.convertTo(tableData.getColumn(indexColumn).getType().getValueType(), database.getMode());
Row result; Row result;
Long pos = rows.get(v); Long pos = rows.get(v);
if (pos == null) { if (pos == null) {
......
...@@ -64,7 +64,7 @@ public class LinkedCursor implements Cursor { ...@@ -64,7 +64,7 @@ public class LinkedCursor implements Cursor {
current = tableLink.getTemplateRow(); current = tableLink.getTemplateRow();
for (int i = 0; i < current.getColumnCount(); i++) { for (int i = 0; i < current.getColumnCount(); i++) {
Column col = tableLink.getColumn(i); Column col = tableLink.getColumn(i);
Value v = DataType.readValue(session, rs, i + 1, col.getType()); Value v = DataType.readValue(session, rs, i + 1, col.getType().getValueType());
current.setValue(i, v); current.setValue(i, v);
} }
return true; return true;
......
...@@ -22,6 +22,7 @@ import org.h2.table.TableFilter; ...@@ -22,6 +22,7 @@ import org.h2.table.TableFilter;
import org.h2.table.TableLink; import org.h2.table.TableLink;
import org.h2.util.StatementBuilder; import org.h2.util.StatementBuilder;
import org.h2.util.Utils; import org.h2.util.Utils;
import org.h2.value.TypeInfo;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueNull; import org.h2.value.ValueNull;
...@@ -131,12 +132,13 @@ public class LinkedIndex extends BaseIndex { ...@@ -131,12 +132,13 @@ public class LinkedIndex extends BaseIndex {
} }
private void addParameter(StatementBuilder buff, Column col) { private void addParameter(StatementBuilder buff, Column col) {
if (col.getType() == Value.STRING_FIXED && link.isOracle()) { TypeInfo type = col.getType();
if (type.getValueType() == Value.STRING_FIXED && link.isOracle()) {
// workaround for Oracle // workaround for Oracle
// create table test(id int primary key, name char(15)); // create table test(id int primary key, name char(15));
// insert into test values(1, 'Hello') // insert into test values(1, 'Hello')
// select * from test where name = ? -- where ? = "Hello" > no rows // select * from test where name = ? -- where ? = "Hello" > no rows
buff.append("CAST(? AS CHAR(").append(col.getPrecision()).append("))"); buff.append("CAST(? AS CHAR(").append(type.getPrecision()).append("))");
} else { } else {
buff.append('?'); buff.append('?');
} }
......
...@@ -101,7 +101,7 @@ public class NonUniqueHashIndex extends BaseIndex { ...@@ -101,7 +101,7 @@ public class NonUniqueHashIndex extends BaseIndex {
* case we need to convert, otherwise the ValueHashMap will not find the * case we need to convert, otherwise the ValueHashMap will not find the
* result. * result.
*/ */
v = v.convertTo(tableData.getColumn(indexColumn).getType(), database.getMode()); v = v.convertTo(tableData.getColumn(indexColumn).getType().getValueType(), database.getMode());
ArrayList<Long> positions = rows.get(v); ArrayList<Long> positions = rows.get(v);
return new NonUniqueHashCursor(session, tableData, positions); return new NonUniqueHashCursor(session, tableData, positions);
} }
......
...@@ -87,7 +87,7 @@ public class SpatialTreeIndex extends BaseIndex implements SpatialIndex { ...@@ -87,7 +87,7 @@ public class SpatialTreeIndex extends BaseIndex implements SpatialIndex {
} }
this.needRebuild = create; this.needRebuild = create;
if (!database.isStarting()) { if (!database.isStarting()) {
if (columns[0].column.getType() != Value.GEOMETRY) { if (columns[0].column.getType().getValueType() != Value.GEOMETRY) {
throw DbException.getUnsupportedException( throw DbException.getUnsupportedException(
"spatial index on non-geometry column, " + "spatial index on non-geometry column, " +
columns[0].column.getCreateSQL()); columns[0].column.getCreateSQL());
......
...@@ -30,6 +30,7 @@ import org.h2.table.Column; ...@@ -30,6 +30,7 @@ import org.h2.table.Column;
import org.h2.table.IndexColumn; import org.h2.table.IndexColumn;
import org.h2.table.TableFilter; import org.h2.table.TableFilter;
import org.h2.value.CompareMode; import org.h2.value.CompareMode;
import org.h2.value.TypeInfo;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueArray; import org.h2.value.ValueArray;
import org.h2.value.ValueLong; import org.h2.value.ValueLong;
...@@ -288,7 +289,8 @@ public final class MVSecondaryIndex extends BaseIndex implements MVIndex { ...@@ -288,7 +289,8 @@ public final class MVSecondaryIndex extends BaseIndex implements MVIndex {
int idx = c.getColumnId(); int idx = c.getColumnId();
Value v = r.getValue(idx); Value v = r.getValue(idx);
if (v != null) { if (v != null) {
array[i] = v.convertTo(c.getType(), -1, database.getMode(), null, c.getExtTypeInfo()); TypeInfo type = c.getType();
array[i] = v.convertTo(type.getValueType(), -1, database.getMode(), null, type.getExtTypeInfo());
} }
} }
array[keyColumns - 1] = key != null ? key : ValueLong.get(r.getKey()); array[keyColumns - 1] = key != null ? key : ValueLong.get(r.getKey());
......
...@@ -88,7 +88,7 @@ public class MVSpatialIndex extends BaseIndex implements SpatialIndex, MVIndex { ...@@ -88,7 +88,7 @@ public class MVSpatialIndex extends BaseIndex implements SpatialIndex, MVIndex {
throw DbException.getUnsupportedException( throw DbException.getUnsupportedException(
"Nulls last is not supported"); "Nulls last is not supported");
} }
if (col.column.getType() != Value.GEOMETRY) { if (col.column.getType().getValueType() != Value.GEOMETRY) {
throw DbException.getUnsupportedException( throw DbException.getUnsupportedException(
"Spatial index on non-geometry column, " "Spatial index on non-geometry column, "
+ col.column.getCreateSQL()); + col.column.getCreateSQL());
......
...@@ -136,7 +136,7 @@ public class MVTable extends TableBase { ...@@ -136,7 +136,7 @@ public class MVTable extends TableBase {
this.isHidden = data.isHidden; this.isHidden = data.isHidden;
boolean b = false; boolean b = false;
for (Column col : getColumns()) { for (Column col : getColumns()) {
if (DataType.isLargeObject(col.getType())) { if (DataType.isLargeObject(col.getType().getValueType())) {
b = true; b = true;
break; break;
} }
......
...@@ -28,6 +28,7 @@ import org.h2.util.MathUtils; ...@@ -28,6 +28,7 @@ import org.h2.util.MathUtils;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
import org.h2.value.DataType; import org.h2.value.DataType;
import org.h2.value.ExtTypeInfo; import org.h2.value.ExtTypeInfo;
import org.h2.value.TypeInfo;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueInt; import org.h2.value.ValueInt;
import org.h2.value.ValueLong; import org.h2.value.ValueLong;
...@@ -64,11 +65,7 @@ public class Column { ...@@ -64,11 +65,7 @@ public class Column {
public static final int NULLABLE_UNKNOWN = public static final int NULLABLE_UNKNOWN =
ResultSetMetaData.columnNullableUnknown; ResultSetMetaData.columnNullableUnknown;
private final int type; private final TypeInfo type;
private long precision;
private int scale;
private ExtTypeInfo extTypeInfo;
private int displaySize;
private Table table; private Table table;
private String name; private String name;
private int columnId; private int columnId;
...@@ -90,28 +87,28 @@ public class Column { ...@@ -90,28 +87,28 @@ public class Column {
private boolean visible = true; private boolean visible = true;
private Domain domain; private Domain domain;
public Column(String name, int type) { public Column(String name, int valueType) {
this(name, type, -1, -1, -1, null); this(name, valueType, -1, -1, -1, null);
} }
public Column(String name, int type, long precision, int scale, public Column(String name, int valueType, long precision, int scale, int displaySize) {
int displaySize) { this(name, valueType, precision, scale, displaySize, null);
this(name, type, precision, scale, displaySize, null);
} }
public Column(String name, int type, long precision, int scale, int displaySize, ExtTypeInfo extTypeInfo) { public Column(String name, int valueType, long precision, int scale, int displaySize, ExtTypeInfo extTypeInfo) {
this.name = name; this.name = name;
this.type = type; if (precision == -1 && scale == -1 && displaySize == -1 && valueType != Value.UNKNOWN) {
if (precision == -1 && scale == -1 && displaySize == -1 && type != Value.UNKNOWN) { DataType dt = DataType.getDataType(valueType);
DataType dt = DataType.getDataType(type);
precision = dt.defaultPrecision; precision = dt.defaultPrecision;
scale = dt.defaultScale; scale = dt.defaultScale;
displaySize = dt.defaultDisplaySize; displaySize = dt.defaultDisplaySize;
} }
this.precision = precision; this.type = new TypeInfo(valueType, precision, scale, displaySize, extTypeInfo);
this.scale = scale; }
this.displaySize = displaySize;
this.extTypeInfo = extTypeInfo; public Column(String name, TypeInfo type) {
this.name = name;
this.type = type;
} }
@Override @Override
...@@ -141,7 +138,7 @@ public class Column { ...@@ -141,7 +138,7 @@ public class Column {
} }
public Column getClone() { public Column getClone() {
Column newColumn = new Column(name, type, precision, scale, displaySize, extTypeInfo); Column newColumn = new Column(name, type);
newColumn.copy(this); newColumn.copy(this);
return newColumn; return newColumn;
} }
...@@ -167,7 +164,8 @@ public class Column { ...@@ -167,7 +164,8 @@ public class Column {
*/ */
public Value convert(Value v, Mode mode) { public Value convert(Value v, Mode mode) {
try { try {
return v.convertTo(type, MathUtils.convertLongToInt(precision), mode, this, extTypeInfo); return v.convertTo(type.getValueType(), MathUtils.convertLongToInt(type.getPrecision()), mode, this,
type.getExtTypeInfo());
} catch (DbException e) { } catch (DbException e) {
if (e.getErrorCode() == ErrorCode.DATA_CONVERSION_ERROR_1) { if (e.getErrorCode() == ErrorCode.DATA_CONVERSION_ERROR_1) {
String target = (table == null ? "" : table.getName() + ": ") + String target = (table == null ? "" : table.getName() + ": ") +
...@@ -271,30 +269,14 @@ public class Column { ...@@ -271,30 +269,14 @@ public class Column {
return name; return name;
} }
public int getType() { public TypeInfo getType() {
return type; return type;
} }
public long getPrecision() {
return precision;
}
public int getDisplaySize() {
return displaySize;
}
public int getScale() {
return scale;
}
public void setNullable(boolean b) { public void setNullable(boolean b) {
nullable = b; nullable = b;
} }
public ExtTypeInfo getExtTypeInfo() {
return extTypeInfo;
}
public boolean getVisible() { public boolean getVisible() {
return visible; return visible;
} }
...@@ -350,9 +332,10 @@ public class Column { ...@@ -350,9 +332,10 @@ public class Column {
} }
if (value == ValueNull.INSTANCE && !nullable) { if (value == ValueNull.INSTANCE && !nullable) {
if (mode.convertInsertNullToZero) { if (mode.convertInsertNullToZero) {
DataType dt = DataType.getDataType(type); int t = type.getValueType();
DataType dt = DataType.getDataType(t);
if (dt.decimal) { if (dt.decimal) {
value = ValueInt.get(0).convertTo(type); value = ValueInt.get(0).convertTo(t);
} else if (dt.type == Value.TIMESTAMP) { } else if (dt.type == Value.TIMESTAMP) {
value = session.getCurrentCommandStart().convertTo(Value.TIMESTAMP); value = session.getCurrentCommandStart().convertTo(Value.TIMESTAMP);
} else if (dt.type == Value.TIMESTAMP_TZ) { } else if (dt.type == Value.TIMESTAMP_TZ) {
...@@ -362,7 +345,7 @@ public class Column { ...@@ -362,7 +345,7 @@ public class Column {
} else if (dt.type == Value.DATE) { } else if (dt.type == Value.DATE) {
value = session.getCurrentCommandStart().convertTo(Value.DATE); value = session.getCurrentCommandStart().convertTo(Value.DATE);
} else { } else {
value = ValueString.get("").convertTo(type); value = ValueString.get("").convertTo(t);
} }
} else { } else {
throw DbException.get(ErrorCode.NULL_NOT_ALLOWED, name); throw DbException.get(ErrorCode.NULL_NOT_ALLOWED, name);
...@@ -382,7 +365,8 @@ public class Column { ...@@ -382,7 +365,8 @@ public class Column {
checkConstraint.getSQL()); checkConstraint.getSQL());
} }
} }
value = value.convertScale(mode.convertOnlyToSmallerScale, scale); value = value.convertScale(mode.convertOnlyToSmallerScale, type.getScale());
long precision = type.getPrecision();
if (precision > 0) { if (precision > 0) {
if (!value.checkPrecision(precision)) { if (!value.checkPrecision(precision)) {
String s = value.getTraceSQL(); String s = value.getTraceSQL();
...@@ -393,8 +377,8 @@ public class Column { ...@@ -393,8 +377,8 @@ public class Column {
getCreateSQL(), s + " (" + value.getPrecision() + ")"); getCreateSQL(), s + " (" + value.getPrecision() + ")");
} }
} }
if (value != ValueNull.INSTANCE && DataType.isExtInfoType(type) && extTypeInfo != null) { if (value != ValueNull.INSTANCE && DataType.isExtInfoType(type.getValueType()) && type.getExtTypeInfo() != null) {
value = extTypeInfo.cast(value); value = type.getExtTypeInfo().cast(value);
} }
updateSequenceIfRequired(session, value); updateSequenceIfRequired(session, value);
return value; return value;
...@@ -493,44 +477,7 @@ public class Column { ...@@ -493,44 +477,7 @@ public class Column {
if (originalSQL != null) { if (originalSQL != null) {
buff.append(originalSQL); buff.append(originalSQL);
} else { } else {
DataType dataType = DataType.getDataType(type); type.getSQL(buff);
if (type == Value.TIMESTAMP_TZ) {
buff.append("TIMESTAMP");
} else {
buff.append(dataType.name);
}
switch (type) {
case Value.DECIMAL:
buff.append('(').append(precision).append(", ").append(scale).append(')');
break;
case Value.GEOMETRY:
if (extTypeInfo == null) {
break;
}
//$FALL-THROUGH$
case Value.ENUM:
buff.append(extTypeInfo.getCreateSQL());
break;
case Value.BYTES:
case Value.STRING:
case Value.STRING_IGNORECASE:
case Value.STRING_FIXED:
if (precision < Integer.MAX_VALUE) {
buff.append('(').append(precision).append(')');
}
break;
case Value.TIME:
case Value.TIMESTAMP:
case Value.TIMESTAMP_TZ:
if (scale != dataType.defaultScale) {
buff.append('(').append(scale).append(')');
}
if (type == Value.TIMESTAMP_TZ) {
buff.append(" WITH TIME ZONE");
}
break;
default:
}
} }
if (!visible) { if (!visible) {
...@@ -729,11 +676,11 @@ public class Column { ...@@ -729,11 +676,11 @@ public class Column {
} }
int getPrecisionAsInt() { int getPrecisionAsInt() {
return MathUtils.convertLongToInt(precision); return MathUtils.convertLongToInt(type.getPrecision());
} }
DataType getDataType() { DataType getDataType() {
return DataType.getDataType(type); return DataType.getDataType(type.getValueType());
} }
/** /**
...@@ -803,10 +750,10 @@ public class Column { ...@@ -803,10 +750,10 @@ public class Column {
if (type != newColumn.type) { if (type != newColumn.type) {
return false; return false;
} }
if (precision > newColumn.precision) { if (type.getPrecision() > newColumn.type.getPrecision()) {
return false; return false;
} }
if (scale != newColumn.scale) { if (type.getScale() != newColumn.type.getScale()) {
return false; return false;
} }
if (nullable && !newColumn.nullable) { if (nullable && !newColumn.nullable) {
...@@ -836,7 +783,7 @@ public class Column { ...@@ -836,7 +783,7 @@ public class Column {
if (onUpdateExpression != null || newColumn.onUpdateExpression != null) { if (onUpdateExpression != null || newColumn.onUpdateExpression != null) {
return false; return false;
} }
if (!Objects.equals(extTypeInfo, newColumn.extTypeInfo)) { if (!Objects.equals(type.getExtTypeInfo(), newColumn.type.getExtTypeInfo())) {
return false; return false;
} }
return true; return true;
...@@ -850,11 +797,7 @@ public class Column { ...@@ -850,11 +797,7 @@ public class Column {
public void copy(Column source) { public void copy(Column source) {
checkConstraint = source.checkConstraint; checkConstraint = source.checkConstraint;
checkConstraintSQL = source.checkConstraintSQL; checkConstraintSQL = source.checkConstraintSQL;
displaySize = source.displaySize;
name = source.name; name = source.name;
precision = source.precision;
extTypeInfo = source.extTypeInfo;
scale = source.scale;
// table is not set // table is not set
// columnId is not set // columnId is not set
nullable = source.nullable; nullable = source.nullable;
......
...@@ -849,7 +849,7 @@ public class MetaTable extends Table { ...@@ -849,7 +849,7 @@ public class MetaTable extends Table {
Domain domain = c.getDomain(); Domain domain = c.getDomain();
DataType dataType = c.getDataType(); DataType dataType = c.getDataType();
ValueInt precision = ValueInt.get(c.getPrecisionAsInt()); ValueInt precision = ValueInt.get(c.getPrecisionAsInt());
ValueInt scale = ValueInt.get(c.getScale()); ValueInt scale = ValueInt.get(c.getType().getScale());
Sequence sequence = c.getSequence(); Sequence sequence = c.getSequence();
boolean hasDateTimePrecision; boolean hasDateTimePrecision;
int type = dataType.type; int type = dataType.type;
...@@ -1792,7 +1792,7 @@ public class MetaTable extends Table { ...@@ -1792,7 +1792,7 @@ public class MetaTable extends Table {
// PRECISION // PRECISION
ValueInt.get(col.getPrecisionAsInt()), ValueInt.get(col.getPrecisionAsInt()),
// SCALE // SCALE
ValueInt.get(col.getScale()), ValueInt.get(col.getType().getScale()),
// TYPE_NAME // TYPE_NAME
col.getDataType().name, col.getDataType().name,
// SELECTIVITY INT // SELECTIVITY INT
......
...@@ -78,7 +78,7 @@ public class RegularTable extends TableBase { ...@@ -78,7 +78,7 @@ public class RegularTable extends TableBase {
this.isHidden = data.isHidden; this.isHidden = data.isHidden;
boolean b = false; boolean b = false;
for (Column col : getColumns()) { for (Column col : getColumns()) {
if (DataType.isLargeObject(col.getType())) { if (DataType.isLargeObject(col.getType().getValueType())) {
b = true; b = true;
break; break;
} }
......
...@@ -454,7 +454,7 @@ public abstract class Table extends SchemaObjectBase { ...@@ -454,7 +454,7 @@ public abstract class Table extends SchemaObjectBase {
} }
for (int i = 0; i < columns.length; i++) { for (int i = 0; i < columns.length; i++) {
Column col = columns[i]; Column col = columns[i];
int dataType = col.getType(); int dataType = col.getType().getValueType();
if (dataType == Value.UNKNOWN) { if (dataType == Value.UNKNOWN) {
throw DbException.get( throw DbException.get(
ErrorCode.UNKNOWN_DATA_TYPE_1, col.getSQL()); ErrorCode.UNKNOWN_DATA_TYPE_1, col.getSQL());
......
...@@ -51,7 +51,7 @@ public abstract class TableBase extends Table { ...@@ -51,7 +51,7 @@ public abstract class TableBase extends Table {
if (first.sortType != SortOrder.ASCENDING) { if (first.sortType != SortOrder.ASCENDING) {
return SearchRow.ROWID_INDEX; return SearchRow.ROWID_INDEX;
} }
switch (first.column.getType()) { switch (first.column.getType().getValueType()) {
case Value.BYTE: case Value.BYTE:
case Value.SHORT: case Value.SHORT:
case Value.INT: case Value.INT:
......
...@@ -184,7 +184,7 @@ public class TableView extends Table { ...@@ -184,7 +184,7 @@ public class TableView extends Table {
int type = Value.UNKNOWN; int type = Value.UNKNOWN;
if (columnTemplates != null && columnTemplates.length > i) { if (columnTemplates != null && columnTemplates.length > i) {
name = columnTemplates[i].getName(); name = columnTemplates[i].getName();
type = columnTemplates[i].getType(); type = columnTemplates[i].getType().getValueType();
} }
if (name == null) { if (name == null) {
name = expr.getAlias(); name = expr.getAlias();
...@@ -199,7 +199,7 @@ public class TableView extends Table { ...@@ -199,7 +199,7 @@ public class TableView extends Table {
ExtTypeInfo extTypeInfo = null; ExtTypeInfo extTypeInfo = null;
if (DataType.isExtInfoType(type)) { if (DataType.isExtInfoType(type)) {
if (expr instanceof ExpressionColumn) { if (expr instanceof ExpressionColumn) {
extTypeInfo = ((ExpressionColumn) expr).getColumn().getExtTypeInfo(); extTypeInfo = ((ExpressionColumn) expr).getColumn().getType().getExtTypeInfo();
} }
} }
Column col = new Column(name, type, precision, scale, displaySize, extTypeInfo); Column col = new Column(name, type, precision, scale, displaySize, extTypeInfo);
......
/*
* Copyright 2004-2019 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.value;
/**
* Data type with parameters.
*/
public class TypeInfo {
private final int valueType;
private final long precision;
private final int scale;
private final int displaySize;
private final ExtTypeInfo extTypeInfo;
/**
* Creates new instance of data type with parameters.
*
* @param valueType
* the value type
* @param precision
* the precision
* @param scale
* the scale
* @param displaySize
* the display size in characters
* @param extTypeInfo
* the extended type information, or null
*/
public TypeInfo(int valueType, long precision, int scale, int displaySize, ExtTypeInfo extTypeInfo) {
this.valueType = valueType;
this.precision = precision;
this.scale = scale;
this.displaySize = displaySize;
this.extTypeInfo = extTypeInfo;
}
/**
* Returns the value type.
*
* @return the value type
*/
public int getValueType() {
return valueType;
}
/**
* Returns the precision.
*
* @return the precision
*/
public long getPrecision() {
return precision;
}
/**
* Returns the scale.
*
* @return the scale
*/
public int getScale() {
return scale;
}
/**
* Returns the display size in characters.
*
* @return the display size
*/
public int getDisplaySize() {
return displaySize;
}
/**
* Returns the extended type information, or null.
*
* @return the extended type information, or null
*/
public ExtTypeInfo getExtTypeInfo() {
return extTypeInfo;
}
/**
* Appends SQL representation of this object to the specified string
* builder.
*
* @param builder
* string builder
* @return the specified string builder
*/
public StringBuilder getSQL(StringBuilder builder) {
DataType dataType = DataType.getDataType(valueType);
if (valueType == Value.TIMESTAMP_TZ) {
builder.append("TIMESTAMP");
} else {
builder.append(dataType.name);
}
switch (valueType) {
case Value.DECIMAL:
builder.append('(').append(precision).append(", ").append(scale).append(')');
break;
case Value.GEOMETRY:
if (extTypeInfo == null) {
break;
}
//$FALL-THROUGH$
case Value.ENUM:
builder.append(extTypeInfo.getCreateSQL());
break;
case Value.BYTES:
case Value.STRING:
case Value.STRING_IGNORECASE:
case Value.STRING_FIXED:
if (precision < Integer.MAX_VALUE) {
builder.append('(').append(precision).append(')');
}
break;
case Value.TIME:
case Value.TIMESTAMP:
case Value.TIMESTAMP_TZ:
if (scale != dataType.defaultScale) {
builder.append('(').append(scale).append(')');
}
if (valueType == Value.TIMESTAMP_TZ) {
builder.append(" WITH TIME ZONE");
}
}
return builder;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论