Permalink
Browse files

Merge pull request #426 from Gordiychuk/fix_bigint

fix: fix invalid values when receiving int2, int4, int8 via getBigDecimal()
  • Loading branch information...
davecramer committed Nov 13, 2015
2 parents f71150e + e6f1beb commit fad7f52a242430220fcb91617e8bac91aa2c0af4
@@ -651,7 +651,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,0 +1,18 @@
package org.postgresql.test;
import java.util.Properties;
import org.postgresql.test.jdbc2.CursorFetchTest;
public class CursorFetchBinaryTest extends CursorFetchTest {
public CursorFetchBinaryTest(String name)
{
super(name);
}
@Override
protected void updateProperties(Properties props)
{
forceBinary(props);
}
}
@@ -0,0 +1,48 @@
package org.postgresql.test.jdbc2;
import junit.framework.TestCase;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import org.postgresql.PGProperty;
import org.postgresql.test.TestUtil;
public class BaseTest extends TestCase {
protected Connection con;
public BaseTest(String name)
{
super(name);
try
{
new org.postgresql.Driver();
}
catch (Exception ex)
{
/* ignore */
}
}
protected void updateProperties(Properties props) {
}
protected void forceBinary(Properties props)
{
PGProperty.PREPARE_THRESHOLD.set(props, -1);
}
protected void setUp() throws Exception
{
Properties props = new Properties();
updateProperties(props);
con = TestUtil.openDB(props);
}
protected void tearDown() throws SQLException
{
TestUtil.closeDB(con);
}
}
@@ -0,0 +1,16 @@
package org.postgresql.test.jdbc2;
import java.util.Properties;
public class BatchExecuteBinaryTest extends BatchExecuteTest {
public BatchExecuteBinaryTest(String name)
{
super(name);
}
@Override
protected void updateProperties(Properties props)
{
forceBinary(props);
}
}
@@ -7,18 +7,14 @@
*/
package org.postgresql.test.jdbc2;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.sql.BatchUpdateException;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
import org.postgresql.test.TestUtil;
import org.postgresql.util.PSQLException;
import junit.framework.TestCase;
import java.sql.*;
/* TODO tests that can be added to this test case
* - SQLExceptions chained to a BatchUpdateException
@@ -28,26 +24,19 @@
/*
* Test case for Statement.batchExecute()
*/
public class BatchExecuteTest extends TestCase
public class BatchExecuteTest extends BaseTest
{
private Connection con;
public BatchExecuteTest(String name)
{
super(name);
try
{
Class.forName("org.postgresql.Driver");
}
catch( Exception ex){}
}
// Set up the fixture for this testcase: a connection to a database with
// a table for this test.
protected void setUp() throws Exception
{
con = TestUtil.openDB();
super.setUp();
Statement stmt = con.createStatement();
// Drop the test table if it already exists for some reason. It is
@@ -65,12 +54,12 @@ protected void setUp() throws Exception
}
// Tear down the fixture for this test case.
protected void tearDown() throws Exception
protected void tearDown() throws SQLException
{
con.setAutoCommit(true);
TestUtil.dropTable(con, "testbatch");
TestUtil.closeDB(con);
super.tearDown();
}
public void testSupportsBatchUpdates() throws Exception
@@ -9,37 +9,33 @@
import java.sql.*;
import junit.framework.TestCase;
import org.postgresql.test.TestUtil;
/*
* Tests for using non-zero setFetchSize().
*/
public class CursorFetchTest extends TestCase
public class CursorFetchTest extends BaseTest
{
private Connection con;
public CursorFetchTest(String name)
{
super(name);
}
protected void setUp() throws Exception
{
con = TestUtil.openDB();
super.setUp();
TestUtil.createTable(con, "test_fetch", "value integer");
con.setAutoCommit(false);
}
protected void tearDown() throws Exception
protected void tearDown() throws SQLException
{
if (!con.getAutoCommit())
con.rollback();
con.setAutoCommit(true);
TestUtil.dropTable(con, "test_fetch");
TestUtil.closeDB(con);
super.tearDown();
}
protected void createRows(int count) throws Exception
@@ -11,6 +11,7 @@
import junit.framework.TestSuite;
import org.postgresql.test.CursorFetchBinaryTest;
import org.postgresql.test.TestUtil;
/*
@@ -64,13 +65,15 @@ public static TestSuite suite() throws Exception
// PreparedStatement
suite.addTestSuite(PreparedStatementTest.class);
suite.addTestSuite(PreparedStatementBinaryTest.class);
suite.addTestSuite(StatementTest.class);
// ServerSide Prepared Statements
suite.addTestSuite(ServerPreparedStmtTest.class);
// BatchExecute
suite.addTestSuite(BatchExecuteTest.class);
suite.addTestSuite(BatchExecuteBinaryTest.class);
// Other misc tests, based on previous problems users have had or specific
@@ -89,6 +92,7 @@ public static TestSuite suite() throws Exception
suite.addTestSuite(CallableStmtTest.class );
suite.addTestSuite(CursorFetchTest.class);
suite.addTestSuite(CursorFetchBinaryTest.class);
suite.addTestSuite(ServerCursorTest.class);
suite.addTestSuite(IntervalTest.class);
@@ -0,0 +1,16 @@
package org.postgresql.test.jdbc2;
import java.util.Properties;
public class PreparedStatementBinaryTest extends PreparedStatementTest {
public PreparedStatementBinaryTest(String name)
{
super(name);
}
@Override
protected void updateProperties(Properties props)
{
forceBinary(props);
}
}
Oops, something went wrong.

0 comments on commit fad7f52

Please sign in to comment.