Age Owner Branch data TLA Line data Source code
1 : : /*
2 : : * cash.c
3 : : * Written by D'Arcy J.M. Cain
4 : : * darcy@druid.net
5 : : * http://www.druid.net/darcy/
6 : : *
7 : : * Functions to allow input and output of money normally but store
8 : : * and handle it as 64 bit ints
9 : : *
10 : : * A slightly modified version of this file and a discussion of the
11 : : * workings can be found in the book "Software Solutions in C" by
12 : : * Dale Schumacher, Academic Press, ISBN: 0-12-632360-7 except that
13 : : * this version handles 64 bit numbers and so can hold values up to
14 : : * $92,233,720,368,547,758.07.
15 : : *
16 : : * src/backend/utils/adt/cash.c
17 : : */
18 : :
19 : : #include "postgres.h"
20 : :
21 : : #include <limits.h>
22 : : #include <ctype.h>
23 : : #include <math.h>
24 : :
25 : : #include "common/int.h"
26 : : #include "libpq/pqformat.h"
27 : : #include "nodes/miscnodes.h"
28 : : #include "utils/builtins.h"
29 : : #include "utils/cash.h"
30 : : #include "utils/float.h"
31 : : #include "utils/numeric.h"
32 : : #include "utils/pg_locale.h"
33 : :
34 : :
35 : : /*************************************************************************
36 : : * Private routines
37 : : ************************************************************************/
38 : :
39 : : static void
644 heikki.linnakangas@i 40 :CBC 16 : append_num_word(StringInfo buf, Cash value)
41 : : {
42 : : static const char *const small[] = {
43 : : "zero", "one", "two", "three", "four", "five", "six", "seven",
44 : : "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
45 : : "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty",
46 : : "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"
47 : : };
2759 andres@anarazel.de 48 : 16 : const char *const *big = small + 18;
7062 darcy@druid.net 49 : 16 : int tu = value % 100;
50 : :
51 : : /* deal with the simple cases first */
52 [ + + ]: 16 : if (value <= 20)
53 : : {
644 heikki.linnakangas@i 54 : 4 : appendStringInfoString(buf, small[value]);
55 : 4 : return;
56 : : }
57 : :
58 : : /* is it an even multiple of 100? */
7062 darcy@druid.net 59 [ - + ]: 12 : if (!tu)
60 : : {
644 heikki.linnakangas@i 61 :UBC 0 : appendStringInfo(buf, "%s hundred", small[value / 100]);
62 : 0 : return;
63 : : }
64 : :
65 : : /* more than 99? */
7062 darcy@druid.net 66 [ + + ]:CBC 12 : if (value > 99)
67 : : {
68 : : /* is it an even multiple of 10 other than 10? */
69 [ - + - - ]: 8 : if (value % 10 == 0 && tu > 10)
644 heikki.linnakangas@i 70 :UBC 0 : appendStringInfo(buf, "%s hundred %s",
71 : 0 : small[value / 100], big[tu / 10]);
7062 darcy@druid.net 72 [ - + ]:CBC 8 : else if (tu < 20)
644 heikki.linnakangas@i 73 :UBC 0 : appendStringInfo(buf, "%s hundred and %s",
74 : 0 : small[value / 100], small[tu]);
75 : : else
644 heikki.linnakangas@i 76 :CBC 8 : appendStringInfo(buf, "%s hundred %s %s",
77 : 8 : small[value / 100], big[tu / 10], small[tu % 10]);
78 : : }
79 : : else
80 : : {
81 : : /* is it an even multiple of 10 other than 10? */
7062 darcy@druid.net 82 [ - + - - ]: 4 : if (value % 10 == 0 && tu > 10)
644 heikki.linnakangas@i 83 :UBC 0 : appendStringInfoString(buf, big[tu / 10]);
7062 darcy@druid.net 84 [ - + ]:CBC 4 : else if (tu < 20)
644 heikki.linnakangas@i 85 :UBC 0 : appendStringInfoString(buf, small[tu]);
86 : : else
644 heikki.linnakangas@i 87 :CBC 4 : appendStringInfo(buf, "%s %s", big[tu / 10], small[tu % 10]);
88 : : }
89 : : }
90 : :
91 : : static inline Cash
655 nathan@postgresql.or 92 : 20 : cash_pl_cash(Cash c1, Cash c2)
93 : : {
94 : : Cash res;
95 : :
96 [ + + ]: 20 : if (unlikely(pg_add_s64_overflow(c1, c2, &res)))
97 [ + - ]: 4 : ereport(ERROR,
98 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
99 : : errmsg("money out of range")));
100 : :
101 : 16 : return res;
102 : : }
103 : :
104 : : static inline Cash
105 : 12 : cash_mi_cash(Cash c1, Cash c2)
106 : : {
107 : : Cash res;
108 : :
109 [ + + ]: 12 : if (unlikely(pg_sub_s64_overflow(c1, c2, &res)))
110 [ + - ]: 4 : ereport(ERROR,
111 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
112 : : errmsg("money out of range")));
113 : :
114 : 8 : return res;
115 : : }
116 : :
117 : : static inline Cash
118 : 32 : cash_mul_float8(Cash c, float8 f)
119 : : {
120 : 32 : float8 res = rint(float8_mul((float8) c, f));
121 : :
122 [ + + + + : 32 : if (unlikely(isnan(res) || !FLOAT8_FITS_IN_INT64(res)))
+ + + + ]
123 [ + - ]: 16 : ereport(ERROR,
124 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
125 : : errmsg("money out of range")));
126 : :
127 : 16 : return (Cash) res;
128 : : }
129 : :
130 : : static inline Cash
131 : 22 : cash_div_float8(Cash c, float8 f)
132 : : {
133 : 22 : float8 res = rint(float8_div((float8) c, f));
134 : :
135 [ + - + + : 22 : if (unlikely(isnan(res) || !FLOAT8_FITS_IN_INT64(res)))
- + + + ]
136 [ + - ]: 4 : ereport(ERROR,
137 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
138 : : errmsg("money out of range")));
139 : :
140 : 18 : return (Cash) res;
141 : : }
142 : :
143 : : static inline Cash
144 : 28 : cash_mul_int64(Cash c, int64 i)
145 : : {
146 : : Cash res;
147 : :
148 [ + + ]: 28 : if (unlikely(pg_mul_s64_overflow(c, i, &res)))
149 [ + - ]: 4 : ereport(ERROR,
150 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
151 : : errmsg("money out of range")));
152 : :
153 : 24 : return res;
154 : : }
155 : :
156 : : static inline Cash
157 : 46 : cash_div_int64(Cash c, int64 i)
158 : : {
159 [ + + ]: 46 : if (unlikely(i == 0))
160 [ + - ]: 4 : ereport(ERROR,
161 : : (errcode(ERRCODE_DIVISION_BY_ZERO),
162 : : errmsg("division by zero")));
163 : :
164 : 42 : return c / i;
165 : : }
166 : :
167 : : /* cash_in()
168 : : * Convert a string to a cash data type.
169 : : * Format is [$]###[,]###[.##]
170 : : * Examples: 123.45 $123.45 $123,456.78
171 : : *
172 : : */
173 : : Datum
9406 tgl@sss.pgh.pa.us 174 : 879 : cash_in(PG_FUNCTION_ARGS)
175 : : {
176 : 879 : char *str = PG_GETARG_CSTRING(0);
1238 177 : 879 : Node *escontext = fcinfo->context;
178 : : Cash result;
10466 bruce@momjian.us 179 : 879 : Cash value = 0;
180 : 879 : Cash dec = 0;
181 : 879 : Cash sgn = 1;
5302 tgl@sss.pgh.pa.us 182 : 879 : bool seen_dot = false;
10466 bruce@momjian.us 183 : 879 : const char *s = str;
184 : : int fpoint;
185 : : char dsymbol;
186 : : const char *ssymbol,
187 : : *psymbol,
188 : : *nsymbol,
189 : : *csymbol;
9292 tgl@sss.pgh.pa.us 190 : 879 : struct lconv *lconvert = PGLC_localeconv();
191 : :
192 : : /*
193 : : * frac_digits will be CHAR_MAX in some locales, notably C. However, just
194 : : * testing for == CHAR_MAX is risky, because of compilers like gcc that
195 : : * "helpfully" let you alter the platform-standard definition of whether
196 : : * char is signed or not. If we are so unfortunate as to get compiled
197 : : * with a nonstandard -fsigned-char or -funsigned-char switch, then our
198 : : * idea of CHAR_MAX will not agree with libc's. The safest course is not
199 : : * to test for CHAR_MAX at all, but to impose a range check for plausible
200 : : * frac_digits values.
201 : : */
9299 202 : 879 : fpoint = lconvert->frac_digits;
203 [ - + - - ]: 879 : if (fpoint < 0 || fpoint > 10)
204 : 879 : fpoint = 2; /* best guess in this case, I think */
205 : :
206 : : /* we restrict dsymbol to be a single byte, but not the other symbols */
5302 207 [ - + ]: 879 : if (*lconvert->mon_decimal_point != '\0' &&
5302 tgl@sss.pgh.pa.us 208 [ # # ]:UBC 0 : lconvert->mon_decimal_point[1] == '\0')
209 : 0 : dsymbol = *lconvert->mon_decimal_point;
210 : : else
5302 tgl@sss.pgh.pa.us 211 :CBC 879 : dsymbol = '.';
212 [ - + ]: 879 : if (*lconvert->mon_thousands_sep != '\0')
5302 tgl@sss.pgh.pa.us 213 :UBC 0 : ssymbol = lconvert->mon_thousands_sep;
214 : : else /* ssymbol should not equal dsymbol */
5302 tgl@sss.pgh.pa.us 215 [ + - ]:CBC 879 : ssymbol = (dsymbol != ',') ? "," : ".";
216 [ - + ]: 879 : csymbol = (*lconvert->currency_symbol != '\0') ? lconvert->currency_symbol : "$";
217 [ - + ]: 879 : psymbol = (*lconvert->positive_sign != '\0') ? lconvert->positive_sign : "+";
218 [ - + ]: 879 : nsymbol = (*lconvert->negative_sign != '\0') ? lconvert->negative_sign : "-";
219 : :
220 : : #ifdef CASHDEBUG
221 : : printf("cashin- precision '%d'; decimal '%c'; thousands '%s'; currency '%s'; positive '%s'; negative '%s'\n",
222 : : fpoint, dsymbol, ssymbol, csymbol, psymbol, nsymbol);
223 : : #endif
224 : :
225 : : /* we need to add all sorts of checking here. For now just */
226 : : /* strip all leading whitespace and any leading currency symbol */
9284 227 [ - + ]: 879 : while (isspace((unsigned char) *s))
10108 bruce@momjian.us 228 :UBC 0 : s++;
10108 bruce@momjian.us 229 [ + + ]:CBC 879 : if (strncmp(s, csymbol, strlen(csymbol)) == 0)
230 : 80 : s += strlen(csymbol);
5301 tgl@sss.pgh.pa.us 231 [ - + ]: 879 : while (isspace((unsigned char) *s))
5301 tgl@sss.pgh.pa.us 232 :UBC 0 : s++;
233 : :
234 : : #ifdef CASHDEBUG
235 : : printf("cashin- string is '%s'\n", s);
236 : : #endif
237 : :
238 : : /* a leading minus or paren signifies a negative number */
239 : : /* again, better heuristics needed */
240 : : /* XXX - doesn't properly check for balanced parens - djmc */
10108 bruce@momjian.us 241 [ + + ]:CBC 879 : if (strncmp(s, nsymbol, strlen(nsymbol)) == 0)
242 : : {
10291 lockhart@fourpalms.o 243 : 323 : sgn = -1;
244 : 323 : s += strlen(nsymbol);
245 : : }
246 [ + + ]: 556 : else if (*s == '(')
247 : : {
10467 bruce@momjian.us 248 : 8 : sgn = -1;
249 : 8 : s++;
250 : : }
5302 tgl@sss.pgh.pa.us 251 [ - + ]: 548 : else if (strncmp(s, psymbol, strlen(psymbol)) == 0)
5302 tgl@sss.pgh.pa.us 252 :UBC 0 : s += strlen(psymbol);
253 : :
254 : : #ifdef CASHDEBUG
255 : : printf("cashin- string is '%s'\n", s);
256 : : #endif
257 : :
258 : : /* allow whitespace and currency symbol after the sign, too */
9284 tgl@sss.pgh.pa.us 259 [ - + ]:CBC 879 : while (isspace((unsigned char) *s))
10108 bruce@momjian.us 260 :UBC 0 : s++;
10108 bruce@momjian.us 261 [ + + ]:CBC 879 : if (strncmp(s, csymbol, strlen(csymbol)) == 0)
262 : 4 : s += strlen(csymbol);
5301 tgl@sss.pgh.pa.us 263 [ - + ]: 879 : while (isspace((unsigned char) *s))
5301 tgl@sss.pgh.pa.us 264 :UBC 0 : s++;
265 : :
266 : : #ifdef CASHDEBUG
267 : : printf("cashin- string is '%s'\n", s);
268 : : #endif
269 : :
270 : : /*
271 : : * We accumulate the absolute amount in "value" and then apply the sign at
272 : : * the end. (The sign can appear before or after the digits, so it would
273 : : * be more complicated to do otherwise.) Because of the larger range of
274 : : * negative signed integers, we build "value" in the negative and then
275 : : * flip the sign at the end, catching most-negative-number overflow if
276 : : * necessary.
277 : : */
278 : :
5302 tgl@sss.pgh.pa.us 279 [ + + ]:CBC 8718 : for (; *s; s++)
280 : : {
281 : : /*
282 : : * We look for digits as long as we have found less than the required
283 : : * number of decimal places.
284 : : */
6173 285 [ + + + + : 7895 : if (isdigit((unsigned char) *s) && (!seen_dot || dec < fpoint))
+ + ]
10467 bruce@momjian.us 286 : 7064 : {
3066 andres@anarazel.de 287 : 7076 : int8 digit = *s - '0';
288 : :
289 [ + + + + ]: 14144 : if (pg_mul_s64_overflow(value, 10, &value) ||
290 : 7068 : pg_sub_s64_overflow(value, digit, &value))
1238 tgl@sss.pgh.pa.us 291 [ + + ]: 12 : ereturn(escontext, (Datum) 0,
292 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
293 : : errmsg("value \"%s\" is out of range for type %s",
294 : : str, "money")));
295 : :
10467 bruce@momjian.us 296 [ + + ]: 7064 : if (seen_dot)
297 : 1530 : dec++;
298 : : }
299 : : /* decimal point? then start counting fractions... */
300 [ + + + - ]: 819 : else if (*s == dsymbol && !seen_dot)
301 : : {
5302 tgl@sss.pgh.pa.us 302 : 771 : seen_dot = true;
303 : : }
304 : : /* ignore if "thousands" separator, else we're done */
305 [ + + ]: 48 : else if (strncmp(s, ssymbol, strlen(ssymbol)) == 0)
306 : 4 : s += strlen(ssymbol) - 1;
307 : : else
10467 bruce@momjian.us 308 : 44 : break;
309 : : }
310 : :
311 : : /* round off if there's another digit */
5302 tgl@sss.pgh.pa.us 312 [ + + + + ]: 867 : if (isdigit((unsigned char) *s) && *s >= '5')
313 : : {
314 : : /* remember we build the value in the negative */
3066 andres@anarazel.de 315 [ + + ]: 20 : if (pg_sub_s64_overflow(value, 1, &value))
1238 tgl@sss.pgh.pa.us 316 [ + - ]: 4 : ereturn(escontext, (Datum) 0,
317 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
318 : : errmsg("value \"%s\" is out of range for type %s",
319 : : str, "money")));
320 : : }
321 : :
322 : : /* adjust for less than required decimal places */
5302 323 [ + + ]: 1055 : for (; dec < fpoint; dec++)
324 : : {
3066 andres@anarazel.de 325 [ + + ]: 208 : if (pg_mul_s64_overflow(value, 10, &value))
1238 tgl@sss.pgh.pa.us 326 [ + - ]: 16 : ereturn(escontext, (Datum) 0,
327 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
328 : : errmsg("value \"%s\" is out of range for type %s",
329 : : str, "money")));
330 : : }
331 : :
332 : : /*
333 : : * should only be trailing digits followed by whitespace, right paren,
334 : : * trailing sign, and/or trailing currency symbol
335 : : */
6872 336 [ + + ]: 871 : while (isdigit((unsigned char) *s))
337 : 24 : s++;
338 : :
5302 339 [ + + ]: 855 : while (*s)
340 : : {
341 [ + - + + ]: 16 : if (isspace((unsigned char) *s) || *s == ')')
342 : 8 : s++;
343 [ - + ]: 8 : else if (strncmp(s, nsymbol, strlen(nsymbol)) == 0)
344 : : {
5302 tgl@sss.pgh.pa.us 345 :UBC 0 : sgn = -1;
346 : 0 : s += strlen(nsymbol);
347 : : }
5301 tgl@sss.pgh.pa.us 348 [ - + ]:CBC 8 : else if (strncmp(s, psymbol, strlen(psymbol)) == 0)
5301 tgl@sss.pgh.pa.us 349 :UBC 0 : s += strlen(psymbol);
5301 tgl@sss.pgh.pa.us 350 [ - + ]:CBC 8 : else if (strncmp(s, csymbol, strlen(csymbol)) == 0)
5301 tgl@sss.pgh.pa.us 351 :UBC 0 : s += strlen(csymbol);
352 : : else
1238 tgl@sss.pgh.pa.us 353 [ + + ]:CBC 8 : ereturn(escontext, (Datum) 0,
354 : : (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
355 : : errmsg("invalid input syntax for type %s: \"%s\"",
356 : : "money", str)));
357 : : }
358 : :
359 : : /*
360 : : * If the value is supposed to be positive, flip the sign, but check for
361 : : * the most negative number.
362 : : */
3526 peter_e@gmx.net 363 [ + + ]: 839 : if (sgn > 0)
364 : : {
3066 andres@anarazel.de 365 [ + + ]: 524 : if (value == PG_INT64_MIN)
1238 tgl@sss.pgh.pa.us 366 [ + - ]: 8 : ereturn(escontext, (Datum) 0,
367 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
368 : : errmsg("value \"%s\" is out of range for type %s",
369 : : str, "money")));
3066 andres@anarazel.de 370 : 516 : result = -value;
371 : : }
372 : : else
3526 peter_e@gmx.net 373 : 315 : result = value;
374 : :
375 : : #ifdef CASHDEBUG
376 : : printf("cashin- result is " INT64_FORMAT "\n", result);
377 : : #endif
378 : :
9406 tgl@sss.pgh.pa.us 379 : 831 : PG_RETURN_CASH(result);
380 : : }
381 : :
382 : :
383 : : /* cash_out()
384 : : * Function to convert cash to a dollars and cents representation, using
385 : : * the lc_monetary locale's formatting.
386 : : */
387 : : Datum
388 : 265 : cash_out(PG_FUNCTION_ARGS)
389 : : {
390 : 265 : Cash value = PG_GETARG_CASH(0);
391 : : uint64 uvalue;
392 : : char *result;
393 : : char buf[128];
394 : : char *bufptr;
395 : : int digit_pos;
396 : : int points,
397 : : mon_group;
398 : : char dsymbol;
399 : : const char *ssymbol,
400 : : *csymbol,
401 : : *signsymbol;
402 : : char sign_posn,
403 : : cs_precedes,
404 : : sep_by_space;
9292 405 : 265 : struct lconv *lconvert = PGLC_localeconv();
406 : :
407 : : /* see comments about frac_digits in cash_in() */
9299 408 : 265 : points = lconvert->frac_digits;
409 [ - + - - ]: 265 : if (points < 0 || points > 10)
410 : 265 : points = 2; /* best guess in this case, I think */
411 : :
412 : : /*
413 : : * As with frac_digits, must apply a range check to mon_grouping to avoid
414 : : * being fooled by variant CHAR_MAX values.
415 : : */
10441 lockhart@fourpalms.o 416 : 265 : mon_group = *lconvert->mon_grouping;
9299 tgl@sss.pgh.pa.us 417 [ - + - - ]: 265 : if (mon_group <= 0 || mon_group > 6)
418 : 265 : mon_group = 3;
419 : :
420 : : /* we restrict dsymbol to be a single byte, but not the other symbols */
5302 421 [ - + ]: 265 : if (*lconvert->mon_decimal_point != '\0' &&
5302 tgl@sss.pgh.pa.us 422 [ # # ]:UBC 0 : lconvert->mon_decimal_point[1] == '\0')
423 : 0 : dsymbol = *lconvert->mon_decimal_point;
424 : : else
5302 tgl@sss.pgh.pa.us 425 :CBC 265 : dsymbol = '.';
426 [ - + ]: 265 : if (*lconvert->mon_thousands_sep != '\0')
5302 tgl@sss.pgh.pa.us 427 :UBC 0 : ssymbol = lconvert->mon_thousands_sep;
428 : : else /* ssymbol should not equal dsymbol */
5302 tgl@sss.pgh.pa.us 429 [ + - ]:CBC 265 : ssymbol = (dsymbol != ',') ? "," : ".";
430 [ - + ]: 265 : csymbol = (*lconvert->currency_symbol != '\0') ? lconvert->currency_symbol : "$";
431 : :
10461 bruce@momjian.us 432 [ + + ]: 265 : if (value < 0)
433 : : {
434 : : /* set up formatting data */
5301 tgl@sss.pgh.pa.us 435 [ - + ]: 56 : signsymbol = (*lconvert->negative_sign != '\0') ? lconvert->negative_sign : "-";
436 : 56 : sign_posn = lconvert->n_sign_posn;
437 : 56 : cs_precedes = lconvert->n_cs_precedes;
438 : 56 : sep_by_space = lconvert->n_sep_by_space;
439 : : }
440 : : else
441 : : {
442 : 209 : signsymbol = lconvert->positive_sign;
443 : 209 : sign_posn = lconvert->p_sign_posn;
444 : 209 : cs_precedes = lconvert->p_cs_precedes;
445 : 209 : sep_by_space = lconvert->p_sep_by_space;
446 : : }
447 : :
448 : : /* make the amount positive for digit-reconstruction loop */
628 nathan@postgresql.or 449 : 265 : uvalue = pg_abs_s64(value);
450 : :
451 : : /* we build the digits+decimal-point+sep string right-to-left in buf[] */
5302 tgl@sss.pgh.pa.us 452 : 265 : bufptr = buf + sizeof(buf) - 1;
453 : 265 : *bufptr = '\0';
454 : :
455 : : /*
456 : : * Generate digits till there are no non-zero digits left and we emitted
457 : : * at least one to the left of the decimal point. digit_pos is the
458 : : * current digit position, with zero as the digit just left of the decimal
459 : : * point, increasing to the right.
460 : : */
461 : 265 : digit_pos = points;
462 : : do
463 : : {
464 [ + - + + ]: 2139 : if (points && digit_pos == 0)
465 : : {
466 : : /* insert decimal point, but not if value cannot be fractional */
467 : 265 : *(--bufptr) = dsymbol;
468 : : }
5301 469 [ + + + + ]: 1874 : else if (digit_pos < 0 && (digit_pos % mon_group) == 0)
470 : : {
471 : : /* insert thousands sep, but only to left of radix point */
5302 472 : 351 : bufptr -= strlen(ssymbol);
473 : 351 : memcpy(bufptr, ssymbol, strlen(ssymbol));
474 : : }
475 : :
628 nathan@postgresql.or 476 : 2139 : *(--bufptr) = (uvalue % 10) + '0';
477 : 2139 : uvalue = uvalue / 10;
5302 tgl@sss.pgh.pa.us 478 : 2139 : digit_pos--;
628 nathan@postgresql.or 479 [ + + + + ]: 2139 : } while (uvalue || digit_pos >= 0);
480 : :
481 : : /*----------
482 : : * Now, attach currency symbol and sign symbol in the correct order.
483 : : *
484 : : * The POSIX spec defines these values controlling this code:
485 : : *
486 : : * p/n_sign_posn:
487 : : * 0 Parentheses enclose the quantity and the currency_symbol.
488 : : * 1 The sign string precedes the quantity and the currency_symbol.
489 : : * 2 The sign string succeeds the quantity and the currency_symbol.
490 : : * 3 The sign string precedes the currency_symbol.
491 : : * 4 The sign string succeeds the currency_symbol.
492 : : *
493 : : * p/n_cs_precedes: 0 means currency symbol after value, else before it.
494 : : *
495 : : * p/n_sep_by_space:
496 : : * 0 No <space> separates the currency symbol and value.
497 : : * 1 If the currency symbol and sign string are adjacent, a <space>
498 : : * separates them from the value; otherwise, a <space> separates
499 : : * the currency symbol from the value.
500 : : * 2 If the currency symbol and sign string are adjacent, a <space>
501 : : * separates them; otherwise, a <space> separates the sign string
502 : : * from the value.
503 : : *----------
504 : : */
5301 tgl@sss.pgh.pa.us 505 [ - + - - : 265 : switch (sign_posn)
- ]
506 : : {
5301 tgl@sss.pgh.pa.us 507 :UBC 0 : case 0:
508 [ # # ]: 0 : if (cs_precedes)
4587 peter_e@gmx.net 509 [ # # ]: 0 : result = psprintf("(%s%s%s)",
510 : : csymbol,
511 : : (sep_by_space == 1) ? " " : "",
512 : : bufptr);
513 : : else
514 [ # # ]: 0 : result = psprintf("(%s%s%s)",
515 : : bufptr,
516 : : (sep_by_space == 1) ? " " : "",
517 : : csymbol);
5301 tgl@sss.pgh.pa.us 518 : 0 : break;
5301 tgl@sss.pgh.pa.us 519 :CBC 265 : case 1:
520 : : default:
521 [ + - ]: 265 : if (cs_precedes)
4587 peter_e@gmx.net 522 [ - + - + ]: 265 : result = psprintf("%s%s%s%s%s",
523 : : signsymbol,
524 : : (sep_by_space == 2) ? " " : "",
525 : : csymbol,
526 : : (sep_by_space == 1) ? " " : "",
527 : : bufptr);
528 : : else
4587 peter_e@gmx.net 529 [ # # # # ]:UBC 0 : result = psprintf("%s%s%s%s%s",
530 : : signsymbol,
531 : : (sep_by_space == 2) ? " " : "",
532 : : bufptr,
533 : : (sep_by_space == 1) ? " " : "",
534 : : csymbol);
5301 tgl@sss.pgh.pa.us 535 :CBC 265 : break;
5301 tgl@sss.pgh.pa.us 536 :UBC 0 : case 2:
537 [ # # ]: 0 : if (cs_precedes)
4587 peter_e@gmx.net 538 [ # # # # ]: 0 : result = psprintf("%s%s%s%s%s",
539 : : csymbol,
540 : : (sep_by_space == 1) ? " " : "",
541 : : bufptr,
542 : : (sep_by_space == 2) ? " " : "",
543 : : signsymbol);
544 : : else
545 [ # # # # ]: 0 : result = psprintf("%s%s%s%s%s",
546 : : bufptr,
547 : : (sep_by_space == 1) ? " " : "",
548 : : csymbol,
549 : : (sep_by_space == 2) ? " " : "",
550 : : signsymbol);
5301 tgl@sss.pgh.pa.us 551 : 0 : break;
552 : 0 : case 3:
553 [ # # ]: 0 : if (cs_precedes)
4587 peter_e@gmx.net 554 [ # # # # ]: 0 : result = psprintf("%s%s%s%s%s",
555 : : signsymbol,
556 : : (sep_by_space == 2) ? " " : "",
557 : : csymbol,
558 : : (sep_by_space == 1) ? " " : "",
559 : : bufptr);
560 : : else
561 [ # # # # ]: 0 : result = psprintf("%s%s%s%s%s",
562 : : bufptr,
563 : : (sep_by_space == 1) ? " " : "",
564 : : signsymbol,
565 : : (sep_by_space == 2) ? " " : "",
566 : : csymbol);
5301 tgl@sss.pgh.pa.us 567 : 0 : break;
568 : 0 : case 4:
569 [ # # ]: 0 : if (cs_precedes)
4587 peter_e@gmx.net 570 [ # # # # ]: 0 : result = psprintf("%s%s%s%s%s",
571 : : csymbol,
572 : : (sep_by_space == 2) ? " " : "",
573 : : signsymbol,
574 : : (sep_by_space == 1) ? " " : "",
575 : : bufptr);
576 : : else
577 [ # # # # ]: 0 : result = psprintf("%s%s%s%s%s",
578 : : bufptr,
579 : : (sep_by_space == 1) ? " " : "",
580 : : csymbol,
581 : : (sep_by_space == 2) ? " " : "",
582 : : signsymbol);
5301 tgl@sss.pgh.pa.us 583 : 0 : break;
584 : : }
585 : :
9406 tgl@sss.pgh.pa.us 586 :CBC 265 : PG_RETURN_CSTRING(result);
587 : : }
588 : :
589 : : /*
590 : : * cash_recv - converts external binary format to cash
591 : : */
592 : : Datum
8393 tgl@sss.pgh.pa.us 593 :UBC 0 : cash_recv(PG_FUNCTION_ARGS)
594 : : {
595 : 0 : StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
596 : :
6832 597 : 0 : PG_RETURN_CASH((Cash) pq_getmsgint64(buf));
598 : : }
599 : :
600 : : /*
601 : : * cash_send - converts cash to binary format
602 : : */
603 : : Datum
8393 604 : 0 : cash_send(PG_FUNCTION_ARGS)
605 : : {
606 : 0 : Cash arg1 = PG_GETARG_CASH(0);
607 : : StringInfoData buf;
608 : :
609 : 0 : pq_begintypsend(&buf);
6832 610 : 0 : pq_sendint64(&buf, arg1);
8393 611 : 0 : PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
612 : : }
613 : :
614 : : /*
615 : : * Comparison functions
616 : : */
617 : :
618 : : Datum
9406 tgl@sss.pgh.pa.us 619 :CBC 552 : cash_eq(PG_FUNCTION_ARGS)
620 : : {
621 : 552 : Cash c1 = PG_GETARG_CASH(0);
622 : 552 : Cash c2 = PG_GETARG_CASH(1);
623 : :
624 : 552 : PG_RETURN_BOOL(c1 == c2);
625 : : }
626 : :
627 : : Datum
628 : 8 : cash_ne(PG_FUNCTION_ARGS)
629 : : {
630 : 8 : Cash c1 = PG_GETARG_CASH(0);
631 : 8 : Cash c2 = PG_GETARG_CASH(1);
632 : :
633 : 8 : PG_RETURN_BOOL(c1 != c2);
634 : : }
635 : :
636 : : Datum
637 : 551 : cash_lt(PG_FUNCTION_ARGS)
638 : : {
639 : 551 : Cash c1 = PG_GETARG_CASH(0);
640 : 551 : Cash c2 = PG_GETARG_CASH(1);
641 : :
642 : 551 : PG_RETURN_BOOL(c1 < c2);
643 : : }
644 : :
645 : : Datum
646 : 551 : cash_le(PG_FUNCTION_ARGS)
647 : : {
648 : 551 : Cash c1 = PG_GETARG_CASH(0);
649 : 551 : Cash c2 = PG_GETARG_CASH(1);
650 : :
651 : 551 : PG_RETURN_BOOL(c1 <= c2);
652 : : }
653 : :
654 : : Datum
655 : 551 : cash_gt(PG_FUNCTION_ARGS)
656 : : {
657 : 551 : Cash c1 = PG_GETARG_CASH(0);
658 : 551 : Cash c2 = PG_GETARG_CASH(1);
659 : :
660 : 551 : PG_RETURN_BOOL(c1 > c2);
661 : : }
662 : :
663 : : Datum
664 : 551 : cash_ge(PG_FUNCTION_ARGS)
665 : : {
666 : 551 : Cash c1 = PG_GETARG_CASH(0);
667 : 551 : Cash c2 = PG_GETARG_CASH(1);
668 : :
669 : 551 : PG_RETURN_BOOL(c1 >= c2);
670 : : }
671 : :
672 : : Datum
8297 673 : 663 : cash_cmp(PG_FUNCTION_ARGS)
674 : : {
675 : 663 : Cash c1 = PG_GETARG_CASH(0);
676 : 663 : Cash c2 = PG_GETARG_CASH(1);
677 : :
678 [ + + ]: 663 : if (c1 > c2)
679 : 561 : PG_RETURN_INT32(1);
680 [ + + ]: 102 : else if (c1 == c2)
681 : 7 : PG_RETURN_INT32(0);
682 : : else
683 : 95 : PG_RETURN_INT32(-1);
684 : : }
685 : :
686 : :
687 : : /* cash_pl()
688 : : * Add two cash values.
689 : : */
690 : : Datum
9406 691 : 20 : cash_pl(PG_FUNCTION_ARGS)
692 : : {
693 : 20 : Cash c1 = PG_GETARG_CASH(0);
694 : 20 : Cash c2 = PG_GETARG_CASH(1);
695 : :
655 nathan@postgresql.or 696 : 20 : PG_RETURN_CASH(cash_pl_cash(c1, c2));
697 : : }
698 : :
699 : :
700 : : /* cash_mi()
701 : : * Subtract two cash values.
702 : : */
703 : : Datum
9406 tgl@sss.pgh.pa.us 704 : 12 : cash_mi(PG_FUNCTION_ARGS)
705 : : {
706 : 12 : Cash c1 = PG_GETARG_CASH(0);
707 : 12 : Cash c2 = PG_GETARG_CASH(1);
708 : :
655 nathan@postgresql.or 709 : 12 : PG_RETURN_CASH(cash_mi_cash(c1, c2));
710 : : }
711 : :
712 : :
713 : : /* cash_div_cash()
714 : : * Divide cash by cash, returning float8.
715 : : */
716 : : Datum
5772 tgl@sss.pgh.pa.us 717 : 4 : cash_div_cash(PG_FUNCTION_ARGS)
718 : : {
719 : 4 : Cash dividend = PG_GETARG_CASH(0);
720 : 4 : Cash divisor = PG_GETARG_CASH(1);
721 : : float8 quotient;
722 : :
723 [ - + ]: 4 : if (divisor == 0)
5772 tgl@sss.pgh.pa.us 724 [ # # ]:UBC 0 : ereport(ERROR,
725 : : (errcode(ERRCODE_DIVISION_BY_ZERO),
726 : : errmsg("division by zero")));
727 : :
5772 tgl@sss.pgh.pa.us 728 :CBC 4 : quotient = (float8) dividend / (float8) divisor;
729 : 4 : PG_RETURN_FLOAT8(quotient);
730 : : }
731 : :
732 : :
733 : : /* cash_mul_flt8()
734 : : * Multiply cash by float8.
735 : : */
736 : : Datum
9408 737 : 16 : cash_mul_flt8(PG_FUNCTION_ARGS)
738 : : {
739 : 16 : Cash c = PG_GETARG_CASH(0);
740 : 16 : float8 f = PG_GETARG_FLOAT8(1);
741 : :
655 nathan@postgresql.or 742 : 16 : PG_RETURN_CASH(cash_mul_float8(c, f));
743 : : }
744 : :
745 : :
746 : : /* flt8_mul_cash()
747 : : * Multiply float8 by cash.
748 : : */
749 : : Datum
9408 tgl@sss.pgh.pa.us 750 : 4 : flt8_mul_cash(PG_FUNCTION_ARGS)
751 : : {
752 : 4 : float8 f = PG_GETARG_FLOAT8(0);
753 : 4 : Cash c = PG_GETARG_CASH(1);
754 : :
655 nathan@postgresql.or 755 : 4 : PG_RETURN_CASH(cash_mul_float8(c, f));
756 : : }
757 : :
758 : :
759 : : /* cash_div_flt8()
760 : : * Divide cash by float8.
761 : : */
762 : : Datum
9408 tgl@sss.pgh.pa.us 763 : 9 : cash_div_flt8(PG_FUNCTION_ARGS)
764 : : {
765 : 9 : Cash c = PG_GETARG_CASH(0);
766 : 9 : float8 f = PG_GETARG_FLOAT8(1);
767 : :
655 nathan@postgresql.or 768 : 9 : PG_RETURN_CASH(cash_div_float8(c, f));
769 : : }
770 : :
771 : :
772 : : /* cash_mul_flt4()
773 : : * Multiply cash by float4.
774 : : */
775 : : Datum
9408 tgl@sss.pgh.pa.us 776 : 8 : cash_mul_flt4(PG_FUNCTION_ARGS)
777 : : {
778 : 8 : Cash c = PG_GETARG_CASH(0);
779 : 8 : float4 f = PG_GETARG_FLOAT4(1);
780 : :
655 nathan@postgresql.or 781 : 8 : PG_RETURN_CASH(cash_mul_float8(c, (float8) f));
782 : : }
783 : :
784 : :
785 : : /* flt4_mul_cash()
786 : : * Multiply float4 by cash.
787 : : */
788 : : Datum
9408 tgl@sss.pgh.pa.us 789 : 4 : flt4_mul_cash(PG_FUNCTION_ARGS)
790 : : {
791 : 4 : float4 f = PG_GETARG_FLOAT4(0);
792 : 4 : Cash c = PG_GETARG_CASH(1);
793 : :
655 nathan@postgresql.or 794 : 4 : PG_RETURN_CASH(cash_mul_float8(c, (float8) f));
795 : : }
796 : :
797 : :
798 : : /* cash_div_flt4()
799 : : * Divide cash by float4.
800 : : *
801 : : */
802 : : Datum
9408 tgl@sss.pgh.pa.us 803 : 13 : cash_div_flt4(PG_FUNCTION_ARGS)
804 : : {
805 : 13 : Cash c = PG_GETARG_CASH(0);
806 : 13 : float4 f = PG_GETARG_FLOAT4(1);
807 : :
655 nathan@postgresql.or 808 : 13 : PG_RETURN_CASH(cash_div_float8(c, (float8) f));
809 : : }
810 : :
811 : :
812 : : /* cash_mul_int8()
813 : : * Multiply cash by int8.
814 : : */
815 : : Datum
7062 darcy@druid.net 816 : 4 : cash_mul_int8(PG_FUNCTION_ARGS)
817 : : {
818 : 4 : Cash c = PG_GETARG_CASH(0);
819 : 4 : int64 i = PG_GETARG_INT64(1);
820 : :
655 nathan@postgresql.or 821 : 4 : PG_RETURN_CASH(cash_mul_int64(c, i));
822 : : }
823 : :
824 : :
825 : : /* int8_mul_cash()
826 : : * Multiply int8 by cash.
827 : : */
828 : : Datum
7062 darcy@druid.net 829 : 4 : int8_mul_cash(PG_FUNCTION_ARGS)
830 : : {
831 : 4 : int64 i = PG_GETARG_INT64(0);
832 : 4 : Cash c = PG_GETARG_CASH(1);
833 : :
655 nathan@postgresql.or 834 : 4 : PG_RETURN_CASH(cash_mul_int64(c, i));
835 : : }
836 : :
837 : : /* cash_div_int8()
838 : : * Divide cash by 8-byte integer.
839 : : */
840 : : Datum
7062 darcy@druid.net 841 : 14 : cash_div_int8(PG_FUNCTION_ARGS)
842 : : {
843 : 14 : Cash c = PG_GETARG_CASH(0);
844 : 14 : int64 i = PG_GETARG_INT64(1);
845 : :
655 nathan@postgresql.or 846 : 14 : PG_RETURN_CASH(cash_div_int64(c, i));
847 : : }
848 : :
849 : :
850 : : /* cash_mul_int4()
851 : : * Multiply cash by int4.
852 : : */
853 : : Datum
9457 tgl@sss.pgh.pa.us 854 : 8 : cash_mul_int4(PG_FUNCTION_ARGS)
855 : : {
856 : 8 : Cash c = PG_GETARG_CASH(0);
6832 857 : 8 : int32 i = PG_GETARG_INT32(1);
858 : :
655 nathan@postgresql.or 859 : 8 : PG_RETURN_CASH(cash_mul_int64(c, (int64) i));
860 : : }
861 : :
862 : :
863 : : /* int4_mul_cash()
864 : : * Multiply int4 by cash.
865 : : */
866 : : Datum
9457 tgl@sss.pgh.pa.us 867 : 4 : int4_mul_cash(PG_FUNCTION_ARGS)
868 : : {
869 : 4 : int32 i = PG_GETARG_INT32(0);
870 : 4 : Cash c = PG_GETARG_CASH(1);
871 : :
655 nathan@postgresql.or 872 : 4 : PG_RETURN_CASH(cash_mul_int64(c, (int64) i));
873 : : }
874 : :
875 : :
876 : : /* cash_div_int4()
877 : : * Divide cash by 4-byte integer.
878 : : *
879 : : */
880 : : Datum
9457 tgl@sss.pgh.pa.us 881 : 14 : cash_div_int4(PG_FUNCTION_ARGS)
882 : : {
883 : 14 : Cash c = PG_GETARG_CASH(0);
6832 884 : 14 : int32 i = PG_GETARG_INT32(1);
885 : :
655 nathan@postgresql.or 886 : 14 : PG_RETURN_CASH(cash_div_int64(c, (int64) i));
887 : : }
888 : :
889 : :
890 : : /* cash_mul_int2()
891 : : * Multiply cash by int2.
892 : : */
893 : : Datum
9465 tgl@sss.pgh.pa.us 894 : 4 : cash_mul_int2(PG_FUNCTION_ARGS)
895 : : {
896 : 4 : Cash c = PG_GETARG_CASH(0);
897 : 4 : int16 s = PG_GETARG_INT16(1);
898 : :
655 nathan@postgresql.or 899 : 4 : PG_RETURN_CASH(cash_mul_int64(c, (int64) s));
900 : : }
901 : :
902 : : /* int2_mul_cash()
903 : : * Multiply int2 by cash.
904 : : */
905 : : Datum
9465 tgl@sss.pgh.pa.us 906 : 4 : int2_mul_cash(PG_FUNCTION_ARGS)
907 : : {
908 : 4 : int16 s = PG_GETARG_INT16(0);
909 : 4 : Cash c = PG_GETARG_CASH(1);
910 : :
655 nathan@postgresql.or 911 : 4 : PG_RETURN_CASH(cash_mul_int64(c, (int64) s));
912 : : }
913 : :
914 : : /* cash_div_int2()
915 : : * Divide cash by int2.
916 : : *
917 : : */
918 : : Datum
9465 tgl@sss.pgh.pa.us 919 : 18 : cash_div_int2(PG_FUNCTION_ARGS)
920 : : {
921 : 18 : Cash c = PG_GETARG_CASH(0);
922 : 18 : int16 s = PG_GETARG_INT16(1);
923 : :
655 nathan@postgresql.or 924 : 18 : PG_RETURN_CASH(cash_div_int64(c, (int64) s));
925 : : }
926 : :
927 : : /* cashlarger()
928 : : * Return larger of two cash values.
929 : : */
930 : : Datum
9406 tgl@sss.pgh.pa.us 931 : 4 : cashlarger(PG_FUNCTION_ARGS)
932 : : {
933 : 4 : Cash c1 = PG_GETARG_CASH(0);
934 : 4 : Cash c2 = PG_GETARG_CASH(1);
935 : : Cash result;
936 : :
937 : 4 : result = (c1 > c2) ? c1 : c2;
938 : :
939 : 4 : PG_RETURN_CASH(result);
940 : : }
941 : :
942 : : /* cashsmaller()
943 : : * Return smaller of two cash values.
944 : : */
945 : : Datum
946 : 4 : cashsmaller(PG_FUNCTION_ARGS)
947 : : {
948 : 4 : Cash c1 = PG_GETARG_CASH(0);
949 : 4 : Cash c2 = PG_GETARG_CASH(1);
950 : : Cash result;
951 : :
952 : 4 : result = (c1 < c2) ? c1 : c2;
953 : :
954 : 4 : PG_RETURN_CASH(result);
955 : : }
956 : :
957 : : /* cash_words()
958 : : * This converts an int4 as well but to a representation using words
959 : : * Obviously way North American centric - sorry
960 : : */
961 : : Datum
9433 bruce@momjian.us 962 : 8 : cash_words(PG_FUNCTION_ARGS)
963 : : {
9434 tgl@sss.pgh.pa.us 964 : 8 : Cash value = PG_GETARG_CASH(0);
965 : : uint64 val;
966 : : StringInfoData buf;
967 : : text *res;
968 : : Cash dollars;
969 : : Cash m0;
970 : : Cash m1;
971 : : Cash m2;
972 : : Cash m3;
973 : : Cash m4;
974 : : Cash m5;
975 : : Cash m6;
976 : :
644 heikki.linnakangas@i 977 : 8 : initStringInfo(&buf);
978 : :
979 : : /* work with positive numbers */
9434 tgl@sss.pgh.pa.us 980 [ - + ]: 8 : if (value < 0)
981 : : {
9434 tgl@sss.pgh.pa.us 982 :UBC 0 : value = -value;
644 heikki.linnakangas@i 983 : 0 : appendStringInfoString(&buf, "minus ");
984 : : }
985 : :
986 : : /* Now treat as unsigned, to avoid trouble at INT_MIN */
7062 darcy@druid.net 987 :CBC 8 : val = (uint64) value;
988 : :
644 heikki.linnakangas@i 989 : 8 : dollars = val / INT64CONST(100);
6172 bruce@momjian.us 990 : 8 : m0 = val % INT64CONST(100); /* cents */
3240 tgl@sss.pgh.pa.us 991 : 8 : m1 = (val / INT64CONST(100)) % 1000; /* hundreds */
992 : 8 : m2 = (val / INT64CONST(100000)) % 1000; /* thousands */
6172 bruce@momjian.us 993 : 8 : m3 = (val / INT64CONST(100000000)) % 1000; /* millions */
3240 tgl@sss.pgh.pa.us 994 : 8 : m4 = (val / INT64CONST(100000000000)) % 1000; /* billions */
6539 995 : 8 : m5 = (val / INT64CONST(100000000000000)) % 1000; /* trillions */
6172 bruce@momjian.us 996 : 8 : m6 = (val / INT64CONST(100000000000000000)) % 1000; /* quadrillions */
997 : :
7062 darcy@druid.net 998 [ - + ]: 8 : if (m6)
999 : : {
644 heikki.linnakangas@i 1000 :UBC 0 : append_num_word(&buf, m6);
1001 : 0 : appendStringInfoString(&buf, " quadrillion ");
1002 : : }
1003 : :
7062 darcy@druid.net 1004 [ - + ]:CBC 8 : if (m5)
1005 : : {
644 heikki.linnakangas@i 1006 :UBC 0 : append_num_word(&buf, m5);
1007 : 0 : appendStringInfoString(&buf, " trillion ");
1008 : : }
1009 : :
7062 darcy@druid.net 1010 [ - + ]:CBC 8 : if (m4)
1011 : : {
644 heikki.linnakangas@i 1012 :UBC 0 : append_num_word(&buf, m4);
1013 : 0 : appendStringInfoString(&buf, " billion ");
1014 : : }
1015 : :
10467 bruce@momjian.us 1016 [ - + ]:CBC 8 : if (m3)
1017 : : {
644 heikki.linnakangas@i 1018 :UBC 0 : append_num_word(&buf, m3);
1019 : 0 : appendStringInfoString(&buf, " million ");
1020 : : }
1021 : :
10467 bruce@momjian.us 1022 [ - + ]:CBC 8 : if (m2)
1023 : : {
644 heikki.linnakangas@i 1024 :UBC 0 : append_num_word(&buf, m2);
1025 : 0 : appendStringInfoString(&buf, " thousand ");
1026 : : }
1027 : :
10467 bruce@momjian.us 1028 [ + - ]:CBC 8 : if (m1)
644 heikki.linnakangas@i 1029 : 8 : append_num_word(&buf, m1);
1030 : :
1031 [ - + ]: 8 : if (dollars == 0)
644 heikki.linnakangas@i 1032 :UBC 0 : appendStringInfoString(&buf, "zero");
1033 : :
644 heikki.linnakangas@i 1034 [ - + ]:CBC 8 : appendStringInfoString(&buf, dollars == 1 ? " dollar and " : " dollars and ");
1035 : 8 : append_num_word(&buf, m0);
1036 [ - + ]: 8 : appendStringInfoString(&buf, m0 == 1 ? " cent" : " cents");
1037 : :
1038 : : /* capitalize output */
155 jdavis@postgresql.or 1039 :GNC 8 : buf.data[0] = pg_ascii_toupper((unsigned char) buf.data[0]);
1040 : :
1041 : : /* return as text datum */
644 heikki.linnakangas@i 1042 :CBC 8 : res = cstring_to_text_with_len(buf.data, buf.len);
1043 : 8 : pfree(buf.data);
1044 : 8 : PG_RETURN_TEXT_P(res);
1045 : : }
1046 : :
1047 : :
1048 : : /* cash_numeric()
1049 : : * Convert cash to numeric.
1050 : : */
1051 : : Datum
5772 tgl@sss.pgh.pa.us 1052 : 16 : cash_numeric(PG_FUNCTION_ARGS)
1053 : : {
1054 : 16 : Cash money = PG_GETARG_CASH(0);
1055 : : Datum result;
1056 : : int fpoint;
1057 : 16 : struct lconv *lconvert = PGLC_localeconv();
1058 : :
1059 : : /* see comments about frac_digits in cash_in() */
1060 : 16 : fpoint = lconvert->frac_digits;
1061 [ - + - - ]: 16 : if (fpoint < 0 || fpoint > 10)
1062 : 16 : fpoint = 2;
1063 : :
1064 : : /* convert the integral money value to numeric */
2064 peter@eisentraut.org 1065 : 16 : result = NumericGetDatum(int64_to_numeric(money));
1066 : :
1067 : : /* scale appropriately, if needed */
2475 tgl@sss.pgh.pa.us 1068 [ + - ]: 16 : if (fpoint > 0)
1069 : : {
1070 : : int64 scale;
1071 : : int i;
1072 : : Datum numeric_scale;
1073 : : Datum quotient;
1074 : :
1075 : : /* compute required scale factor */
1076 : 16 : scale = 1;
1077 [ + + ]: 48 : for (i = 0; i < fpoint; i++)
1078 : 32 : scale *= 10;
2064 peter@eisentraut.org 1079 : 16 : numeric_scale = NumericGetDatum(int64_to_numeric(scale));
1080 : :
1081 : : /*
1082 : : * Given integral inputs approaching INT64_MAX, select_div_scale()
1083 : : * might choose a result scale of zero, causing loss of fractional
1084 : : * digits in the quotient. We can ensure an exact result by setting
1085 : : * the dscale of either input to be at least as large as the desired
1086 : : * result scale. numeric_round() will do that for us.
1087 : : */
2475 tgl@sss.pgh.pa.us 1088 : 16 : numeric_scale = DirectFunctionCall2(numeric_round,
1089 : : numeric_scale,
1090 : : Int32GetDatum(fpoint));
1091 : :
1092 : : /* Now we can safely divide ... */
1093 : 16 : quotient = DirectFunctionCall2(numeric_div, result, numeric_scale);
1094 : :
1095 : : /* ... and forcibly round to exactly the intended number of digits */
1096 : 16 : result = DirectFunctionCall2(numeric_round,
1097 : : quotient,
1098 : : Int32GetDatum(fpoint));
1099 : : }
1100 : :
1101 : 16 : PG_RETURN_DATUM(result);
1102 : : }
1103 : :
1104 : : /* numeric_cash()
1105 : : * Convert numeric to cash.
1106 : : */
1107 : : Datum
5772 1108 : 8 : numeric_cash(PG_FUNCTION_ARGS)
1109 : : {
36 peter@eisentraut.org 1110 :GNC 8 : Numeric amount = PG_GETARG_NUMERIC(0);
1111 : : Cash result;
1112 : : int fpoint;
1113 : : int64 scale;
1114 : : int i;
1115 : : Numeric numeric_scale;
5772 tgl@sss.pgh.pa.us 1116 :CBC 8 : struct lconv *lconvert = PGLC_localeconv();
1117 : :
1118 : : /* see comments about frac_digits in cash_in() */
1119 : 8 : fpoint = lconvert->frac_digits;
1120 [ - + - - ]: 8 : if (fpoint < 0 || fpoint > 10)
1121 : 8 : fpoint = 2;
1122 : :
1123 : : /* compute required scale factor */
1124 : 8 : scale = 1;
1125 [ + + ]: 24 : for (i = 0; i < fpoint; i++)
1126 : 16 : scale *= 10;
1127 : :
1128 : : /* multiply the input amount by scale factor */
36 peter@eisentraut.org 1129 :GNC 8 : numeric_scale = int64_to_numeric(scale);
1130 : :
1131 : 8 : amount = numeric_mul_safe(amount, numeric_scale, fcinfo->context);
1132 [ - + - - : 8 : if (unlikely(SOFT_ERROR_OCCURRED(fcinfo->context)))
- - - + ]
36 peter@eisentraut.org 1133 :UNC 0 : PG_RETURN_NULL();
1134 : :
1135 : : /* note that numeric_int8 will round to nearest integer for us */
36 peter@eisentraut.org 1136 :GNC 8 : result = numeric_int8_safe(amount, fcinfo->context);
1137 [ - + - - : 8 : if (unlikely(SOFT_ERROR_OCCURRED(fcinfo->context)))
- - - + ]
36 peter@eisentraut.org 1138 :UNC 0 : PG_RETURN_NULL();
1139 : :
5772 tgl@sss.pgh.pa.us 1140 :CBC 8 : PG_RETURN_CASH(result);
1141 : : }
1142 : :
1143 : : /* int4_cash()
1144 : : * Convert int4 (int) to cash
1145 : : */
1146 : : Datum
5509 rhaas@postgresql.org 1147 : 28 : int4_cash(PG_FUNCTION_ARGS)
1148 : : {
5504 bruce@momjian.us 1149 : 28 : int32 amount = PG_GETARG_INT32(0);
1150 : : Cash result;
1151 : : int fpoint;
1152 : : int64 scale;
1153 : : int i;
5509 rhaas@postgresql.org 1154 : 28 : struct lconv *lconvert = PGLC_localeconv();
1155 : :
1156 : : /* see comments about frac_digits in cash_in() */
1157 : 28 : fpoint = lconvert->frac_digits;
1158 [ - + - - ]: 28 : if (fpoint < 0 || fpoint > 10)
1159 : 28 : fpoint = 2;
1160 : :
1161 : : /* compute required scale factor */
1162 : 28 : scale = 1;
1163 [ + + ]: 84 : for (i = 0; i < fpoint; i++)
1164 : 56 : scale *= 10;
1165 : :
1166 : : /* compute amount * scale, checking for overflow */
36 peter@eisentraut.org 1167 [ - + ]:GNC 28 : if (unlikely(pg_mul_s64_overflow(amount, scale, &result)))
36 peter@eisentraut.org 1168 [ # # ]:UNC 0 : ereturn(fcinfo->context, (Datum) 0,
1169 : : errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1170 : : errmsg("bigint out of range"));
1171 : :
5509 rhaas@postgresql.org 1172 :CBC 28 : PG_RETURN_CASH(result);
1173 : : }
1174 : :
1175 : : /* int8_cash()
1176 : : * Convert int8 (bigint) to cash
1177 : : */
1178 : : Datum
1179 : 16 : int8_cash(PG_FUNCTION_ARGS)
1180 : : {
5504 bruce@momjian.us 1181 : 16 : int64 amount = PG_GETARG_INT64(0);
1182 : : Cash result;
1183 : : int fpoint;
1184 : : int64 scale;
1185 : : int i;
5509 rhaas@postgresql.org 1186 : 16 : struct lconv *lconvert = PGLC_localeconv();
1187 : :
1188 : : /* see comments about frac_digits in cash_in() */
1189 : 16 : fpoint = lconvert->frac_digits;
1190 [ - + - - ]: 16 : if (fpoint < 0 || fpoint > 10)
1191 : 16 : fpoint = 2;
1192 : :
1193 : : /* compute required scale factor */
1194 : 16 : scale = 1;
1195 [ + + ]: 48 : for (i = 0; i < fpoint; i++)
1196 : 32 : scale *= 10;
1197 : :
1198 : : /* compute amount * scale, checking for overflow */
36 peter@eisentraut.org 1199 [ - + ]:GNC 16 : if (unlikely(pg_mul_s64_overflow(amount, scale, &result)))
36 peter@eisentraut.org 1200 [ # # ]:UNC 0 : ereturn(fcinfo->context, (Datum) 0,
1201 : : errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1202 : : errmsg("bigint out of range"));
1203 : :
5509 rhaas@postgresql.org 1204 :CBC 16 : PG_RETURN_CASH(result);
1205 : : }
|