Thread: Re: [INTERFACES] how to obtain column info

Re: [INTERFACES] how to obtain column info

From
"Bryan White"
Date:
>With libpq, how do you obtain column info (like type) for a table *before*
>returning a result set? For instance, knowing the column type before
>constructing an insert statement. A date will need quotes, a number won't,
>a string will, etc.


I just had to figure this one out myself.

First do a select of the form: select * from table where 'false'::bool;

Then call PQftype to get the type OID for fields.

Now comes the part that was not well documented and somebody tell me if
there is a better way.  On my machine the pgsql source is in /usr/src/pgsql
and the install files are in /usr/local/pgsql.  The file
/usr/src/pgsql/src/include/catalog/pg_type.h contains #defines for the type
oid values.  Unfortunatly it also contains lots of other stuff that the 'c'
compiler might not like.  Actually with the right macros you could probably
use it as it.  Note this file is not in /usr/local/pgsql/catalog where you
might expect it.

What I did was copy the file to another directory and strip out all the
funky stuff.  What you want are the lines of the form:

#define XXXXOID NN

where XXXX is a type and NN is a number.  Compare these values against the
return of PQftype().