提交 4ba32c2c authored 作者: ravd's avatar ravd 提交者: AniaIRafal

Added support for ENUM in prepared statement where clause

上级 3ac73142
......@@ -16,10 +16,7 @@ import org.h2.table.Column;
import org.h2.table.ColumnResolver;
import org.h2.table.TableFilter;
import org.h2.util.MathUtils;
import org.h2.value.Value;
import org.h2.value.ValueBoolean;
import org.h2.value.ValueGeometry;
import org.h2.value.ValueNull;
import org.h2.value.*;
/**
* Example comparison expressions are ID=1, NAME=NAME, NAME IS NULL.
......@@ -266,12 +263,28 @@ public class Comparison extends Condition {
}
}
int dataType = Value.getHigherOrder(left.getType(), right.getType());
l = l.convertTo(dataType);
r = r.convertTo(dataType);
if (dataType == Value.ENUM) {
String[] enumerators = getEnumerators(l, r);
l = l.convertToEnum(enumerators);
r = r.convertToEnum(enumerators);
} else {
l = l.convertTo(dataType);
r = r.convertTo(dataType);
}
boolean result = compareNotNull(database, l, r, compareType);
return ValueBoolean.get(result);
}
private String[] getEnumerators(Value left, Value right) {
if (left.getType() == Value.ENUM) {
return ((ValueEnum) left).getEnumerators();
} else if (right.getType() == Value.ENUM) {
return ((ValueEnum) right).getEnumerators();
} else {
return new String[0];
}
}
/**
* Compare two values, given the values are not NULL.
*
......
......@@ -592,6 +592,17 @@ public abstract class Value {
return convertTo(targetType, -1, null);
}
/**
* Convert value to ENUM value
* @param enumerators allowed values for the ENUM to which the value is converted
* @return value represented as ENUM
*/
public Value convertToEnum(String[] enumerators) {
// Use -1 to indicate "default behaviour" where value conversion should not
// depend on any datatype precision.
return convertTo(ENUM, -1, null, null, enumerators);
}
/**
* Compare a value to the specified type.
*
......
......@@ -479,6 +479,22 @@ public class TestPreparedStatement extends TestBase {
assertEquals(Integer.class, o.getClass());
}
for (int i = 0; i < goodSizes.length; i++) {
PreparedStatement prep = conn.prepareStatement("SELECT * FROM test_enum WHERE size = ?");
prep.setObject(1, goodSizes[i]);
ResultSet rs = prep.executeQuery();
rs.next();
String s = rs.getString(1);
assertTrue(s.equals(goodSizes[i]));
assertFalse(rs.next());
}
for (int i = 0; i < badSizes.length; i++) {
PreparedStatement prep = conn.prepareStatement("SELECT * FROM test_enum WHERE size = ?");
prep.setObject(1, badSizes[i]);
assertThrows(ErrorCode.ENUM_VALUE_NOT_PERMITTED, prep).executeQuery();
}
stat.execute("DROP TABLE test_enum");
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论