提交 9e18b5c8 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #353 from royrwood/master

Add support for converting JAVA_OBJECT to UUID
......@@ -314,9 +314,9 @@ public abstract class Value {
return 40;
case BLOB:
return 41;
case UUID:
return 42;
case JAVA_OBJECT:
return 42;
case UUID:
return 43;
case GEOMETRY:
return 44;
......@@ -910,6 +910,14 @@ public abstract class Value {
case BYTES:
return ValueUuid.get(getBytesNoCopy());
case JAVA_OBJECT:
Object object = JdbcUtils.deserialize(getBytesNoCopy(), getDataHandler());
if (object instanceof java.util.UUID) {
java.util.UUID uuid = (java.util.UUID) object;
return ValueUuid.get(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits());
}
else {
throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, getString());
}
case TIMESTAMP_TZ:
throw DbException.get(
ErrorCode.DATA_CONVERSION_ERROR_1, getString());
......
......@@ -58,6 +58,7 @@ public class TestPreparedStatement extends TestBase {
testExecuteUpdateCall(conn);
testPrepareExecute(conn);
testUUID(conn);
testUUIDAsJavaObject(conn);
testScopedGeneratedKey(conn);
testLobTempFiles(conn);
testExecuteErrorTwice(conn);
......@@ -458,6 +459,28 @@ public class TestPreparedStatement extends TestBase {
stat.execute("drop table test_uuid");
}
private void testUUIDAsJavaObject(Connection conn) throws SQLException {
String uuidStr = "12345678-1234-4321-8765-123456789012";
Statement stat = conn.createStatement();
stat.execute("create table test_uuid(id uuid primary key)");
UUID origUUID = UUID.fromString(uuidStr);
PreparedStatement prep = conn.prepareStatement("insert into test_uuid values(?)");
prep.setObject(1, origUUID, java.sql.Types.JAVA_OBJECT);
prep.execute();
prep = conn.prepareStatement("select * from test_uuid where id=?");
prep.setObject(1, origUUID, java.sql.Types.JAVA_OBJECT);
ResultSet rs = prep.executeQuery();
rs.next();
Object o = rs.getObject(1);
assertTrue(o instanceof UUID);
UUID selectedUUID = (UUID) o;
assertTrue(selectedUUID.toString().equals(uuidStr));
assertTrue(selectedUUID.equals(origUUID));
stat.execute("drop table test_uuid");
}
private void testUUIDGeneratedKeys(Connection conn) throws SQLException {
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE TEST_UUID(id UUID DEFAULT " +
......
......@@ -285,8 +285,18 @@ public class TestValue extends TestBase {
ValueUuid min = ValueUuid.get(minHigh, minLow);
assertEquals("00000000-0000-4000-8000-000000000000", min.getString());
ValueJavaObject vo = ValueJavaObject.getNoCopy(UUID.randomUUID(), null, null);
assertThrows(DbException.class, vo).convertTo(Value.UUID);
// Test conversion from ValueJavaObject to ValueUuid
String uuidStr = "12345678-1234-4321-8765-123456789012";
UUID origUUID = UUID.fromString(uuidStr);
ValueJavaObject valObj = ValueJavaObject.getNoCopy(origUUID, null, null);
Value valUUID = valObj.convertTo(Value.UUID);
assertTrue(valUUID instanceof ValueUuid);
assertTrue((valUUID.getString().equals(uuidStr)));
assertTrue(valUUID.getObject().equals(origUUID));
ValueJavaObject vo_string = ValueJavaObject.getNoCopy(new String("This is not a ValueUuid object"), null, null);
assertThrows(DbException.class, vo_string).convertTo(Value.UUID);
}
private void testModulusDouble() {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论