Re: Function with DEFAULT arguments - Mailing list pgsql-general

From Pavel Stehule
Subject Re: Function with DEFAULT arguments
Date
Msg-id 162867791003122120y5c2ead20v8b95b77f5a673cac@mail.gmail.com
Whole thread Raw
In response to Re: Function with DEFAULT arguments  ("dario.ber@libero.it" <dario.ber@libero.it>)
List pgsql-general
2010/3/13 dario.ber@libero.it <dario.ber@libero.it>:
>>----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

what is sense of this?

It is a problem, because pg selection of adequate function is based on
type compatibility. and varchar isn't compatible with n - params -
mainly we don't see, what is content of some string (if there is one
parameter, or ten parameters).

If you need carry some unspecified number of parameters, then use
function with hstore type parameter.

Regards
Pavel Stehule

>
> --
> Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>

pgsql-general by date:

Previous
From: John R Pierce
Date:
Subject: Re: How to remove superuser
Next
From: "Joshua D. Drake"
Date:
Subject: Re: unexplained autovacuum to prevent wraparound