>----Messaggio originale----
>Da: tgl@sss.pgh.pa.us
>Data: 12/03/2010 17.51
>A: "dario.ber@libero.it"<dario.ber@libero.it>
>Cc: <pgsql-general@postgresql.org>
>Ogg: Re: [GENERAL] Function with DEFAULT arguments
>
>"dario.ber@libero.it" <dario.ber@libero.it> writes:
>> I'm trying to use the DEFAULT option to pass parameters to the arguments of
a
>> function.
>> When I call that function, how can I change the default value of some
>> arguments and leave as default the value of other arguments?
>
>You can only omit arguments from right to left, so basically what this
>requires is some foresight while choosing the function's argument order.
>
> regards, tom lane
Thanks for replies!
Would it be a very horrible workaround to pass a single string to the function
which contains the user's parameters? This string then is parsed into the
individual arguments/defaults inside the function. In this way there is no need
to have arguments in any order.
Example using plpythonu:
CREATE OR REPLACE FUNCTION test_default(arg_string text) RETURNS text AS
$$
## List of pseudo-arguments the function can take
arg_1= 'arg_1'
arg_2= 'arg_2'
arg_3= 'arg_3'
## Convert the argument string to a dictionary
arg_dict= eval('{' + arg_string + '}')
## Retrieve user's parameters and assign defaults
try:
arg_1= arg_dict[arg_1]
except:
arg_1= 'A'
try:
arg_2= arg_dict[arg_2]
except:
arg_2= 'B'
try:
arg_3= arg_dict[arg_3]
except:
arg_3= 'C'
## Do something with the parameters
return('One: ' + arg_1 + '; Two: ' + arg_2 + '; Three: ' + arg_3)
$$
language 'plpythonu';
-- Execute with default 'pseudo-arguments' only:
SELECT test_default($$ $$);
--> One: A; Two: B; Three: C
-- With arg_2 as default:
SELECT test_default($$ arg_3:'z', arg_1:'x' $$);
--> One: x; Two: B; Three: z
All the best
Dario