aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sql/serenitas.c28
1 files changed, 16 insertions, 12 deletions
diff --git a/sql/serenitas.c b/sql/serenitas.c
index 0cb2d417..034af647 100644
--- a/sql/serenitas.c
+++ b/sql/serenitas.c
@@ -298,12 +298,13 @@ update_attach(PG_FUNCTION_ARGS)
/* int attname = SPI_fnumer(tupdesc, "traded_level"); */
/* Oid traded_level = DatumGetObjectId(SPI_getbinval(rettuple, tupdesc, SPI_fnumber(tupdesc, "traded_level"), &isnull)); */
int16 orig_attach, orig_detach;
- orig_attach = DatumGetInt16(SPI_getbinval(rettuple, tupdesc, SPI_fnumber(tupdesc, "orig_attach"), &isnull));
+ bool attach_is_null, detach_is_null;
+ orig_attach = DatumGetInt16(SPI_getbinval(rettuple, tupdesc, SPI_fnumber(tupdesc, "orig_attach"), &attach_is_null));
+ orig_detach = DatumGetInt16(SPI_getbinval(rettuple, tupdesc, SPI_fnumber(tupdesc, "orig_detach"), &detach_is_null));
- if (isnull)
+ if (attach_is_null && detach_is_null)
return PointerGetDatum(rettuple);
- else
- orig_detach = DatumGetInt16(SPI_getbinval(rettuple, tupdesc, SPI_fnumber(tupdesc, "orig_detach"), &isnull));
+
char* sql_query = "SELECT indexfactor, cumulativeloss "
"FROM index_factors "
"WHERE redindexcode=$1";
@@ -313,13 +314,18 @@ update_attach(PG_FUNCTION_ARGS)
Datum values[1];
int ret;
values[0] = SPI_getbinval(rettuple, tupdesc, SPI_fnumber(tupdesc, "security_id"), &isnull);
-
+ if (isnull) {
+ elog(ERROR, "no security id");
+ }
if (SPI_connect() == SPI_ERROR_CONNECT) {
elog(ERROR, "something wrong happened");
}
ret = SPI_execute_with_args(sql_query, nargs, argtypes, values, nulls, true, 1);
double factor, cumloss;
- if (ret == SPI_OK_SELECT && SPI_tuptable != NULL && SPI_processed == 1) {
+ if (SPI_processed != 1) {
+ return PointerGetDatum(rettuple);
+ }
+ if (ret == SPI_OK_SELECT && SPI_tuptable != NULL) {
SPITupleTable *tuptable = SPI_tuptable;
TupleDesc tupdesc = tuptable->tupdesc;
bool isnull;
@@ -328,15 +334,13 @@ update_attach(PG_FUNCTION_ARGS)
cumloss = DatumGetFloat8(SPI_getbinval(tuple, tupdesc, 2, &isnull));
elog(INFO, "%f %f", factor, cumloss);
} else {
- return PointerGetDatum(rettuple);
+ elog(ERROR, "SPI query didn't work");
}
SPI_finish();
double detach, attach;
- detach = factor * Min(Max((orig_detach - cumloss) / factor, 0.0), 1.0);
- if (isnull)
- attach = 0;
- else
- attach = factor * Min(Max((orig_attach - cumloss) / factor, 0.0), 1.0);
+ detach = detach_is_null ? 1.0 : factor * Min(Max((orig_detach - cumloss) / factor, 0.0), 1.0);
+ attach = attach_is_null ? 0.0 : factor * Min(Max((orig_attach - cumloss) / factor, 0.0), 1.0);
+
int chnattrs = 2;
int chattrs[2];
Datum newvals[2];