Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes QueryResultUtil#getValueByColumnType unsigned int & bigint return value. #3288

Merged
merged 5 commits into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ private static Object getValueByColumnType(final ResultSet resultSet, final int
case Types.TINYINT:
case Types.SMALLINT:
case Types.INTEGER:
return resultSet.getInt(columnIndex);
return getIntTypeValue(resultSet, columnIndex);
terrymanu marked this conversation as resolved.
Show resolved Hide resolved
case Types.BIGINT:
return resultSet.getLong(columnIndex);
return getBigIntTypeValue(resultSet, columnIndex);
case Types.NUMERIC:
case Types.DECIMAL:
return resultSet.getBigDecimal(columnIndex);
Expand Down Expand Up @@ -135,4 +135,28 @@ private static Object getValueByColumnType(final ResultSet resultSet, final int
return resultSet.getObject(columnIndex);
}
}

private static Object getIntTypeValue(final ResultSet resultSet, final int columnIndex) throws SQLException {
terrymanu marked this conversation as resolved.
Show resolved Hide resolved
ResultSetMetaData metaData = resultSet.getMetaData();
if (metaData.isSigned(columnIndex)) {
return resultSet.getInt(columnIndex);
}
long value = resultSet.getLong(columnIndex);
if (value > Integer.MAX_VALUE) {
return value;
}
return (int) value;
}

private static Object getBigIntTypeValue(final ResultSet resultSet, final int columnIndex) throws SQLException {
ResultSetMetaData metaData = resultSet.getMetaData();
if (metaData.isSigned(columnIndex)) {
return resultSet.getLong(columnIndex);
}
BigDecimal value = resultSet.getBigDecimal(columnIndex);
if (new BigDecimal(Long.MAX_VALUE).compareTo(value) <= -1) {
return value;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe return value.toBigInteger()?

Copy link
Contributor Author

@dongzl dongzl Oct 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe return value.toBigInteger()?

Hi, yes, return BigInteger is a good way, as the same with ShardingSphere team review suggestion. Thanks.

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use BigInteger replace BigDecimal may be better.

can use getBigDecimal().toBigInteger to get BigInteger

return value.longValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

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 @@ -71,40 +71,91 @@ public void assertGetValueByBoolean() throws SQLException {
assertTrue((boolean) QueryResultUtil.getValue(resultSet, 1));
assertTrue((boolean) QueryResultUtil.getValue(resultSet, 1, boolean.class));
}

@Test
public void assertGetValueByTinyint() throws SQLException {
public void assertGetValueBySignedTinyint() throws SQLException {
when(resultSetMetaData.getColumnType(1)).thenReturn(Types.TINYINT);
when(resultSetMetaData.isSigned(1)).thenReturn(true);
when(resultSet.getInt(1)).thenReturn(1);
assertThat((int) QueryResultUtil.getValue(resultSet, 1), is(1));
assertThat((int) QueryResultUtil.getValue(resultSet, 1, int.class), is(1));
}

@Test
public void assertGetValueBySmallint() throws SQLException {
public void assertGetValueByUnSignedTinyint() throws SQLException {
when(resultSetMetaData.getColumnType(1)).thenReturn(Types.TINYINT);
when(resultSetMetaData.isSigned(1)).thenReturn(false);
when(resultSet.getLong(1)).thenReturn(Long.valueOf(1));
assertThat((int) QueryResultUtil.getValue(resultSet, 1), is(1));
}

@Test
public void assertGetValueBySignedSmallint() throws SQLException {
when(resultSetMetaData.getColumnType(1)).thenReturn(Types.SMALLINT);
when(resultSetMetaData.isSigned(1)).thenReturn(true);
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 assertGetValueByUnSignedSmallint() throws SQLException {
when(resultSetMetaData.getColumnType(1)).thenReturn(Types.SMALLINT);
when(resultSetMetaData.isSigned(1)).thenReturn(false);
when(resultSet.getLong(1)).thenReturn(Long.valueOf(32767));
assertThat((int) QueryResultUtil.getValue(resultSet, 1), is(32767));
}

@Test
public void assertGetValueByInteger() throws SQLException {
public void assertGetValueBySignedInteger() 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((int) QueryResultUtil.getValue(resultSet, 1, int.class), is(Integer.MAX_VALUE));

}

@Test
public void assertGetValueByUnSignedBeyondIntegerRange() throws SQLException {
when(resultSetMetaData.getColumnType(1)).thenReturn(Types.INTEGER);
when(resultSetMetaData.isSigned(1)).thenReturn(false);
when(resultSet.getLong(1)).thenReturn(Long.MAX_VALUE);
assertThat((long) QueryResultUtil.getValue(resultSet, 1), is(Long.MAX_VALUE));
}

@Test
public void assertGetValueByUnSignedInIntegerRange() throws SQLException {
when(resultSetMetaData.getColumnType(1)).thenReturn(Types.INTEGER);
when(resultSetMetaData.isSigned(1)).thenReturn(false);
when(resultSet.getLong(1)).thenReturn(Long.valueOf(Integer.MAX_VALUE));
assertThat((int) QueryResultUtil.getValue(resultSet, 1), is(Integer.MAX_VALUE));
}

@Test
public void assertGetValueByBigint() throws SQLException {
public void assertGetValueBySignedBigint() 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 assertGetValueByUnSignedBeyondBigintRange() throws SQLException {
when(resultSetMetaData.getColumnType(1)).thenReturn(Types.BIGINT);
when(resultSetMetaData.isSigned(1)).thenReturn(false);
BigDecimal bigDecimal = new BigDecimal(Long.MAX_VALUE).multiply(new BigDecimal(2));
when(resultSet.getBigDecimal(1)).thenReturn(bigDecimal);
assertThat((BigDecimal) QueryResultUtil.getValue(resultSet, 1), is(bigDecimal));
}

@Test
public void assertGetValueByUnSignedInBigintRange() 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((long) QueryResultUtil.getValue(resultSet, 1), is(Long.MAX_VALUE));
}

@Test
public void assertGetValueByNumeric() throws SQLException {
Expand Down Expand Up @@ -181,7 +232,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 +241,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