Re: INTERVAL data type and libpq - what format?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Sam Mason <sam(at)samason(dot)me(dot)uk>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: INTERVAL data type and libpq - what format?
Date: 2009-05-19 12:45:50
Message-ID: 12907.1242737150@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

Sam Mason <sam(at)samason(dot)me(dot)uk> writes:
> I don't really know 8.4, but I believe you're saying here that you
> explicitly want the values to be of basic INTERVAL type here, i.e. not
> INTERVAL DAY TO HOUR for parameter 3.

Right, you can get the equivalent behavior from psql thus:

regression=# select '-12345'::interval::interval year;
interval
----------
00:00:00
(1 row)

regression=# select '12 11'::interval::interval year;
ERROR: invalid input syntax for type interval: "12 11"
LINE 1: select '12 11'::interval::interval year;
^

There is not any way to bind a more specific type to a parameter at the
protocol level.

> I think PG may do the right thing if you
> don't specify the types when preparing the query, but haven't tested.

Yeah, that should work (though I haven't verified it either). Another
common trick is to specify the type in the text of the query by casting
the parameter symbol:

PQprepare( ... $2::INTERVAL YEAR ... );

I'd say this is better style than hard-wiring numeric type OIDs into
your code.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alvaro Herrera 2009-05-19 12:52:28 Re: array/function question
Previous Message hubert depesz lubaczewski 2009-05-19 12:22:27 Re: Get block of N numbers from sequence

Browse pgsql-hackers by date

  From Date Subject
Next Message Greg Stark 2009-05-19 12:52:55 Re: Multiple sorts in a query
Previous Message Sebastien FLAESCH 2009-05-19 12:17:20 Re: INTERVAL data type and libpq - what format?