1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| PG_FUNCTION_INFO_V1(cb_reload); Datum cb_reload(PG_FUNCTION_ARGS);
Datum cb_reload(PG_FUNCTION_ARGS) { TriggerData *trigdata = (TriggerData *) fcinfo->context; TupleDesc tupdesc; HeapTuple rettuple; HeapTuple newtuple; HeapTuple trigtuple; HeapTuple spi_tuple; SPITupleTable *spi_tuptable; TupleDesc spi_tupdesc; int ret; int ntup; int i, j; StringInfoData buf; char **tup = NULL; ruledesc *rules = NULL; bool isupdate, isinsert, isdelete;
if (!CALLED_AS_TRIGGER(fcinfo)) elog(ERROR, "trigf: not called by trigger manager");
if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event)) rettuple = trigdata->tg_newtuple; else rettuple = trigdata->tg_trigtuple;
tupdesc = trigdata->tg_relation->rd_att; newtuple = trigdata->tg_newtuple; trigtuple = trigdata->tg_trigtuple;
SPI_connect();
isupdate = TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event); isdelete = TRIGGER_FIRED_BY_DELETE(trigdata->tg_event); isinsert = TRIGGER_FIRED_BY_INSERT(trigdata->tg_event); initStringInfo(&buf); if (isupdate) { appendStringInfo(&buf, "select * from check_balance.rules where id != %s;", SPI_getvalue(newtuple, tupdesc, 1)); }
if (isdelete) { appendStringInfo(&buf, "select * from check_balance.rules where id != %s;", SPI_getvalue(trigtuple, tupdesc, 1)); }
if (isinsert) { appendStringInfoString(&buf, "select * from check_balance.rules;"); }
ret = SPI_execute(buf.data, true, 0); pfree(buf.data);
if (ret != SPI_OK_SELECT) elog(FATAL, "SPI_execute failed: error code %d", ret);
ntup = SPI_processed;
tup = (char**)palloc0(sizeof(char*) * tupdesc->natts);
if (ntup != 0 && SPI_tuptable != NULL) { spi_tuptable = SPI_tuptable; spi_tupdesc = spi_tuptable->tupdesc; for (j = 0; j < ntup; j++) { rules = &rd[j]; spi_tuple = spi_tuptable->vals[j];
memset(&rd[j], 0, sizeof(ruledesc));
for (i = 1; i <= spi_tupdesc->natts; i++) { tup[i-1] = SPI_getvalue(spi_tuple, spi_tupdesc, i); }
memcpy(rules->username, pstrdup(tup[1]), strlen(pstrdup(tup[1]))); memcpy(rules->startime, pstrdup(tup[2]), strlen(pstrdup(tup[2]))); memcpy(rules->endtime, pstrdup(tup[3]), strlen(pstrdup(tup[3]))); memcpy(rules->datname, pstrdup(tup[4]), strlen(pstrdup(tup[4]))); memcpy(rules->relnsp, pstrdup(tup[5]), strlen(pstrdup(tup[5]))); memcpy(rules->relname, pstrdup(tup[6]), strlen(pstrdup(tup[6]))); memcpy(rules->cmdtype, pstrdup(tup[7]), strlen(pstrdup(tup[7])));
*tup = NULL;
elog(LOG, "w:%d,%s,%s,%s,%s,%s,%s,%s", j,rules->username,rules->startime,rules->endtime, rules->datname,rules->relnsp,rules->relname,rules->cmdtype); } }
if (isupdate || isinsert) { rules = &rd[ntup];
memset(&rd[ntup], 0, sizeof(ruledesc));
for (i = 1; i <= tupdesc->natts; i++) { if (isupdate) tup[i-1] = SPI_getvalue(newtuple, tupdesc, i); if (isinsert) tup[i-1] = SPI_getvalue(trigtuple, tupdesc, i); }
memcpy(rules->username, pstrdup(tup[1]), strlen(pstrdup(tup[1]))); memcpy(rules->startime, pstrdup(tup[2]), strlen(pstrdup(tup[2]))); memcpy(rules->endtime, pstrdup(tup[3]), strlen(pstrdup(tup[3]))); memcpy(rules->datname, pstrdup(tup[4]), strlen(pstrdup(tup[4]))); memcpy(rules->relnsp, pstrdup(tup[5]), strlen(pstrdup(tup[5]))); memcpy(rules->relname, pstrdup(tup[6]), strlen(pstrdup(tup[6]))); memcpy(rules->cmdtype, pstrdup(tup[7]), strlen(pstrdup(tup[7])));
*tup = NULL;
elog(LOG, "w:%d,%s,%s,%s,%s,%s,%s,%s", ntup,rules->username,rules->startime,rules->endtime, rules->datname,rules->relnsp,rules->relname,rules->cmdtype); }
SPI_finish();
return PointerGetDatum(rettuple); }
|