diff options
Diffstat (limited to 'sql')
| -rw-r--r-- | sql/serenitas.c | 26 |
1 files changed, 9 insertions, 17 deletions
diff --git a/sql/serenitas.c b/sql/serenitas.c index a6969eb0..fab093b0 100644 --- a/sql/serenitas.c +++ b/sql/serenitas.c @@ -98,9 +98,7 @@ Datum cds_accrued(PG_FUNCTION_ARGS) { static inline int get_TCurve(const char* buf, uint32_t length, TCurve* curve) { if (LZ4_decompress_safe(buf, (char*)curve, length, 512) < 0) { - return -1; - } else { - return 0; + elog(ERROR, "error during decompression"); } } @@ -158,6 +156,9 @@ double calc(TDate today, TDate start_date, TDate end_date, double recovery, doub PG_FUNCTION_INFO_V1(upfront_from_level); Datum upfront_from_level(PG_FUNCTION_ARGS) { + if (SPI_connect() == SPI_ERROR_CONNECT) { + elog(ERROR, "something wrong happened"); + } const text* redindexcode = PG_GETARG_TEXT_PP(0); DateADT maturity = PG_GETARG_DATEADT(1); float8 traded_level = PG_GETARG_FLOAT8(2); @@ -174,9 +175,7 @@ Datum upfront_from_level(PG_FUNCTION_ARGS) { int ret; values[0] = PointerGetDatum(redindexcode); values[1] = DateADTGetDatum(maturity); - if (SPI_connect() == SPI_ERROR_CONNECT) { - elog(ERROR, "something wrong happened"); - } + ret = SPI_execute_with_args(sql_query, nargs, argtypes, values, nulls, true, 1); proc = SPI_processed; int coupon; @@ -195,7 +194,6 @@ Datum upfront_from_level(PG_FUNCTION_ARGS) { cumulativeloss = DatumGetFloat8(SPI_getbinval(tuple, tupdesc, 5, &isnull)); elog(INFO, "%s %d %d %f %f", index, coupon, issue_date, factor, cumulativeloss); } else { - pfree(currency); elog(ERROR, "something wrong happened"); } SPI_freetuptable(SPI_tuptable); @@ -217,28 +215,22 @@ Datum upfront_from_level(PG_FUNCTION_ARGS) { HeapTuple tuple = tuptable->vals[0]; tmp = SPI_getbinval(tuple, tupdesc, 1, &isnull); if (isnull) { - pfree(currency); elog(ERROR, "no curve for that date"); } else { buf = DatumGetByteaPP(tmp); uint32 data_length = VARSIZE_ANY_EXHDR(buf); const char *raw_data = VARDATA_ANY(buf); - curve = (TCurve*)malloc(512); - if (get_TCurve(raw_data, data_length, curve) == -1 ) { - free(curve); - pfree(currency); - elog(ERROR, "error during decompression"); - } + curve = (TCurve*)palloc(512); + get_TCurve(raw_data, data_length, curve) } } else { - pfree(currency); elog(ERROR, "no curve for that date"); } + pfree(currency); + pfree(curve); SPI_finish(); double recovery = 0.4; double upfront; upfront = calc(TDate_from_DateADT(trade_date), issue_date, TDate_from_DateADT(maturity), recovery, coupon / 10000.0, cal_from_currency(currency), curve, traded_level, true); - pfree(currency); - free(curve); PG_RETURN_FLOAT8(upfront); } |
