X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 115F0D1B276 for ; Wed, 19 May 2004 09:44:41 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 97874-03 for ; Wed, 19 May 2004 09:44:24 -0300 (ADT) Received: from net2.micro-automation.com (net2.micro-automation.com [64.7.141.29]) by svr1.postgresql.org (Postfix) with SMTP id D2785D1B23E for ; Wed, 19 May 2004 09:44:19 -0300 (ADT) Received: (qmail 1159 invoked from network); 19 May 2004 12:44:20 -0000 Received: from dcdsl.ebox.com (HELO ?192.168.1.36?) (davec@64.7.143.116) by net2.micro-automation.com with SMTP; 19 May 2004 12:44:20 -0000 Subject: Re: patch for getUDT #3 From: Dave Cramer Reply-To: pg@fastcrypt.com To: Kris Jurka Cc: "pgsql-jdbc@postgresql.org" In-Reply-To: <1084919524.22930.170.camel@localhost.localdomain> References: <40A379EB.1080106@hugsmidjan.is> <1084460311.22930.15.camel@localhost.localdomain> <1084836296.22930.102.camel@localhost.localdomain> <1084882736.29453.134.camel@localhost.localdomain> <1084919524.22930.170.camel@localhost.localdomain> Content-Type: multipart/mixed; boundary="=-hvlD4G6CCcBVylo/6FRY" Organization: Cramer Consulting Message-Id: <1084970659.22887.187.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.6 Date: Wed, 19 May 2004 08:44:19 -0400 X-AntiVirus: scanned for viruses by AMaViS 0.2.1 (http://amavis.org/) X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200405/120 X-Sequence-Number: 10130 --=-hvlD4G6CCcBVylo/6FRY Content-Type: text/plain Content-Transfer-Encoding: 7bit Just added the check to make sure the type is not in pg_catalog schema. Dave On Tue, 2004-05-18 at 18:32, Dave Cramer wrote: > Other than the hack of what do with undefined types, and one more little > problem which is how to qualify user defined types. This is more > difficult than first glance. It is possible to just say typowner != 1, > but this doesn't solve the problem if I own the cluster. > > Here is round 2 > On Tue, 2004-05-18 at 08:18, Dave Cramer wrote: > > On Mon, 2004-05-17 at 20:20, Kris Jurka wrote: > > > On Mon, 17 May 2004, Dave Cramer wrote: > > > > > > > While we don't support JAVA_OBJECT we do support struct, and distinct. > > > > > > After doing some more reading I would agree that domains == distinct and > > > complext types == struct, if that's what you're suggesting. The patch > > > shows no signs of this though, it just blindly selects everything out of > > > pg_type. > > > > Yes > > > > > > To be worthy of applying it seems this patch should: > > > > > > 1) Only return domains and complex types, correctly setting data_type and > > > base_type for domains. > > > > > > 2) Use pg_description to retrieve a comment for the remarks column. > > > > > > 3) Filter out toast entries by checking pg_namespace.nspname != 'pg_toast' > > > instead of using a like expression on type name. > > > > this must remain, as there will be numerous pg_toast_... entries created > > by large columns > > > > > > 4) Use the escapeQuotes function to ensure that the schema and type > > > patterns don't have single quotes in them. > > > > > > 5) Use the types[] parameter to filter out undesired data_type values. > > > > > > 6) Use ORDER BY to return the results in the order the spec says. > > > > > > 7) Use createMetaDataStatement() instead of createStatement() when > > > returning the query results. This makes the produced ResultSet scrollable > > > instead of the default forward only behavior. > > > > > > I would personally like to see a version of this that ran on a 7.2 server > > > because the regression tests currently pass against a 7.2 server. It > > > isn't within our stated goals of supporting the past two versions of the > > > driver, but I think it's a reasonable thing to do. > > > > This probably makes things alot more difficult as schema's don't exist > > in 7.2 but... > > > > > > Kris Jurka > > > > > > > > > > > > > > > > > > -- Dave Cramer 519 939 0336 ICQ # 14675561 --=-hvlD4G6CCcBVylo/6FRY Content-Disposition: attachment; filename=UDT.patch Content-Type: text/x-patch; name=UDT.patch; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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 19 May 2004 12:11:52 -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,197 ---- 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 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 19 May 2004 12:11:52 -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 ){} + } + } } --=-hvlD4G6CCcBVylo/6FRY--