提交 636d2484 authored 作者: Thomas Mueller's avatar Thomas Mueller

java.util.UUID is now supported in PreparedStatement.setObject and user defined…

java.util.UUID is now supported in PreparedStatement.setObject and user defined Java functions. ResultSet.getObject() returns a java.util.UUID when using the UUID data type.
上级 ec66b936
......@@ -21,6 +21,7 @@ import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;
import org.h2.constant.ErrorCode;
import org.h2.constant.SysProperties;
......@@ -804,6 +805,8 @@ public class DataType {
return Value.BLOB;
} else if (java.sql.Blob.class.isAssignableFrom(x)) {
return Value.BLOB;
} else if (UUID.class.isAssignableFrom(x)) {
return Value.UUID;
} else if (Object[].class.isAssignableFrom(x)) {
return Value.ARRAY;
} else if (Void.TYPE == x) {
......@@ -871,6 +874,9 @@ public class DataType {
return ValueLob.createBlob(((java.sql.Blob) x).getBinaryStream(), -1, session.getDataHandler());
} else if (x instanceof ResultSet) {
return ValueResultSet.get((ResultSet) x);
} else if (x instanceof UUID) {
UUID u = (UUID) x;
return ValueUuid.get(u.getMostSignificantBits(), u.getLeastSignificantBits());
} else if (x instanceof Object[]) {
// (a.getClass().isArray());
// (a.getClass().getComponentType().isPrimitive());
......
......@@ -8,6 +8,7 @@ package org.h2.value;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.UUID;
import org.h2.util.ByteUtils;
import org.h2.util.RandomUtils;
......@@ -156,8 +157,7 @@ public class ValueUuid extends Value {
}
public Object getObject() {
// TODO needs to be documented
return new long[]{high, low};
return new UUID(high, low);
}
public byte[] getBytes() {
......
......@@ -22,6 +22,7 @@ import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Properties;
import java.util.UUID;
import org.h2.api.AggregateFunction;
import org.h2.test.TestBase;
......@@ -47,6 +48,7 @@ public class TestFunctions extends TestBase implements AggregateFunction {
public void test() throws Exception {
deleteDb("functions");
testUUID();
testDeterministic();
testTransactionId();
testPrecision();
......@@ -57,6 +59,22 @@ public class TestFunctions extends TestBase implements AggregateFunction {
deleteDb("functions");
}
private void testUUID() throws SQLException {
Connection conn = getConnection("functions");
Statement stat = conn.createStatement();
ResultSet rs;
stat.execute("create alias xorUUID for \""+getClass().getName()+".xorUUID\"");
setCount(0);
rs = stat.executeQuery("call xorUUID(random_uuid(), random_uuid())");
rs.next();
Object o = rs.getObject(1);
assertEquals(UUID.class.toString(), o.getClass().toString());
stat.execute("drop alias xorUUID");
conn.close();
}
private void testDeterministic() throws SQLException {
Connection conn = getConnection("functions");
Statement stat = conn.createStatement();
......@@ -626,6 +644,11 @@ public class TestFunctions extends TestBase implements AggregateFunction {
}
//## Java 1.5 end ##
public static UUID xorUUID(UUID a, UUID b) {
return new UUID(a.getMostSignificantBits() ^ b.getMostSignificantBits(),
a.getLeastSignificantBits() ^ b.getLeastSignificantBits());
}
public void add(Object value) {
// ignore
}
......
......@@ -19,6 +19,7 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.UUID;
import org.h2.test.TestBase;
......@@ -42,6 +43,7 @@ public class TestPreparedStatement extends TestBase {
deleteDb("preparedStatement");
Connection conn = getConnection("preparedStatement");
testUUID(conn);
testLobTempFiles(conn);
testExecuteErrorTwice(conn);
testTempView(conn);
......@@ -302,6 +304,21 @@ public class TestPreparedStatement extends TestBase {
assertFalse(rs.next());
}
private void testUUID(Connection conn) throws SQLException {
Statement stat = conn.createStatement();
stat.execute("create table test_uuid(id uuid primary key)");
UUID uuid = new UUID(-2, -1);
PreparedStatement prep = conn.prepareStatement("insert into test_uuid values(?)");
prep.setObject(1, uuid);
prep.execute();
ResultSet rs = stat.executeQuery("select * from test_uuid");
rs.next();
assertEquals("ffffffff-ffff-fffe-ffff-ffffffffffff", rs.getString(1));
Object o = rs.getObject(1);
assertEquals("java.util.UUID", o.getClass().getName());
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 random_UUID() PRIMARY KEY)");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论