PostgreSQL Source Code git master
Loading...
Searching...
No Matches
cash.c File Reference
#include "postgres.h"
#include <limits.h>
#include <ctype.h>
#include <math.h>
#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "utils/builtins.h"
#include "utils/cash.h"
#include "utils/float.h"
#include "utils/numeric.h"
#include "utils/pg_locale.h"
Include dependency graph for cash.c:

Go to the source code of this file.

Functions

static void append_num_word (StringInfo buf, Cash value)
 
static Cash cash_pl_cash (Cash c1, Cash c2)
 
static Cash cash_mi_cash (Cash c1, Cash c2)
 
static Cash cash_mul_float8 (Cash c, float8 f)
 
static Cash cash_div_float8 (Cash c, float8 f)
 
static Cash cash_mul_int64 (Cash c, int64 i)
 
static Cash cash_div_int64 (Cash c, int64 i)
 
Datum cash_in (PG_FUNCTION_ARGS)
 
Datum cash_out (PG_FUNCTION_ARGS)
 
Datum cash_recv (PG_FUNCTION_ARGS)
 
Datum cash_send (PG_FUNCTION_ARGS)
 
Datum cash_eq (PG_FUNCTION_ARGS)
 
Datum cash_ne (PG_FUNCTION_ARGS)
 
Datum cash_lt (PG_FUNCTION_ARGS)
 
Datum cash_le (PG_FUNCTION_ARGS)
 
Datum cash_gt (PG_FUNCTION_ARGS)
 
Datum cash_ge (PG_FUNCTION_ARGS)
 
Datum cash_cmp (PG_FUNCTION_ARGS)
 
Datum cash_pl (PG_FUNCTION_ARGS)
 
Datum cash_mi (PG_FUNCTION_ARGS)
 
Datum cash_div_cash (PG_FUNCTION_ARGS)
 
Datum cash_mul_flt8 (PG_FUNCTION_ARGS)
 
Datum flt8_mul_cash (PG_FUNCTION_ARGS)
 
Datum cash_div_flt8 (PG_FUNCTION_ARGS)
 
Datum cash_mul_flt4 (PG_FUNCTION_ARGS)
 
Datum flt4_mul_cash (PG_FUNCTION_ARGS)
 
Datum cash_div_flt4 (PG_FUNCTION_ARGS)
 
Datum cash_mul_int8 (PG_FUNCTION_ARGS)
 
Datum int8_mul_cash (PG_FUNCTION_ARGS)
 
Datum cash_div_int8 (PG_FUNCTION_ARGS)
 
Datum cash_mul_int4 (PG_FUNCTION_ARGS)
 
Datum int4_mul_cash (PG_FUNCTION_ARGS)
 
Datum cash_div_int4 (PG_FUNCTION_ARGS)
 
Datum cash_mul_int2 (PG_FUNCTION_ARGS)
 
Datum int2_mul_cash (PG_FUNCTION_ARGS)
 
Datum cash_div_int2 (PG_FUNCTION_ARGS)
 
Datum cashlarger (PG_FUNCTION_ARGS)
 
Datum cashsmaller (PG_FUNCTION_ARGS)
 
Datum cash_words (PG_FUNCTION_ARGS)
 
Datum cash_numeric (PG_FUNCTION_ARGS)
 
Datum numeric_cash (PG_FUNCTION_ARGS)
 
Datum int4_cash (PG_FUNCTION_ARGS)
 
Datum int8_cash (PG_FUNCTION_ARGS)
 

Function Documentation

◆ append_num_word()

static void append_num_word ( StringInfo  buf,
Cash  value 
)
static

Definition at line 40 of file cash.c.

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 };
48 const char *const *big = small + 18;
49 int tu = value % 100;
50
51 /* deal with the simple cases first */
52 if (value <= 20)
53 {
55 return;
56 }
57
58 /* is it an even multiple of 100? */
59 if (!tu)
60 {
61 appendStringInfo(buf, "%s hundred", small[value / 100]);
62 return;
63 }
64
65 /* more than 99? */
66 if (value > 99)
67 {
68 /* is it an even multiple of 10 other than 10? */
69 if (value % 10 == 0 && tu > 10)
70 appendStringInfo(buf, "%s hundred %s",
71 small[value / 100], big[tu / 10]);
72 else if (tu < 20)
73 appendStringInfo(buf, "%s hundred and %s",
74 small[value / 100], small[tu]);
75 else
76 appendStringInfo(buf, "%s hundred %s %s",
77 small[value / 100], big[tu / 10], small[tu % 10]);
78 }
79 else
80 {
81 /* is it an even multiple of 10 other than 10? */
82 if (value % 10 == 0 && tu > 10)
84 else if (tu < 20)
86 else
87 appendStringInfo(buf, "%s %s", big[tu / 10], small[tu % 10]);
88 }
89}
static struct @177 value
static char buf[DEFAULT_XLOG_SEG_SIZE]
static int fb(int x)
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition stringinfo.c:145
void appendStringInfoString(StringInfo str, const char *s)
Definition stringinfo.c:230

References appendStringInfo(), appendStringInfoString(), buf, fb(), and value.

Referenced by cash_words().

◆ cash_cmp()

Datum cash_cmp ( PG_FUNCTION_ARGS  )

Definition at line 675 of file cash.c.

676{
679
680 if (c1 > c2)
682 else if (c1 == c2)
684 else
685 PG_RETURN_INT32(-1);
686}
int64 Cash
Definition cash.h:17
#define PG_GETARG_CASH(n)
Definition cash.h:32
#define PG_RETURN_INT32(x)
Definition fmgr.h:355

References fb(), PG_GETARG_CASH, and PG_RETURN_INT32.

◆ cash_div_cash()

Datum cash_div_cash ( PG_FUNCTION_ARGS  )

Definition at line 722 of file cash.c.

723{
727
728 if (divisor == 0)
731 errmsg("division by zero")));
732
735}
double float8
Definition c.h:714
int errcode(int sqlerrcode)
Definition elog.c:875
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
#define PG_RETURN_FLOAT8(x)
Definition fmgr.h:369
static char * errmsg

References ereport, errcode(), errmsg, ERROR, fb(), PG_GETARG_CASH, and PG_RETURN_FLOAT8.

◆ cash_div_float8()

static Cash cash_div_float8 ( Cash  c,
float8  f 
)
inlinestatic

Definition at line 131 of file cash.c.

132{
133 float8 res = rint(float8_div((float8) c, f));
134
135 if (unlikely(isnan(res) || !FLOAT8_FITS_IN_INT64(res)))
138 errmsg("money out of range")));
139
140 return (Cash) res;
141}
#define FLOAT8_FITS_IN_INT64(num)
Definition c.h:1179
#define unlikely(x)
Definition c.h:438
static float8 float8_div(const float8 val1, const float8 val2)
Definition float.h:230
char * c

References ereport, errcode(), errmsg, ERROR, fb(), float8_div(), FLOAT8_FITS_IN_INT64, and unlikely.

Referenced by cash_div_flt4(), and cash_div_flt8().

◆ cash_div_flt4()

Datum cash_div_flt4 ( PG_FUNCTION_ARGS  )

Definition at line 814 of file cash.c.

815{
816 Cash c = PG_GETARG_CASH(0);
818
820}
float float4
Definition c.h:713
static Cash cash_div_float8(Cash c, float8 f)
Definition cash.c:131
#define PG_RETURN_CASH(x)
Definition cash.h:33
#define PG_GETARG_FLOAT4(n)
Definition fmgr.h:282

References cash_div_float8(), PG_GETARG_CASH, PG_GETARG_FLOAT4, and PG_RETURN_CASH.

◆ cash_div_flt8()

Datum cash_div_flt8 ( PG_FUNCTION_ARGS  )

Definition at line 771 of file cash.c.

772{
773 Cash c = PG_GETARG_CASH(0);
775
777}
#define PG_GETARG_FLOAT8(n)
Definition fmgr.h:283

References cash_div_float8(), PG_GETARG_CASH, PG_GETARG_FLOAT8, and PG_RETURN_CASH.

◆ cash_div_int2()

Datum cash_div_int2 ( PG_FUNCTION_ARGS  )

Definition at line 939 of file cash.c.

940{
941 Cash c = PG_GETARG_CASH(0);
942 int16 s = PG_GETARG_INT16(1);
943
945}
int64_t int64
Definition c.h:621
int16_t int16
Definition c.h:619
static Cash cash_div_int64(Cash c, int64 i)
Definition cash.c:157
#define PG_GETARG_INT16(n)
Definition fmgr.h:271

References cash_div_int64(), PG_GETARG_CASH, PG_GETARG_INT16, and PG_RETURN_CASH.

◆ cash_div_int4()

Datum cash_div_int4 ( PG_FUNCTION_ARGS  )

Definition at line 898 of file cash.c.

899{
900 Cash c = PG_GETARG_CASH(0);
902
904}
int32_t int32
Definition c.h:620
#define PG_GETARG_INT32(n)
Definition fmgr.h:269
int i
Definition isn.c:77

References cash_div_int64(), i, PG_GETARG_CASH, PG_GETARG_INT32, and PG_RETURN_CASH.

◆ cash_div_int64()

static Cash cash_div_int64 ( Cash  c,
int64  i 
)
inlinestatic

Definition at line 157 of file cash.c.

158{
159 if (unlikely(i == 0))
162 errmsg("division by zero")));
163
164 return c / i;
165}

References ereport, errcode(), errmsg, ERROR, fb(), i, and unlikely.

Referenced by cash_div_int2(), cash_div_int4(), and cash_div_int8().

◆ cash_div_int8()

Datum cash_div_int8 ( PG_FUNCTION_ARGS  )

Definition at line 855 of file cash.c.

856{
857 Cash c = PG_GETARG_CASH(0);
859
861}
#define PG_GETARG_INT64(n)
Definition fmgr.h:284

References cash_div_int64(), i, PG_GETARG_CASH, PG_GETARG_INT64, and PG_RETURN_CASH.

◆ cash_eq()

Datum cash_eq ( PG_FUNCTION_ARGS  )

Definition at line 621 of file cash.c.

622{
625
626 PG_RETURN_BOOL(c1 == c2);
627}
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360

References fb(), PG_GETARG_CASH, and PG_RETURN_BOOL.

◆ cash_ge()

Datum cash_ge ( PG_FUNCTION_ARGS  )

Definition at line 666 of file cash.c.

667{
670
671 PG_RETURN_BOOL(c1 >= c2);
672}

References fb(), PG_GETARG_CASH, and PG_RETURN_BOOL.

◆ cash_gt()

Datum cash_gt ( PG_FUNCTION_ARGS  )

Definition at line 657 of file cash.c.

658{
661
663}

References fb(), PG_GETARG_CASH, and PG_RETURN_BOOL.

◆ cash_in()

Datum cash_in ( PG_FUNCTION_ARGS  )

Definition at line 175 of file cash.c.

176{
177 char *str = PG_GETARG_CSTRING(0);
178 Node *escontext = fcinfo->context;
179 Cash result;
180 Cash value = 0;
181 Cash dec = 0;
182 Cash sgn = 1;
183 bool seen_dot = false;
184 const char *s = str;
185 int fpoint;
186 char dsymbol;
187 const char *ssymbol,
188 *psymbol,
189 *nsymbol,
190 *csymbol;
191 struct lconv *lconvert = PGLC_localeconv();
192
193 /*
194 * frac_digits will be CHAR_MAX in some locales, notably C. However, just
195 * testing for == CHAR_MAX is risky, because of compilers like gcc that
196 * "helpfully" let you alter the platform-standard definition of whether
197 * char is signed or not. If we are so unfortunate as to get compiled
198 * with a nonstandard -fsigned-char or -funsigned-char switch, then our
199 * idea of CHAR_MAX will not agree with libc's. The safest course is not
200 * to test for CHAR_MAX at all, but to impose a range check for plausible
201 * frac_digits values.
202 */
203 fpoint = lconvert->frac_digits;
205 fpoint = 2; /* best guess in this case, I think */
206
207 /* we restrict dsymbol to be a single byte, but not the other symbols */
208 if (*lconvert->mon_decimal_point != '\0' &&
209 lconvert->mon_decimal_point[1] == '\0')
210 dsymbol = *lconvert->mon_decimal_point;
211 else
212 dsymbol = '.';
213 if (*lconvert->mon_thousands_sep != '\0')
214 ssymbol = lconvert->mon_thousands_sep;
215 else /* ssymbol should not equal dsymbol */
216 ssymbol = (dsymbol != ',') ? "," : ".";
217 csymbol = (*lconvert->currency_symbol != '\0') ? lconvert->currency_symbol : "$";
218 psymbol = (*lconvert->positive_sign != '\0') ? lconvert->positive_sign : "+";
219 nsymbol = (*lconvert->negative_sign != '\0') ? lconvert->negative_sign : "-";
220
221#ifdef CASHDEBUG
222 printf("cashin- precision '%d'; decimal '%c'; thousands '%s'; currency '%s'; positive '%s'; negative '%s'\n",
224#endif
225
226 /* we need to add all sorts of checking here. For now just */
227 /* strip all leading whitespace and any leading currency symbol */
228 while (isspace((unsigned char) *s))
229 s++;
230 if (strncmp(s, csymbol, strlen(csymbol)) == 0)
231 s += strlen(csymbol);
232 while (isspace((unsigned char) *s))
233 s++;
234
235#ifdef CASHDEBUG
236 printf("cashin- string is '%s'\n", s);
237#endif
238
239 /* a leading minus or paren signifies a negative number */
240 /* again, better heuristics needed */
241 /* XXX - doesn't properly check for balanced parens - djmc */
242 if (strncmp(s, nsymbol, strlen(nsymbol)) == 0)
243 {
244 sgn = -1;
245 s += strlen(nsymbol);
246 }
247 else if (*s == '(')
248 {
249 sgn = -1;
250 s++;
251 }
252 else if (strncmp(s, psymbol, strlen(psymbol)) == 0)
253 s += strlen(psymbol);
254
255#ifdef CASHDEBUG
256 printf("cashin- string is '%s'\n", s);
257#endif
258
259 /* allow whitespace and currency symbol after the sign, too */
260 while (isspace((unsigned char) *s))
261 s++;
262 if (strncmp(s, csymbol, strlen(csymbol)) == 0)
263 s += strlen(csymbol);
264 while (isspace((unsigned char) *s))
265 s++;
266
267#ifdef CASHDEBUG
268 printf("cashin- string is '%s'\n", s);
269#endif
270
271 /*
272 * We accumulate the absolute amount in "value" and then apply the sign at
273 * the end. (The sign can appear before or after the digits, so it would
274 * be more complicated to do otherwise.) Because of the larger range of
275 * negative signed integers, we build "value" in the negative and then
276 * flip the sign at the end, catching most-negative-number overflow if
277 * necessary.
278 */
279
280 for (; *s; s++)
281 {
282 /*
283 * We look for digits as long as we have found less than the required
284 * number of decimal places.
285 */
286 if (isdigit((unsigned char) *s) && (!seen_dot || dec < fpoint))
287 {
288 int8 digit = *s - '0';
289
290 if (pg_mul_s64_overflow(value, 10, &value) ||
292 ereturn(escontext, (Datum) 0,
294 errmsg("value \"%s\" is out of range for type %s",
295 str, "money")));
296
297 if (seen_dot)
298 dec++;
299 }
300 /* decimal point? then start counting fractions... */
301 else if (*s == dsymbol && !seen_dot)
302 {
303 seen_dot = true;
304 }
305 /* ignore if "thousands" separator, else we're done */
306 else if (strncmp(s, ssymbol, strlen(ssymbol)) == 0)
307 s += strlen(ssymbol) - 1;
308 else
309 break;
310 }
311
312 /* round off if there's another digit */
313 if (isdigit((unsigned char) *s) && *s >= '5')
314 {
315 /* remember we build the value in the negative */
317 ereturn(escontext, (Datum) 0,
319 errmsg("value \"%s\" is out of range for type %s",
320 str, "money")));
321 }
322
323 /* adjust for less than required decimal places */
324 for (; dec < fpoint; dec++)
325 {
327 ereturn(escontext, (Datum) 0,
329 errmsg("value \"%s\" is out of range for type %s",
330 str, "money")));
331 }
332
333 /*
334 * should only be trailing digits followed by whitespace, right paren,
335 * trailing sign, and/or trailing currency symbol
336 */
337 while (isdigit((unsigned char) *s))
338 s++;
339
340 while (*s)
341 {
342 if (isspace((unsigned char) *s) || *s == ')')
343 s++;
344 else if (strncmp(s, nsymbol, strlen(nsymbol)) == 0)
345 {
346 sgn = -1;
347 s += strlen(nsymbol);
348 }
349 else if (strncmp(s, psymbol, strlen(psymbol)) == 0)
350 s += strlen(psymbol);
351 else if (strncmp(s, csymbol, strlen(csymbol)) == 0)
352 s += strlen(csymbol);
353 else
354 ereturn(escontext, (Datum) 0,
356 errmsg("invalid input syntax for type %s: \"%s\"",
357 "money", str)));
358 }
359
360 /*
361 * If the value is supposed to be positive, flip the sign, but check for
362 * the most negative number.
363 */
364 if (sgn > 0)
365 {
366 if (value == PG_INT64_MIN)
367 ereturn(escontext, (Datum) 0,
369 errmsg("value \"%s\" is out of range for type %s",
370 str, "money")));
371 result = -value;
372 }
373 else
374 result = value;
375
376#ifdef CASHDEBUG
377 printf("cashin- result is " INT64_FORMAT "\n", result);
378#endif
379
381}
#define INT64_FORMAT
Definition c.h:634
int8_t int8
Definition c.h:618
#define PG_INT64_MIN
Definition c.h:675
uint32 result
#define ereturn(context, dummy_value,...)
Definition elog.h:280
#define PG_GETARG_CSTRING(n)
Definition fmgr.h:278
const char * str
static bool pg_mul_s64_overflow(int64 a, int64 b, int64 *result)
Definition int.h:293
static bool pg_sub_s64_overflow(int64 a, int64 b, int64 *result)
Definition int.h:262
struct lconv * PGLC_localeconv(void)
Definition pg_locale.c:506
#define printf(...)
Definition port.h:267
uint64_t Datum
Definition postgres.h:70
Definition nodes.h:135

References ereturn, errcode(), errmsg, fb(), INT64_FORMAT, PG_GETARG_CSTRING, PG_INT64_MIN, pg_mul_s64_overflow(), PG_RETURN_CASH, pg_sub_s64_overflow(), PGLC_localeconv(), printf, result, str, and value.

◆ cash_le()

Datum cash_le ( PG_FUNCTION_ARGS  )

Definition at line 648 of file cash.c.

649{
652
653 PG_RETURN_BOOL(c1 <= c2);
654}

References fb(), PG_GETARG_CASH, and PG_RETURN_BOOL.

◆ cash_lt()

Datum cash_lt ( PG_FUNCTION_ARGS  )

Definition at line 639 of file cash.c.

640{
643
645}

References fb(), PG_GETARG_CASH, and PG_RETURN_BOOL.

◆ cash_mi()

Datum cash_mi ( PG_FUNCTION_ARGS  )

Definition at line 708 of file cash.c.

709{
712
714}
static Cash cash_mi_cash(Cash c1, Cash c2)
Definition cash.c:105

References cash_mi_cash(), fb(), PG_GETARG_CASH, and PG_RETURN_CASH.

◆ cash_mi_cash()

static Cash cash_mi_cash ( Cash  c1,
Cash  c2 
)
inlinestatic

Definition at line 105 of file cash.c.

106{
107 Cash res;
108
109 if (unlikely(pg_sub_s64_overflow(c1, c2, &res)))
112 errmsg("money out of range")));
113
114 return res;
115}

References ereport, errcode(), errmsg, ERROR, fb(), pg_sub_s64_overflow(), and unlikely.

Referenced by cash_mi().

◆ cash_mul_float8()

static Cash cash_mul_float8 ( Cash  c,
float8  f 
)
inlinestatic

Definition at line 118 of file cash.c.

119{
120 float8 res = rint(float8_mul((float8) c, f));
121
122 if (unlikely(isnan(res) || !FLOAT8_FITS_IN_INT64(res)))
125 errmsg("money out of range")));
126
127 return (Cash) res;
128}
static float8 float8_mul(const float8 val1, const float8 val2)
Definition float.h:192

References ereport, errcode(), errmsg, ERROR, fb(), FLOAT8_FITS_IN_INT64, float8_mul(), and unlikely.

Referenced by cash_mul_flt4(), cash_mul_flt8(), flt4_mul_cash(), and flt8_mul_cash().

◆ cash_mul_flt4()

Datum cash_mul_flt4 ( PG_FUNCTION_ARGS  )

Definition at line 785 of file cash.c.

786{
787 Cash c = PG_GETARG_CASH(0);
789
791}
static Cash cash_mul_float8(Cash c, float8 f)
Definition cash.c:118

References cash_mul_float8(), PG_GETARG_CASH, PG_GETARG_FLOAT4, and PG_RETURN_CASH.

◆ cash_mul_flt8()

Datum cash_mul_flt8 ( PG_FUNCTION_ARGS  )

Definition at line 743 of file cash.c.

744{
745 Cash c = PG_GETARG_CASH(0);
747
749}

References cash_mul_float8(), PG_GETARG_CASH, PG_GETARG_FLOAT8, and PG_RETURN_CASH.

◆ cash_mul_int2()

Datum cash_mul_int2 ( PG_FUNCTION_ARGS  )

Definition at line 912 of file cash.c.

913{
914 Cash c = PG_GETARG_CASH(0);
915 int16 s = PG_GETARG_INT16(1);
916
918}
static Cash cash_mul_int64(Cash c, int64 i)
Definition cash.c:144

References cash_mul_int64(), PG_GETARG_CASH, PG_GETARG_INT16, and PG_RETURN_CASH.

◆ cash_mul_int4()

Datum cash_mul_int4 ( PG_FUNCTION_ARGS  )

Definition at line 869 of file cash.c.

870{
871 Cash c = PG_GETARG_CASH(0);
873
875}

References cash_mul_int64(), i, PG_GETARG_CASH, PG_GETARG_INT32, and PG_RETURN_CASH.

◆ cash_mul_int64()

static Cash cash_mul_int64 ( Cash  c,
int64  i 
)
inlinestatic

Definition at line 144 of file cash.c.

145{
146 Cash res;
147
148 if (unlikely(pg_mul_s64_overflow(c, i, &res)))
151 errmsg("money out of range")));
152
153 return res;
154}

References ereport, errcode(), errmsg, ERROR, fb(), i, pg_mul_s64_overflow(), and unlikely.

Referenced by cash_mul_int2(), cash_mul_int4(), cash_mul_int8(), int2_mul_cash(), int4_mul_cash(), and int8_mul_cash().

◆ cash_mul_int8()

Datum cash_mul_int8 ( PG_FUNCTION_ARGS  )

Definition at line 828 of file cash.c.

829{
830 Cash c = PG_GETARG_CASH(0);
832
834}

References cash_mul_int64(), i, PG_GETARG_CASH, PG_GETARG_INT64, and PG_RETURN_CASH.

◆ cash_ne()

Datum cash_ne ( PG_FUNCTION_ARGS  )

Definition at line 630 of file cash.c.

631{
634
635 PG_RETURN_BOOL(c1 != c2);
636}

References fb(), PG_GETARG_CASH, and PG_RETURN_BOOL.

◆ cash_numeric()

Datum cash_numeric ( PG_FUNCTION_ARGS  )

Definition at line 1076 of file cash.c.

1077{
1079 Datum result;
1080 int fpoint;
1081 struct lconv *lconvert = PGLC_localeconv();
1082
1083 /* see comments about frac_digits in cash_in() */
1084 fpoint = lconvert->frac_digits;
1086 fpoint = 2;
1087
1088 /* convert the integral money value to numeric */
1090
1091 /* scale appropriately, if needed */
1092 if (fpoint > 0)
1093 {
1094 int64 scale;
1095 int i;
1098
1099 /* compute required scale factor */
1100 scale = 1;
1101 for (i = 0; i < fpoint; i++)
1102 scale *= 10;
1104
1105 /*
1106 * Given integral inputs approaching INT64_MAX, select_div_scale()
1107 * might choose a result scale of zero, causing loss of fractional
1108 * digits in the quotient. We can ensure an exact result by setting
1109 * the dscale of either input to be at least as large as the desired
1110 * result scale. numeric_round() will do that for us.
1111 */
1115
1116 /* Now we can safely divide ... */
1118
1119 /* ... and forcibly round to exactly the intended number of digits */
1121 quotient,
1123 }
1124
1126}
Datum numeric_div(PG_FUNCTION_ARGS)
Definition numeric.c:3139
Datum numeric_round(PG_FUNCTION_ARGS)
Definition numeric.c:1528
Numeric int64_to_numeric(int64 val)
Definition numeric.c:4264
Datum numeric_scale(PG_FUNCTION_ARGS)
Definition numeric.c:4103
#define DirectFunctionCall2(func, arg1, arg2)
Definition fmgr.h:690
#define PG_RETURN_DATUM(x)
Definition fmgr.h:354
static Datum NumericGetDatum(Numeric X)
Definition numeric.h:76
static int scale
Definition pgbench.c:182
static Datum Int32GetDatum(int32 X)
Definition postgres.h:212

References DirectFunctionCall2, fb(), i, Int32GetDatum(), int64_to_numeric(), numeric_div(), numeric_round(), numeric_scale(), NumericGetDatum(), PG_GETARG_CASH, PG_RETURN_DATUM, PGLC_localeconv(), result, and scale.

◆ cash_out()

Datum cash_out ( PG_FUNCTION_ARGS  )

Definition at line 390 of file cash.c.

391{
394 char *result;
395 char buf[128];
396 char *bufptr;
397 int digit_pos;
398 int points,
399 mon_group;
400 char dsymbol;
401 const char *ssymbol,
402 *csymbol,
403 *signsymbol;
404 char sign_posn,
407 struct lconv *lconvert = PGLC_localeconv();
408
409 /* see comments about frac_digits in cash_in() */
410 points = lconvert->frac_digits;
412 points = 2; /* best guess in this case, I think */
413
414 /*
415 * As with frac_digits, must apply a range check to mon_grouping to avoid
416 * being fooled by variant CHAR_MAX values.
417 */
418 mon_group = *lconvert->mon_grouping;
420 mon_group = 3;
421
422 /* we restrict dsymbol to be a single byte, but not the other symbols */
423 if (*lconvert->mon_decimal_point != '\0' &&
424 lconvert->mon_decimal_point[1] == '\0')
425 dsymbol = *lconvert->mon_decimal_point;
426 else
427 dsymbol = '.';
428 if (*lconvert->mon_thousands_sep != '\0')
429 ssymbol = lconvert->mon_thousands_sep;
430 else /* ssymbol should not equal dsymbol */
431 ssymbol = (dsymbol != ',') ? "," : ".";
432 csymbol = (*lconvert->currency_symbol != '\0') ? lconvert->currency_symbol : "$";
433
434 if (value < 0)
435 {
436 /* set up formatting data */
437 signsymbol = (*lconvert->negative_sign != '\0') ? lconvert->negative_sign : "-";
438 sign_posn = lconvert->n_sign_posn;
439 cs_precedes = lconvert->n_cs_precedes;
440 sep_by_space = lconvert->n_sep_by_space;
441 }
442 else
443 {
444 signsymbol = lconvert->positive_sign;
445 sign_posn = lconvert->p_sign_posn;
446 cs_precedes = lconvert->p_cs_precedes;
447 sep_by_space = lconvert->p_sep_by_space;
448 }
449
450 /* make the amount positive for digit-reconstruction loop */
452
453 /* we build the digits+decimal-point+sep string right-to-left in buf[] */
454 bufptr = buf + sizeof(buf) - 1;
455 *bufptr = '\0';
456
457 /*
458 * Generate digits till there are no non-zero digits left and we emitted
459 * at least one to the left of the decimal point. digit_pos is the
460 * current digit position, with zero as the digit just left of the decimal
461 * point, increasing to the right.
462 */
464 do
465 {
466 if (points && digit_pos == 0)
467 {
468 /* insert decimal point, but not if value cannot be fractional */
469 *(--bufptr) = dsymbol;
470 }
471 else if (digit_pos < 0 && (digit_pos % mon_group) == 0)
472 {
473 /* insert thousands sep, but only to left of radix point */
474 bufptr -= strlen(ssymbol);
475 memcpy(bufptr, ssymbol, strlen(ssymbol));
476 }
477
478 *(--bufptr) = (uvalue % 10) + '0';
479 uvalue = uvalue / 10;
480 digit_pos--;
481 } while (uvalue || digit_pos >= 0);
482
483 /*----------
484 * Now, attach currency symbol and sign symbol in the correct order.
485 *
486 * The POSIX spec defines these values controlling this code:
487 *
488 * p/n_sign_posn:
489 * 0 Parentheses enclose the quantity and the currency_symbol.
490 * 1 The sign string precedes the quantity and the currency_symbol.
491 * 2 The sign string succeeds the quantity and the currency_symbol.
492 * 3 The sign string precedes the currency_symbol.
493 * 4 The sign string succeeds the currency_symbol.
494 *
495 * p/n_cs_precedes: 0 means currency symbol after value, else before it.
496 *
497 * p/n_sep_by_space:
498 * 0 No <space> separates the currency symbol and value.
499 * 1 If the currency symbol and sign string are adjacent, a <space>
500 * separates them from the value; otherwise, a <space> separates
501 * the currency symbol from the value.
502 * 2 If the currency symbol and sign string are adjacent, a <space>
503 * separates them; otherwise, a <space> separates the sign string
504 * from the value.
505 *----------
506 */
507 switch (sign_posn)
508 {
509 case 0:
510 if (cs_precedes)
511 result = psprintf("(%s%s%s)",
512 csymbol,
513 (sep_by_space == 1) ? " " : "",
514 bufptr);
515 else
516 result = psprintf("(%s%s%s)",
517 bufptr,
518 (sep_by_space == 1) ? " " : "",
519 csymbol);
520 break;
521 case 1:
522 default:
523 if (cs_precedes)
524 result = psprintf("%s%s%s%s%s",
526 (sep_by_space == 2) ? " " : "",
527 csymbol,
528 (sep_by_space == 1) ? " " : "",
529 bufptr);
530 else
531 result = psprintf("%s%s%s%s%s",
533 (sep_by_space == 2) ? " " : "",
534 bufptr,
535 (sep_by_space == 1) ? " " : "",
536 csymbol);
537 break;
538 case 2:
539 if (cs_precedes)
540 result = psprintf("%s%s%s%s%s",
541 csymbol,
542 (sep_by_space == 1) ? " " : "",
543 bufptr,
544 (sep_by_space == 2) ? " " : "",
545 signsymbol);
546 else
547 result = psprintf("%s%s%s%s%s",
548 bufptr,
549 (sep_by_space == 1) ? " " : "",
550 csymbol,
551 (sep_by_space == 2) ? " " : "",
552 signsymbol);
553 break;
554 case 3:
555 if (cs_precedes)
556 result = psprintf("%s%s%s%s%s",
558 (sep_by_space == 2) ? " " : "",
559 csymbol,
560 (sep_by_space == 1) ? " " : "",
561 bufptr);
562 else
563 result = psprintf("%s%s%s%s%s",
564 bufptr,
565 (sep_by_space == 1) ? " " : "",
567 (sep_by_space == 2) ? " " : "",
568 csymbol);
569 break;
570 case 4:
571 if (cs_precedes)
572 result = psprintf("%s%s%s%s%s",
573 csymbol,
574 (sep_by_space == 2) ? " " : "",
576 (sep_by_space == 1) ? " " : "",
577 bufptr);
578 else
579 result = psprintf("%s%s%s%s%s",
580 bufptr,
581 (sep_by_space == 1) ? " " : "",
582 csymbol,
583 (sep_by_space == 2) ? " " : "",
584 signsymbol);
585 break;
586 }
587
589}
uint64_t uint64
Definition c.h:625
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
#define PG_RETURN_CSTRING(x)
Definition fmgr.h:364
static uint64 pg_abs_s64(int64 a)
Definition int.h:352
char * psprintf(const char *fmt,...)
Definition psprintf.c:43

References buf, fb(), memcpy(), pg_abs_s64(), PG_GETARG_CASH, PG_RETURN_CSTRING, PGLC_localeconv(), psprintf(), result, and value.

◆ cash_pl()

Datum cash_pl ( PG_FUNCTION_ARGS  )

Definition at line 694 of file cash.c.

695{
698
700}
static Cash cash_pl_cash(Cash c1, Cash c2)
Definition cash.c:92

References cash_pl_cash(), fb(), PG_GETARG_CASH, and PG_RETURN_CASH.

◆ cash_pl_cash()

static Cash cash_pl_cash ( Cash  c1,
Cash  c2 
)
inlinestatic

Definition at line 92 of file cash.c.

93{
94 Cash res;
95
96 if (unlikely(pg_add_s64_overflow(c1, c2, &res)))
99 errmsg("money out of range")));
100
101 return res;
102}
static bool pg_add_s64_overflow(int64 a, int64 b, int64 *result)
Definition int.h:235

References ereport, errcode(), errmsg, ERROR, fb(), pg_add_s64_overflow(), and unlikely.

Referenced by cash_pl().

◆ cash_recv()

Datum cash_recv ( PG_FUNCTION_ARGS  )

Definition at line 595 of file cash.c.

596{
598
600}
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
int64 pq_getmsgint64(StringInfo msg)
Definition pqformat.c:452
struct StringInfoData * StringInfo
Definition string.h:15

References buf, PG_GETARG_POINTER, PG_RETURN_CASH, and pq_getmsgint64().

◆ cash_send()

Datum cash_send ( PG_FUNCTION_ARGS  )

Definition at line 606 of file cash.c.

607{
610
614}
#define PG_RETURN_BYTEA_P(x)
Definition fmgr.h:373
void pq_begintypsend(StringInfo buf)
Definition pqformat.c:325
bytea * pq_endtypsend(StringInfo buf)
Definition pqformat.c:345
static void pq_sendint64(StringInfo buf, uint64 i)
Definition pqformat.h:152

References buf, fb(), PG_GETARG_CASH, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), and pq_sendint64().

◆ cash_words()

Datum cash_words ( PG_FUNCTION_ARGS  )

Definition at line 985 of file cash.c.

986{
988 uint64 val;
990 text *res;
992 Cash m0;
993 Cash m1;
994 Cash m2;
995 Cash m3;
996 Cash m4;
997 Cash m5;
998 Cash m6;
999
1001
1002 /* work with positive numbers */
1003 if (value < 0)
1004 {
1005 value = -value;
1006 appendStringInfoString(&buf, "minus ");
1007 }
1008
1009 /* Now treat as unsigned, to avoid trouble at INT_MIN */
1010 val = (uint64) value;
1011
1012 dollars = val / INT64CONST(100);
1013 m0 = val % INT64CONST(100); /* cents */
1014 m1 = (val / INT64CONST(100)) % 1000; /* hundreds */
1015 m2 = (val / INT64CONST(100000)) % 1000; /* thousands */
1016 m3 = (val / INT64CONST(100000000)) % 1000; /* millions */
1017 m4 = (val / INT64CONST(100000000000)) % 1000; /* billions */
1018 m5 = (val / INT64CONST(100000000000000)) % 1000; /* trillions */
1019 m6 = (val / INT64CONST(100000000000000000)) % 1000; /* quadrillions */
1020
1021 if (m6)
1022 {
1024 appendStringInfoString(&buf, " quadrillion ");
1025 }
1026
1027 if (m5)
1028 {
1030 appendStringInfoString(&buf, " trillion ");
1031 }
1032
1033 if (m4)
1034 {
1036 appendStringInfoString(&buf, " billion ");
1037 }
1038
1039 if (m3)
1040 {
1042 appendStringInfoString(&buf, " million ");
1043 }
1044
1045 if (m2)
1046 {
1048 appendStringInfoString(&buf, " thousand ");
1049 }
1050
1051 if (m1)
1053
1054 if (dollars == 0)
1055 appendStringInfoString(&buf, "zero");
1056
1057 appendStringInfoString(&buf, dollars == 1 ? " dollar and " : " dollars and ");
1059 appendStringInfoString(&buf, m0 == 1 ? " cent" : " cents");
1060
1061 /* capitalize output */
1062 buf.data[0] = pg_ascii_toupper((unsigned char) buf.data[0]);
1063
1064 /* return as text datum */
1065 res = cstring_to_text_with_len(buf.data, buf.len);
1066 pfree(buf.data);
1067 PG_RETURN_TEXT_P(res);
1068}
#define INT64CONST(x)
Definition c.h:630
static void append_num_word(StringInfo buf, Cash value)
Definition cash.c:40
#define PG_RETURN_TEXT_P(x)
Definition fmgr.h:374
long val
Definition informix.c:689
void pfree(void *pointer)
Definition mcxt.c:1619
static unsigned char pg_ascii_toupper(unsigned char ch)
Definition port.h:178
void initStringInfo(StringInfo str)
Definition stringinfo.c:97
Definition c.h:776
text * cstring_to_text_with_len(const char *s, int len)
Definition varlena.c:196

References append_num_word(), appendStringInfoString(), buf, cstring_to_text_with_len(), fb(), initStringInfo(), INT64CONST, pfree(), pg_ascii_toupper(), PG_GETARG_CASH, PG_RETURN_TEXT_P, val, and value.

◆ cashlarger()

Datum cashlarger ( PG_FUNCTION_ARGS  )

Definition at line 952 of file cash.c.

953{
956 Cash result;
957
958 result = (c1 > c2) ? c1 : c2;
959
961}

References fb(), PG_GETARG_CASH, PG_RETURN_CASH, and result.

◆ cashsmaller()

Datum cashsmaller ( PG_FUNCTION_ARGS  )

Definition at line 968 of file cash.c.

969{
972 Cash result;
973
974 result = (c1 < c2) ? c1 : c2;
975
977}

References fb(), PG_GETARG_CASH, PG_RETURN_CASH, and result.

◆ flt4_mul_cash()

Datum flt4_mul_cash ( PG_FUNCTION_ARGS  )

Definition at line 799 of file cash.c.

800{
802 Cash c = PG_GETARG_CASH(1);
803
805}

References cash_mul_float8(), PG_GETARG_CASH, PG_GETARG_FLOAT4, and PG_RETURN_CASH.

◆ flt8_mul_cash()

Datum flt8_mul_cash ( PG_FUNCTION_ARGS  )

Definition at line 757 of file cash.c.

758{
760 Cash c = PG_GETARG_CASH(1);
761
763}

References cash_mul_float8(), PG_GETARG_CASH, PG_GETARG_FLOAT8, and PG_RETURN_CASH.

◆ int2_mul_cash()

Datum int2_mul_cash ( PG_FUNCTION_ARGS  )

Definition at line 925 of file cash.c.

926{
927 int16 s = PG_GETARG_INT16(0);
928 Cash c = PG_GETARG_CASH(1);
929
931}

References cash_mul_int64(), PG_GETARG_CASH, PG_GETARG_INT16, and PG_RETURN_CASH.

◆ int4_cash()

Datum int4_cash ( PG_FUNCTION_ARGS  )

Definition at line 1173 of file cash.c.

1174{
1176 Cash result;
1177 int fpoint;
1178 int64 scale;
1179 int i;
1180 struct lconv *lconvert = PGLC_localeconv();
1181
1182 /* see comments about frac_digits in cash_in() */
1183 fpoint = lconvert->frac_digits;
1185 fpoint = 2;
1186
1187 /* compute required scale factor */
1188 scale = 1;
1189 for (i = 0; i < fpoint; i++)
1190 scale *= 10;
1191
1192 /* compute amount * scale, checking for overflow */
1194 ereturn(fcinfo->context, (Datum) 0,
1196 errmsg("bigint out of range"));
1197
1199}

References ereturn, errcode(), errmsg, fb(), i, PG_GETARG_INT32, pg_mul_s64_overflow(), PG_RETURN_CASH, PGLC_localeconv(), result, scale, and unlikely.

◆ int4_mul_cash()

Datum int4_mul_cash ( PG_FUNCTION_ARGS  )

Definition at line 883 of file cash.c.

884{
886 Cash c = PG_GETARG_CASH(1);
887
889}

References cash_mul_int64(), i, PG_GETARG_CASH, PG_GETARG_INT32, and PG_RETURN_CASH.

◆ int8_cash()

Datum int8_cash ( PG_FUNCTION_ARGS  )

Definition at line 1206 of file cash.c.

1207{
1209 Cash result;
1210 int fpoint;
1211 int64 scale;
1212 int i;
1213 struct lconv *lconvert = PGLC_localeconv();
1214
1215 /* see comments about frac_digits in cash_in() */
1216 fpoint = lconvert->frac_digits;
1218 fpoint = 2;
1219
1220 /* compute required scale factor */
1221 scale = 1;
1222 for (i = 0; i < fpoint; i++)
1223 scale *= 10;
1224
1225 /* compute amount * scale, checking for overflow */
1227 ereturn(fcinfo->context, (Datum) 0,
1229 errmsg("bigint out of range"));
1230
1232}

References ereturn, errcode(), errmsg, fb(), i, PG_GETARG_INT64, pg_mul_s64_overflow(), PG_RETURN_CASH, PGLC_localeconv(), result, scale, and unlikely.

◆ int8_mul_cash()

Datum int8_mul_cash ( PG_FUNCTION_ARGS  )

Definition at line 842 of file cash.c.

843{
845 Cash c = PG_GETARG_CASH(1);
846
848}

References cash_mul_int64(), i, PG_GETARG_CASH, PG_GETARG_INT64, and PG_RETURN_CASH.

◆ numeric_cash()

Datum numeric_cash ( PG_FUNCTION_ARGS  )

Definition at line 1133 of file cash.c.

1134{
1136 Cash result;
1137 int fpoint;
1138 int64 scale;
1139 int i;
1141 struct lconv *lconvert = PGLC_localeconv();
1142
1143 /* see comments about frac_digits in cash_in() */
1144 fpoint = lconvert->frac_digits;
1146 fpoint = 2;
1147
1148 /* compute required scale factor */
1149 scale = 1;
1150 for (i = 0; i < fpoint; i++)
1151 scale *= 10;
1152
1153 /* multiply the input amount by scale factor */
1155
1156 amount = numeric_mul_safe(amount, numeric_scale, fcinfo->context);
1157 if (unlikely(SOFT_ERROR_OCCURRED(fcinfo->context)))
1159
1160 /* note that numeric_int8 will round to nearest integer for us */
1161 result = numeric_int8_safe(amount, fcinfo->context);
1162 if (unlikely(SOFT_ERROR_OCCURRED(fcinfo->context)))
1164
1166}
int64 numeric_int8_safe(Numeric num, Node *escontext)
Definition numeric.c:4445
Numeric numeric_mul_safe(Numeric num1, Numeric num2, Node *escontext)
Definition numeric.c:3038
#define PG_RETURN_NULL()
Definition fmgr.h:346
#define SOFT_ERROR_OCCURRED(escontext)
Definition miscnodes.h:53
#define PG_GETARG_NUMERIC(n)
Definition numeric.h:81

References fb(), i, int64_to_numeric(), numeric_int8_safe(), numeric_mul_safe(), numeric_scale(), PG_GETARG_NUMERIC, PG_RETURN_CASH, PG_RETURN_NULL, PGLC_localeconv(), result, scale, SOFT_ERROR_OCCURRED, and unlikely.