pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feed From: Dave Cramer <pg@fastcrypt.com>
To: pgsql-jdbc@postgresql.org <pgsql-jdbc@postgresql.org>
Subject: internal type cache, and getUDT implementation
Date: Fri, 21 May 2004 09:37:43 -0400
Message-ID: <1085146663.1687.52.camel@localhost.localdomain> (raw )
I've been working on getUDT's and in order to fully comply with the
spec, java.sql.Types.* has to be returned for the base type in the case
of a domain.
This requires quite a big select statement, which essentially does a
case 'char' then java.sql.VARCHAR.... attached is the patch for this.
However at one time I considered translating the returned value in the
result set. This required that I knew the pg_type oid for each type in
advance. Currently the driver gets and cache's these in a static hashmap
as it requires them.
This is either inefficient or flawed depending on the way you look at
it.
Flawed: The assumption here is that we don't know the type oid's in
advance and that the backend might change them. If this is the case and
the driver happens to be connected to two different versions ( with
different pg_type oid's ) of backends, then one of them will be wrong,
as the cache is static.
Inefficient: I've asked on hackers and they are willing to guarantee
that type oid's won't change, so why bother looking them up ?
Dave
--
Dave Cramer
519 939 0336
ICQ # 14675561
Attachments:
UDT.patch (text/x-patch, 14.7K) download
Index: jdbc2/AbstractJdbc2Connection.java
===================================================================
RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/jdbc2/AbstractJdbc2Connection.java,v
retrieving revision 1.8
diff -c -b -B -r1.8 AbstractJdbc2Connection.java
*** jdbc2/AbstractJdbc2Connection.java 15 Jan 2004 08:50:39 -0000 1.8
--- jdbc2/AbstractJdbc2Connection.java 21 May 2004 13:30:42 -0000
***************
*** 113,119 ****
*
* Tip: keep these grouped together by the Types. value
*/
! private static final String jdbc2Types[] = {
"int2",
"int4", "oid",
"int8",
--- 113,119 ----
*
* Tip: keep these grouped together by the Types. value
*/
! static final String jdbc2Types[] = {
"int2",
"int4", "oid",
"int8",
***************
*** 142,148 ****
*
* Tip: keep these grouped together by the Types. value
*/
! private static final int jdbc2Typei[] = {
Types.SMALLINT,
Types.INTEGER, Types.INTEGER,
Types.BIGINT,
--- 142,148 ----
*
* Tip: keep these grouped together by the Types. value
*/
! static final int jdbc2Typei[] = {
Types.SMALLINT,
Types.INTEGER, Types.INTEGER,
Types.BIGINT,
Index: jdbc2/AbstractJdbc2DatabaseMetaData.java
===================================================================
RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java,v
retrieving revision 1.7
diff -c -b -B -r1.7 AbstractJdbc2DatabaseMetaData.java
*** jdbc2/AbstractJdbc2DatabaseMetaData.java 24 Apr 2004 20:58:16 -0000 1.7
--- jdbc2/AbstractJdbc2DatabaseMetaData.java 21 May 2004 13:30:43 -0000
***************
*** 107,114 ****
return true;
}
! /*
! * Return user defined types in a schema
*/
public java.sql.ResultSet getUDTs(String catalog,
String schemaPattern,
--- 107,120 ----
return true;
}
! /**
! *
! * @param catalog String
! * @param schemaPattern String
! * @param typeNamePattern String
! * @param types int[]
! * @throws SQLException
! * @return ResultSet
*/
public java.sql.ResultSet getUDTs(String catalog,
String schemaPattern,
***************
*** 116,122 ****
int[] types
) throws SQLException
{
! throw org.postgresql.Driver.notImplemented();
}
--- 122,207 ----
int[] types
) throws SQLException
{
! String sql = "select "
! + "null as type_cat, n.nspname as type_schem, t.typname as type_name, null as class_name, "
! + "CASE WHEN t.typtype='c' then "+ java.sql.Types.STRUCT + " else " + java.sql.Types.DISTINCT +" end as data_type, pg_catalog.obj_description(t.oid, 'pg_type') "
! + "as remarks, CASE WHEN t.typtype = 'd' then (select CASE";
!
! for ( int i=0; i < AbstractJdbc2Connection.jdbc2Types.length; i++ )
! {
! sql += " when typname = '"+ AbstractJdbc2Connection.jdbc2Types[i] + "' then " + AbstractJdbc2Connection.jdbc2Typei[i] ;
! }
!
! sql += "else " + java.sql.Types.OTHER + " end from pg_type where oid=t.typbasetype) "
! + "else null end as base_type "
! + "from pg_catalog.pg_type t, pg_catalog.pg_namespace n where t.typnamespace = n.oid and n.nspname != 'pg_catalog' and n.nspname != 'pg_toast'";
!
!
!
! String toAdd = "";
! if ( types != null )
! {
! toAdd += " and ( ";
! for (int i = 0; i < types.length; i++)
! {
! toAdd += " t.typtype = ";
! switch (types[i] )
! {
! case java.sql.Types.STRUCT:
! toAdd += "'c'";
! break;
! case java.sql.Types.DISTINCT:
! toAdd += "'d'";
! break;
!
! case java.sql.Types.JAVA_OBJECT:
! default:
! toAdd += "'j'"; // this is totally bogus, but we need to fill in the other side of the equals, just in case
! break;
! }
! if ( i+1 < types.length )
! {
! toAdd += " or ";
! }
! }
! toAdd += " ) ";
! }
! // spec says that if typeNamePattern is not null then the schema and catalog are ignored
!
! if (typeNamePattern != null)
! {
! // search for qualifier
! int firstQualifier = typeNamePattern.indexOf('.') ;
! int secondQualifier = typeNamePattern.lastIndexOf('.');
!
! if ( firstQualifier != -1 ) // if one of them is -1 they both will be
! {
! if ( firstQualifier != secondQualifier )
! {
! // we have a catalog.schema.typename, ignore catalog
! schemaPattern = typeNamePattern.substring(firstQualifier+1, secondQualifier);
! }
! else
! {
! // we just have a schema.typename
! schemaPattern = typeNamePattern.substring(0,firstQualifier);
! }
! // strip out just the typeName
! typeNamePattern = typeNamePattern.substring(secondQualifier+1);
! }
! toAdd += " and t.typname like '" + escapeQuotes(typeNamePattern) + "'";
! }
!
! // schemaPattern may have been modified above
! if ( schemaPattern != null)
! {
! toAdd += " and n.nspname like '" + escapeQuotes(schemaPattern) +"'";
! }
! sql += toAdd;
! sql += " order by data_type, type_schem, type_name";
! java.sql.ResultSet rs = createMetaDataStatement().executeQuery(sql);
!
! return rs;
}
Index: test/jdbc2/DatabaseMetaDataTest.java
===================================================================
RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java,v
retrieving revision 1.22
diff -c -b -B -r1.22 DatabaseMetaDataTest.java
*** test/jdbc2/DatabaseMetaDataTest.java 12 Dec 2003 18:30:27 -0000 1.22
--- test/jdbc2/DatabaseMetaDataTest.java 21 May 2004 13:30:43 -0000
***************
*** 524,527 ****
--- 524,756 ----
}
}
+ public void testGetUDTQualified()
+ {
+ Statement stmt = null;
+ try
+ {
+ stmt = con.createStatement();
+ stmt.execute("create schema jdbc");
+ stmt.execute("create type jdbc.testint8 as (i int8)");
+ DatabaseMetaData dbmd = con.getMetaData();
+ ResultSet rs = dbmd.getUDTs(null, null ,"jdbc.testint8" , null);
+ assertTrue(rs.next());
+ String cat, schema, typeName, remarks, className;
+ int dataType;
+ int baseType;
+
+ cat = rs.getString("type_cat");
+ schema = rs.getString("type_schem");
+ typeName = rs.getString( "type_name");
+ className = rs.getString("class_name");
+ dataType = rs.getInt("data_type");
+ remarks = rs.getString( "remarks" );
+ baseType = rs.getInt("base_type");
+ this.assertEquals("type name ", "testint8", typeName);
+ this.assertEquals("schema name ", "jdbc",schema);
+
+ // now test to see if the fully qualified stuff works as planned
+ rs = dbmd.getUDTs("catalog", "public" ,"catalog.jdbc.testint8" , null);
+ assertTrue(rs.next());
+ cat = rs.getString("type_cat");
+ schema = rs.getString("type_schem");
+ typeName = rs.getString( "type_name");
+ className = rs.getString("class_name");
+ dataType = rs.getInt("data_type");
+ remarks = rs.getString( "remarks" );
+ baseType = rs.getInt("base_type");
+ this.assertEquals("type name ", "testint8", typeName);
+ this.assertEquals("schema name ", "jdbc",schema);
+ }
+ catch( SQLException ex )
+ {
+ fail( ex.getMessage() );
+ }
+ finally
+ {
+ try
+ {
+ if (stmt != null )
+ stmt.close();
+ stmt = con.createStatement();
+ stmt.execute("drop type jdbc.testint8");
+ stmt.execute("drop schema jdbc");
+ }
+ catch( Exception ex ){}
+ }
+
+ }
+ public void testGetUDT1()
+ {
+ try
+ {
+ Statement stmt = con.createStatement();
+ stmt.execute("create domain testint8 as int8");
+ stmt.execute("comment on domain testint8 is 'jdbc123'" );
+ DatabaseMetaData dbmd = con.getMetaData();
+ ResultSet rs = dbmd.getUDTs(null, null ,"testint8" , null);
+ assertTrue(rs.next());
+ String cat, schema, typeName, remarks, className;
+ int dataType;
+ int baseType;
+
+ cat = rs.getString("type_cat");
+ schema = rs.getString("type_schem");
+ typeName = rs.getString( "type_name");
+ className = rs.getString("class_name");
+ dataType = rs.getInt("data_type");
+ remarks = rs.getString( "remarks" );
+
+ baseType = rs.getInt("base_type");
+ this.assertTrue("base type", !rs.wasNull() );
+ this.assertEquals("data type", Types.DISTINCT, dataType );
+ this.assertEquals("type name ", "testint8", typeName);
+ this.assertEquals("remarks", "jdbc123", remarks);
+
+ }
+ catch( SQLException ex )
+ {
+ fail( ex.getMessage() );
+ }
+ finally
+ {
+ try
+ {
+ Statement stmt = con.createStatement();
+ stmt.execute("drop domain testint8");
+ }
+ catch( Exception ex ){}
+ }
+ }
+
+
+ public void testGetUDT2()
+ {
+ try
+ {
+ Statement stmt = con.createStatement();
+ stmt.execute("create domain testint8 as int8");
+ stmt.execute("comment on domain testint8 is 'jdbc123'");
+ DatabaseMetaData dbmd = con.getMetaData();
+ ResultSet rs = dbmd.getUDTs(null, null, "testint8", new int[]
+ {Types.DISTINCT, Types.STRUCT});
+ assertTrue(rs.next());
+ String cat, schema, typeName, remarks, className;
+ int dataType;
+ int baseType;
+
+ cat = rs.getString("type_cat");
+ schema = rs.getString("type_schem");
+ typeName = rs.getString("type_name");
+ className = rs.getString("class_name");
+ dataType = rs.getInt("data_type");
+ remarks = rs.getString("remarks");
+
+ baseType = rs.getInt("base_type");
+ this.assertTrue("base type", !rs.wasNull());
+ this.assertEquals("data type", Types.DISTINCT, dataType);
+ this.assertEquals("type name ", "testint8", typeName);
+ this.assertEquals("remarks", "jdbc123", remarks);
+
+ }
+ catch (SQLException ex)
+ {
+ fail(ex.getMessage());
+ }
+ finally
+ {
+ try
+ {
+ Statement stmt = con.createStatement();
+ stmt.execute("drop domain testint8");
+ }
+ catch (Exception ex)
+ {}
+ }
+ }
+ public void testGetUDT3()
+ {
+ try
+ {
+ Statement stmt = con.createStatement();
+ stmt.execute("create domain testint8 as int8");
+ stmt.execute("comment on domain testint8 is 'jdbc123'");
+ DatabaseMetaData dbmd = con.getMetaData();
+ ResultSet rs = dbmd.getUDTs(null, null, "testint8", new int[]
+ {Types.DISTINCT});
+ assertTrue(rs.next());
+ String cat, schema, typeName, remarks, className;
+ int dataType;
+ int baseType;
+
+ cat = rs.getString("type_cat");
+ schema = rs.getString("type_schem");
+ typeName = rs.getString("type_name");
+ className = rs.getString("class_name");
+ dataType = rs.getInt("data_type");
+ remarks = rs.getString("remarks");
+
+ baseType = rs.getInt("base_type");
+ this.assertTrue("base type", !rs.wasNull());
+ this.assertEquals("data type", Types.DISTINCT, dataType);
+ this.assertEquals("type name ", "testint8", typeName);
+ this.assertEquals("remarks", "jdbc123", remarks);
+
+ }
+ catch (SQLException ex)
+ {
+ fail(ex.getMessage());
+ }
+ finally
+ {
+ try
+ {
+ Statement stmt = con.createStatement();
+ stmt.execute("drop domain testint8");
+ }
+ catch (Exception ex)
+ {}
+ }
+ }
+
+ public void testGetUDT4()
+ {
+ try
+ {
+ Statement stmt = con.createStatement();
+ stmt.execute("create type testint8 as (i int8)");
+ DatabaseMetaData dbmd = con.getMetaData();
+ ResultSet rs = dbmd.getUDTs(null, null ,"testint8" , null);
+ assertTrue(rs.next());
+ String cat, schema, typeName, remarks, className;
+ int dataType;
+ int baseType;
+
+ cat = rs.getString("type_cat");
+ schema = rs.getString("type_schem");
+ typeName = rs.getString( "type_name");
+ className = rs.getString("class_name");
+ dataType = rs.getInt("data_type");
+ remarks = rs.getString( "remarks" );
+
+ baseType = rs.getInt("base_type");
+ this.assertTrue("base type", rs.wasNull() );
+ this.assertEquals("data type", Types.STRUCT, dataType );
+ this.assertEquals("type name ", "testint8", typeName);
+
+ }
+ catch( SQLException ex )
+ {
+ fail( ex.getMessage() );
+ }
+ finally
+ {
+ try
+ {
+ Statement stmt = con.createStatement();
+ stmt.execute("drop type testint8");
+ }
+ catch( Exception ex ){}
+ }
+ }
}
Message-ID: <1085146663.1687.52.camel@localhost.localdomain>
Permalink: ../1085146663.1687.52.camel@localhost.localdomain/
Also on: postgresql.org/message-id/1085146663.1687.52.camel@localhost.localdomain
copy link · copy postgr.es · copy git am
reply Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: pg@fastcrypt.com
Subject: Re: internal type cache, and getUDT implementation
In-Reply-To: <1085146663.1687.52.camel@localhost.localdomain>
References: <1085146663.1687.52.camel@localhost.localdomain>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox