Permalink
Browse files

fix: setNull for types not in java.sql.Types (e.g. uuid) (#1160)

Fix PreparedStatement.setNull(pos, type, pgtype)

This enables to pass nulls for uuids like
"where ? is null"; setNull(1, Types.OTHER, "uuid");

fixes #1159
  • Loading branch information...
davecramer authored and vlsi committed Jul 14, 2018
1 parent 776e171 commit 6287c9547880b5cdfc596cf932354511a4e310cb
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
## [Unreleased]
### Changed
- PreparedStatement.setNull(int parameterIndex, int t, String typeName) no longer ignores the typeName
argument if it is not null [PR 1160](https://github.com/pgjdbc/pgjdbc/pull/1160)
### Added
@@ -1232,9 +1232,25 @@ public void setClob(int i, Clob x) throws SQLException {
setLong(i, oid);
}
public void setNull(int i, int t, String s) throws SQLException {
public void setNull(int parameterIndex, int t, String typeName) throws SQLException {
if (typeName == null) {
setNull(parameterIndex, t);
return;
}
checkClosed();
setNull(i, t);
TypeInfo typeInfo = connection.getTypeInfo();
int oid = typeInfo.getPGType(typeName);
if (adjustIndex) {
parameterIndex--;
}
preparedParameters.setNull(parameterIndex, oid);
}
public void setRef(int i, Ref x) throws SQLException {
@@ -12,6 +12,7 @@
import static org.junit.Assert.fail;
import org.postgresql.PGStatement;
import org.postgresql.core.ServerVersion;
import org.postgresql.jdbc.PgStatement;
import org.postgresql.jdbc.PreferQueryMode;
import org.postgresql.test.TestUtil;
@@ -39,6 +40,7 @@
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collection;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Handler;
@@ -285,6 +287,20 @@ public void testSetNull() throws SQLException {
pstmt.executeUpdate();
pstmt.close();
assumeMinimumServerVersion(ServerVersion.v8_3);
pstmt = con.prepareStatement("select 'ok' where ?=? or (? is null) ");
pstmt.setObject(1, UUID.randomUUID(), Types.OTHER);
pstmt.setNull(2, Types.OTHER, "uuid");
pstmt.setNull(3, Types.OTHER, "uuid");
ResultSet rs = pstmt.executeQuery();
assertTrue(rs.next());
assertEquals("ok",rs.getObject(1));
rs.close();
pstmt.close();
}
@Test
@@ -560,7 +560,7 @@ public void testToString() throws SQLException {
public void nullArray() throws SQLException {
PreparedStatement ps = con.prepareStatement("INSERT INTO arrtest(floatarr) VALUES (?)");
ps.setNull(1, Types.ARRAY, "float8");
ps.setNull(1, Types.ARRAY, "float8[]");
ps.execute();
ps.close();

0 comments on commit 6287c95

Please sign in to comment.