postgresql-jdbc.git / summary / log / commit / refs
commit ced437167ea554fca9f27201fefb732b5e2de7bb
Author: Kris Jurka <books@ejurka.com>
Date: Tue Apr 15 05:22:14 2008 +0000
For array types, return information about their base types for
things like getPrecision.
org/postgresql/jdbc2/TypeInfoCache.java | 23 +++++++++++++++++++++-
.../test/jdbc2/DatabaseMetaDataTest.java | 18 ++++++++++++++++-
2 files changed, 39 insertions(+), 2 deletions(-)
diff --git a/org/postgresql/jdbc2/TypeInfoCache.java b/org/postgresql/jdbc2/TypeInfoCache.java
index b16e4b44..7f3da180 100644
--- a/org/postgresql/jdbc2/TypeInfoCache.java
+++ b/org/postgresql/jdbc2/TypeInfoCache.java
@@ -3,7 +3,7 @@
* Copyright (c) 2005-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
- * $PostgreSQL: pgjdbc/org/postgresql/jdbc2/TypeInfoCache.java,v 1.13 2008/04/15 04:23:57 jurka Exp $
+ * $PostgreSQL: pgjdbc/org/postgresql/jdbc2/TypeInfoCache.java,v 1.14 2008/04/15 04:49:59 jurka Exp $
*
*-------------------------------------------------------------------------
*/
@@ -266,6 +266,21 @@ public class TypeInfoCache implements TypeInfo {
return getPGType("_" + elementTypeName);
}
+ /**
+ * Return the oid of the array's base element if it's an array,
+ * if not return the provided oid. This doesn't do any database
+ * lookups, so it's only useful for the originally provided type
+ * mappings. This is fine for it's intended uses where we only have
+ * intimate knowledge of types that are already known to the driver.
+ */
+ protected synchronized int convertArrayToBaseOid(int oid)
+ {
+ Integer i = (Integer)_pgArrayToPgType.get(new Integer(oid));
+ if (i == null)
+ return oid;
+ return i.intValue();
+ }
+
public synchronized int getPGArrayElement (int oid) throws SQLException
{
if (oid == Oid.UNSPECIFIED)
@@ -336,6 +351,7 @@ public class TypeInfoCache implements TypeInfo {
}
public int getPrecision(int oid, int typmod) {
+ oid = convertArrayToBaseOid(oid);
switch (oid) {
case Oid.INT2:
return 5;
@@ -397,6 +413,7 @@ public class TypeInfoCache implements TypeInfo {
}
public int getScale(int oid, int typmod) {
+ oid = convertArrayToBaseOid(oid);
switch(oid) {
case Oid.FLOAT4:
return 8;
@@ -423,6 +440,7 @@ public class TypeInfoCache implements TypeInfo {
}
public boolean isCaseSensitive(int oid) {
+ oid = convertArrayToBaseOid(oid);
switch(oid) {
case Oid.OID:
case Oid.INT2:
@@ -447,6 +465,7 @@ public class TypeInfoCache implements TypeInfo {
}
public boolean isSigned(int oid) {
+ oid = convertArrayToBaseOid(oid);
switch(oid) {
case Oid.INT2:
case Oid.INT4:
@@ -461,6 +480,7 @@ public class TypeInfoCache implements TypeInfo {
}
public int getDisplaySize(int oid, int typmod) {
+ oid = convertArrayToBaseOid(oid);
switch(oid) {
case Oid.INT2:
return 6; // -32768 to +32767
@@ -548,6 +568,7 @@ public class TypeInfoCache implements TypeInfo {
}
public int getMaximumPrecision(int oid) {
+ oid = convertArrayToBaseOid(oid);
switch(oid) {
case Oid.NUMERIC:
return 1000;
diff --git a/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java b/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java
index 1dae7aac..ed9ccf0f 100644
--- a/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java
+++ b/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java
@@ -3,7 +3,7 @@
* Copyright (c) 2004-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
-* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java,v 1.41 2008/01/20 15:13:29 jurka Exp $
+* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java,v 1.42 2008/04/15 04:49:59 jurka Exp $
*
*-------------------------------------------------------------------------
*/
@@ -39,6 +39,7 @@ public class DatabaseMetaDataTest extends TestCase
TestUtil.createTable( con, "sercoltest", "a int, b serial, c bigserial");
TestUtil.createTable( con, "\"a\\\"", "a int4");
TestUtil.createTable( con, "\"a'\"", "a int4");
+ TestUtil.createTable( con, "arraytable", "a numeric(5,2)[], b varchar(100)[]");
Statement stmt = con.createStatement();
//we add the following comments to ensure the joins to the comments
@@ -70,6 +71,7 @@ public class DatabaseMetaDataTest extends TestCase
TestUtil.dropSequence( con, "sercoltest_c_seq");
TestUtil.dropTable( con, "\"a\\\"");
TestUtil.dropTable( con, "\"a'\"");
+ TestUtil.dropTable( con, "arraytable");
stmt.execute("DROP FUNCTION f1(int, varchar)");
if (TestUtil.haveMinimumServerVersion(con, "8.0")) {
@@ -921,4 +923,18 @@ public class DatabaseMetaDataTest extends TestCase
}
}
+ public void testInformationAboutArrayTypes() throws SQLException
+ {
+ DatabaseMetaData dbmd = con.getMetaData();
+ ResultSet rs = dbmd.getColumns("", "", "arraytable", "");
+ assertTrue(rs.next());
+ assertEquals("a", rs.getString("COLUMN_NAME"));
+ assertEquals(5, rs.getInt("COLUMN_SIZE"));
+ assertEquals(2, rs.getInt("DECIMAL_DIGITS"));
+ assertTrue(rs.next());
+ assertEquals("b", rs.getString("COLUMN_NAME"));
+ assertEquals(100, rs.getInt("COLUMN_SIZE"));
+ assertTrue(!rs.next());
+ }
+
}
[parent: 13ec63a117e0]