Re: Dynamic binding in plpgsql function

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Pierre Racine <Pierre(dot)Racine(at)sbf(dot)ulaval(dot)ca>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Dynamic binding in plpgsql function
Date: 2011-03-01 23:17:14
Message-ID: AANLkTikmzTNN2z6ZLd8u6erSgM1-GSN8iH-WZTQd+h8Z@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello

2011/3/2 Pierre Racine <Pierre(dot)Racine(at)sbf(dot)ulaval(dot)ca>:
> Hi,
>
> I would like to write a generic plpgsql function with a text parameter being a callback function name so that my general function can call this callback function. e.g.:
>
> CREATE OR REPLACE FUNCTION ST_MyCallbackFunction(y int)
>    RETURNS int AS $$
>    DECLARE
>    BEGIN
>        RETURN someCalculationBasedOnY;
>    END;
>    $$ LANGUAGE 'plpgsql';
>
> CREATE OR REPLACE FUNCTION ST_MyGeneralFunction(callback text)
>    RETURNS SETOF geomval AS $$
>    DECLARE
>        x integer;
>        y integer;
>    BEGIN
>        y := somecalculation;
>        x := 'callback'(y);  --This is what I need

EXECUTE 'SELECT ' || callback || '($1)' USING y INTO x;

there are no other way than EXECUTE

attention - there is a sql injection risk

regards

Pavel Stehule

>        RETURN x;
>    END;
>    $$ LANGUAGE 'plpgsql';
>
> I don't want to do an EXECUTE statement since I have no table to put after the FROM clause. I want to assign the resulting value directly to a variable like in my example.
>
> Can I/How can I achieve this?
>
> Thanks,
>
> Pierre
>
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Vibhor Kumar 2011-03-01 23:23:30 Re: Dynamic binding in plpgsql function
Previous Message Pierre Racine 2011-03-01 23:01:36 Dynamic binding in plpgsql function