Skip to content

Commit

Permalink
fix: setNull for types not in java.sql.Types (e.g. uuid) (#1160)
Browse files Browse the repository at this point in the history
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 6287c95
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
20 changes: 18 additions & 2 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgPreparedStatement.java
Expand Up @@ -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 {
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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();
Expand Down

0 comments on commit 6287c95

Please sign in to comment.