From 502240bb3dee9f7e40440fbdfb23ff4ca5bf850a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E5=AE=97=E7=A3=8A?= <598363102@qq.com> Date: Thu, 17 Oct 2019 16:05:29 +0800 Subject: [PATCH] fixes QueryResultUtil#getValueByColumnType unsigned int & bigint return value. (#3288) * fixes unsigned int & bigint return value (#3231) * add unsigned int & bigint return value test case (#3231) * fixes test case failure (#3231) * fixes code review problem (#3231) * fixes code format (#3231) --- .../sql/execute/result/QueryResultUtil.java | 5 ++-- .../execute/result/MemoryQueryResultTest.java | 14 +++++----- .../execute/result/QueryResultUtilTest.java | 27 ++++++++++++++----- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/sharding-core/sharding-core-execute/src/main/java/org/apache/shardingsphere/core/execute/sql/execute/result/QueryResultUtil.java b/sharding-core/sharding-core-execute/src/main/java/org/apache/shardingsphere/core/execute/sql/execute/result/QueryResultUtil.java index 5f2747b24eb0d..e84117b1fbea6 100644 --- a/sharding-core/sharding-core-execute/src/main/java/org/apache/shardingsphere/core/execute/sql/execute/result/QueryResultUtil.java +++ b/sharding-core/sharding-core-execute/src/main/java/org/apache/shardingsphere/core/execute/sql/execute/result/QueryResultUtil.java @@ -104,10 +104,11 @@ private static Object getValueByColumnType(final ResultSet resultSet, final int return resultSet.getBoolean(columnIndex); case Types.TINYINT: case Types.SMALLINT: - case Types.INTEGER: return resultSet.getInt(columnIndex); + case Types.INTEGER: + return metaData.isSigned(columnIndex) ? resultSet.getInt(columnIndex) : resultSet.getLong(columnIndex); case Types.BIGINT: - return resultSet.getLong(columnIndex); + return metaData.isSigned(columnIndex) ? resultSet.getLong(columnIndex) : resultSet.getBigDecimal(columnIndex).toBigInteger(); case Types.NUMERIC: case Types.DECIMAL: return resultSet.getBigDecimal(columnIndex); diff --git a/sharding-core/sharding-core-execute/src/test/java/org/apache/shardingsphere/core/execute/sql/execute/result/MemoryQueryResultTest.java b/sharding-core/sharding-core-execute/src/test/java/org/apache/shardingsphere/core/execute/sql/execute/result/MemoryQueryResultTest.java index 22e6d5b1e1e5f..0c8e1dda1ba46 100644 --- a/sharding-core/sharding-core-execute/src/test/java/org/apache/shardingsphere/core/execute/sql/execute/result/MemoryQueryResultTest.java +++ b/sharding-core/sharding-core-execute/src/test/java/org/apache/shardingsphere/core/execute/sql/execute/result/MemoryQueryResultTest.java @@ -56,7 +56,7 @@ public final class MemoryQueryResultTest { private final ShardingEncryptor shardingEncryptor = mock(ShardingEncryptor.class); - + @Test public void assertConstructorWithShardingRule() throws SQLException { MemoryQueryResult queryResult = new MemoryQueryResult(getResultSet(), getShardingRule(), new ShardingProperties(new Properties()), getSqlStatementContext()); @@ -92,7 +92,7 @@ private EncryptRule getEncryptRule() { when(encryptTable.getCipherColumns()).thenReturn(Collections.singleton("order_id")); return result; } - + private SQLStatementContext getSqlStatementContext() { SQLStatementContext result = mock(SQLStatementContext.class); TablesContext tablesContext = mock(TablesContext.class); @@ -112,14 +112,14 @@ public void assertNext() throws SQLException { public void assertGetValueWithColumnIndex() throws SQLException { MemoryQueryResult queryResult = new MemoryQueryResult(getResultSet()); queryResult.next(); - assertThat(queryResult.getValue(1, Integer.class), Is.is(1)); + assertThat(queryResult.getValue(1, Integer.class), Is.is(Long.valueOf(1))); } @Test public void assertGetValueWithColumnLabel() throws SQLException { MemoryQueryResult queryResult = new MemoryQueryResult(getResultSet()); queryResult.next(); - assertThat(queryResult.getValue("order_id", Integer.class), Is.is(1)); + assertThat(queryResult.getValue("order_id", Integer.class), Is.is(Long.valueOf(1))); } @Test @@ -155,6 +155,7 @@ private ResultSetMetaData getResultSetMetaDataWithException() { when(metaData.getColumnLabel(1)).thenReturn("order_id"); when(metaData.getColumnName(1)).thenThrow(new SQLException()); when(metaData.getColumnType(1)).thenReturn(Types.INTEGER); + when(metaData.isSigned(1)).thenReturn(true); return metaData; } @@ -162,14 +163,14 @@ private ResultSetMetaData getResultSetMetaDataWithException() { public void assertGetCalendarValueWithColumnIndex() throws SQLException { MemoryQueryResult queryResult = new MemoryQueryResult(getResultSet()); queryResult.next(); - assertThat(queryResult.getCalendarValue(1, Integer.class, Calendar.getInstance()), Is.is(1)); + assertThat(queryResult.getCalendarValue(1, Integer.class, Calendar.getInstance()), Is.is(Long.valueOf(1))); } @Test public void assertGetCalendarValueWithColumnLabel() throws SQLException { MemoryQueryResult queryResult = new MemoryQueryResult(getResultSet()); queryResult.next(); - assertThat(queryResult.getCalendarValue("order_id", Integer.class, Calendar.getInstance()), Is.is(1)); + assertThat(queryResult.getCalendarValue("order_id", Integer.class, Calendar.getInstance()), Is.is(Long.valueOf(1))); } @Test @@ -242,6 +243,7 @@ private ResultSetMetaData getResultSetMetaData() throws SQLException { when(result.getColumnLabel(1)).thenReturn("order_id"); when(result.getColumnName(1)).thenReturn("order_id"); when(result.getColumnType(1)).thenReturn(Types.INTEGER); + when(result.isSigned(1)).thenReturn(true); when(result.isCaseSensitive(1)).thenReturn(false); return result; } diff --git a/sharding-core/sharding-core-execute/src/test/java/org/apache/shardingsphere/core/execute/sql/execute/result/QueryResultUtilTest.java b/sharding-core/sharding-core-execute/src/test/java/org/apache/shardingsphere/core/execute/sql/execute/result/QueryResultUtilTest.java index 1729cd10506cb..92bc35af4d179 100644 --- a/sharding-core/sharding-core-execute/src/test/java/org/apache/shardingsphere/core/execute/sql/execute/result/QueryResultUtilTest.java +++ b/sharding-core/sharding-core-execute/src/test/java/org/apache/shardingsphere/core/execute/sql/execute/result/QueryResultUtilTest.java @@ -25,6 +25,7 @@ import org.mockito.junit.MockitoJUnitRunner; import java.math.BigDecimal; +import java.math.BigInteger; import java.sql.Blob; import java.sql.Clob; import java.sql.Date; @@ -86,26 +87,44 @@ public void assertGetValueBySmallint() throws SQLException { when(resultSet.getInt(1)).thenReturn(32767); assertThat((int) QueryResultUtil.getValue(resultSet, 1), is(32767)); assertThat((int) QueryResultUtil.getValue(resultSet, 1, int.class), is(32767)); - } @Test public void assertGetValueByInteger() throws SQLException { when(resultSetMetaData.getColumnType(1)).thenReturn(Types.INTEGER); + when(resultSetMetaData.isSigned(1)).thenReturn(true); when(resultSet.getInt(1)).thenReturn(Integer.MAX_VALUE); - assertThat((int) QueryResultUtil.getValue(resultSet, 1), is(Integer.MAX_VALUE)); + assertThat((long) QueryResultUtil.getValue(resultSet, 1), is(Long.valueOf(Integer.MAX_VALUE))); assertThat((int) QueryResultUtil.getValue(resultSet, 1, int.class), is(Integer.MAX_VALUE)); + } + @Test + public void assertGetValueByUnSignedInteger() throws SQLException { + when(resultSetMetaData.getColumnType(1)).thenReturn(Types.INTEGER); + when(resultSetMetaData.isSigned(1)).thenReturn(false); + when(resultSet.getLong(1)).thenReturn(Long.MAX_VALUE); + when(resultSet.getInt(1)).thenReturn(Integer.MAX_VALUE); + assertThat((long) QueryResultUtil.getValue(resultSet, 1), is(Long.MAX_VALUE)); + assertThat((int) QueryResultUtil.getValue(resultSet, 1, int.class), is(Integer.MAX_VALUE)); } @Test public void assertGetValueByBigint() throws SQLException { when(resultSetMetaData.getColumnType(1)).thenReturn(Types.BIGINT); + when(resultSetMetaData.isSigned(1)).thenReturn(true); when(resultSet.getLong(1)).thenReturn(Long.MAX_VALUE); assertThat((long) QueryResultUtil.getValue(resultSet, 1), is(Long.MAX_VALUE)); assertThat((long) QueryResultUtil.getValue(resultSet, 1, long.class), is(Long.MAX_VALUE)); } + @Test + public void assertGetValueByUnSignedBigint() throws SQLException { + when(resultSetMetaData.getColumnType(1)).thenReturn(Types.BIGINT); + when(resultSetMetaData.isSigned(1)).thenReturn(false); + when(resultSet.getBigDecimal(1)).thenReturn(new BigDecimal(Long.MAX_VALUE)); + assertThat((BigInteger) QueryResultUtil.getValue(resultSet, 1), is(BigInteger.valueOf(Long.MAX_VALUE))); + } + @Test public void assertGetValueByNumeric() throws SQLException { when(resultSetMetaData.getColumnType(1)).thenReturn(Types.NUMERIC); @@ -120,7 +139,6 @@ public void assertGetValueByDecimal() throws SQLException { when(resultSet.getBigDecimal(1)).thenReturn(BigDecimal.TEN); assertThat((BigDecimal) QueryResultUtil.getValue(resultSet, 1), is(BigDecimal.TEN)); assertThat((BigDecimal) QueryResultUtil.getValue(resultSet, 1, BigDecimal.class), is(BigDecimal.TEN)); - } @Test @@ -138,7 +156,6 @@ public void assertGetValueByDouble() throws SQLException { when(resultSet.getDouble(1)).thenReturn(Double.MAX_VALUE); assertThat((double) QueryResultUtil.getValue(resultSet, 1), is(Double.MAX_VALUE)); assertThat((double) QueryResultUtil.getValue(resultSet, 1, double.class), is(Double.MAX_VALUE)); - } @Test @@ -181,7 +198,6 @@ public void assertGetValueByVarBinary() throws SQLException { when(resultSet.getBlob(1)).thenReturn(blob); assertThat((Blob) QueryResultUtil.getValue(resultSet, 1), is(blob)); assertThat((Blob) QueryResultUtil.getValue(resultSet, 1, Blob.class), is(blob)); - } @Test @@ -191,7 +207,6 @@ public void assertGetValueByLongVarBinary() throws SQLException { when(resultSet.getBlob(1)).thenReturn(blob); assertThat((Blob) QueryResultUtil.getValue(resultSet, 1), is(blob)); assertThat((Blob) QueryResultUtil.getValue(resultSet, 1, Blob.class), is(blob)); - } @Test