Re: Call plpgsql function with an array of custom type

From: Rory Campbell-Lange <rory(at)campbell-lange(dot)net>
To: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
Cc: psycopg(at)postgresql(dot)org
Subject: Re: Call plpgsql function with an array of custom type
Date: 2017-06-11 15:21:00
Message-ID: 20170611152100.GA5588@campbell-lange.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

On 11/06/17, Adrian Klaver (adrian(dot)klaver(at)aklaver(dot)com) wrote:
> On 06/11/2017 03:43 AM, Rory Campbell-Lange wrote:
> >I'm prototyping a database interface to be called from Python and PHP in
> >one of our projects.
...
> >
> > register_composite('test.dow_session', cur)
> >
> >But:
> > query = 'select * from fn_test01(%s, %s)'
> >
> > qargs = (5, "[[0,2]::test.dow_session]")
> > cur.execute(query, qargs)
> > DETAIL: "[" must introduce explicitly-specified array dimensions.
> >
> > qargs = (5, "[[0,2]]::test.dow_session")
> > cur.execute(query, qargs)
> > DETAIL: "[" must introduce explicitly-specified array dimensions.
> >
> > qargs = (5, ["(0,2)::test.dow_session"])
> > cur.execute(query, qargs)
> > ProgrammingError: function fn_test01(integer, text[]) does not exist
> >
> >all don't work.
>
>
> query = 'select * from fn_test01(%s, %s::dow_session[])'
> qargs = (5, ["(0,2)", "(1, 3)"])
> cur.execute(query, qargs)
> rs = cur.fetchall()
> rs
> [('',)]

Hi Adrian

That works perfectly. Thank you so much!

In [108]: query = 'select * from fn_test03(%s, %s::dow_session[])'
In [109]: qargs = (5, [(0,2), (1, 3)])
In [110]: cur.execute(query, qargs)
In [111]: rs = cur.fetchall()
In [112]: rs
Out[112]: [(0, 2), (1, 3)]

With regards
Rory

p.s.

For the record here is a slightly modified test setup.

/* create type in postgres */
CREATE TYPE dow_session AS (
dow INT
,session INT
);

/* create test function in postgres */
CREATE OR REPLACE FUNCTION fn_test03 (
num INT
,ds dow_session[]
) RETURNS SETOF dow_session AS $$
DECLARE
r dow_session;
BEGIN
FOREACH r IN ARRAY ds
LOOP
RETURN NEXT r;
END LOOP;
RETURN;
END;
$$ LANGUAGE plpgsql;

In response to

Browse psycopg by date

  From Date Subject
Next Message Daniele Varrazzo 2017-06-12 10:33:38 Re: Site search
Previous Message Adrian Klaver 2017-06-11 15:19:49 Site search