提交 2b755214 authored 作者: Max Englander's avatar Max Englander

Enum type does not support precision or scale

上级 354a8a9f
......@@ -4058,6 +4058,7 @@ public class Parser {
}
long precision = -1;
int displaySize = -1;
java.util.Set<String> permittedValues = new HashSet<>();
int scale = -1;
String comment = null;
Column templateColumn = null;
......@@ -4131,6 +4132,13 @@ public class Parser {
}
read(")");
}
} else if (dataType.supportsPermittedValues) {
if (readIf("(")) {
permittedValues.add(readString());
while(readIf(","))
readString();
read(")");
}
} else if (readIf("(")) {
// Support for MySQL: INT(11), MEDIUMINT(8) and so on.
// Just ignore the precision.
......@@ -4151,8 +4159,13 @@ public class Parser {
throw DbException.get(ErrorCode.INVALID_VALUE_SCALE_PRECISION,
Integer.toString(scale), Long.toString(precision));
}
Column column = new Column(columnName, type, precision, scale,
Column column;
if (permittedValues.isEmpty()) {
column = new Column(columnName, type, precision, scale,
displaySize);
} else {
column = new Column(columnName, type, permittedValues);
}
if (templateColumn != null) {
column.setNullable(templateColumn.isNullable());
column.setDefaultExpression(session,
......
......@@ -13,6 +13,7 @@ import org.h2.table.TableFilter;
import org.h2.value.Value;
import org.h2.value.ValueArray;
import org.h2.value.ValueBoolean;
import org.h2.value.ValueEnum;
import org.h2.value.ValueNull;
/**
......
......@@ -6,6 +6,7 @@
package org.h2.table;
import java.sql.ResultSetMetaData;
import java.util.Set;
import org.h2.api.ErrorCode;
import org.h2.command.Parser;
import org.h2.engine.Constants;
......@@ -66,6 +67,7 @@ public class Column {
private final int type;
private long precision;
private int scale;
private Set<String> permittedValues;
private int displaySize;
private Table table;
private String name;
......@@ -88,11 +90,20 @@ public class Column {
private boolean primaryKey;
public Column(String name, int type) {
this(name, type, -1, -1, -1);
this(name, type, -1, -1, -1, null);
}
public Column(String name, int type, Set<String> permittedValues) {
this(name, type, -1, -1, -1, permittedValues);
}
public Column(String name, int type, long precision, int scale,
int displaySize) {
this(name, type, precision, scale, displaySize, null);
}
public Column(String name, int type, long precision, int scale,
int displaySize, Set<String> permittedValues) {
this.name = name;
this.type = type;
if (precision == -1 && scale == -1 && displaySize == -1 && type != Value.UNKNOWN) {
......@@ -104,6 +115,7 @@ public class Column {
this.precision = precision;
this.scale = scale;
this.displaySize = displaySize;
this.permittedValues = permittedValues;
}
@Override
......@@ -133,7 +145,7 @@ public class Column {
}
public Column getClone() {
Column newColumn = new Column(name, type, precision, scale, displaySize);
Column newColumn = new Column(name, type, precision, scale, displaySize, permittedValues);
newColumn.copy(this);
return newColumn;
}
......@@ -257,6 +269,14 @@ public class Column {
nullable = b;
}
public Set<String> getPermittedValues() {
return permittedValues;
}
public void setPermittedValues(Set<String> permittedValues) {
this.permittedValues = permittedValues;
}
/**
* Validate the value, convert it if required, and update the sequence value
* if required. If the value is null, the default value (NULL if no default
......@@ -733,6 +753,7 @@ public class Column {
displaySize = source.displaySize;
name = source.name;
precision = source.precision;
permittedValues = source.permittedValues;
scale = source.scale;
// table is not set
// columnId is not set
......
......@@ -140,6 +140,11 @@ public class DataType {
*/
public boolean caseSensitive;
/**
* If permitted values are supports.
*/
public boolean supportsPermittedValues;
/**
* If the precision parameter is supported.
*/
......@@ -380,6 +385,11 @@ public class DataType {
new String[]{"RESULT_SET"},
400
);
add(Value.ENUM, Types.OTHER, "Enum",
createEnum(),
new String[]{"ENUM"},
48
);
for (Integer i : TYPES_BY_VALUE_TYPE.keySet()) {
Value.getOrder(i);
}
......@@ -401,6 +411,7 @@ public class DataType {
dt.params = dataType.params;
dt.prefix = dataType.prefix;
dt.suffix = dataType.suffix;
dt.supportsPermittedValues = dataType.supportsPermittedValues;
dt.supportsPrecision = dataType.supportsPrecision;
dt.supportsScale = dataType.supportsScale;
dt.defaultPrecision = dataType.defaultPrecision;
......@@ -454,6 +465,13 @@ public class DataType {
return dataType;
}
private static DataType createEnum() {
DataType dataType = createString(false);
dataType.supportsPermittedValues = true;
dataType.supportsPrecision = false;
dataType.supportsScale = false;
return dataType;
}
private static DataType createString(boolean caseSensitive) {
DataType dataType = new DataType();
dataType.prefix = "'";
......@@ -664,6 +682,13 @@ public class DataType {
v = ValueArray.get(values);
break;
}
case Value.ENUM: {
Object x = rs.getObject(columnIndex);
if (x == null) {
return ValueNull.INSTANCE;
}
return ValueEnum.get((String)x);
}
case Value.RESULT_SET: {
ResultSet x = (ResultSet) rs.getObject(columnIndex);
if (x == null) {
......@@ -998,6 +1023,9 @@ public class DataType {
return ValueJavaObject.getNoCopy(x, null, session.getDataHandler());
}
if (x instanceof String) {
if (type == Value.ENUM) {
return ValueEnum.get((String) x);
}
return ValueString.get((String) x);
} else if (x instanceof Value) {
return (Value) x;
......@@ -1145,8 +1173,8 @@ public class DataType {
* @return true if the value type is a String type
*/
public static boolean isStringType(int type) {
if (type == Value.STRING || type == Value.STRING_FIXED
|| type == Value.STRING_IGNORECASE) {
if (type == Value.ENUM || type == Value.STRING
|| type == Value.STRING_FIXED || type == Value.STRING_IGNORECASE) {
return true;
}
return false;
......
......@@ -166,6 +166,11 @@ public abstract class Value {
*/
public static final int TIMESTAMP_TZ = 24;
/**
* The value type for ENUM values.
*/
public static final int ENUM = 25;
/**
* The number of value types.
*/
......@@ -321,6 +326,8 @@ public abstract class Value {
return 50;
case RESULT_SET:
return 51;
case ENUM:
return 52;
default:
throw DbException.throwInternalError("type:"+type);
}
......@@ -965,6 +972,8 @@ public abstract class Value {
return ValueUuid.get(s);
case GEOMETRY:
return ValueGeometry.get(s);
case ENUM:
return ValueEnum.get(s);
default:
throw DbException.throwInternalError("type=" + targetType);
}
......
/*
* Copyright 2004-2014 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;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.h2.engine.SysProperties;
import org.h2.util.MathUtils;
import org.h2.util.StringUtils;
/**
* Implementation of the ENUM data type.
*/
public class ValueEnum extends ValueString {
protected ValueEnum(String value) {
super(value);
}
@Override
public boolean equals(Object other) {
return other instanceof ValueEnum
&& value.equals(((ValueEnum) other).value);
}
@Override
protected int compareSecure(Value o, CompareMode mode) {
// compatibility: the other object could be another type
ValueEnum v = (ValueEnum) o;
return mode.compareString(value, v.value, false);
}
@Override
public int getType() {
return Value.ENUM;
}
/**
* Create a new String value of the current class.
* This method is meant to be overridden by subclasses.
*
* @param s the string
* @return the value
*/
protected Value getNew(String s) {
return ValueEnum.get(s);
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论