diff --git a/org/postgresql/core/BaseConnection.java b/org/postgresql/core/BaseConnection.java index 6373d53..2ed095c 100644 --- a/org/postgresql/core/BaseConnection.java +++ b/org/postgresql/core/BaseConnection.java @@ -154,4 +154,13 @@ public interface BaseConnection extends PGConnection, Connection * @return True for binary transfer, false for text transfer. */ public boolean binaryTransferSend(int oid); + + /** + * True if primitive types should be returned wherever possible, false for + * the default (usually object types in 8.3+ as they can represent nulls). + * If true, {@code null} values should be represented as {@code 0}. + * + * @return True if primitive types should be returned wherever possible + */ + public boolean forcePrimitives(); } diff --git a/org/postgresql/jdbc2/AbstractJdbc2Array.java b/org/postgresql/jdbc2/AbstractJdbc2Array.java index 23bc4f7..7190754 100644 --- a/org/postgresql/jdbc2/AbstractJdbc2Array.java +++ b/org/postgresql/jdbc2/AbstractJdbc2Array.java @@ -95,7 +95,7 @@ public abstract class AbstractJdbc2Array private AbstractJdbc2Array(BaseConnection connection, int oid) throws SQLException { this.connection = connection; this.oid = oid; - this.useObjects = connection.haveMinimumCompatibleVersion("8.3"); + this.useObjects = connection.haveMinimumCompatibleVersion("8.3") && !connection.forcePrimitives(); this.haveMinServer82 = connection.haveMinimumServerVersion("8.2"); } diff --git a/org/postgresql/jdbc2/AbstractJdbc2Connection.java b/org/postgresql/jdbc2/AbstractJdbc2Connection.java index 905df2b..b963a8c 100644 --- a/org/postgresql/jdbc2/AbstractJdbc2Connection.java +++ b/org/postgresql/jdbc2/AbstractJdbc2Connection.java @@ -77,6 +77,8 @@ public abstract class AbstractJdbc2Connection implements BaseConnection /** Set of oids that use binary transfer when receiving from server. */ private Set useBinaryReceiveForOids; + private final boolean forcePrimitives; + public abstract DatabaseMetaData getMetaData() throws SQLException; // @@ -260,6 +262,8 @@ public abstract class AbstractJdbc2Connection implements BaseConnection openStackTrace = new Throwable("Connection was created at this point:"); enableDriverManagerLogging(); } + + forcePrimitives = Boolean.parseBoolean(info.getProperty("forcePrimitives")); } private Set getOidSet(String oidList) throws PSQLException { @@ -1234,4 +1238,9 @@ public abstract class AbstractJdbc2Connection implements BaseConnection { return protoConnection.getBackendPID(); } + + @Override + public boolean forcePrimitives() { + return forcePrimitives; + } } diff --git a/org/postgresql/test/TestUtil.java b/org/postgresql/test/TestUtil.java index 7c6c612..0042085 100644 --- a/org/postgresql/test/TestUtil.java +++ b/org/postgresql/test/TestUtil.java @@ -45,6 +45,11 @@ public class TestUtil sendBufferSize = "&sendBufferSize="+getSendBufferSize(); } + String forcePrimitives = ""; + if (getForcePrimitives() != null && !getForcePrimitives().equals("")) { + forcePrimitives = "&forcePrimitives="+getForcePrimitives(); + } + return "jdbc:postgresql://" + getServer() + ":" + getPort() + "/" @@ -54,7 +59,8 @@ public class TestUtil + protocolVersion + binaryTransfer + receiveBufferSize - + sendBufferSize; + + sendBufferSize + + forcePrimitives; } /* @@ -126,6 +132,11 @@ public class TestUtil return System.getProperty("binaryTransfer"); } + public static String getForcePrimitives() + { + return System.getProperty("forcePrimitives"); + } + public static int getSendBufferSize() { return Integer.parseInt(System.getProperty("sendBufferSize", "-1")); diff --git a/org/postgresql/test/jdbc4/Jdbc4TestSuite.java b/org/postgresql/test/jdbc4/Jdbc4TestSuite.java index 0bf8b30..fbc9455 100644 --- a/org/postgresql/test/jdbc4/Jdbc4TestSuite.java +++ b/org/postgresql/test/jdbc4/Jdbc4TestSuite.java @@ -31,6 +31,7 @@ public class Jdbc4TestSuite extends TestSuite suite.addTestSuite(ArrayTest.class); suite.addTestSuite(ConnectionTest.class); suite.addTestSuite(WrapperTest.class); + suite.addTestSuite(PrimitivesTest.class); Connection connection = TestUtil.openDB(); try diff --git a/org/postgresql/test/jdbc4/PrimitivesTest.java b/org/postgresql/test/jdbc4/PrimitivesTest.java new file mode 100644 index 0000000..a0708d0 --- /dev/null +++ b/org/postgresql/test/jdbc4/PrimitivesTest.java @@ -0,0 +1,46 @@ +package org.postgresql.test.jdbc4; + +import java.sql.Array; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.postgresql.test.TestUtil; + +import junit.framework.TestCase; + +public class PrimitivesTest extends TestCase { + + private Connection _conn; + + public PrimitivesTest(String name) { + super(name); + } + + protected void setUp() throws Exception { + System.setProperty("forcePrimitives", "true"); + _conn = TestUtil.openDB(); + } + + protected void tearDown() throws SQLException { + TestUtil.closeDB(_conn); + System.clearProperty("forcePrimitives"); + } + + public void testCreateArrayOfInt() throws SQLException { + PreparedStatement pstmt = _conn.prepareStatement("SELECT ?::int[]"); + Integer in[] = { 0, -1, 2 }; + pstmt.setArray(1, _conn.createArrayOf("int4", in)); + + ResultSet rs = pstmt.executeQuery(); + assertTrue(rs.next()); + Array arr = rs.getArray(1); + int out[] = (int[]) arr.getArray(); + + assertEquals(3, out.length); + assertEquals(0, out[0]); + assertEquals(-1, out[1]); + assertEquals(2, out[2]); + } +}