Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 27D7C63EC5D for ; Tue, 20 Jul 2010 17:40:39 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 78774-08 for ; Tue, 20 Jul 2010 20:40:31 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.7.6 Received: from smtp1-g21.free.fr (smtp1-g21.free.fr [212.27.42.1]) by mail.postgresql.org (Postfix) with ESMTP id 2F14463EC2C for ; Tue, 20 Jul 2010 17:40:29 -0300 (ADT) Received: from toto.localnet (unknown [78.228.108.65]) by smtp1-g21.free.fr (Postfix) with ESMTP id D878394009A for ; Tue, 20 Jul 2010 22:40:23 +0200 (CEST) From: Florence Cousin To: pgsql-jdbc@postgresql.org Subject: Patch for the documentation (PGResultSetMetaData) Date: Tue, 20 Jul 2010 22:40:21 +0200 User-Agent: KMail/1.13.5 (Linux/2.6.34-ARCH; KDE/4.4.5; x86_64; ; ) MIME-Version: 1.0 Content-Type: Multipart/Mixed; boundary="Boundary-00=_2mgRMlVts5YuXJN" Message-Id: <201007202240.22057.cousinflo@free.fr> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.8 tagged_above=-10 required=5 tests=BAYES_50=0.8, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201007/68 X-Sequence-Number: 20733 --Boundary-00=_2mgRMlVts5YuXJN Content-Type: Text/Plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Hi, I have written a short text to explain the use of PGResultSetMetaData, with an example. Your comments are welcome. Regards, Florence. --Boundary-00=_2mgRMlVts5YuXJN Content-Type: text/x-patch; charset="UTF-8"; name="pgjdbc.xml.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="pgjdbc.xml.patch" *** pgjdbc.xml.old 2009-06-20 17:19:40.000000000 +0200 --- pgjdbc.xml 2010-07-20 22:15:26.543010752 +0200 *************** *** 3187,3192 **** --- 3187,3255 ---- + + PGResultSetMetaData interface + + If a column is aliased in the query, the standard ResultSetMetaData interface can not determine the schema and table name for this column. + So for consistency reasons, getColumnName and getSchemaName always returns a zero-length string (""). + + + To determine the names of the underlying columns, table and schema of the ResultSet, you have to use the PGResultSetMetaData interface. + This interface gives the names of the columns, table and schema of the underlying base column, table or schema name if it can determine it. Note that is not possible when the column is an operation (see example below). + + + Using ResultSetMetaData and PGResultSetMetaData + + Statement stmt = null; + ResultSet resultSet = null; + + // Query + String sql = "SELECT city as town, temp_lo + 273 , temp_hi FROM weather"; + try { + stmt = connexion.createStatement(); + resultSet = stmt.executeQuery(sql); + + // Metadata + ResultSetMetaData rsm; + PGResultSetMetaData pgRsm; + + rsm = resultSet.getMetaData(); + pgRsm = (PGResultSetMetaData)rsm; + + System.out.println("1rst column : "); + System.out.println("column name : " + rsm.getColumnName(1) + "; base column : " + pgRsm.getBaseColumnName(1)); + System.out.println("table name : " + rsm.getTableName(1) + "; base table : " + pgRsm.getBaseTableName(1)); + System.out.println("schema name : " + rsm.getSchemaName(1) + "; base schema : " + pgRsm.getBaseSchemaName(1)); + System.out.println("2nd column : "); + System.out.println("column name : " + rsm.getColumnName(2) + "; base column : " + pgRsm.getBaseColumnName(2)); + System.out.println("table name : " + rsm.getTableName(2) + "; base table : " + pgRsm.getBaseTableName(2)); + System.out.println("schema name : " + rsm.getSchemaName(2) + "; base schema: " + pgRsm.getBaseSchemaName(2)); + System.out.println("3rd column : "); + System.out.println("column name : " + rsm.getColumnName(3) + "; base column : " + pgRsm.getBaseColumnName(3)); + System.out.println("table name : " + rsm.getTableName(3) + "; base table : " + pgRsm.getBaseTableName(3)); + System.out.println("schema name : " + rsm.getSchemaName(3) + "; base schema: " + pgRsm.getBaseSchemaName(3)); + + + + Which will produce the following output : + + + 1rst column : + column name : town; base column : city + table name : ; base table : weather + schema name : ; base schema : public + 2nd column : + column name : ?column?; base column : + table name : ; base table : + schema name : ; base schema: + 3rd column : + column name : temp_hi; base column : temp_hi + table name : ; base table : weather + schema name : ; base schema: public + + + + Further Reading --Boundary-00=_2mgRMlVts5YuXJN--