pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Get a table name
8+ messages / 7 participants
[nested] [flat]

* Get a table name
@ 2009-12-27 10:32  Gianvito Pio <pio.gianvito@gmail.com>
  0 siblings, 2 replies; 8+ messages in thread

From: Gianvito Pio @ 2009-12-27 10:32 UTC (permalink / raw)
  To: pgsql-jdbc

Hi all,
I'm trying to get the table name of a column in this way:

ResultSet rs;
         try
         {
             rs = ps.executeQuery();
             ResultSetMetaData rsmd = rs.getMetaData();
             System.out.println(rsmd.getTableName(..));

but the getTableName( ..) method gives me an empty string. Isn't there a 
way to know the table name of a specific field I'm obtaining? Thanks



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Get a table name
@ 2009-12-27 12:59  Filip Rembiałkowski <filip.rembialkowski@gmail.com>
  parent: Gianvito Pio <pio.gianvito@gmail.com>
  1 sibling, 1 reply; 8+ messages in thread

From: Filip Rembiałkowski @ 2009-12-27 12:59 UTC (permalink / raw)
  To: Gianvito Pio <pio.gianvito@gmail.com>; pgsql-jdbc

In general, no. That's because resultset columns can be "entangled" in
more complex way than "column a of table b".

2009/12/27, Gianvito Pio <pio.gianvito@gmail.com>:
> Hi all,
> I'm trying to get the table name of a column in this way:
>
> ResultSet rs;
>          try
>          {
>              rs = ps.executeQuery();
>              ResultSetMetaData rsmd = rs.getMetaData();
>              System.out.println(rsmd.getTableName(..));
>
> but the getTableName( ..) method gives me an empty string. Isn't there a
> way to know the table name of a specific field I'm obtaining? Thanks
>
> --
> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-jdbc
>

-- 
Wysłane z mojego urządzenia przenośnego

Filip Rembiałkowski
JID,mailto:filip.rembialkowski@gmail.com
http://filip.rembialkowski.net/



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Get a table name
@ 2009-12-27 22:12  Maciek Sakrejda <msakrejda@truviso.com>
  parent: Filip Rembiałkowski <filip.rembialkowski@gmail.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Maciek Sakrejda @ 2009-12-27 22:12 UTC (permalink / raw)
  To: Filip Rembiałkowski <filip.rembialkowski@gmail.com>; +Cc: Gianvito Pio <pio.gianvito@gmail.com>; pgsql-jdbc

>In general, no. That's because resultset columns can be "entangled" in more complex way than "column a of table b".

Actually, this *is* defined at the protocol level:



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Get a table name
@ 2009-12-28 01:28  Oliver Jowett <oliver@opencloud.com>
  parent: Gianvito Pio <pio.gianvito@gmail.com>
  1 sibling, 1 reply; 8+ messages in thread

From: Oliver Jowett @ 2009-12-28 01:28 UTC (permalink / raw)
  To: Gianvito Pio <pio.gianvito@gmail.com>; +Cc: pgsql-jdbc

Gianvito Pio wrote:
> Hi all,
> I'm trying to get the table name of a column in this way:
> 
> ResultSet rs;
>         try
>         {
>             rs = ps.executeQuery();
>             ResultSetMetaData rsmd = rs.getMetaData();
>             System.out.println(rsmd.getTableName(..));
> 
> but the getTableName( ..) method gives me an empty string. Isn't there a 
> way to know the table name of a specific field I'm obtaining? Thanks

You can use PGResultSetMetaData.getBaseTableName() if you want to know 
the name of the underlying table that provided the data for a column 
(where known).

We concluded a while back that getTableName() is meant to return the 
aliased table name in the query, not the underlying table name. It's not 
easy for the driver to find the aliased name, so it always returns an 
empty string, as you found.

-O



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Get a table name
@ 2009-12-28 01:31  Oliver Jowett <oliver@opencloud.com>
  parent: Maciek Sakrejda <msakrejda@truviso.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Oliver Jowett @ 2009-12-28 01:31 UTC (permalink / raw)
  To: Maciek Sakrejda <msakrejda@truviso.com>; +Cc: Filip Rembiałkowski <filip.rembialkowski@gmail.com>; Gianvito Pio <pio.gianvito@gmail.com>; pgsql-jdbc

Maciek Sakrejda wrote:
>> In general, no. That's because resultset columns can be "entangled" in more complex way than "column a of table b".
> 
> Actually, this *is* defined at the protocol level:

> No idea whether JDBC exposes this or whether the server actually sends
> the correct oids, or if this is just part of the protocol as a
> future-proofing attempt.

The driver exposes this via PGResultSetMetadata.getBaseTableName(), and 
I believe the server does send useful data here.

-O




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Get a table name
@ 2009-12-28 01:38  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Oliver Jowett <oliver@opencloud.com>
  0 siblings, 0 replies; 8+ messages in thread

From: Tom Lane @ 2009-12-28 01:38 UTC (permalink / raw)
  To: Oliver Jowett <oliver@opencloud.com>; +Cc: Maciek Sakrejda <msakrejda@truviso.com>; Filip Rembiałkowski <filip.rembialkowski@gmail.com>; Gianvito Pio <pio.gianvito@gmail.com>; pgsql-jdbc

Oliver Jowett <oliver@opencloud.com> writes:
> Maciek Sakrejda wrote:
>> No idea whether JDBC exposes this or whether the server actually sends
>> the correct oids, or if this is just part of the protocol as a
>> future-proofing attempt.

> The driver exposes this via PGResultSetMetadata.getBaseTableName(), and 
> I believe the server does send useful data here.

For the record, my recollection is that that part of the protocol was
put in specifically at the request of the JDBC hackers.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Get a table name
@ 2009-12-28 01:42  dmp <danap@ttc-cmc.net>
  0 siblings, 0 replies; 8+ messages in thread

From: dmp @ 2009-12-28 01:42 UTC (permalink / raw)
  To: pgsql-jdbc

> Gianvito Pio wrote:
> Hi all,
> I'm trying to get the table name of a column in this way:
>
> ResultSet rs;
>         try
>         {
>             rs = ps.executeQuery();
>             ResultSetMetaData rsmd = rs.getMetaData();
>             System.out.println(rsmd.getTableName(..));
>
> but the getTableName( ..) method gives me an empty string. Isn't there 
> a way to know the table name of a specific field I'm obtaining? Thanks


Well that is interesting.
danap.

      // Method Instances
    
      String sqlStatementString;
      Statement sqlStatement;
      ResultSet rs, db_resultSet;
      DatabaseMetaData dbMetaData;
      ResultSetMetaData tableMetaData;

       // ====================================================
      // Setting Up the Column Names, Form Fields, ComboBox
      // Text, Hashmaps, Special Fields, & Primary Key(s).

         sqlStatementString = "SELECT * FROM " + schemaTableName + " 
LIMIT 1";
         System.out.println(sqlStatementString);

         db_resultSet = sqlStatement.executeQuery(sqlStatementString);

         // Primary Key(s)
         dbMetaData = dbConnection.getMetaData();
         tableMetaData = db_resultSet.getMetaData();
         System.out.println("TableName:" + tableMetaData.getTableName(1));
         System.out.println("CatalogName:" + 
tableMetaData.getCatalogName(1));
         System.out.println("SchemaName:" + tableMetaData.getSchemaName(1));

         rs = dbMetaData.getPrimaryKeys(tableMetaData.getCatalogName(1),
                                        tableMetaData.getSchemaName(1),
                                        tableMetaData.getTableName(1));
         while (rs.next())
         {
            if (rs.getString("COLUMN_NAME").indexOf("chunk") == -1 &&
                rs.getString("TABLE_NAME").equals(tableName))
            {
               primaryKeys.add(rs.getString("COLUMN_NAME"));
               System.out.println(rs.getString("TABLE_NAME") + " " + 
rs.getString("Column_NAME"));
            }
         }

DBTablesPanel actionPerformed() Connection Created
SELECT * FROM "public"."keY_tAble2" LIMIT 1
TableName:
CatalogName:
SchemaName:
keY_tAble2 Host
keY_tAble2 Db
keY_tAble2 Username
DBTablesPanel actionPerformed() Connection Closed



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Get a table name
@ 2009-12-29 12:20  Kris Jurka <books@ejurka.com>
  parent: Oliver Jowett <oliver@opencloud.com>
  0 siblings, 0 replies; 8+ messages in thread

From: Kris Jurka @ 2009-12-29 12:20 UTC (permalink / raw)
  To: Oliver Jowett <oliver@opencloud.com>; +Cc: Gianvito Pio <pio.gianvito@gmail.com>; pgsql-jdbc



On Mon, 28 Dec 2009, Oliver Jowett wrote:

> We concluded a while back that getTableName() is meant to return the 
> aliased table name in the query, not the underlying table name. It's not 
> easy for the driver to find the aliased name, so it always returns an 
> empty string, as you found.
>

Just recently I was reading the Mysql JDBC driver's documentation [1] and 
came across the "useColumnNamesInFindColumn" config option that comes with 
this description:

     Prior to JDBC-4.0, the JDBC specification had a bug related to what
     could be given as a "column name" to ResultSet methods like
     findColumn(), or getters that took a String property. JDBC-4.0
     clarified "column name" to mean the label, as given in an "AS"
     clause and returned by ResultSetMetaData.getColumnLabel(), and if
     no AS clause, the column name. Setting this property to "true" will
     give behavior that is congruent to JDBC-3.0 and earlier versions of
     the JDBC specification, but which because of the specification bug
     could give unexpected results. This property is preferred over
     "useOldAliasMetadataBehavior" unless you need the specific
     behavior that it provides with respect to ResultSetMetadata.

This claims that we've got things backwards now despite our extensive 
previous discussion because the spec was misleading. I gave the JDBC 4 
spec another read and didn't find anything particularly revealing, but 
taking a look at the Javadoc for getColumnLabel [2] does clear things up:

     Gets the designated column's suggested title for use in printouts
     and displays. The suggested title is usually specified by the SQL
     AS clause. If a SQL AS is not specified, the value returned from
     getColumnLabel will be the same as the value returned by the
     getColumnName method.

For reference the previous discussion started at [3] and continued to [4]. 
So it looks like we should fix this up and ensure there's a way of 
getting to old mode as well.

Kris Jurka

[1] http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html
[2] http://java.sun.com/javase/6/docs/api/java/sql/ResultSetMetaData.html#getColumnLabel(int)
[3] http://archives.postgresql.org/pgsql-jdbc/2004-07/threads.php#00314
[4] http://archives.postgresql.org/pgsql-jdbc/2004-08/threads.php#00008



^ permalink  raw  reply  [nested|flat] 8+ messages in thread


end of thread, other threads:[~2009-12-29 12:20 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2009-12-27 10:32 Get a table name Gianvito Pio <pio.gianvito@gmail.com>
2009-12-27 12:59 ` Filip Rembiałkowski <filip.rembialkowski@gmail.com>
2009-12-27 22:12   ` Maciek Sakrejda <msakrejda@truviso.com>
2009-12-28 01:31     ` Oliver Jowett <oliver@opencloud.com>
2009-12-28 01:38       ` Tom Lane <tgl@sss.pgh.pa.us>
2009-12-28 01:28 ` Oliver Jowett <oliver@opencloud.com>
2009-12-29 12:20   ` Kris Jurka <books@ejurka.com>
2009-12-28 01:42 Re: Get a table name dmp <danap@ttc-cmc.net>

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