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 673 of file cash.c.

674{
677
678 if (c1 > c2)
680 else if (c1 == c2)
682 else
683 PG_RETURN_INT32(-1);
684}
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 717 of file cash.c.

718{
722
723 if (divisor == 0)
726 errmsg("division by zero")));
727
730}
double float8
Definition c.h:714
int errcode(int sqlerrcode)
Definition elog.c:874
#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 803 of file cash.c.

804{
805 Cash c = PG_GETARG_CASH(0);
807
809}
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 763 of file cash.c.

764{
765 Cash c = PG_GETARG_CASH(0);
767
769}
#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 919 of file cash.c.

920{
921 Cash c = PG_GETARG_CASH(0);
922 int16 s = PG_GETARG_INT16(1);
923
925}
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 881 of file cash.c.

882{
883 Cash c = PG_GETARG_CASH(0);
885
887}
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 841 of file cash.c.

842{
843 Cash c = PG_GETARG_CASH(0);
845
847}
#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 619 of file cash.c.

620{
623
624 PG_RETURN_BOOL(c1 == c2);
625}
#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 664 of file cash.c.

665{
668
669 PG_RETURN_BOOL(c1 >= c2);
670}

References fb(), PG_GETARG_CASH, and PG_RETURN_BOOL.

◆ cash_gt()

Datum cash_gt ( PG_FUNCTION_ARGS  )

Definition at line 655 of file cash.c.

656{
659
661}

References fb(), PG_GETARG_CASH, and PG_RETURN_BOOL.

◆ cash_in()

Datum cash_in ( PG_FUNCTION_ARGS  )

Definition at line 174 of file cash.c.

175{
176 char *str = PG_GETARG_CSTRING(0);
177 Node *escontext = fcinfo->context;
178 Cash result;
179 Cash value = 0;
180 Cash dec = 0;
181 Cash sgn = 1;
182 bool seen_dot = false;
183 const char *s = str;
184 int fpoint;
185 char dsymbol;
186 const char *ssymbol,
187 *psymbol,
188 *nsymbol,
189 *csymbol;
190 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 */
202 fpoint = lconvert->frac_digits;
204 fpoint = 2; /* best guess in this case, I think */
205
206 /* we restrict dsymbol to be a single byte, but not the other symbols */
207 if (*lconvert->mon_decimal_point != '\0' &&
208 lconvert->mon_decimal_point[1] == '\0')
209 dsymbol = *lconvert->mon_decimal_point;
210 else
211 dsymbol = '.';
212 if (*lconvert->mon_thousands_sep != '\0')
213 ssymbol = lconvert->mon_thousands_sep;
214 else /* ssymbol should not equal dsymbol */
215 ssymbol = (dsymbol != ',') ? "," : ".";
216 csymbol = (*lconvert->currency_symbol != '\0') ? lconvert->currency_symbol : "$";
217 psymbol = (*lconvert->positive_sign != '\0') ? lconvert->positive_sign : "+";
218 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",
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 */
227 while (isspace((unsigned char) *s))
228 s++;
229 if (strncmp(s, csymbol, strlen(csymbol)) == 0)
230 s += strlen(csymbol);
231 while (isspace((unsigned char) *s))
232 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 */
241 if (strncmp(s, nsymbol, strlen(nsymbol)) == 0)
242 {
243 sgn = -1;
244 s += strlen(nsymbol);
245 }
246 else if (*s == '(')
247 {
248 sgn = -1;
249 s++;
250 }
251 else if (strncmp(s, psymbol, strlen(psymbol)) == 0)
252 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 */
259 while (isspace((unsigned char) *s))
260 s++;
261 if (strncmp(s, csymbol, strlen(csymbol)) == 0)
262 s += strlen(csymbol);
263 while (isspace((unsigned char) *s))
264 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
279 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 */
285 if (isdigit((unsigned char) *s) && (!seen_dot || dec < fpoint))
286 {
287 int8 digit = *s - '0';
288
289 if (pg_mul_s64_overflow(value, 10, &value) ||
291 ereturn(escontext, (Datum) 0,
293 errmsg("value \"%s\" is out of range for type %s",
294 str, "money")));
295
296 if (seen_dot)
297 dec++;
298 }
299 /* decimal point? then start counting fractions... */
300 else if (*s == dsymbol && !seen_dot)
301 {
302 seen_dot = true;
303 }
304 /* ignore if "thousands" separator, else we're done */
305 else if (strncmp(s, ssymbol, strlen(ssymbol)) == 0)
306 s += strlen(ssymbol) - 1;
307 else
308 break;
309 }
310
311 /* round off if there's another digit */
312 if (isdigit((unsigned char) *s) && *s >= '5')
313 {
314 /* remember we build the value in the negative */
316 ereturn(escontext, (Datum) 0,
318 errmsg("value \"%s\" is out of range for type %s",
319 str, "money")));
320 }
321
322 /* adjust for less than required decimal places */
323 for (; dec < fpoint; dec++)
324 {
326 ereturn(escontext, (Datum) 0,
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 */
336 while (isdigit((unsigned char) *s))
337 s++;
338
339 while (*s)
340 {
341 if (isspace((unsigned char) *s) || *s == ')')
342 s++;
343 else if (strncmp(s, nsymbol, strlen(nsymbol)) == 0)
344 {
345 sgn = -1;
346 s += strlen(nsymbol);
347 }
348 else if (strncmp(s, psymbol, strlen(psymbol)) == 0)
349 s += strlen(psymbol);
350 else if (strncmp(s, csymbol, strlen(csymbol)) == 0)
351 s += strlen(csymbol);
352 else
353 ereturn(escontext, (Datum) 0,
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 */
363 if (sgn > 0)
364 {
365 if (value == PG_INT64_MIN)
366 ereturn(escontext, (Datum) 0,
368 errmsg("value \"%s\" is out of range for type %s",
369 str, "money")));
370 result = -value;
371 }
372 else
373 result = value;
374
375#ifdef CASHDEBUG
376 printf("cashin- result is " INT64_FORMAT "\n", result);
377#endif
378
380}
#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:266
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 646 of file cash.c.

647{
650
651 PG_RETURN_BOOL(c1 <= c2);
652}

References fb(), PG_GETARG_CASH, and PG_RETURN_BOOL.

◆ cash_lt()

Datum cash_lt ( PG_FUNCTION_ARGS  )

Definition at line 637 of file cash.c.

638{
641
643}

References fb(), PG_GETARG_CASH, and PG_RETURN_BOOL.

◆ cash_mi()

Datum cash_mi ( PG_FUNCTION_ARGS  )

Definition at line 704 of file cash.c.

705{
708
710}
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 776 of file cash.c.

777{
778 Cash c = PG_GETARG_CASH(0);
780
782}
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 737 of file cash.c.

738{
739 Cash c = PG_GETARG_CASH(0);
741
743}

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 894 of file cash.c.

895{
896 Cash c = PG_GETARG_CASH(0);
897 int16 s = PG_GETARG_INT16(1);
898
900}
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 854 of file cash.c.

855{
856 Cash c = PG_GETARG_CASH(0);
858
860}

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 816 of file cash.c.

817{
818 Cash c = PG_GETARG_CASH(0);
820
822}

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 628 of file cash.c.

629{
632
633 PG_RETURN_BOOL(c1 != c2);
634}

References fb(), PG_GETARG_CASH, and PG_RETURN_BOOL.

◆ cash_numeric()

Datum cash_numeric ( PG_FUNCTION_ARGS  )

Definition at line 1052 of file cash.c.

1053{
1055 Datum result;
1056 int fpoint;
1057 struct lconv *lconvert = PGLC_localeconv();
1058
1059 /* see comments about frac_digits in cash_in() */
1060 fpoint = lconvert->frac_digits;
1062 fpoint = 2;
1063
1064 /* convert the integral money value to numeric */
1066
1067 /* scale appropriately, if needed */
1068 if (fpoint > 0)
1069 {
1070 int64 scale;
1071 int i;
1074
1075 /* compute required scale factor */
1076 scale = 1;
1077 for (i = 0; i < fpoint; i++)
1078 scale *= 10;
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 */
1091
1092 /* Now we can safely divide ... */
1094
1095 /* ... and forcibly round to exactly the intended number of digits */
1097 quotient,
1099 }
1100
1102}
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:686
#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 388 of file cash.c.

389{
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,
405 struct lconv *lconvert = PGLC_localeconv();
406
407 /* see comments about frac_digits in cash_in() */
408 points = lconvert->frac_digits;
410 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 */
416 mon_group = *lconvert->mon_grouping;
418 mon_group = 3;
419
420 /* we restrict dsymbol to be a single byte, but not the other symbols */
421 if (*lconvert->mon_decimal_point != '\0' &&
422 lconvert->mon_decimal_point[1] == '\0')
423 dsymbol = *lconvert->mon_decimal_point;
424 else
425 dsymbol = '.';
426 if (*lconvert->mon_thousands_sep != '\0')
427 ssymbol = lconvert->mon_thousands_sep;
428 else /* ssymbol should not equal dsymbol */
429 ssymbol = (dsymbol != ',') ? "," : ".";
430 csymbol = (*lconvert->currency_symbol != '\0') ? lconvert->currency_symbol : "$";
431
432 if (value < 0)
433 {
434 /* set up formatting data */
435 signsymbol = (*lconvert->negative_sign != '\0') ? lconvert->negative_sign : "-";
436 sign_posn = lconvert->n_sign_posn;
437 cs_precedes = lconvert->n_cs_precedes;
438 sep_by_space = lconvert->n_sep_by_space;
439 }
440 else
441 {
442 signsymbol = lconvert->positive_sign;
443 sign_posn = lconvert->p_sign_posn;
444 cs_precedes = lconvert->p_cs_precedes;
445 sep_by_space = lconvert->p_sep_by_space;
446 }
447
448 /* make the amount positive for digit-reconstruction loop */
450
451 /* we build the digits+decimal-point+sep string right-to-left in buf[] */
452 bufptr = buf + sizeof(buf) - 1;
453 *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 */
462 do
463 {
464 if (points && digit_pos == 0)
465 {
466 /* insert decimal point, but not if value cannot be fractional */
467 *(--bufptr) = dsymbol;
468 }
469 else if (digit_pos < 0 && (digit_pos % mon_group) == 0)
470 {
471 /* insert thousands sep, but only to left of radix point */
472 bufptr -= strlen(ssymbol);
473 memcpy(bufptr, ssymbol, strlen(ssymbol));
474 }
475
476 *(--bufptr) = (uvalue % 10) + '0';
477 uvalue = uvalue / 10;
478 digit_pos--;
479 } 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 */
505 switch (sign_posn)
506 {
507 case 0:
508 if (cs_precedes)
509 result = psprintf("(%s%s%s)",
510 csymbol,
511 (sep_by_space == 1) ? " " : "",
512 bufptr);
513 else
514 result = psprintf("(%s%s%s)",
515 bufptr,
516 (sep_by_space == 1) ? " " : "",
517 csymbol);
518 break;
519 case 1:
520 default:
521 if (cs_precedes)
522 result = psprintf("%s%s%s%s%s",
524 (sep_by_space == 2) ? " " : "",
525 csymbol,
526 (sep_by_space == 1) ? " " : "",
527 bufptr);
528 else
529 result = psprintf("%s%s%s%s%s",
531 (sep_by_space == 2) ? " " : "",
532 bufptr,
533 (sep_by_space == 1) ? " " : "",
534 csymbol);
535 break;
536 case 2:
537 if (cs_precedes)
538 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 result = psprintf("%s%s%s%s%s",
546 bufptr,
547 (sep_by_space == 1) ? " " : "",
548 csymbol,
549 (sep_by_space == 2) ? " " : "",
550 signsymbol);
551 break;
552 case 3:
553 if (cs_precedes)
554 result = psprintf("%s%s%s%s%s",
556 (sep_by_space == 2) ? " " : "",
557 csymbol,
558 (sep_by_space == 1) ? " " : "",
559 bufptr);
560 else
561 result = psprintf("%s%s%s%s%s",
562 bufptr,
563 (sep_by_space == 1) ? " " : "",
565 (sep_by_space == 2) ? " " : "",
566 csymbol);
567 break;
568 case 4:
569 if (cs_precedes)
570 result = psprintf("%s%s%s%s%s",
571 csymbol,
572 (sep_by_space == 2) ? " " : "",
574 (sep_by_space == 1) ? " " : "",
575 bufptr);
576 else
577 result = psprintf("%s%s%s%s%s",
578 bufptr,
579 (sep_by_space == 1) ? " " : "",
580 csymbol,
581 (sep_by_space == 2) ? " " : "",
582 signsymbol);
583 break;
584 }
585
587}
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 691 of file cash.c.

692{
695
697}
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 593 of file cash.c.

594{
596
598}
#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 604 of file cash.c.

605{
608
612}
#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 962 of file cash.c.

963{
965 uint64 val;
967 text *res;
969 Cash m0;
970 Cash m1;
971 Cash m2;
972 Cash m3;
973 Cash m4;
974 Cash m5;
975 Cash m6;
976
978
979 /* work with positive numbers */
980 if (value < 0)
981 {
982 value = -value;
983 appendStringInfoString(&buf, "minus ");
984 }
985
986 /* Now treat as unsigned, to avoid trouble at INT_MIN */
987 val = (uint64) value;
988
989 dollars = val / INT64CONST(100);
990 m0 = val % INT64CONST(100); /* cents */
991 m1 = (val / INT64CONST(100)) % 1000; /* hundreds */
992 m2 = (val / INT64CONST(100000)) % 1000; /* thousands */
993 m3 = (val / INT64CONST(100000000)) % 1000; /* millions */
994 m4 = (val / INT64CONST(100000000000)) % 1000; /* billions */
995 m5 = (val / INT64CONST(100000000000000)) % 1000; /* trillions */
996 m6 = (val / INT64CONST(100000000000000000)) % 1000; /* quadrillions */
997
998 if (m6)
999 {
1001 appendStringInfoString(&buf, " quadrillion ");
1002 }
1003
1004 if (m5)
1005 {
1007 appendStringInfoString(&buf, " trillion ");
1008 }
1009
1010 if (m4)
1011 {
1013 appendStringInfoString(&buf, " billion ");
1014 }
1015
1016 if (m3)
1017 {
1019 appendStringInfoString(&buf, " million ");
1020 }
1021
1022 if (m2)
1023 {
1025 appendStringInfoString(&buf, " thousand ");
1026 }
1027
1028 if (m1)
1030
1031 if (dollars == 0)
1032 appendStringInfoString(&buf, "zero");
1033
1034 appendStringInfoString(&buf, dollars == 1 ? " dollar and " : " dollars and ");
1036 appendStringInfoString(&buf, m0 == 1 ? " cent" : " cents");
1037
1038 /* capitalize output */
1039 buf.data[0] = pg_ascii_toupper((unsigned char) buf.data[0]);
1040
1041 /* return as text datum */
1042 res = cstring_to_text_with_len(buf.data, buf.len);
1043 pfree(buf.data);
1044 PG_RETURN_TEXT_P(res);
1045}
#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:1616
static unsigned char pg_ascii_toupper(unsigned char ch)
Definition port.h:177
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 931 of file cash.c.

932{
935 Cash result;
936
937 result = (c1 > c2) ? c1 : c2;
938
940}

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

◆ cashsmaller()

Datum cashsmaller ( PG_FUNCTION_ARGS  )

Definition at line 946 of file cash.c.

947{
950 Cash result;
951
952 result = (c1 < c2) ? c1 : c2;
953
955}

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

◆ flt4_mul_cash()

Datum flt4_mul_cash ( PG_FUNCTION_ARGS  )

Definition at line 789 of file cash.c.

790{
792 Cash c = PG_GETARG_CASH(1);
793
795}

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 750 of file cash.c.

751{
753 Cash c = PG_GETARG_CASH(1);
754
756}

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 906 of file cash.c.

907{
908 int16 s = PG_GETARG_INT16(0);
909 Cash c = PG_GETARG_CASH(1);
910
912}

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 1147 of file cash.c.

1148{
1150 Cash result;
1151 int fpoint;
1152 int64 scale;
1153 int i;
1154 struct lconv *lconvert = PGLC_localeconv();
1155
1156 /* see comments about frac_digits in cash_in() */
1157 fpoint = lconvert->frac_digits;
1159 fpoint = 2;
1160
1161 /* compute required scale factor */
1162 scale = 1;
1163 for (i = 0; i < fpoint; i++)
1164 scale *= 10;
1165
1166 /* compute amount * scale, checking for overflow */
1168 ereturn(fcinfo->context, (Datum) 0,
1170 errmsg("bigint out of range"));
1171
1173}

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 867 of file cash.c.

868{
870 Cash c = PG_GETARG_CASH(1);
871
873}

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 1179 of file cash.c.

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

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 829 of file cash.c.

830{
832 Cash c = PG_GETARG_CASH(1);
833
835}

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 1108 of file cash.c.

1109{
1111 Cash result;
1112 int fpoint;
1113 int64 scale;
1114 int i;
1116 struct lconv *lconvert = PGLC_localeconv();
1117
1118 /* see comments about frac_digits in cash_in() */
1119 fpoint = lconvert->frac_digits;
1121 fpoint = 2;
1122
1123 /* compute required scale factor */
1124 scale = 1;
1125 for (i = 0; i < fpoint; i++)
1126 scale *= 10;
1127
1128 /* multiply the input amount by scale factor */
1130
1131 amount = numeric_mul_safe(amount, numeric_scale, fcinfo->context);
1132 if (unlikely(SOFT_ERROR_OCCURRED(fcinfo->context)))
1134
1135 /* note that numeric_int8 will round to nearest integer for us */
1136 result = numeric_int8_safe(amount, fcinfo->context);
1137 if (unlikely(SOFT_ERROR_OCCURRED(fcinfo->context)))
1139
1141}
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.