Search Postgresql Archives

Re: Dynamic binding in plpgsql function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





2011/3/2 Merlin Moncure <mmoncure@xxxxxxxxx>
On Wed, Mar 2, 2011 at 8:34 AM, Dmitriy Igrishin <dmitigr@xxxxxxxxx> wrote:
> 2011/3/2 Merlin Moncure <mmoncure@xxxxxxxxx>
>> On Tue, Mar 1, 2011 at 5:17 PM, Pavel Stehule <pavel.stehule@xxxxxxxxx>
>> wrote:
>> > Hello
>> >
>> > 2011/3/2 Pierre Racine <Pierre.Racine@xxxxxxxxxxxxx>:
>> >> 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
>>
>> another way if you are willing to write some C is to wrap and expose
>> OidFunctionCall1 so it takes oid of function you want to call.
>
> As always, Merlin shows the uncommon thinking! :-)
> Respect!

Thank you, and I would like to be able to claim original invention of
this idea, however I cannot: it came from none other than Tom
Lane...http://postgresql.1045698.n5.nabble.com/improvise-callbacks-in-plpgsql-td2052928.html
(for whom nary a day goes by that I am not thankful for his tireless
efforts).
Ahh, thanks to Tom Lane then !
And thank you for the link of interesting topic from past :-)


merlin



--
// Dmitriy.



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux