提交 bf23f856 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Fix ON DUPLICATE KEY UPDATE with ENUM

上级 542100a4
......@@ -272,7 +272,7 @@ public class Comparison extends Condition {
}
int dataType = Value.getHigherOrder(left.getType(), right.getType());
if (dataType == Value.ENUM) {
String[] enumerators = getEnumerators(l, r);
String[] enumerators = ValueEnum.getEnumeratorsForBinaryOperation(l, r);
l = l.convertToEnum(enumerators);
r = r.convertToEnum(enumerators);
} else {
......@@ -283,16 +283,6 @@ public class Comparison extends Condition {
return ValueBoolean.get(result);
}
private static 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.
*
......
......@@ -40,6 +40,7 @@ import org.h2.schema.TriggerObject;
import org.h2.util.Utils;
import org.h2.value.CompareMode;
import org.h2.value.Value;
import org.h2.value.ValueEnum;
import org.h2.value.ValueNull;
/**
......@@ -1192,8 +1193,14 @@ public abstract class Table extends SchemaObjectBase {
return 0;
}
int dataType = Value.getHigherOrder(a.getType(), b.getType());
a = a.convertTo(dataType);
b = b.convertTo(dataType);
if (dataType == Value.ENUM) {
String[] enumerators = ValueEnum.getEnumeratorsForBinaryOperation(a, b);
a = a.convertToEnum(enumerators);
b = b.convertToEnum(enumerators);
} else {
a = a.convertTo(dataType);
b = b.convertTo(dataType);
}
return a.compareTypeSafe(b, compareMode);
}
......
......@@ -81,6 +81,26 @@ public class ValueEnum extends ValueEnumBase {
throw DbException.get(ErrorCode.GENERAL_ERROR_1, "Unexpected error");
}
/**
* Returns enumerators for the two specified values for a binary operation.
*
* @param left
* left (first) operand
* @param right
* right (second) operand
* @return enumerators from the left or the right value, or an empty array if
* both values do not have enumerators
*/
public static String[] getEnumeratorsForBinaryOperation(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];
}
}
public String[] getEnumerators() {
return enumerators;
}
......
......@@ -40,6 +40,7 @@ public class TestDuplicateKeyUpdate extends TestBase {
testOnDuplicateKeyInsertMultiValue(conn);
testPrimaryKeyAndUniqueKey(conn);
testUpdateCountAndQualifiedNames(conn);
testEnum(conn);
conn.close();
deleteDb("duplicateKeyUpdate");
}
......@@ -300,4 +301,14 @@ public class TestDuplicateKeyUpdate extends TestBase {
stat.execute("drop schema s2 cascade");
}
private void testEnum(Connection conn) throws SQLException {
Statement stat = conn.createStatement();
stat.execute("create table test(e enum('a', 'b') unique)");
PreparedStatement ps = conn.prepareStatement("insert into test(e) values (?) on duplicate key update e = e");
ps.setString(1, "a");
assertEquals(1, ps.executeUpdate());
assertEquals(0, ps.executeUpdate());
stat.execute("drop table test");
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论