On Fri, Jun 22, 2012 at 12:28 PM, D'Arcy Cain <darcy@druid.net> wrote:
>> I doubt that an "auto reverse the arguments" facility would be very
>> much cheaper. You could maybe argue that the aggregated maintenance
>> and space costs of all the commutator-pair functions are enough to
>> justify having some such solution instead, but I'm doubtful --- and
>> even if true, getting from here to there would be painful.
>
>
> And it would only apply to a very specific type of function.
>
> The other idea I had was to just have the second C function call the
> first but that didn't work. Here is what I tried.
>
> PG_FUNCTION_INFO_V1(chkpass_eq);
> Datum
> chkpass_eq(PG_FUNCTION_ARGS)
> {
> chkpass *a1 = (chkpass *) PG_GETARG_POINTER(0);
> text *a2 = (text *) PG_GETARG_TEXT_P(1);
> char str[9];
>
> strlcpy(str, a2->vl_dat, sizeof(str));
> PG_RETURN_BOOL(strcmp(a1->password, crypt(str, a1->password)) == 0);
> }
>
> PG_FUNCTION_INFO_V1(chkpass_eq2);
> Datum
> chkpass_eq2(PG_FUNCTION_ARGS)
> {
> return chkpass_eq(PG_GETARG_POINTER(1), PG_GETARG_TEXT_P(0));
> }
>
> Now in this specific case the function is trivial and writing it twice
> is no big deal but in general I hate writing the same code twice. I
> suppose I could extract the actual operation out to a third function
> and call it from the others. I may do that anyway just for the value
> of the example. Or is there a way to do what I tried above?
I think DirectionFunctionCall2 is what you want.
--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company