mirror of https://github.com/citusdata/citus.git
wip: trigger
parent
f622647037
commit
720008c918
|
@ -12,6 +12,7 @@
|
|||
#include "catalog/toasting.h"
|
||||
#include "commands/defrem.h"
|
||||
#include "commands/tablecmds.h"
|
||||
#include "commands/trigger.h"
|
||||
#include "commands/view.h"
|
||||
#include "distributed/citus_ruleutils.h"
|
||||
#include "distributed/pg_cimv.h"
|
||||
|
@ -106,6 +107,81 @@ static char* CIMVTriggerFuncName(int prefixId, const char* relname);
|
|||
static char* CIMVInternalPrefix(const RangeVar* baseTable, int prefixId);
|
||||
static void AlterTableOwner(RangeVar* tableName, char* ownerName);
|
||||
|
||||
|
||||
extern Datum trigf(PG_FUNCTION_ARGS);
|
||||
|
||||
PG_FUNCTION_INFO_V1(trigf);
|
||||
|
||||
Datum
|
||||
trigf(PG_FUNCTION_ARGS)
|
||||
{
|
||||
TriggerData *trigdata = (TriggerData *) fcinfo->context;
|
||||
Trigger* trigger = trigdata->tg_trigger;
|
||||
TupleDesc tupdesc;
|
||||
HeapTuple rettuple;
|
||||
char *when;
|
||||
bool checknull = false;
|
||||
bool isnull;
|
||||
int ret, i;
|
||||
|
||||
|
||||
/* make sure it's called as a trigger at all */
|
||||
if (!CALLED_AS_TRIGGER(fcinfo))
|
||||
elog(ERROR, "trigf: not called by trigger manager");
|
||||
|
||||
/* tuple to return to executor */
|
||||
if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
|
||||
rettuple = trigdata->tg_newtuple;
|
||||
else
|
||||
rettuple = trigdata->tg_trigtuple;
|
||||
|
||||
if (TRIGGER_FOR_INSERT(trigger->tgtype)) {
|
||||
|
||||
}
|
||||
|
||||
/* check for null values */
|
||||
if (!TRIGGER_FIRED_BY_DELETE(trigdata->tg_event)
|
||||
&& TRIGGER_FIRED_BEFORE(trigdata->tg_event))
|
||||
checknull = true;
|
||||
|
||||
if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))
|
||||
when = "before";
|
||||
else
|
||||
when = "after ";
|
||||
|
||||
tupdesc = trigdata->tg_relation->rd_att;
|
||||
|
||||
/* connect to SPI manager */
|
||||
if ((ret = SPI_connect()) < 0)
|
||||
elog(INFO, "trigf (fired %s): SPI_connect returned %d", when, ret);
|
||||
|
||||
/* get number of rows in table */
|
||||
ret = SPI_exec("SELECT count(*) FROM ttest", 0);
|
||||
|
||||
if (ret < 0)
|
||||
elog(NOTICE, "trigf (fired %s): SPI_exec returned %d", when, ret);
|
||||
|
||||
/* count(*) returns int8, so be careful to convert */
|
||||
i = DatumGetInt64(SPI_getbinval(SPI_tuptable->vals[0],
|
||||
SPI_tuptable->tupdesc,
|
||||
1,
|
||||
&isnull));
|
||||
|
||||
elog (INFO, "trigf (fired %s): there are %d rows in ttest", when, i);
|
||||
|
||||
SPI_finish();
|
||||
|
||||
if (checknull)
|
||||
{
|
||||
SPI_getbinval(rettuple, tupdesc, 1, &isnull);
|
||||
if (isnull)
|
||||
rettuple = NULL;
|
||||
}
|
||||
|
||||
return PointerGetDatum(rettuple);
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
ProcessCreateMaterializedViewStmt(const CreateTableAsStmt *stmt, const char *query_string,
|
||||
PlannedStmt *pstmt)
|
||||
|
|
Loading…
Reference in New Issue