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

enum-support: add more test cases

上级 06c8cb75
...@@ -197,11 +197,11 @@ public class ErrorCode { ...@@ -197,11 +197,11 @@ public class ErrorCode {
/** /**
* The error with code <code>22032</code> is thrown when an * The error with code <code>22032</code> is thrown when an
* attempt is made to add or modify an ENUM-typed column so * attempt is made to add or modify an ENUM-typed column so
* that it would not have any enumerated values. * that one or more of its enumerators would be empty.
* *
* Example: * Example:
* <pre> * <pre>
* CREATE TABLE TEST(CASE ENUM()); * CREATE TABLE TEST(CASE ENUM(' '));
* </pre> * </pre>
*/ */
public static final int ENUM_EMPTY = 22032; public static final int ENUM_EMPTY = 22032;
......
...@@ -146,6 +146,7 @@ import org.h2.value.ValueBoolean; ...@@ -146,6 +146,7 @@ import org.h2.value.ValueBoolean;
import org.h2.value.ValueBytes; import org.h2.value.ValueBytes;
import org.h2.value.ValueDate; import org.h2.value.ValueDate;
import org.h2.value.ValueDecimal; import org.h2.value.ValueDecimal;
import org.h2.value.ValueEnum;
import org.h2.value.ValueInt; import org.h2.value.ValueInt;
import org.h2.value.ValueLong; import org.h2.value.ValueLong;
import org.h2.value.ValueNull; import org.h2.value.ValueNull;
...@@ -4127,7 +4128,8 @@ public class Parser { ...@@ -4127,7 +4128,8 @@ public class Parser {
} }
long precision = -1; long precision = -1;
int displaySize = -1; int displaySize = -1;
java.util.List<String> enumerators = null; java.util.List<String> enumeratorList = null;
String[] enumerators = null;
int scale = -1; int scale = -1;
String comment = null; String comment = null;
Column templateColumn = null; Column templateColumn = null;
...@@ -4203,20 +4205,27 @@ public class Parser { ...@@ -4203,20 +4205,27 @@ public class Parser {
} }
} else if (dataType.enumerated) { } else if (dataType.enumerated) {
if (readIf("(")) { if (readIf("(")) {
enumerators = new ArrayList<String>(); enumeratorList = new ArrayList<String>();
original += '('; original += '(';
String enumerator0 = readString(); String enumerator0 = readString();
enumerators.add(enumerator0.toLowerCase().trim()); enumeratorList.add(enumerator0.toLowerCase().trim());
original += "'" + enumerator0 + "'"; original += "'" + enumerator0 + "'";
while(readIf(",")) { while(readIf(",")) {
original += ','; original += ',';
String enumeratorN = readString(); String enumeratorN = readString();
original += "'" + enumeratorN + "'"; original += "'" + enumeratorN + "'";
enumerators.add(enumeratorN.toLowerCase().trim()); enumeratorList.add(enumeratorN.toLowerCase().trim());
} }
read(")"); read(")");
original += ')'; original += ')';
} }
enumerators
= enumeratorList.toArray(new String[enumeratorList.size()]);
try {
ValueEnum.check(enumerators);
} catch(DbException e) {
throw e.addSQL(original);
}
} 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.
...@@ -4237,8 +4246,10 @@ public class Parser { ...@@ -4237,8 +4246,10 @@ 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 = new Column(columnName, type, precision, scale,
displaySize, enumerators == null ? null : enumerators.toArray(new String[enumerators.size()])); displaySize, enumerators == null ? null : enumerators);
if (templateColumn != null) { if (templateColumn != null) {
column.setNullable(templateColumn.isNullable()); column.setNullable(templateColumn.isNullable());
column.setDefaultExpression(session, column.setDefaultExpression(session,
......
...@@ -35,7 +35,7 @@ public class ValueEnum extends Value { ...@@ -35,7 +35,7 @@ public class ValueEnum extends Value {
return convertTo(Value.INT).add(iv); return convertTo(Value.INT).add(iv);
} }
private static final void check(final String[] enumerators) { public static final void check(final String[] enumerators) {
switch (validate(enumerators)) { switch (validate(enumerators)) {
case VALID: case VALID:
return; return;
...@@ -251,6 +251,10 @@ public class ValueEnum extends Value { ...@@ -251,6 +251,10 @@ public class ValueEnum extends Value {
} }
private static Validation validate(final String[] enumerators) { private static Validation validate(final String[] enumerators) {
if (enumerators == null || enumerators.length == 0) {
return Validation.EMPTY;
}
for (int i = 0; i < enumerators.length; i++) { for (int i = 0; i < enumerators.length; i++) {
if (enumerators[i] == null || enumerators[i].trim().equals("")) { if (enumerators[i] == null || enumerators[i].trim().equals("")) {
return Validation.EMPTY; return Validation.EMPTY;
......
...@@ -10590,13 +10590,21 @@ create table z.z (id int); ...@@ -10590,13 +10590,21 @@ create table z.z (id int);
drop schema z; drop schema z;
> ok > ok
--- enum support ----------------
create table card (rank int, suit enum('hearts', 'clubs', 'spades', 'diamonds')); --- ENUM support
----------------
--- ENUM basic operations
create table card (rank int, suit enum('hearts', 'clubs', 'spades'));
> ok > ok
insert into card (rank, suit) values (0, 'clubs'), (3, 'hearts'); insert into card (rank, suit) values (0, 'clubs'), (3, 'hearts');
> update count: 2 > update count: 2
alter table card alter column suit enum('hearts', 'clubs', 'spades', 'diamonds');
> ok
select * from card; select * from card;
> RANK SUIT > RANK SUIT
> ---- ------ > ---- ------
...@@ -10624,11 +10632,32 @@ select rank from card where suit = 'diamonds'; ...@@ -10624,11 +10632,32 @@ select rank from card where suit = 'diamonds';
> ---- > ----
> 8 > 8
--- ENUM integer-based operations
select rank from card where suit = 1; select rank from card where suit = 1;
> RANK > RANK
> ---- > ----
> 0 > 0
> 10 > 10
insert into card (rank, suit) values(5, 2);
> update count: 1
select * from card where rank = 5;
> RANK SUIT
> ---- ------
> 5 spades
--- ENUM edge cases
insert into card (rank, suit) values(6, ' ');
> exception
alter table card alter column suit enum('hearts', 'clubs', 'spades', 'diamonds', 'clubs');
> exception
alter table card alter column suit enum('hearts', 'clubs', 'spades', 'diamonds', '');
> exception
drop table card; drop table card;
> ok > ok
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论