Skip to content
Permalink
Browse files
fix: add support for full names of data types (#1719)
* fix: add support for full names of data types

Currently the driver only recognizes alias names (e.g. "varchar",
"float8", "timetz") and not full names (e.g. "character varying",
"double precision", "time with time zone"); add support for the full
names of data types.

Closes #1563

* fix: add timestamp with timezone to full data type name array test
  • Loading branch information
MSGoodman committed Feb 26, 2020
1 parent 30843e4 commit f554118a95cda1470fc7ac8b67b8ae7c18b25826
Showing 2 changed files with 68 additions and 2 deletions.
@@ -77,9 +77,11 @@ public class TypeInfoCache implements TypeInfo {
{"money", Oid.MONEY, Types.DOUBLE, "java.lang.Double", Oid.MONEY_ARRAY},
{"numeric", Oid.NUMERIC, Types.NUMERIC, "java.math.BigDecimal", Oid.NUMERIC_ARRAY},
{"float4", Oid.FLOAT4, Types.REAL, "java.lang.Float", Oid.FLOAT4_ARRAY},
{"double precision", Oid.FLOAT8, Types.DOUBLE, "java.lang.Double", Oid.FLOAT8_ARRAY},
{"float8", Oid.FLOAT8, Types.DOUBLE, "java.lang.Double", Oid.FLOAT8_ARRAY},
{"char", Oid.CHAR, Types.CHAR, "java.lang.String", Oid.CHAR_ARRAY},
{"bpchar", Oid.BPCHAR, Types.CHAR, "java.lang.String", Oid.BPCHAR_ARRAY},
{"character varying", Oid.VARCHAR, Types.VARCHAR, "java.lang.String", Oid.VARCHAR_ARRAY},
{"varchar", Oid.VARCHAR, Types.VARCHAR, "java.lang.String", Oid.VARCHAR_ARRAY},
{"text", Oid.TEXT, Types.VARCHAR, "java.lang.String", Oid.TEXT_ARRAY},
{"name", Oid.NAME, Types.VARCHAR, "java.lang.String", Oid.NAME_ARRAY},
@@ -88,10 +90,13 @@ public class TypeInfoCache implements TypeInfo {
{"bit", Oid.BIT, Types.BIT, "java.lang.Boolean", Oid.BIT_ARRAY},
{"date", Oid.DATE, Types.DATE, "java.sql.Date", Oid.DATE_ARRAY},
{"time", Oid.TIME, Types.TIME, "java.sql.Time", Oid.TIME_ARRAY},
{"time without time zone", Oid.TIME, Types.TIME, "java.sql.Time", Oid.TIME_ARRAY},
{"timetz", Oid.TIMETZ, Types.TIME, "java.sql.Time", Oid.TIMETZ_ARRAY},
{"time with time zone", Oid.TIMETZ, Types.TIME, "java.sql.Time", Oid.TIMETZ_ARRAY},
{"timestamp", Oid.TIMESTAMP, Types.TIMESTAMP, "java.sql.Timestamp", Oid.TIMESTAMP_ARRAY},
{"timestamptz", Oid.TIMESTAMPTZ, Types.TIMESTAMP, "java.sql.Timestamp",
Oid.TIMESTAMPTZ_ARRAY},
{"timestamp without time zone", Oid.TIMESTAMP, Types.TIMESTAMP, "java.sql.Timestamp", Oid.TIMESTAMP_ARRAY},
{"timestamptz", Oid.TIMESTAMPTZ, Types.TIMESTAMP, "java.sql.Timestamp", Oid.TIMESTAMPTZ_ARRAY},
{"timestamp with time zone", Oid.TIMESTAMPTZ, Types.TIMESTAMP, "java.sql.Timestamp", Oid.TIMESTAMPTZ_ARRAY},
//#if mvn.project.property.postgresql.jdbc.spec >= "JDBC4.2"
{"refcursor", Oid.REF_CURSOR, Types.REF_CURSOR, "java.sql.ResultSet", Oid.REF_CURSOR_ARRAY},
//#endif
@@ -30,6 +30,8 @@
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collection;
@@ -172,6 +174,65 @@ public void testSetPrimitiveArraysObjects() throws SQLException {
rs.close();
}

@Test
public void testSetArraysWithFullDataTypeNames() throws SQLException {
// Create a test table and insert arrays with data types identified by their full names
TestUtil.createTable(conn, "arrtest_fullnames", "floatarr double precision[], varchararr character varying(20)[], timearr time without time zone[], tzarr timestamp with time zone[]");

PreparedStatement pstmt = conn.prepareStatement("INSERT INTO arrtest_fullnames VALUES (?,?,?,?)");

final PGConnection arraySupport = conn.unwrap(PGConnection.class);

pstmt.setArray(1, arraySupport.createArrayOf("double precision", new Object[] { 1d, 2d, 3d }));
pstmt.setObject(2, arraySupport.createArrayOf("character varying", new String[] { "abc", "f'a", "fa\"b" }));
pstmt.setObject(3, arraySupport.createArrayOf("time without time zone", new Object[] { Time.valueOf("12:34:56"), Time.valueOf("03:30:25"), Time.valueOf("05:14:53") }));
pstmt.setObject(4, arraySupport.createArrayOf("timestamp with time zone", new Object[] { "1996-01-23 12:00:00-08", "1996-07-09 05:07:30+00", "1997-08-16 16:51:00-04" }));

pstmt.executeUpdate();
pstmt.close();

// Select the created record and ensure the returned data has the correct type and content
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT floatarr, varchararr, timearr, tzarr FROM arrtest_fullnames");
Assert.assertTrue(rs.next());

Array arr = rs.getArray(1);
Assert.assertEquals(Types.DOUBLE, arr.getBaseType());
Double[] doublearr = (Double[]) arr.getArray();
Assert.assertEquals(3, doublearr.length);
Assert.assertEquals(1d, doublearr[0].intValue(), 0);
Assert.assertEquals(2d, doublearr[1].intValue(), 0);
Assert.assertEquals(3d, doublearr[2].intValue(), 0);

arr = rs.getArray(2);
Assert.assertEquals(Types.VARCHAR, arr.getBaseType());
String[] varchararr = (String[]) arr.getArray();
Assert.assertEquals(3, varchararr.length);
Assert.assertEquals("abc", varchararr[0]);
Assert.assertEquals("f'a", varchararr[1]);
Assert.assertEquals("fa\"b", varchararr[2]);

arr = rs.getArray(3);
Assert.assertEquals(Types.TIME, arr.getBaseType());
Time[] timearr = (Time []) arr.getArray();
Assert.assertEquals(3, timearr.length);
Assert.assertEquals(Time.valueOf("12:34:56"), timearr[0]);
Assert.assertEquals(Time.valueOf("03:30:25"), timearr[1]);
Assert.assertEquals(Time.valueOf("05:14:53"), timearr[2]);

arr = rs.getArray(4);
Assert.assertEquals(Types.TIMESTAMP, arr.getBaseType());
Timestamp[] tzarr = (Timestamp[]) arr.getArray();
Assert.assertEquals(3, timearr.length);
Assert.assertEquals(822427200000L, tzarr[0].getTime());
Assert.assertEquals(836888850000L, tzarr[1].getTime());
Assert.assertEquals(871764660000L, tzarr[2].getTime());

rs.close();

TestUtil.dropTable(conn, "arrtest_fullnames");
}

@Test
public void testSetNullArrays() throws SQLException {
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO arrtest VALUES (?,?,?)");

0 comments on commit f554118

Please sign in to comment.