Re: composite types

Daniele Varrazzo <[email protected]> Sun, 29 Aug 2010 22:07:05 +0100
Newsgroups gmane.comp.python.db.psycopg.devel
Message-ID <[email protected]>
On Sun, Aug 29, 2010 at 2:35 PM, Imre Horvath <[email protected]> wrote:
> Hi!
>
> How can I use composite type array function parameters with psycopg2?
>
> The situation is:
>
> create type mytype as
> (
> =A0 =A0 =A0 =A0i integer;
> =A0 =A0 =A0 =A0t character varying;
> );
>
> create function myfunc(in param mytype[]) as
> .....
>
> How can I call this function form psycopg2?

If you are able to call the function from psql, i.e. using a syntax such as:

=3D> select myfunc(array[(10,'foo'),(20,'bar')]::mytype[]);

then you have to pass a similar string to a cur.execute (I don't think
callproc is going to be useful). You can use for instance:

>>> cur.execute("select myfunc(%s::mytype[])", ([('foo',10), ('bar',20)],))
# would call: "select myfunc(ARRAY[(E'foo', 10), (E'bar', 20)]::mytype[])"

if you have some object you defined yourself instead of a list of
tuples of strings and ints, and want psycopg to automatically convert
it, take a look at
http://initd.org/psycopg/docs/advanced.html#adapting-new-python-types-to-sq=
l-syntax

-- Daniele