pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Mike Martin <mmartin@vieo.com>
To: pgsql-jdbc@postgresql.org
Subject: Wrong column names in ResultSetMetaData
Date: Wed, 28 Jul 2004 18:43:44 -0500
Message-ID: <ce9dna$pne$1@news.hub.org> (raw)

With the new V3 driver the column names in ResultSetMetaData
don't reflect the aliases used in the SQL.  I.e. if I do:

    SELECT name as name_alias FROM ...

the metadata says I have a result column called "name" instead
of "name_alias".

Client: PostgreSQL 7.5devel JDBC2 with SSL (build 304)
Server: PostgreSQL 7.4.2 on i386-redhat-linux-gnu, compiled by GCC 2.96

The code below works with the 7.4 driver but fails with 7.5.
Is this more likely to be in the driver or in the server-side
V3 code?

Mike


AliasTest.java
-------------------

import java.sql.*;
import java.util.*;

public class AliasTest
{
    public static void main(String args[]) throws Exception
    {
        if (args.length != 3)
            throw new IllegalArgumentException("usage: java AliasTest
<database_spec> <username> <password>");

        Class.forName("org.postgresql.Driver");

        Connection conn = DriverManager.getConnection("jdbc:postgresql:" +
args[0], args[1], args[2]);
        Statement stmt = conn.createStatement();

        try { stmt.execute("DROP TABLE alias_test"); } catch (SQLException
e) {}
        stmt.execute("CREATE TABLE alias_test ( name varchar(64) not null
primary key )");
        stmt.executeUpdate("INSERT INTO alias_test ( name ) VALUES (
'abc' )");
        stmt.close();

        PreparedStatement ps = conn.prepareStatement("SELECT name as
name_alias FROM alias_test");
        ResultSet rs = ps.executeQuery();

        if (!rs.next()) throw new Exception("No row?");

        List expectedColumnNames = Arrays.asList(new String[] {
"name_alias" });
        List actualColumnNames = new ArrayList();
        ResultSetMetaData rsmd = rs.getMetaData();

        int colCount = rsmd.getColumnCount();

        for (int colNum = 1; colNum <= colCount; ++colNum)
            actualColumnNames.add(rsmd.getColumnName(colNum));

        if (!expectedColumnNames.equals(actualColumnNames))
            throw new Exception("Wrong column names in result set metadata,
expected " +
                expectedColumnNames + ", got " + actualColumnNames);

        rs.close();
        ps.close();
        conn.close();
    }
}





view thread (27+ messages)  latest in thread

Message-ID: <ce9dna$pne$1@news.hub.org>
Permalink:  ../ce9dna$pne$1@news.hub.org/
Also on:    postgresql.org/message-id/ce9dna$pne$1@news.hub.org

 · 

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: mmartin@vieo.com
  Subject: Re: Wrong column names in ResultSetMetaData
  In-Reply-To: <ce9dna$pne$1@news.hub.org>
  References: <ce9dna$pne$1@news.hub.org>

* 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