Thread: C-language example of using/returning numeric

C-language example of using/returning numeric

From
Mike Mascari
Date:
Does anyone happen to have a server-side C-language example of
using the Version-1 calling conventions with the NUMERIC data
type? Is it possible? For example, instead of writing:

PG_FUNCTION_INFO_V1(add_one);

Datum add_one(PG_FUNCTION_ARGS)
{
    int32 arg = PG_GETARG_INT32(0);
    PG_RETURN_INT32(arg + 1);
}

I'd like to do the equivalent with NUMERIC. I see the
appropriate macros declared in utils/numeric.h. But I'm not sure
I understand the representation of NumericData:

varlen <- self-explanatory
n_weight <- ??
n_rscale <- scale of numeric
n_sign_dscale <- ??
n_data[1] <- ?? is this BCD?

Are there any helper functions available to the C-language
server-side developer wrt numeric?

Thanks for any tips,

Mike Mascari
mascarm@mascari.com




Re: C-language example of using/returning numeric

From
Tom Lane
Date:
Mike Mascari <mascarm@mascari.com> writes:
> Does anyone happen to have a server-side C-language example of
> using the Version-1 calling conventions with the NUMERIC data
> type?

utils/adt/numeric.c is full of examples --- just add the
PG_FUNCTION_INFO_V1() macro and you have a perfectly good external
function.

> Are there any helper functions available to the C-language
> server-side developer wrt numeric?

They are all in numeric.c ... unfortunately, they're mostly static.

What exactly do you need to do?

            regards, tom lane

Re: C-language example of using/returning numeric

From
Mike Mascari
Date:
Tom Lane wrote:
> Mike Mascari <mascarm@mascari.com> writes:
>
>>Does anyone happen to have a server-side C-language example of
>>using the Version-1 calling conventions with the NUMERIC data
>>type?
>
>
> utils/adt/numeric.c is full of examples --- just add the
> PG_FUNCTION_INFO_V1() macro and you have a perfectly good external
> function.
>

Great. I'll have a look.

>
>>Are there any helper functions available to the C-language
>>server-side developer wrt numeric?
>
>
> They are all in numeric.c ... unfortunately, they're mostly static.
>
> What exactly do you need to do?

Nothing fancy. I had written code which took an
application-specific input mask and generated an int8
representation, similar to TO_NUMBER(). The problem with int8
values is that they are limited to around 18 digits. The code
would take a user-defined input mask and a value such as:

'000-000/0000' and '213-555/5555' and return 2135555555 as a
result. It would also verify that the data supplied matched the
mask. The mask could include: 'L' or 'A' for alphanumeric or
alpha characters as well. If the mask included alpha or
alphanumeric placeholders, the function returned 0.

If the mask was entirely numeric, the user would be allowed to
supply a range of values. By converting the text to a numeric
value, I could perform range checks against quantities. E.g.: A
user orders 100 widgets, is prompted for their serial numbers,
supplies 213-555/5500 and 213-555/5600 as the range, and the
order is validated. Since NUMERIC was limited to 1000 digits, I
was just going to change the code to return a NUMERIC instead of
an int8. I guess I'll just return a TEXT result and CAST the
result as a NUMERIC for the various range checks.

Thanks!

Mike Mascari
mascarm@mascari.com