Thread: PQfnumber and duplicate column names....

PQfnumber and duplicate column names....

From
Ralf Hasemann
Date:
Hi everybody,

at the moemnt I am writing a little app that uses the libpq client
library.

The function PQfnumber returns the column number associated with a
given column name.

Here is the spec of the function:

<snip------------------>
PQfnumber Returns the column number associated with the given column
name.
int PQfnumber(const PGresult *res, const char *column_name);

-1 is returned if the given name does not match any column. The given
name is treated like an identifier in an SQL command,
that is, it is downcased unless double-quoted.
For example, given a query result generated from the SQL command

select 1 as FOO, 2 as "BAR";

we would have the results:

PQfnumber(res, "FOO") 0
PQfnumber(res, "foo") 0
PQfnumber(res, "BAR") -1
PQfnumber(res, "\"BAR\"") 1
<snap------------------->

What happens if the statement looks like:

select 1 as FOO, 2 as FOO;  (the database has no probs with that)

What result will PQfnumber return ????

PQfnumber(res, "FOO") 0 or 1 ???

Thx for all help!

Ralf Hasemann


Re: PQfnumber and duplicate column names....

From
Tom Lane
Date:
Ralf Hasemann <rhasemann@mac.com> writes:
> What happens if the statement looks like:
> select 1 as FOO, 2 as FOO;  (the database has no probs with that)
> What result will PQfnumber return ????

When in doubt, look in the source code ...

/*
 * PQfnumber: find column number given column name
 *
 * The column name is parsed as if it were in a SQL statement, including
 * case-folding and double-quote processing.  But note a possible gotcha:
 * downcasing in the frontend might follow different locale rules than
 * downcasing in the backend...
 *
 * Returns -1 if no match.  In the present backend it is also possible
 * to have multiple matches, in which case the first one is found.
 */


            regards, tom lane