Skip to content

Commit

Permalink
fixes QueryResultUtil#getValueByColumnType unsigned int & bigint retu…
Browse files Browse the repository at this point in the history
…rn 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)
  • Loading branch information
董宗磊 authored and terrymanu committed Oct 17, 2019
1 parent 0ae7bd3 commit 502240b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand All @@ -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.<Object>is(1));
assertThat(queryResult.getValue(1, Integer.class), Is.<Object>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.<Object>is(1));
assertThat(queryResult.getValue("order_id", Integer.class), Is.<Object>is(Long.valueOf(1)));
}

@Test
Expand Down Expand Up @@ -155,21 +155,22 @@ 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;
}

@Test
public void assertGetCalendarValueWithColumnIndex() throws SQLException {
MemoryQueryResult queryResult = new MemoryQueryResult(getResultSet());
queryResult.next();
assertThat(queryResult.getCalendarValue(1, Integer.class, Calendar.getInstance()), Is.<Object>is(1));
assertThat(queryResult.getCalendarValue(1, Integer.class, Calendar.getInstance()), Is.<Object>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.<Object>is(1));
assertThat(queryResult.getCalendarValue("order_id", Integer.class, Calendar.getInstance()), Is.<Object>is(Long.valueOf(1)));
}

@Test
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 502240b

Please sign in to comment.