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

Fix handling of UUID in Datatype.readValue()

上级 7dd4829b
......@@ -136,6 +136,10 @@ public class TableFunction extends Function {
simple.setAutoClose(false);
for (int i = 0; i < columnCount; i++) {
String name = rs.getColumnName(i);
/*
* TODO Some types, such as Value.BYTES and Value.UUID are mapped to the same
* SQL type and we can lose real type here.
*/
int sqlType = DataType.convertTypeToSQLType(rs.getColumnType(i));
int precision = MathUtils.convertLongToInt(rs.getColumnPrecision(i));
int scale = rs.getColumnScale(i);
......
......@@ -514,9 +514,18 @@ public class DataType {
return ValueNull.INSTANCE;
}
case Value.BYTES: {
byte[] buff = rs.getBytes(columnIndex);
v = buff == null ? (Value) ValueNull.INSTANCE :
ValueBytes.getNoCopy(buff);
/*
* Both BINARY and UUID may be mapped to Value.BYTES. getObject() returns byte[]
* for SQL BINARY, UUID for SQL UUID and null for SQL NULL.
*/
Object o = rs.getObject(columnIndex);
if (o instanceof byte[]) {
v = ValueBytes.getNoCopy((byte[]) o);
} else if (o != null) {
UUID u = (UUID) o;
v = ValueUuid.get(u.getMostSignificantBits(), u.getLeastSignificantBits());
} else
v = ValueNull.INSTANCE;
break;
}
case Value.UUID: {
......
......@@ -8,12 +8,15 @@ package org.h2.test.unit;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.Arrays;
import java.util.UUID;
import org.h2.api.ErrorCode;
import org.h2.message.DbException;
import org.h2.test.TestBase;
......@@ -50,6 +53,7 @@ public class TestValue extends TestBase {
@Override
public void test() throws SQLException {
testResultSetOperations();
testBinaryAndUuid();
testCastTrim();
testValueResultSet();
testDataType();
......@@ -87,6 +91,7 @@ public class TestValue extends TestBase {
testResultSetOperation(new Time(7));
testResultSetOperation(new Timestamp(8));
testResultSetOperation(new BigDecimal("9"));
testResultSetOperation(UUID.randomUUID());
SimpleResultSet rs2 = new SimpleResultSet();
rs2.setAutoClose(false);
......@@ -114,6 +119,32 @@ public class TestValue extends TestBase {
}
}
private void testBinaryAndUuid() throws SQLException {
Connection conn = getConnection("binaryAndUuid");
try {
UUID uuid = UUID.randomUUID();
PreparedStatement prep;
ResultSet rs;
// Check conversion to byte[]
prep = conn.prepareStatement("SELECT * FROM TABLE(X BINARY=?)");
prep.setObject(1, new Object[] { uuid });
rs = prep.executeQuery();
rs.next();
assertTrue(Arrays.equals(
ValueUuid.get(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits()).getBytes(),
(byte[]) rs.getObject(1)));
// Check that type is not changed
prep = conn.prepareStatement("SELECT * FROM TABLE(X UUID=?)");
prep.setObject(1, new Object[] { uuid });
rs = prep.executeQuery();
rs.next();
assertEquals(uuid, rs.getObject(1));
} finally {
conn.close();
deleteDb("binaryAndUuid");
}
}
private void testCastTrim() {
Value v;
String spaces = new String(new char[100]).replace((char) 0, ' ');
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论