Imre Horvath | 29 Aug 2010 15:35
Picon

composite types

Hi!

How can I use composite type array function parameters with psycopg2?

The situation is:

create type mytype as
(
        i integer;
        t character varying;
);

create function myfunc(in param mytype[]) as
.....

How can I call this function form psycopg2?

Thanks in advance:
Imre Horvath
Daniele Varrazzo | 29 Aug 2010 23:07
Picon
Gravatar

Re: composite types

On Sun, Aug 29, 2010 at 2:35 PM, Imre Horvath <blemidon@...> wrote:
> Hi!
>
> How can I use composite type array function parameters with psycopg2?
>
> The situation is:
>
> create type mytype as
> (
>        i integer;
>        t 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:

=> 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
(Continue reading)

Imre Horvath | 30 Aug 2010 08:10
Picon

Re: composite types

Thanks,
I'll use type adaptation then, i don't want to build sql string in my
client...

Imre Horvath
2010. 08. 29, vasárnap keltezéssel 22.07-kor Daniele Varrazzo ezt írta:
> On Sun, Aug 29, 2010 at 2:35 PM, Imre Horvath <blemidon <at> gmail.com> wrote:
> > Hi!
> >
> > How can I use composite type array function parameters with psycopg2?
> >
> > The situation is:
> >
> > create type mytype as
> > (
> >        i integer;
> >        t 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:
> 
> => 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:
(Continue reading)


Gmane