pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
how to obtain column info
5+ messages / 5 participants
[nested] [flat]

* how to obtain column info
@ 1998-11-11 00:38 Ken J. Wright <ken@ori-ind.com>
  1998-11-11 01:34 ` Re: [INTERFACES] how to obtain column info Cary B. O'Brien <cobrien@access.digex.net>
  1998-11-11 05:33 ` Re: [INTERFACES] how to obtain column info Brett McCormick <brett@funky.chicken.org>
  1998-11-11 11:44 ` Re: [INTERFACES] how to obtain column info Herouth Maoz <herouth@oumail.openu.ac.il>
  0 siblings, 3 replies; 5+ messages in thread

From: Ken J. Wright @ 1998-11-11 00:38 UTC (permalink / raw)
  To: pgsql-interfaces

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.

Thanks,

Ken



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

* Re: [INTERFACES] how to obtain column info
  1998-11-11 00:38 how to obtain column info Ken J. Wright <ken@ori-ind.com>
@ 1998-11-11 01:34 ` Cary B. O'Brien <cobrien@access.digex.net>
  2 siblings, 0 replies; 5+ messages in thread

From: Cary B. O'Brien @ 1998-11-11 01:34 UTC (permalink / raw)
  To: ken@ori-ind.com; +Cc: pgsql-interfaces

> 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 usually do a 'select * from table_x where oid < 0' or something like
that to get an empty result set.  This might be a stupid thing to do on
a big table, thought.  You might want to select a column with an index.
Anyone have a better idea?

Actually, you should be able to get the information out of the pg_ tables.

-- cary



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

* Re: [INTERFACES] how to obtain column info
  1998-11-11 00:38 how to obtain column info Ken J. Wright <ken@ori-ind.com>
@ 1998-11-11 05:33 ` Brett McCormick <brett@funky.chicken.org>
  2 siblings, 0 replies; 5+ messages in thread

From: Brett McCormick @ 1998-11-11 05:33 UTC (permalink / raw)
  To: Ken J. Wright <ken@ori-ind.com>; +Cc: pgsql-interfaces


If you just want to know whether or not to quote it, just see if it
has any non-numerical characters.  If so, quote it, otherwise, leave
it alone.  Seems like their would be a loss in performace if you
checked the types instead of just quoting everything (which works
fine).

On Tue, 10 November 1998, at 16:38:02, Ken J. Wright wrote:

> 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.
> 
> Thanks,
> 
> Ken
> 



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

* Re: [INTERFACES] how to obtain column info
  1998-11-11 00:38 how to obtain column info Ken J. Wright <ken@ori-ind.com>
@ 1998-11-11 11:44 ` Herouth Maoz <herouth@oumail.openu.ac.il>
  2 siblings, 0 replies; 5+ messages in thread

From: Herouth Maoz @ 1998-11-11 11:44 UTC (permalink / raw)
  To: Cary B. O'Brien <cobrien@access.digex.net>; ken@ori-ind.com; +Cc: pgsql-interfaces

At 3:34 +0200 on 11/11/98, Cary B. O'Brien wrote:


> I usually do a 'select * from table_x where oid < 0' or something like
> that to get an empty result set.

I always maintain that it's more elegant to write

SELECT *
FROM table_x
WHERE 'false'::bool;

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma





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

* Re: [INTERFACES] how to obtain column info
@ 1998-11-11 01:28 Bryan White <brywhite@visi.net>
  0 siblings, 0 replies; 5+ messages in thread

From: Bryan White @ 1998-11-11 01:28 UTC (permalink / raw)
  To: pgsql-interfaces; Ken J. Wright <ken@ori-ind.com>

>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().




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


end of thread, other threads:[~1998-11-11 11:44 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1998-11-11 00:38 how to obtain column info Ken J. Wright <ken@ori-ind.com>
1998-11-11 01:34 ` Cary B. O'Brien <cobrien@access.digex.net>
1998-11-11 05:33 ` Brett McCormick <brett@funky.chicken.org>
1998-11-11 11:44 ` Herouth Maoz <herouth@oumail.openu.ac.il>
1998-11-11 01:28 Re: [INTERFACES] how to obtain column info Bryan White <brywhite@visi.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