Skip to content

Commit

Permalink
Use the doubleValue of non-BigDecimal Number arguments to setObject.
Browse files Browse the repository at this point in the history
Previously the longValue was used, which meant that floating point numbers
would lose their fractional part. This commit creates a new BigDecimal from
the doubleValue instead.
  • Loading branch information
ingramj committed Feb 27, 2016
1 parent db08fc1 commit d070dce
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Expand Up @@ -844,7 +844,7 @@ public void setObject(final int parameterIndex, final Object obj, final int targ
if (obj instanceof BigDecimal) {
setBigDecimal(parameterIndex, (BigDecimal) obj);
} else {
setLong(parameterIndex, bd.longValue());
setBigDecimal(parameterIndex, new BigDecimal(bd.doubleValue()));
}
break;
case Types.BIT:
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/org/mariadb/jdbc/PreparedStatementTest.java
Expand Up @@ -31,6 +31,7 @@ public static void initClass() throws SQLException {
+ "`Webinar10-TM/ProjComp` text",
"ENGINE=InnoDB DEFAULT CHARSET=utf8");
createTable("test_insert_select","`field1` varchar(20)");
createTable("test_decimal_insert", "`field1` decimal(10, 7)");
}

@Test
Expand Down Expand Up @@ -113,6 +114,24 @@ public void testBigInt() throws SQLException {
assertEquals(0, rs.getBigDecimal(1).toBigInteger().compareTo(bigT));
}

/**
* setObject should not truncate doubles.
*
* @throws SQLException exception
*/
@Test
public void testDoubleToDecimal() throws SQLException {
PreparedStatement stmt = sharedConnection.prepareStatement("INSERT INTO test_decimal_insert (field1) VALUES (?)");
Double value = 0.3456789;
stmt.setObject(1, value, Types.DECIMAL, 7);
stmt.executeUpdate();
stmt = sharedConnection.prepareStatement("SELECT `field1` FROM test_decimal_insert");
ResultSet rs = stmt.executeQuery();

assertTrue(rs.next());
assertEquals(value, rs.getDouble(1), 0.00000001);
}

@Test
public void testPreparedStatementsWithQuotes() throws SQLException {

Expand Down

0 comments on commit d070dce

Please sign in to comment.