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