From: Paolo Ambrosio <p.ambrosio@tecnoteca.it>
To: pgsql-jdbc@postgresql.org
Subject: getSuperTables
Date: Thu, 29 Apr 2010 14:49:29 +0200 (CEST)
Message-ID: <21934325.2411272545369820.JavaMail.root@zimbra> (raw)
In-Reply-To: <29382200.2391272545227538.JavaMail.root@zimbra>
Hello,
I need the JDBC3 method getSuperTables for a software I'm working on.
I have implemented it using syntax and coding guidelines as close as possible to the rest of the surronding code. I tried it with PostgreSQL 8.4, and I don't know the structure of the catalog for version 7, so it throws an exception if the server version not at least 8.0.
I am attaching the patch and I would appreciate if someone could take a look at it, and perhaps commit it into the cvs repository.
Regards,
Paolo
Attachments:pgjdbc-getsupertables.diff (text/x-patch, 3.8K) download
Index: org/postgresql/jdbc3/AbstractJdbc3DatabaseMetaData.java
===================================================================
RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/jdbc3/AbstractJdbc3DatabaseMetaData.java,v
retrieving revision 1.14
diff -r1.14 AbstractJdbc3DatabaseMetaData.java
153,154c153,154
< * @param catalog a catalog name; "" retrieves those without a catalog;
< * <code>null</code> means drop catalog name from the selection criteria
---
> * @param catalog a catalog name; For org.postgresql, this is ignored, and
> * should be set to null
166c166,191
< throw org.postgresql.Driver.notImplemented(this.getClass(), "getSuperTables(String,String,String,String)");
---
> String select;
> String orderby;
> if (connection.haveMinimumServerVersion("8.0"))
> {
> select = "SELECT NULL AS TABLE_CAT, n.nspname AS TABLE_SCHEM, t.relname AS TABLE_NAME, " +
> " st.relname AS SUPERTABLE_NAME " +
> " FROM pg_catalog.pg_inherits AS i " +
> " JOIN pg_catalog.pg_class AS t ON i.inhrelid = t.oid " +
> " JOIN pg_catalog.pg_namespace AS n ON t.relnamespace = n.oid " +
> " JOIN pg_catalog.pg_class AS st ON i.inhparent = st.oid";
> if (schemaPattern != null && !"".equals(schemaPattern))
> {
> select += " AND n.nspname LIKE '" + escapeQuotes(schemaPattern) + "' ";
> }
> if (tableNamePattern != null)
> {
> select += " AND t.relname LIKE '" + escapeQuotes(tableNamePattern) + "' ";
> }
> orderby = " ORDER BY TABLE_SCHEM,TABLE_NAME ";
> }
> else
> {
> throw org.postgresql.Driver.notImplemented(this.getClass(), "getSuperTables(String,String,String,String)");
> }
> String sql = select + orderby;
> return createMetaDataStatement().executeQuery(sql);
Index: org/postgresql/test/jdbc3/DatabaseMetaDataTest.java
===================================================================
RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/test/jdbc3/DatabaseMetaDataTest.java,v
retrieving revision 1.2
diff -r1.2 DatabaseMetaDataTest.java
12a13
> import org.postgresql.util.PSQLException;
28a30
> Statement stmt = _conn.createStatement();
30d31
< Statement stmt = _conn.createStatement();
33a35,36
> stmt.execute("CREATE TABLE supertab ()");
> stmt.execute("CREATE TABLE subtab () INHERITS (supertab)");
37a41,43
> Statement stmt = _conn.createStatement();
> stmt.execute("DROP TABLE subtab");
> stmt.execute("DROP TABLE supertab");
39d44
< Statement stmt = _conn.createStatement();
60a66,95
> public void testGetSuperTables() throws Exception
> {
> DatabaseMetaData dbmd = _conn.getMetaData();
> if (TestUtil.haveMinimumServerVersion(_conn, "8.0"))
> {
> ResultSet rs;
> rs = dbmd.getSuperTables(null, null, null);
> assertTrue(rs.next());
> assertEquals(null, rs.getString("TABLE_CAT"));
> assertEquals("public", rs.getString("TABLE_SCHEM"));
> assertEquals("subtab", rs.getString("TABLE_NAME"));
> assertEquals("supertab", rs.getString("SUPERTABLE_NAME"));
> assertFalse(rs.next());
> rs = dbmd.getSuperTables(null, "%lic", "sub%");
> assertTrue(rs.next());
> rs = dbmd.getSuperTables(null, null, "sup%");
> assertFalse(rs.next());
> }
> else
> {
> try
> {
> dbmd.getSuperTables(null, null, null);
> fail();
> }
> catch (PSQLException e)
> {
> }
> }
> }
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: p.ambrosio@tecnoteca.it
Subject: Re: getSuperTables
In-Reply-To: <21934325.2411272545369820.JavaMail.root@zimbra>
References: <29382200.2391272545227538.JavaMail.root@zimbra> <21934325.2411272545369820.JavaMail.root@zimbra>
* 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