From 818953a2b30480ecc874f812e46fcb82d1a395fd Mon Sep 17 00:00:00 2001 From: Dave Cramer Date: Fri, 1 Mar 2024 07:10:24 -0500 Subject: [PATCH] fix Issue # 3145 boolean types not handled in SimpleQuery mode (#3146) * make sure we handle boolean types in simple query mode * support uuid as well * handle all well known types in text mode and change else if to switch --- .../core/v3/SimpleParameterList.java | 68 ++++++++++++++----- .../test/jdbc42/PreparedStatementTest.java | 12 ++++ 2 files changed, 63 insertions(+), 17 deletions(-) diff --git a/pgjdbc/src/main/java/org/postgresql/core/v3/SimpleParameterList.java b/pgjdbc/src/main/java/org/postgresql/core/v3/SimpleParameterList.java index 9741a05edf..9d236d48e5 100644 --- a/pgjdbc/src/main/java/org/postgresql/core/v3/SimpleParameterList.java +++ b/pgjdbc/src/main/java/org/postgresql/core/v3/SimpleParameterList.java @@ -317,23 +317,57 @@ public String toString(@Positive int index, boolean standardConformingStrings) { } } else { textValue = paramValue.toString(); - int paramType = paramTypes[index]; - if (paramType == Oid.TIMESTAMP) { - type = "timestamp"; - } else if (paramType == Oid.TIMESTAMPTZ) { - type = "timestamp with time zone"; - } else if (paramType == Oid.TIME) { - type = "time"; - } else if (paramType == Oid.TIMETZ) { - type = "time with time zone"; - } else if (paramType == Oid.DATE) { - type = "date"; - } else if (paramType == Oid.INTERVAL) { - type = "interval"; - } else if (paramType == Oid.NUMERIC) { - type = "numeric"; - } else { - type = null; + switch (paramTypes[index]) { + case Oid.INT2: + type = "int2"; + break; + case Oid.INT4: + type = "int4"; + break; + case Oid.INT8: + type = "int8"; + break; + case Oid.FLOAT4: + type = "real"; + break; + case Oid.FLOAT8: + type = "double precision"; + break; + case Oid.TIMESTAMP: + type = "timestamp"; + break; + case Oid.TIMESTAMPTZ: + type = "timestamp with time zone"; + break; + case Oid.TIME: + type = "time"; + break; + case Oid.TIMETZ: + type = "time with time zone"; + break; + case Oid.DATE: + type = "date"; + break; + case Oid.INTERVAL: + type = "interval"; + break; + case Oid.NUMERIC: + type = "numeric"; + break; + case Oid.UUID: + type = "uuid"; + break; + case Oid.BOOL: + type = "boolean"; + break; + case Oid.BOX: + type = "box"; + break; + case Oid.POINT: + type = "point"; + break; + default: + type = null; } } return quoteAndCast(textValue, type, standardConformingStrings); diff --git a/pgjdbc/src/test/java/org/postgresql/test/jdbc42/PreparedStatementTest.java b/pgjdbc/src/test/java/org/postgresql/test/jdbc42/PreparedStatementTest.java index e24328eda9..d31e439a86 100644 --- a/pgjdbc/src/test/java/org/postgresql/test/jdbc42/PreparedStatementTest.java +++ b/pgjdbc/src/test/java/org/postgresql/test/jdbc42/PreparedStatementTest.java @@ -54,6 +54,18 @@ public void testSetNumber() throws SQLException { Assert.assertEquals(new BigDecimal("3.2"), d); } + @Test + public void testSetBoolean() throws SQLException { + try (PreparedStatement ps = con.prepareStatement("select false union select (select ?)")) { + ps.setBoolean(1, true); + + try (ResultSet rs = ps.executeQuery()) { + assert (rs.next()); + rs.getBoolean(1); + } + } + } + @Test public void testTimestampTzSetNull() throws SQLException { PreparedStatement pstmt = con.prepareStatement("INSERT INTO timestamptztable (tstz) VALUES (?)");