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

enum-support: add more test cases

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