From: Glenn Sullivan <glenn.sullivan@nmr.varian.com>
To: herouth maoz <herouth@oumail.openu.ac.il>
Cc: pgsql-sql@hub.org
Subject: Re: [SQL] Getting datatype before SELECT
Date: Thu, 01 Oct 1998 11:39:57 -0600
Message-ID: <3613BE6D.215C6663@nmr.varian.com> (raw)
References: <Pine.GSO.3.96-heb-2.07.981001001050.23056A-100000@shaked.cc.openu.ac.il>
Herouth,
This works great and is indeed faster that other methods
I have now tried.
One question:
The value I get returned from PQftype() for a varchar is 1043
and for an int is 23.
I am uncomfortable just testing for these values. I could
not find in the documentation, what the return values of
PQftype() are. Can anyone point me to that information?
Thanks to all those who have responded, for all the great input
on my original question.
Glenn
herouth maoz wrote:
>
> On Tue, 29 Sep 1998, Glenn Sullivan wrote:
>
> > In my C code which communicates with the Postgres database,
> > I have the need to determine the datatype of a column, before
> > I have done a SELECT command. I have the name of the column,
> > but I cannot seem to figure out how to get the datatype information
> > until after I have done a SELECT. Then I can call PQfnumber() and
> > PQftype() to get the type. Does anyone know how to do this?
>
> In addition to looking up the name of the column, you can use a general
> approach, which is also good in case the select is using an EXPRESSION
> rather than a column name (e.g. SELECT salary/1000 FROM emp).
>
> The approach is to work on your query, and replace its where clause (or
> add one if it doesn't have one) with boolean literal 'false'. This means
> that the query will not return any tuples, nor take much toll on the
> database, but the parser will parse it and give you back all the necessary
> type information.
>
> So, if you want to issue the following query:
>
> SELECT field1, ( field2 * 13 )
> FROM table
> WHERE field1>1000 AND (( field2 % 4 ) = 3 );
>
> And you want to check the types and lengths first, first issue the query:
>
> SELECT field1, ( field2 * 13 )
> FROM table
> WHERE 'false'::bool;
>
> You can use PQftype() etc. on the result, and only then issue the real
> query, and use the information you gathered in this "dummy" pass.
>
> Herouth
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-sql@postgresql.org
Cc: glenn.sullivan@nmr.varian.com, herouth@oumail.openu.ac.il, pgsql-sql@hub.org
Subject: Re: [SQL] Getting datatype before SELECT
In-Reply-To: <3613BE6D.215C6663@nmr.varian.com>
References: <Pine.GSO.3.96-heb-2.07.981001001050.23056A-100000@shaked.cc.openu.ac.il> <3613BE6D.215C6663@nmr.varian.com>
* 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