提交 9015bc1e authored 作者: Max Englander's avatar Max Englander

store enums as ints

上级 ef597352
...@@ -4122,7 +4122,7 @@ public class Parser { ...@@ -4122,7 +4122,7 @@ public class Parser {
} }
long precision = -1; long precision = -1;
int displaySize = -1; int displaySize = -1;
java.util.Set<String> permittedValues = new HashSet<>(); java.util.List<String> enumerators = new ArrayList<String>();
int scale = -1; int scale = -1;
String comment = null; String comment = null;
Column templateColumn = null; Column templateColumn = null;
...@@ -4196,11 +4196,11 @@ public class Parser { ...@@ -4196,11 +4196,11 @@ public class Parser {
} }
read(")"); read(")");
} }
} else if (dataType.supportsPermittedValues) { } else if (dataType.enumerated) {
if (readIf("(")) { if (readIf("(")) {
permittedValues.add(readString()); enumerators.add(readString());
while(readIf(",")) while(readIf(","))
permittedValues.add(readString()); enumerators.add(readString());
read(")"); read(")");
} }
} else if (readIf("(")) { } else if (readIf("(")) {
...@@ -4223,13 +4223,8 @@ public class Parser { ...@@ -4223,13 +4223,8 @@ 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; Column column = new Column(columnName, type, precision, scale,
if (permittedValues.isEmpty()) { displaySize, enumerators);
column = new Column(columnName, type, precision, scale,
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,
......
...@@ -7,7 +7,7 @@ package org.h2.table; ...@@ -7,7 +7,7 @@ package org.h2.table;
import java.sql.ResultSetMetaData; import java.sql.ResultSetMetaData;
import java.util.Iterator; import java.util.Iterator;
import java.util.Set; import java.util.List;
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;
...@@ -68,7 +68,7 @@ public class Column { ...@@ -68,7 +68,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 List<String> enumerators;
private int displaySize; private int displaySize;
private Table table; private Table table;
private String name; private String name;
...@@ -94,8 +94,8 @@ public class Column { ...@@ -94,8 +94,8 @@ public class Column {
this(name, type, -1, -1, -1, null); this(name, type, -1, -1, -1, null);
} }
public Column(String name, int type, Set<String> permittedValues) { public Column(String name, int type, List<String> enumerators) {
this(name, type, -1, -1, -1, permittedValues); this(name, type, -1, -1, -1, enumerators);
} }
public Column(String name, int type, long precision, int scale, public Column(String name, int type, long precision, int scale,
...@@ -104,7 +104,7 @@ public class Column { ...@@ -104,7 +104,7 @@ public class Column {
} }
public Column(String name, int type, long precision, int scale, public Column(String name, int type, long precision, int scale,
int displaySize, Set<String> permittedValues) { int displaySize, List<String> enumerators) {
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) {
...@@ -116,7 +116,7 @@ public class Column { ...@@ -116,7 +116,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; this.enumerators = enumerators;
} }
@Override @Override
...@@ -146,7 +146,7 @@ public class Column { ...@@ -146,7 +146,7 @@ public class Column {
} }
public Column getClone() { public Column getClone() {
Column newColumn = new Column(name, type, precision, scale, displaySize, permittedValues); Column newColumn = new Column(name, type, precision, scale, displaySize, enumerators);
newColumn.copy(this); newColumn.copy(this);
return newColumn; return newColumn;
} }
...@@ -270,12 +270,12 @@ public class Column { ...@@ -270,12 +270,12 @@ public class Column {
nullable = b; nullable = b;
} }
public Set<String> getPermittedValues() { public List<String> getEnumerators() {
return permittedValues; return enumerators;
} }
public void setPermittedValues(Set<String> permittedValues) { public void setEnumerators(List<String> enumerators) {
this.permittedValues = permittedValues; this.enumerators = enumerators;
} }
/** /**
...@@ -357,14 +357,23 @@ public class Column { ...@@ -357,14 +357,23 @@ public class Column {
getCreateSQL(), s + " (" + value.getPrecision() + ")"); getCreateSQL(), s + " (" + value.getPrecision() + ")");
} }
} }
if (permittedValues != null) { if (!enumerators.isEmpty()) {
if (!value.checkPermitted(permittedValues)) { int index;
if (DataType.isStringType(value.getType())) {
index = enumerators.indexOf(value.getString());
} else {
index = value.getInt() < enumerators.size() ? value.getInt() : -1;
}
if (index == -1) {
String s = value.getTraceSQL(); String s = value.getTraceSQL();
if (s.length() > 127) { if (s.length() > 127) {
s = s.substring(0, 128) + "..."; s = s.substring(0, 128) + "...";
} }
throw DbException.get(ErrorCode.VALUE_NOT_PERMITTED, throw DbException.get(ErrorCode.VALUE_NOT_PERMITTED,
getCreateSQL(), s + " (" + value.getString() + ")"); getCreateSQL(), s + " (" + value.getString() + ")");
} else {
value = ValueInt.get(index);
} }
} }
updateSequenceIfRequired(session, value); updateSequenceIfRequired(session, value);
...@@ -458,7 +467,7 @@ public class Column { ...@@ -458,7 +467,7 @@ public class Column {
break; break;
case Value.ENUM: case Value.ENUM:
buff.append('('); buff.append('(');
Iterator<String> it = permittedValues.iterator(); Iterator<String> it = enumerators.iterator();
while(it.hasNext()) { while(it.hasNext()) {
buff.append('\'').append(it.next()).append('\''); buff.append('\'').append(it.next()).append('\'');
if(it.hasNext()) { if(it.hasNext()) {
...@@ -774,7 +783,7 @@ public class Column { ...@@ -774,7 +783,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; enumerators = source.enumerators;
scale = source.scale; scale = source.scale;
// table is not set // table is not set
// columnId is not set // columnId is not set
......
...@@ -143,7 +143,7 @@ public class DataType { ...@@ -143,7 +143,7 @@ public class DataType {
/** /**
* If permitted values are supports. * If permitted values are supports.
*/ */
public boolean supportsPermittedValues; public boolean enumerated;
/** /**
* If the precision parameter is supported. * If the precision parameter is supported.
...@@ -416,7 +416,7 @@ public class DataType { ...@@ -416,7 +416,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.enumerated = dataType.enumerated;
dt.supportsPrecision = dataType.supportsPrecision; dt.supportsPrecision = dataType.supportsPrecision;
dt.supportsScale = dataType.supportsScale; dt.supportsScale = dataType.supportsScale;
dt.defaultPrecision = dataType.defaultPrecision; dt.defaultPrecision = dataType.defaultPrecision;
...@@ -472,7 +472,7 @@ public class DataType { ...@@ -472,7 +472,7 @@ public class DataType {
private static DataType createEnum() { private static DataType createEnum() {
DataType dataType = createString(false); DataType dataType = createString(false);
dataType.supportsPermittedValues = true; dataType.enumerated = true;
dataType.supportsPrecision = false; dataType.supportsPrecision = false;
dataType.supportsScale = false; dataType.supportsScale = false;
return dataType; return dataType;
...@@ -1183,7 +1183,7 @@ public class DataType { ...@@ -1183,7 +1183,7 @@ 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.ENUM || type == Value.STRING if (type == Value.STRING
|| type == Value.STRING_FIXED || type == Value.STRING_IGNORECASE) { || type == Value.STRING_FIXED || type == Value.STRING_IGNORECASE) {
return true; return true;
} }
......
...@@ -852,6 +852,11 @@ public abstract class Value { ...@@ -852,6 +852,11 @@ public abstract class Value {
} }
break; break;
} }
case ENUM:
switch (getType()) {
case INT:
return this;
}
case BLOB: { case BLOB: {
switch (getType()) { switch (getType()) {
case BYTES: case BYTES:
...@@ -944,6 +949,7 @@ public abstract class Value { ...@@ -944,6 +949,7 @@ public abstract class Value {
case JAVA_OBJECT: case JAVA_OBJECT:
return ValueJavaObject.getNoCopy(null, return ValueJavaObject.getNoCopy(null,
StringUtils.convertHexToBytes(s.trim()), getDataHandler()); StringUtils.convertHexToBytes(s.trim()), getDataHandler());
case ENUM:
case STRING: case STRING:
return ValueString.get(s); return ValueString.get(s);
case STRING_IGNORECASE: case STRING_IGNORECASE:
...@@ -973,8 +979,6 @@ public abstract class Value { ...@@ -973,8 +979,6 @@ 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);
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论