📄 cash.c
字号:
{ Cash c1 = PG_GETARG_CASH(0); Cash c2 = PG_GETARG_CASH(1); PG_RETURN_BOOL(c1 > c2);}Datumcash_ge(PG_FUNCTION_ARGS){ Cash c1 = PG_GETARG_CASH(0); Cash c2 = PG_GETARG_CASH(1); PG_RETURN_BOOL(c1 >= c2);}Datumcash_cmp(PG_FUNCTION_ARGS){ Cash c1 = PG_GETARG_CASH(0); Cash c2 = PG_GETARG_CASH(1); if (c1 > c2) PG_RETURN_INT32(1); else if (c1 == c2) PG_RETURN_INT32(0); else PG_RETURN_INT32(-1);}/* cash_pl() * Add two cash values. */Datumcash_pl(PG_FUNCTION_ARGS){ Cash c1 = PG_GETARG_CASH(0); Cash c2 = PG_GETARG_CASH(1); Cash result; result = c1 + c2; PG_RETURN_CASH(result);}/* cash_mi() * Subtract two cash values. */Datumcash_mi(PG_FUNCTION_ARGS){ Cash c1 = PG_GETARG_CASH(0); Cash c2 = PG_GETARG_CASH(1); Cash result; result = c1 - c2; PG_RETURN_CASH(result);}/* cash_mul_flt8() * Multiply cash by float8. */Datumcash_mul_flt8(PG_FUNCTION_ARGS){ Cash c = PG_GETARG_CASH(0); float8 f = PG_GETARG_FLOAT8(1); Cash result; result = c * f; PG_RETURN_CASH(result);}/* flt8_mul_cash() * Multiply float8 by cash. */Datumflt8_mul_cash(PG_FUNCTION_ARGS){ float8 f = PG_GETARG_FLOAT8(0); Cash c = PG_GETARG_CASH(1); Cash result; result = f * c; PG_RETURN_CASH(result);}/* cash_div_flt8() * Divide cash by float8. */Datumcash_div_flt8(PG_FUNCTION_ARGS){ Cash c = PG_GETARG_CASH(0); float8 f = PG_GETARG_FLOAT8(1); Cash result; if (f == 0.0) ereport(ERROR, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero"))); result = rint(c / f); PG_RETURN_CASH(result);}/* cash_mul_flt4() * Multiply cash by float4. */Datumcash_mul_flt4(PG_FUNCTION_ARGS){ Cash c = PG_GETARG_CASH(0); float4 f = PG_GETARG_FLOAT4(1); Cash result; result = c * f; PG_RETURN_CASH(result);}/* flt4_mul_cash() * Multiply float4 by cash. */Datumflt4_mul_cash(PG_FUNCTION_ARGS){ float4 f = PG_GETARG_FLOAT4(0); Cash c = PG_GETARG_CASH(1); Cash result; result = f * c; PG_RETURN_CASH(result);}/* cash_div_flt4() * Divide cash by float4. * */Datumcash_div_flt4(PG_FUNCTION_ARGS){ Cash c = PG_GETARG_CASH(0); float4 f = PG_GETARG_FLOAT4(1); Cash result; if (f == 0.0) ereport(ERROR, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero"))); result = rint(c / f); PG_RETURN_CASH(result);}/* cash_mul_int8() * Multiply cash by int8. */Datumcash_mul_int8(PG_FUNCTION_ARGS){ Cash c = PG_GETARG_CASH(0); int64 i = PG_GETARG_INT64(1); Cash result; result = c * i; PG_RETURN_CASH(result);}/* int8_mul_cash() * Multiply int8 by cash. */Datumint8_mul_cash(PG_FUNCTION_ARGS){ int64 i = PG_GETARG_INT64(0); Cash c = PG_GETARG_CASH(1); Cash result; result = i * c; PG_RETURN_CASH(result);}/* cash_div_int8() * Divide cash by 8-byte integer. */Datumcash_div_int8(PG_FUNCTION_ARGS){ Cash c = PG_GETARG_CASH(0); int64 i = PG_GETARG_INT64(1); Cash result; if (i == 0) ereport(ERROR, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero"))); result = rint(c / i); PG_RETURN_CASH(result);}/* cash_mul_int4() * Multiply cash by int4. */Datumcash_mul_int4(PG_FUNCTION_ARGS){ Cash c = PG_GETARG_CASH(0); int32 i = PG_GETARG_INT32(1); Cash result; result = c * i; PG_RETURN_CASH(result);}/* int4_mul_cash() * Multiply int4 by cash. */Datumint4_mul_cash(PG_FUNCTION_ARGS){ int32 i = PG_GETARG_INT32(0); Cash c = PG_GETARG_CASH(1); Cash result; result = i * c; PG_RETURN_CASH(result);}/* cash_div_int4() * Divide cash by 4-byte integer. * */Datumcash_div_int4(PG_FUNCTION_ARGS){ Cash c = PG_GETARG_CASH(0); int32 i = PG_GETARG_INT32(1); Cash result; if (i == 0) ereport(ERROR, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero"))); result = rint(c / i); PG_RETURN_CASH(result);}/* cash_mul_int2() * Multiply cash by int2. */Datumcash_mul_int2(PG_FUNCTION_ARGS){ Cash c = PG_GETARG_CASH(0); int16 s = PG_GETARG_INT16(1); Cash result; result = c * s; PG_RETURN_CASH(result);}/* int2_mul_cash() * Multiply int2 by cash. */Datumint2_mul_cash(PG_FUNCTION_ARGS){ int16 s = PG_GETARG_INT16(0); Cash c = PG_GETARG_CASH(1); Cash result; result = s * c; PG_RETURN_CASH(result);}/* cash_div_int2() * Divide cash by int2. * */Datumcash_div_int2(PG_FUNCTION_ARGS){ Cash c = PG_GETARG_CASH(0); int16 s = PG_GETARG_INT16(1); Cash result; if (s == 0) ereport(ERROR, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero"))); result = rint(c / s); PG_RETURN_CASH(result);}/* cashlarger() * Return larger of two cash values. */Datumcashlarger(PG_FUNCTION_ARGS){ Cash c1 = PG_GETARG_CASH(0); Cash c2 = PG_GETARG_CASH(1); Cash result; result = (c1 > c2) ? c1 : c2; PG_RETURN_CASH(result);}/* cashsmaller() * Return smaller of two cash values. */Datumcashsmaller(PG_FUNCTION_ARGS){ Cash c1 = PG_GETARG_CASH(0); Cash c2 = PG_GETARG_CASH(1); Cash result; result = (c1 < c2) ? c1 : c2; PG_RETURN_CASH(result);}/* cash_words() * This converts a int4 as well but to a representation using words * Obviously way North American centric - sorry */Datumcash_words(PG_FUNCTION_ARGS){ Cash value = PG_GETARG_CASH(0); uint64 val; char buf[256]; char *p = buf; Cash m0; Cash m1; Cash m2; Cash m3; Cash m4; Cash m5; Cash m6; text *result; /* work with positive numbers */ if (value < 0) { value = -value; strcpy(buf, "minus "); p += 6; } else buf[0] = '\0'; /* Now treat as unsigned, to avoid trouble at INT_MIN */ val = (uint64) value; m0 = val % INT64CONST(100); /* cents */ m1 = (val / INT64CONST(100)) % 1000; /* hundreds */ m2 = (val / INT64CONST(100000)) % 1000; /* thousands */ m3 = (val / INT64CONST(100000000)) % 1000; /* millions */ m4 = (val / INT64CONST(100000000000)) % 1000; /* billions */ m5 = (val / INT64CONST(100000000000000)) % 1000; /* trillions */ m6 = (val / INT64CONST(100000000000000000)) % 1000; /* quadrillions */ if (m6) { strcat(buf, num_word(m6)); strcat(buf, " quadrillion "); } if (m5) { strcat(buf, num_word(m5)); strcat(buf, " trillion "); } if (m4) { strcat(buf, num_word(m4)); strcat(buf, " billion "); } if (m3) { strcat(buf, num_word(m3)); strcat(buf, " million "); } if (m2) { strcat(buf, num_word(m2)); strcat(buf, " thousand "); } if (m1) strcat(buf, num_word(m1)); if (!*p) strcat(buf, "zero"); strcat(buf, (val / 100) == 1 ? " dollar and " : " dollars and "); strcat(buf, num_word(m0)); strcat(buf, m0 == 1 ? " cent" : " cents"); /* capitalize output */ buf[0] = pg_toupper((unsigned char) buf[0]); /* make a text type for output */ result = (text *) palloc(strlen(buf) + VARHDRSZ); SET_VARSIZE(result, strlen(buf) + VARHDRSZ); memcpy(VARDATA(result), buf, strlen(buf)); PG_RETURN_TEXT_P(result);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -