Changeset: 3ea8fee77829 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=3ea8fee77829
Modified Files:
java/src/main/java/nl/cwi/monetdb/jdbc/MonetPreparedStatement.java
Branch: Jul2015
Log Message:
JDBC driver: Added patch for setBigDecimal() from S?ren Haagerup
diffs (59 lines):
diff --git a/java/src/main/java/nl/cwi/monetdb/jdbc/MonetPreparedStatement.java
b/java/src/main/java/nl/cwi/monetdb/jdbc/MonetPreparedStatement.java
--- a/java/src/main/java/nl/cwi/monetdb/jdbc/MonetPreparedStatement.java
+++ b/java/src/main/java/nl/cwi/monetdb/jdbc/MonetPreparedStatement.java
@@ -991,35 +991,30 @@ public class MonetPreparedStatement
*/
@Override
public void setBigDecimal(int idx, BigDecimal x)
- throws SQLException
+ throws SQLException
{
- // get array position
- int i = getParamIdx(idx);
+ // get array position
+ int i = getParamIdx(idx);
- // We need to shave off enough digits to bring ourselves to an
- // acceptable precision if we currently have too many digits.
- int digitsToShave = Math.max(0, x.precision() - digits[i]);
- int targetScale = Math.min(scale[i], x.scale() - digitsToShave);
+ // round to the scale of the DB:
+ x = x.setScale(scale[i], RoundingMode.HALF_UP);
- // However, if we need to shave off more digits than we have
available
- // to the right of the decimal point, then this is impossible.
- if (targetScale < 0)
- throw new SQLDataException("DECIMAL value exceeds
allowed digits/scale: " + x.toPlainString() + " (" + digits[i] + "/" + scale[i]
+ ")", "22003");
+ // if precision is now greater than that of the db, throw an error:
+ if (x.precision() > digits[i]) {
+ throw new SQLDataException("DECIMAL value exceeds allowed
digits/scale: " + x.toPlainString() + " (" + digits[i] + "/" + scale[i] + ")",
"22003");
+ }
- // Reduction is possible via rounding; do it and we're good to
go.
- x = x.round(new MathContext(targetScale, RoundingMode.HALF_UP));
-
- // MonetDB doesn't like leading 0's, since it counts them as
part of
- // the precision, so let's strip them off. (But be careful not
to do
- // this to the exact number "0".) Also strip off trailing
- // numbers that are inherent to the double representation.
- String xStr = x.toPlainString();
- int dot = xStr.indexOf(".");
- if (dot >= 0)
- xStr = xStr.substring(0, Math.min(xStr.length(), dot +
1 + scale[i]));
- while (xStr.startsWith("0") && xStr.length() > 1)
- xStr = xStr.substring(1);
- setValue(idx, xStr);
+ // MonetDB doesn't like leading 0's, since it counts them as part of
+ // the precision, so let's strip them off. (But be careful not to do
+ // this to the exact number "0".) Also strip off trailing
+ // numbers that are inherent to the double representation.
+ String xStr = x.toPlainString();
+ int dot = xStr.indexOf(".");
+ if (dot >= 0)
+ xStr = xStr.substring(0, Math.min(xStr.length(), dot + 1 +
scale[i]));
+ while (xStr.startsWith("0") && xStr.length() > 1)
+ xStr = xStr.substring(1);
+ setValue(idx, xStr);
}
/**
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list