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

store enums as ints

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