This question is specific to libpqxx.
Given an SQL statement like the following:
string s = "SELECT a.foo, b.bar FROM tableOne a, tableTwo b WHERE a.X=b.X"
and sending it to a pqxx transaction:
trans.exec(s.c_str(), s.c_str());
What names will the columns have in the result field?
In other words, assuming 1 row is selected:
pqxx::result::const_iterator row = result.begin();
int foo = row->at(FOO_COLUMN).as<int>();
int bar = row->at(BAR_COLUMN).as<int>();
What values should FOO_COLUMN and BAR_COLUMN have? Would they be "a.foo" and "b.bar", respectively?
If the SQL statement renamed the variables using the "as" keyword, then I suppose the column name would be whatever "as" set it to, is that right?
Normally I would try an SQL and print the column values, but as I am developing both the software and the database itself, doing that test is not very easy right now.
Thanks!
The names are going to be foo
and bar
. If they were aliases in the query, then the aliases would be returned, the original names being lost.
Column names in results never include table names.
If they were named tablename.colname
, it would be ambiguous anyway because SELECT 1 as "foo.colname"
is valid and produces a column foo.colname
despite the fact there is no foo
table.
The way to identify the table from which a column originates, when it applies, is to call pqxx::result::column_table(column_number)
which returns the oid
of the table. The table's name is not available directly but could be queried in pg_class
with the oid
.
Also note that column names don't have to be unique within a resultset. SELECT 1 AS a, 2 AS a
is valid and produces two columns with exactly the same name.