Permalink
Browse files

fix: fix invalid values when receiving int2, int4, int8 via getBigDec…

…imal()

It turns out BigDecimal.valueOf(12, 1) == "12.1" while BigDecimal.valueOf(12).setScale(1) == "12.0"

fixes #424
  • Loading branch information...
vlsi committed Nov 13, 2015
1 parent faac288 commit e6f1beb19c1581b003ac85ab29454ac58b157d96
Showing with 44 additions and 29 deletions.
  1. +1 −1 org/postgresql/jdbc2/AbstractJdbc2Array.java
  2. +43 −28 org/postgresql/jdbc2/AbstractJdbc2ResultSet.java
@@ -636,7 +636,7 @@ else if (type == Types.NUMERIC)
for (; count > 0; count--)
{
Object v = input.get(index++);
oa[length++] = dims > 1 && v != null ? buildArray((PgArrayList) v, 0, -1) : (v == null ? null : AbstractJdbc2ResultSet.toBigDecimal((String) v, -1));
oa[length++] = dims > 1 && v != null ? buildArray((PgArrayList) v, 0, -1) : (v == null ? null : AbstractJdbc2ResultSet.toBigDecimal((String) v));
}
}
@@ -2351,7 +2351,7 @@ private int getFastInt(int columnIndex) throws SQLException,
* @throws SQLException If an error occurs while fetching column.
* @throws NumberFormatException If the number is invalid or the
* out of range for fast parsing. The value must then be parsed by
* {@link #toBigDecimal(String)}.
* {@link #toBigDecimal(String, int)}.
*/
private BigDecimal getFastBigDecimal(int columnIndex) throws SQLException,
NumberFormatException {
@@ -2454,7 +2454,9 @@ public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException
Object obj = internalGetObject(columnIndex, fields[columnIndex - 1]);
if (obj == null) return null;
if (obj instanceof Long || obj instanceof Integer || obj instanceof Byte) {
return BigDecimal.valueOf(((Number) obj).longValue(), scale);
BigDecimal res = BigDecimal.valueOf(((Number) obj).longValue());
res = scaleBigDecimal(res, scale);
return res;
}
return toBigDecimal(trimMoney(String.valueOf(obj)), scale);
}
@@ -2463,7 +2465,9 @@ public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException
Encoding encoding = connection.getEncoding();
if (encoding.hasAsciiNumbers()) {
try {
return getFastBigDecimal(columnIndex);
BigDecimal res = getFastBigDecimal(columnIndex);
res = scaleBigDecimal(res, scale);
return res;
} catch (NumberFormatException ex) {
}
}
@@ -3088,34 +3092,45 @@ public static long toLong(String s) throws SQLException
return 0; // SQL NULL
}
public static BigDecimal toBigDecimal(String s, int scale) throws SQLException
public static BigDecimal toBigDecimal(String s) throws SQLException
{
BigDecimal val;
if (s != null)
if (s == null) {
return null;
}
try
{
try
{
s = s.trim();
val = new BigDecimal(s);
}
catch (NumberFormatException e)
{
throw new PSQLException(GT.tr("Bad value for type {0} : {1}", new Object[]{"BigDecimal",s}),
PSQLState.NUMERIC_VALUE_OUT_OF_RANGE);
}
if (scale == -1)
return val;
try
{
return val.setScale(scale);
}
catch (ArithmeticException e)
{
throw new PSQLException(GT.tr("Bad value for type {0} : {1}", new Object[]{"BigDecimal",s}),
PSQLState.NUMERIC_VALUE_OUT_OF_RANGE);
}
s = s.trim();
return new BigDecimal(s);
}
catch (NumberFormatException e)
{
throw new PSQLException(GT.tr("Bad value for type {0} : {1}", new Object[]{"BigDecimal",s}),
PSQLState.NUMERIC_VALUE_OUT_OF_RANGE);
}
}
public BigDecimal toBigDecimal(String s, int scale) throws SQLException
{
if (s == null) {
return null;
}
BigDecimal val = toBigDecimal(s);
return scaleBigDecimal(val, scale);
}
private BigDecimal scaleBigDecimal(BigDecimal val, int scale) throws PSQLException
{
if (scale == -1)
return val;
try
{
return val.setScale(scale);
}
catch (ArithmeticException e)
{
throw new PSQLException(GT.tr("Bad value for type {0} : {1}", new Object[]{"BigDecimal",val}),
PSQLState.NUMERIC_VALUE_OUT_OF_RANGE);
}
return null; // SQL NULL
}
public static float toFloat(String s) throws SQLException

0 comments on commit e6f1beb

Please sign in to comment.