Skip to content

Commit

Permalink
JDBC-458 Also add support for BigInteger to BigDecimal fields (numeri…
Browse files Browse the repository at this point in the history
…c, decimal)
  • Loading branch information
mrotteveel committed Nov 5, 2016
1 parent d08fd07 commit e9575de
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/main/org/firebirdsql/jdbc/field/FBBigDecimalField.java
Expand Up @@ -126,6 +126,13 @@ public BigDecimal getBigDecimal() throws SQLException {
return fieldDataSize.decode(fieldDescriptor, getFieldData());
}

@Override
public BigInteger getBigInteger() throws SQLException {
BigDecimal value = getBigDecimal();
if (value == null) return null;
return value.toBigInteger();
}

//--- setXXX methods

public void setBoolean(boolean value) throws SQLException {
Expand Down Expand Up @@ -178,6 +185,16 @@ public void setBigDecimal(BigDecimal value) throws SQLException {
setFieldData(fieldDataSize.encode(fieldDescriptor, value));
}

@Override
public void setBigInteger(BigInteger value) throws SQLException {
if (value == null) {
setNull();
return;
}

setBigDecimal(new BigDecimal(value));
}

/**
* Enum for handling the different fielddata sizes of NUMERIC/DECIMAL fields.
*
Expand Down Expand Up @@ -252,7 +269,6 @@ protected byte[] encode(FieldDescriptor fieldDescriptor, BigDecimal value) throw
* @param value
* BigDecimal instance
* @return encoded data
* @throws SQLException
*/
protected abstract byte[] encode(final FieldDescriptor fieldDescriptor,
final BigDecimal value) throws SQLException;
Expand Down
73 changes: 73 additions & 0 deletions src/test/org/firebirdsql/jdbc/field/TestFBBigDecimalField.java
Expand Up @@ -751,6 +751,79 @@ public void setStringNonNumber() throws SQLException {

field.setString("NotANumber");
}

@Test
@Override
public void getObject_BigInteger() throws SQLException {
fieldDescriptor = createLongFieldDescriptor(-2);
field = new FBBigDecimalField(fieldDescriptor, fieldData, Types.NUMERIC);
toReturnLongExpectations(456789123);

assertEquals("Unexpected value for getObject(BigInteger.class)",
BigInteger.valueOf(4567891), field.getObject(BigInteger.class));
}

@Test
public void getObject_BigInteger_null() throws SQLException {
toReturnNullExpectations();

assertNull("Unexpected value for getObject(BigInteger.class)", field.getObject(BigInteger.class));
}

@Test
@Override
public void setObject_BigInteger() throws SQLException {
fieldDescriptor = createLongFieldDescriptor(0);
field = new FBBigDecimalField(fieldDescriptor, fieldData, Types.NUMERIC);
setLongExpectations(10);

field.setObject(BigInteger.TEN);
}

@Test
public void setObject_BigInteger_MAX() throws SQLException {
fieldDescriptor = createLongFieldDescriptor(0);
field = new FBBigDecimalField(fieldDescriptor, fieldData, Types.NUMERIC);
setLongExpectations(Long.MAX_VALUE);

field.setObject(BigInteger.valueOf(Long.MAX_VALUE));
}

@Test
public void setObject_BigInteger_MAX_plus_1() throws SQLException {
fieldDescriptor = createLongFieldDescriptor(0);
field = new FBBigDecimalField(fieldDescriptor, fieldData, Types.NUMERIC);
expectedException.expect(TypeConversionException.class);

field.setObject(BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE));
}

@Test
public void setObject_BigInteger_MIN() throws SQLException {
fieldDescriptor = createLongFieldDescriptor(0);
field = new FBBigDecimalField(fieldDescriptor, fieldData, Types.NUMERIC);
setLongExpectations(Long.MIN_VALUE);

field.setObject(BigInteger.valueOf(Long.MIN_VALUE));
}

@Test
public void setObject_BigInteger_MIN_minus_1() throws SQLException {
fieldDescriptor = createLongFieldDescriptor(0);
field = new FBBigDecimalField(fieldDescriptor, fieldData, Types.NUMERIC);
expectedException.expect(TypeConversionException.class);

field.setObject(BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.ONE));
}

@Test
public void setBigInteger_null() throws SQLException {
fieldDescriptor = createLongFieldDescriptor(0);
field = new FBBigDecimalField(fieldDescriptor, fieldData, Types.NUMERIC);
setNullExpectations();

field.setBigInteger(null);
}

@SuppressWarnings("unused")
@Test
Expand Down

0 comments on commit e9575de

Please sign in to comment.