提交 e8d00a2c authored 作者: Max Englander's avatar Max Englander

enum-support: use toUpperCase(ENGLISH) instead of toLowerCase()

上级 2c903f83
...@@ -4128,7 +4128,6 @@ public class Parser { ...@@ -4128,7 +4128,6 @@ public class Parser {
} }
long precision = -1; long precision = -1;
int displaySize = -1; int displaySize = -1;
java.util.List<String> enumeratorList = null;
String[] enumerators = null; String[] enumerators = null;
int scale = -1; int scale = -1;
String comment = null; String comment = null;
...@@ -4205,22 +4204,21 @@ public class Parser { ...@@ -4205,22 +4204,21 @@ public class Parser {
} }
} else if (dataType.enumerated) { } else if (dataType.enumerated) {
if (readIf("(")) { if (readIf("(")) {
enumeratorList = new ArrayList<String>(); java.util.List<String> enumeratorList = new ArrayList<String>();
original += '('; original += '(';
String enumerator0 = readString(); String enumerator0 = readString();
enumeratorList.add(enumerator0.toLowerCase().trim()); enumeratorList.add(enumerator0);
original += "'" + enumerator0 + "'"; original += "'" + enumerator0 + "'";
while(readIf(",")) { while(readIf(",")) {
original += ','; original += ',';
String enumeratorN = readString(); String enumeratorN = readString();
original += "'" + enumeratorN + "'"; original += "'" + enumeratorN + "'";
enumeratorList.add(enumeratorN.toLowerCase().trim()); enumeratorList.add(enumeratorN);
} }
read(")"); read(")");
original += ')'; original += ')';
enumerators = enumeratorList.toArray(new String[enumeratorList.size()]);
} }
enumerators
= enumeratorList.toArray(new String[enumeratorList.size()]);
try { try {
ValueEnum.check(enumerators); ValueEnum.check(enumerators);
} catch(DbException e) { } catch(DbException e) {
...@@ -4249,7 +4247,7 @@ public class Parser { ...@@ -4249,7 +4247,7 @@ public class Parser {
Column column = new Column(columnName, type, precision, scale, Column column = new Column(columnName, type, precision, scale,
displaySize, enumerators == null ? null : enumerators); displaySize, enumerators);
if (templateColumn != null) { if (templateColumn != null) {
column.setNullable(templateColumn.isNullable()); column.setNullable(templateColumn.isNullable());
column.setDefaultExpression(session, column.setDefaultExpression(session,
......
...@@ -147,7 +147,7 @@ public class Column { ...@@ -147,7 +147,7 @@ public class Column {
} }
public boolean isEnumerated() { public boolean isEnumerated() {
return enumerators != null && enumerators.length > 0; return type == Value.ENUM;
} }
public Column getClone() { public Column getClone() {
......
...@@ -141,7 +141,7 @@ public class DataType { ...@@ -141,7 +141,7 @@ public class DataType {
public boolean caseSensitive; public boolean caseSensitive;
/** /**
* If permitted values are supports. * If enumerated values are supported.
*/ */
public boolean enumerated; public boolean enumerated;
......
package org.h2.value; package org.h2.value;
import java.util.Locale;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
...@@ -35,30 +37,6 @@ public class ValueEnum extends ValueEnumBase { ...@@ -35,30 +37,6 @@ public class ValueEnum extends ValueEnumBase {
} }
} }
private static final void check(final String[] enumerators, final String label) {
check(enumerators);
switch (validate(enumerators, label)) {
case VALID:
return;
default:
throw DbException.get(ErrorCode.ENUM_VALUE_NOT_PERMITTED_2,
toString(enumerators), "'" + label + "'");
}
}
private static final void check(final String[] enumerators, final int ordinal) {
check(enumerators);
switch (validate(enumerators, ordinal)) {
case VALID:
return;
default:
throw DbException.get(ErrorCode.ENUM_VALUE_NOT_PERMITTED_2,
toString(enumerators), Integer.toString(ordinal));
}
}
private static final void check(final String[] enumerators, final Value value) { private static final void check(final String[] enumerators, final Value value) {
check(enumerators); check(enumerators);
...@@ -77,29 +55,20 @@ public class ValueEnum extends ValueEnumBase { ...@@ -77,29 +55,20 @@ public class ValueEnum extends ValueEnumBase {
return MathUtils.compareInt(ordinal(), ev.ordinal()); return MathUtils.compareInt(ordinal(), ev.ordinal());
} }
public static ValueEnum get(final String[] enumerators, final String label) {
check(enumerators, label);
for (int i = 0; i < enumerators.length; i++) {
if (label.equals(enumerators[i]))
return new ValueEnum(enumerators, i);
}
throw DbException.get(ErrorCode.GENERAL_ERROR_1, "Unexpected error");
}
public static ValueEnum get(final String[] enumerators, final int ordinal) {
check(enumerators, ordinal);
return new ValueEnum(enumerators, ordinal);
}
public static ValueEnum get(final String[] enumerators, final Value value) { public static ValueEnum get(final String[] enumerators, final Value value) {
check(enumerators, value); check(enumerators, value);
if (DataType.isStringType(value.getType())) { if (DataType.isStringType(value.getType())) {
return get(enumerators, value.getString()); final String cleanLabel = sanitize(value.getString());
for (int i = 0; i < enumerators.length; i++) {
if (cleanLabel.equals(sanitize(enumerators[i])))
return new ValueEnum(enumerators, i);
}
throw DbException.get(ErrorCode.GENERAL_ERROR_1, "Unexpected error");
} else { } else {
return get(enumerators, value.getInt()); return new ValueEnum(enumerators, value.getInt());
} }
} }
...@@ -107,21 +76,24 @@ public class ValueEnum extends ValueEnumBase { ...@@ -107,21 +76,24 @@ public class ValueEnum extends ValueEnumBase {
return enumerators; return enumerators;
} }
@Override public static boolean isValid(final String enumerators[], final Value value) {
public int hashCode() { return validate(enumerators, value).equals(Validation.VALID);
return enumerators.hashCode() + ordinal();
} }
public static boolean isValid(final String enumerators[], final String label) { private static String sanitize(final String label) {
return validate(enumerators, label).equals(Validation.VALID); return label == null ? null : label.trim().toUpperCase(Locale.ENGLISH);
} }
public static boolean isValid(final String enumerators[], final int ordinal) { public static String[] sanitize(final String[] enumerators) {
return validate(enumerators, ordinal).equals(Validation.VALID); if (enumerators == null || enumerators.length == 0) return null;
}
public static boolean isValid(final String enumerators[], final Value value) { final String[] clean = new String[enumerators.length];
return validate(enumerators, value).equals(Validation.VALID);
for (int i = 0; i < enumerators.length; i++) {
clean[i] = sanitize(enumerators[i]);
}
return clean;
} }
private static String toString(final String[] enumerators) { private static String toString(final String[] enumerators) {
...@@ -136,33 +108,21 @@ public class ValueEnum extends ValueEnumBase { ...@@ -136,33 +108,21 @@ public class ValueEnum extends ValueEnumBase {
return result; return result;
} }
private static Validation validate(final String[] enumerators, final String label) {
check(enumerators);
final String cleanLabel = label.trim().toLowerCase();
for (int i = 0; i < enumerators.length; i++) {
if (cleanLabel.equals(enumerators[i])) {
return Validation.VALID;
}
}
return Validation.INVALID;
}
private static Validation validate(final String[] enumerators) { private static Validation validate(final String[] enumerators) {
if (enumerators == null || enumerators.length == 0) { final String[] cleaned = sanitize(enumerators);
if (cleaned == null || cleaned.length == 0) {
return Validation.EMPTY; return Validation.EMPTY;
} }
for (int i = 0; i < enumerators.length; i++) { for (int i = 0; i < cleaned.length; i++) {
if (enumerators[i] == null || enumerators[i].trim().equals("")) { if (cleaned[i] == null || cleaned[i].equals("")) {
return Validation.EMPTY; return Validation.EMPTY;
} }
if (i < enumerators.length - 1) { if (i < cleaned.length - 1) {
for (int j = i + 1; j < enumerators.length; j++) { for (int j = i + 1; j < cleaned.length; j++) {
if (enumerators[i].equals(enumerators[j])) { if (cleaned[i].equals(cleaned[j])) {
return Validation.DUPLICATE; return Validation.DUPLICATE;
} }
} }
...@@ -172,21 +132,30 @@ public class ValueEnum extends ValueEnumBase { ...@@ -172,21 +132,30 @@ public class ValueEnum extends ValueEnumBase {
return Validation.VALID; return Validation.VALID;
} }
private static Validation validate(final String[] enumerators, final int ordinal) { private static Validation validate(final String[] enumerators, final Value value) {
check(enumerators); final Validation validation = validate(enumerators);
if (!validation.equals(Validation.VALID)) {
if (ordinal < 0 || ordinal >= enumerators.length) { return validation;
return Validation.INVALID;
} }
return Validation.VALID;
}
private static Validation validate(final String[] enumerators, final Value value) {
if (DataType.isStringType(value.getType())) { if (DataType.isStringType(value.getType())) {
return validate(enumerators, value.getString()); final String cleanLabel = sanitize(value.getString());
for (int i = 0; i < enumerators.length; i++) {
if (cleanLabel.equals(sanitize(enumerators[i]))) {
return Validation.VALID;
}
}
return Validation.INVALID;
} else { } else {
return validate(enumerators, value.getInt()); final int ordinal = value.getInt();
if (ordinal < 0 || ordinal >= enumerators.length) {
return Validation.INVALID;
}
return Validation.VALID;
} }
} }
} }
...@@ -2,6 +2,7 @@ package org.h2.value; ...@@ -2,6 +2,7 @@ package org.h2.value;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Locale;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
...@@ -38,8 +39,7 @@ public class ValueEnumBase extends Value { ...@@ -38,8 +39,7 @@ public class ValueEnumBase extends Value {
@Override @Override
public boolean equals(final Object other) { public boolean equals(final Object other) {
return other instanceof ValueEnumBase && return other instanceof ValueEnumBase &&
ordinal() == ((ValueEnumBase) other).ordinal() && ordinal() == ((ValueEnumBase) other).ordinal();
getString() == ((ValueEnumBase) other).getString();
} }
public static ValueEnumBase get(final String label, final int ordinal) { public static ValueEnumBase get(final String label, final int ordinal) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论