On Mar 31, 2010, at 7:07 AM, Kevin Grittner wrote:

> (I was going to mark the TODO as an easy one.)  

I thought it would be pretty simple, too, so I decided to go ahead and write 
and test it as an external module. 

I think the function definition could be pasted directly into an appropriate 
place in src/backend/utils/adt/cash.c, if someone wants to add it to the main 
code base. The SQL to load it would need to be modified somewhat to fit into 
postgres.bki. 

Here is the C source:

#include <postgres.h>
#include <fmgr.h>
#include <utils/cash.h>

PG_MODULE_MAGIC;

PG_FUNCTION_INFO_V1(cash_div_cash);

/* cash_div_cash()
 * Divide cash by cash, returning float8.
 */
Datum
cash_div_cash(PG_FUNCTION_ARGS)
{
    Cash    dividend = PG_GETARG_CASH(0);
    Cash    divisor  = PG_GETARG_CASH(1);
    float8  quotient;
    
    if (divisor == 0)
        ereport(ERROR,
                (errcode(ERRCODE_DIVISION_BY_ZERO),
                 errmsg("division by zero")));
    
    quotient = (float8)dividend / (float8)divisor;
    PG_RETURN_FLOAT8(quotient);
}

-------------------------------------------------------------------------------------

And here is the SQL to load it (assuming it has been compiled as a dynamically 
loadable module named divide_money and placed in the library directory on the 
server):

CREATE FUNCTION cash_div_cash(money, money) RETURNS double precision
    LANGUAGE c IMMUTABLE
    AS '$libdir/divide_money', 'cash_div_cash';
    
CREATE OPERATOR / (
    PROCEDURE = cash_div_cash,
    LEFTARG = money,
    RIGHTARG = money
);


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

Reply via email to