Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 9 commits into from Jul 14, 2018
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 ) {
Copy link
Member

@vlsi vlsi Jul 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you please trim spaces around 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,8 @@
import static org.junit.Assert.fail;

import org.postgresql.PGStatement;
import org.postgresql.core.ServerVersion;
import org.postgresql.core.Version;
import org.postgresql.jdbc.PgStatement;
import org.postgresql.jdbc.PreferQueryMode;
import org.postgresql.test.TestUtil;
Expand Down Expand Up @@ -39,6 +41,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 +288,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[]");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an interesting side effect. I think it is worth including in a notable changes. So clients get prepared for "unexpected" operation of setNull (since String argument has been just ignored for ages)

ps.execute();

ps.close();
Expand Down