Обсуждение: Anonymous record type and inner types.

Поиск
Список
Период
Сортировка

Anonymous record type and inner types.

От
Ronan Dunklau
Дата:
Hello.

Sorry if this question has already been asked, but I couldn't find the answer
anywhere.

When I execute a statement such as "Select (an_int, a_varchar) as a_record
from test, the type returned for a_record is "record" (oid 2249).

Is the "inner type" information propagated somehow (that an_int is an integer,
and a_varchar a varchar) ?

If so, is it easily accessible in client libraries ? I'm using psycopg2 for
python, which itself uses libpq.

Thanks.

Regards,

--
Ronan Dunklau







Вложения

Re: Anonymous record type and inner types.

От
Adrian Klaver
Дата:
On Friday, July 01, 2011 1:37:44 am Ronan Dunklau wrote:
> Hello.
>
> Sorry if this question has already been asked, but I couldn't find the
> answer anywhere.
>
> When I execute a statement such as "Select (an_int, a_varchar) as a_record
> from test, the type returned for a_record is "record" (oid 2249).
>
> Is the "inner type" information propagated somehow (that an_int is an
> integer, and a_varchar a varchar) ?

See below for complete information:
http://www.postgresql.org/docs/9.0/interactive/sql-expressions.html#SQL-SYNTAX-
ROW-CONSTRUCTORS

>
> If so, is it easily accessible in client libraries ? I'm using psycopg2 for
> python, which itself uses libpq.
>
> Thanks.
>
> Regards,
>
> --
> Ronan Dunklau

--
Adrian Klaver
adrian.klaver@gmail.com

Re: Anonymous record type and inner types.

От
Daniele Varrazzo
Дата:
On Fri, Jul 1, 2011 at 9:37 AM, Ronan Dunklau <rdunklau@gmail.com> wrote:

> When I execute a statement such as "Select (an_int, a_varchar) as a_record
> from test, the type returned for a_record is "record" (oid 2249).
>
> Is the "inner type" information propagated somehow (that an_int is an integer,
> and a_varchar a varchar) ?
>
> If so, is it easily accessible in client libraries ? I'm using psycopg2 for
> python, which itself uses libpq.

I don't think the information about the components of the composite
type are automatically propagated to the client nor made accessible by
the libpq.

You may create an user-defined type representing the record you select
and cast the result to it: for example if you "create type thing as
(the_int integer, the_text varchar);" and then "select (an_int,
a_varchar)::thing from test", postgres will pass the "thing" oid to
the client, instead of the one of the generic "record". psycopg can be
programmed to return a tuple/namedtuple allowing you to get the
components as python values: see
<http://initd.org/psycopg/docs/extras.html#composite-types-casting>
for further details.

-- Daniele