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

Enum type does not support precision or scale

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