comparison src/main/java/org/monetdb/jdbc/MonetPreparedStatement.java @ 894:07a4998898a8

Improve and optimize PreparedStatement.setBigDecimal() implementation. It now checks on null input parameter to prevent NPE. Also removed code to trim leading zero's. Extended BugDecimalRound_Bug_3561() test with more values, including a null input parameter.
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 18 Apr 2024 14:37:01 +0200 (12 months ago)
parents 7621c80b08da
children d416e9b6b3d0
comparison
equal deleted inserted replaced
893:3b215a009634 894:07a4998898a8
562 * @param parameterIndex the first parameter is 1, the second is 2, ... 562 * @param parameterIndex the first parameter is 1, the second is 2, ...
563 * @param x the parameter value 563 * @param x the parameter value
564 * @throws SQLException if a database access error occurs 564 * @throws SQLException if a database access error occurs
565 */ 565 */
566 @Override 566 @Override
567 public void setBigDecimal(final int parameterIndex, BigDecimal x) throws SQLException { 567 public void setBigDecimal(final int parameterIndex, final BigDecimal x) throws SQLException
568 {
569 if (x == null) {
570 setValue(parameterIndex, "NULL");
571 return;
572 }
573
568 // get array position 574 // get array position
569 final int i = getParamIdx(parameterIndex); 575 final int i = getParamIdx(parameterIndex);
570 576 // round to the scale of the DB specification:
571 // round to the scale of the DB: 577 final BigDecimal decval = x.setScale(scale[i], java.math.RoundingMode.HALF_UP);
572 x = x.setScale(scale[i], java.math.RoundingMode.HALF_UP); 578 final String decvalStr = decval.toPlainString();
573 579
574 // if precision is now greater than that of the db, throw an error: 580 // if precision is now greater than that of the DB specification, throw an error:
575 if (x.precision() > digits[i]) { 581 if (decval.precision() > digits[i]) {
576 throw new SQLDataException("DECIMAL value exceeds allowed digits/scale: " + x.toPlainString() + " (" + digits[i] + "/" + scale[i] + ")", "22003"); 582 throw new SQLDataException("DECIMAL value '" + decvalStr + "' exceeds allowed digits,scale: (" + digits[i] + "," + scale[i] + ")", "22003");
577 } 583 }
578 584 setValue(parameterIndex, decvalStr);
579 // MonetDB doesn't like leading 0's, since it counts them as part of
580 // the precision, so let's strip them off. (But be careful not to do
581 // this to the exact number "0".) Also strip off trailing
582 // numbers that are inherent to the double representation.
583 String xStr = x.toPlainString();
584 final int dot = xStr.indexOf('.');
585 if (dot >= 0)
586 xStr = xStr.substring(0, Math.min(xStr.length(), dot + 1 + scale[i]));
587 while (xStr.startsWith("0") && xStr.length() > 1)
588 xStr = xStr.substring(1);
589 setValue(parameterIndex, xStr);
590 } 585 }
591 586
592 /** 587 /**
593 * Sets the designated parameter to the given input stream, which will have 588 * Sets the designated parameter to the given input stream, which will have
594 * the specified number of bytes. When a very large binary value is input 589 * the specified number of bytes. When a very large binary value is input