提交 8aced489 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Preserve type names in more places especially for UUID

上级 1b50216c
......@@ -135,14 +135,12 @@ 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));
DataType dataType = DataType.getDataType(rs.getColumnType(i));
int sqlType = dataType.sqlType;
String sqlTypeName = dataType.name;
int precision = MathUtils.convertLongToInt(rs.getColumnPrecision(i));
int scale = rs.getColumnScale(i);
simple.addColumn(name, sqlType, precision, scale);
simple.addColumn(name, sqlType, sqlTypeName, precision, scale);
}
rs.reset();
for (int i = 0; i < maxrows && rs.next(); i++) {
......
......@@ -32,6 +32,8 @@ public final class MergedResultSet {
final int type;
final String typeName;
final int precision;
final int scale;
......@@ -43,14 +45,17 @@ public final class MergedResultSet {
* name of the column
* @param type
* type of the column, see {@link java.sql.Types}
* @param typeName
* type name of the column
* @param precision
* precision of the column
* @param scale
* scale of the column
*/
ColumnInfo(String name, int type, int precision, int scale) {
ColumnInfo(String name, int type, String typeName, int precision, int scale) {
this.name = name;
this.type = type;
this.typeName = typeName;
this.precision = precision;
this.scale = scale;
}
......@@ -94,8 +99,8 @@ public final class MergedResultSet {
}
ColumnInfo[] info = new ColumnInfo[cols];
for (int i = 1; i <= cols; i++) {
ColumnInfo ci = new ColumnInfo(meta.getColumnName(i), meta.getColumnType(i), meta.getPrecision(i),
meta.getScale(i));
ColumnInfo ci = new ColumnInfo(meta.getColumnName(i), meta.getColumnType(i), meta.getColumnTypeName(i),
meta.getPrecision(i), meta.getScale(i));
info[i - 1] = ci;
if (!columns.contains(ci)) {
columns.add(ci);
......@@ -123,7 +128,7 @@ public final class MergedResultSet {
public SimpleResultSet getResult() {
SimpleResultSet rs = new SimpleResultSet();
for (ColumnInfo ci : columns) {
rs.addColumn(ci.name, ci.type, ci.precision, ci.scale);
rs.addColumn(ci.name, ci.type, ci.typeName, ci.precision, ci.scale);
}
for (Map<ColumnInfo, Object> map : data) {
Object[] row = new Object[columns.size()];
......
......@@ -856,6 +856,11 @@ public class DataType {
*/
public static int convertSQLTypeToValueType(int sqlType, String sqlTypeName) {
switch (sqlType) {
case Types.BINARY:
if (sqlTypeName.equalsIgnoreCase("UUID")) {
return Value.UUID;
}
break;
case Types.OTHER:
case Types.JAVA_OBJECT:
if (sqlTypeName.equalsIgnoreCase("geometry")) {
......
......@@ -8,6 +8,7 @@ package org.h2.test.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.UUID;
......@@ -127,6 +128,9 @@ public class TestGetGeneratedKeys extends TestBase {
prep.addBatch();
prep.executeBatch();
ResultSet rs = prep.getGeneratedKeys();
ResultSetMetaData meta = rs.getMetaData();
assertEquals("BIGINT", meta.getColumnTypeName(1));
assertEquals("UUID", meta.getColumnTypeName(2));
rs.next();
assertEquals(1L, rs.getLong(1));
UUID u1 = (UUID) rs.getObject(2);
......
......@@ -8,11 +8,14 @@ package org.h2.test.jdbc;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Driver;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.UUID;
import org.h2.api.ErrorCode;
import org.h2.engine.Constants;
import org.h2.engine.SysProperties;
......@@ -135,6 +138,13 @@ public class TestMetaData extends TestBase {
assertEquals(ResultSet.HOLD_CURSORS_OVER_COMMIT, conn.getHoldability());
assertEquals(ResultSet.HOLD_CURSORS_OVER_COMMIT, rs.getHoldability());
stat.executeUpdate("drop table test");
PreparedStatement prep = conn.prepareStatement("SELECT X FROM TABLE (X UUID = ?)");
prep.setObject(1, UUID.randomUUID());
rs = prep.executeQuery();
rsMeta = rs.getMetaData();
assertEquals("UUID", rsMeta.getColumnTypeName(1));
conn.close();
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论