提交 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 { ...@@ -272,7 +272,7 @@ public class Comparison extends Condition {
} }
int dataType = Value.getHigherOrder(left.getType(), right.getType()); int dataType = Value.getHigherOrder(left.getType(), right.getType());
if (dataType == Value.ENUM) { if (dataType == Value.ENUM) {
String[] enumerators = getEnumerators(l, r); String[] enumerators = ValueEnum.getEnumeratorsForBinaryOperation(l, r);
l = l.convertToEnum(enumerators); l = l.convertToEnum(enumerators);
r = r.convertToEnum(enumerators); r = r.convertToEnum(enumerators);
} else { } else {
...@@ -283,16 +283,6 @@ public class Comparison extends Condition { ...@@ -283,16 +283,6 @@ public class Comparison extends Condition {
return ValueBoolean.get(result); 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. * Compare two values, given the values are not NULL.
* *
......
...@@ -40,6 +40,7 @@ import org.h2.schema.TriggerObject; ...@@ -40,6 +40,7 @@ import org.h2.schema.TriggerObject;
import org.h2.util.Utils; import org.h2.util.Utils;
import org.h2.value.CompareMode; import org.h2.value.CompareMode;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueEnum;
import org.h2.value.ValueNull; import org.h2.value.ValueNull;
/** /**
...@@ -1192,8 +1193,14 @@ public abstract class Table extends SchemaObjectBase { ...@@ -1192,8 +1193,14 @@ public abstract class Table extends SchemaObjectBase {
return 0; return 0;
} }
int dataType = Value.getHigherOrder(a.getType(), b.getType()); int dataType = Value.getHigherOrder(a.getType(), b.getType());
if (dataType == Value.ENUM) {
String[] enumerators = ValueEnum.getEnumeratorsForBinaryOperation(a, b);
a = a.convertToEnum(enumerators);
b = b.convertToEnum(enumerators);
} else {
a = a.convertTo(dataType); a = a.convertTo(dataType);
b = b.convertTo(dataType); b = b.convertTo(dataType);
}
return a.compareTypeSafe(b, compareMode); return a.compareTypeSafe(b, compareMode);
} }
......
...@@ -81,6 +81,26 @@ public class ValueEnum extends ValueEnumBase { ...@@ -81,6 +81,26 @@ public class ValueEnum extends ValueEnumBase {
throw DbException.get(ErrorCode.GENERAL_ERROR_1, "Unexpected error"); 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() { public String[] getEnumerators() {
return enumerators; return enumerators;
} }
......
...@@ -40,6 +40,7 @@ public class TestDuplicateKeyUpdate extends TestBase { ...@@ -40,6 +40,7 @@ public class TestDuplicateKeyUpdate extends TestBase {
testOnDuplicateKeyInsertMultiValue(conn); testOnDuplicateKeyInsertMultiValue(conn);
testPrimaryKeyAndUniqueKey(conn); testPrimaryKeyAndUniqueKey(conn);
testUpdateCountAndQualifiedNames(conn); testUpdateCountAndQualifiedNames(conn);
testEnum(conn);
conn.close(); conn.close();
deleteDb("duplicateKeyUpdate"); deleteDb("duplicateKeyUpdate");
} }
...@@ -300,4 +301,14 @@ public class TestDuplicateKeyUpdate extends TestBase { ...@@ -300,4 +301,14 @@ public class TestDuplicateKeyUpdate extends TestBase {
stat.execute("drop schema s2 cascade"); 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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论