67 elog(
DEBUG4,
"check_primary_key: Enter Function");
77 elog(
ERROR,
"check_primary_key: not fired by trigger manager");
82 elog(
ERROR,
"check_primary_key: must be fired for row");
91 elog(
ERROR,
"check_primary_key: cannot process DELETE events");
99 args = trigger->tgargs;
103 elog(
ERROR,
"check_primary_key: odd number of arguments should be specified");
127 if (
plan->nplans <= 0)
131 for (
i = 0;
i < nkeys;
i++)
139 (
errcode(ERRCODE_UNDEFINED_COLUMN),
140 errmsg(
"there is no attribute \"%s\" in relation \"%s\"",
157 if (
plan->nplans <= 0)
164 if (
plan->nplans <= 0)
174 for (
i = 0;
i < nkeys;
i++)
176 snprintf(sql + strlen(sql),
sizeof(sql) - strlen(sql),
"%s = $%d %s",
177 args[
i + nkeys + 1],
i + 1, (
i < nkeys - 1) ?
"and " :
"");
192 elog(
ERROR,
"check_primary_key: SPI_keepplan failed");
195 *(
plan->splan) = pplan;
207 elog(
ERROR,
"check_primary_key: SPI_execp returned %d", ret);
214 (
errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
215 errmsg(
"tuple references non-existent key"),
216 errdetail(
"Trigger \"%s\" found tuple referencing non-existent key in \"%s\".", trigger->tgname,
relname)));
255 Oid *argtypes = NULL;
265 elog(
DEBUG4,
"check_foreign_key: Enter Function");
275 elog(
ERROR,
"check_foreign_key: not fired by trigger manager");
280 elog(
ERROR,
"check_foreign_key: must be fired for row");
285 elog(
ERROR,
"check_foreign_key: cannot process INSERT events");
302 args = trigger->tgargs;
307 elog(
ERROR,
"check_foreign_key: too short %d (< 5) list of arguments", nargs);
312 elog(
ERROR,
"check_foreign_key: %d (< 1) number of references specified", nrefs);
316 elog(
ERROR,
"check_foreign_key: invalid action %s",
args[1]);
319 nkeys = (nargs - nrefs) / (nrefs + 1);
320 if (nkeys <= 0 || nargs != (nrefs + nkeys * (nrefs + 1)))
322 elog(
ERROR,
"check_foreign_key: invalid number of arguments %d for %d references",
345 if (
plan->nplans <= 0)
351 else if (
plan->nplans != nrefs)
353 elog(
ERROR,
"%s: check_foreign_key: # of plans changed in meantime",
357 for (
i = 0;
i < nkeys;
i++)
365 (
errcode(ERRCODE_UNDEFINED_COLUMN),
366 errmsg(
"there is no attribute \"%s\" in relation \"%s\"",
388 if (newtuple != NULL)
390 char *oldval =
SPI_getvalue(trigtuple, tupdesc, fnumber);
402 if (
plan->nplans <= 0)
412 if (
plan->nplans <= 0)
421 for (r = 0; r < nrefs; r++)
465 for (k = 1; k <= nkeys; k++)
467 int is_char_type = 0;
475 if (strcmp(
type,
"text") == 0 ||
476 strcmp(
type,
"varchar") == 0 ||
477 strcmp(
type,
"char") == 0 ||
478 strcmp(
type,
"bpchar") == 0 ||
479 strcmp(
type,
"date") == 0 ||
480 strcmp(
type,
"timestamp") == 0)
483 elog(
DEBUG4,
"check_foreign_key Debug value %s type %s %d",
484 nv,
type, is_char_type);
490 snprintf(sql + strlen(sql),
sizeof(sql) - strlen(sql),
492 args2[k], (is_char_type > 0) ?
"'" :
"",
493 nv, (is_char_type > 0) ?
"'" :
"", (k < nkeys) ?
", " :
"");
495 strcat(sql,
" where ");
511 for (
i = 1;
i <= nkeys;
i++)
513 snprintf(sql + strlen(sql),
sizeof(sql) - strlen(sql),
515 args2[
i], (
i < nkeys) ?
", " :
"");
517 strcat(sql,
" where ");
521 for (
i = 1;
i <= nkeys;
i++)
523 snprintf(sql + strlen(sql),
sizeof(sql) - strlen(sql),
"%s = $%d %s",
524 args2[
i],
i, (
i < nkeys) ?
"and " :
"");
539 elog(
ERROR,
"check_foreign_key: SPI_keepplan failed");
541 plan->splan[r] = pplan;
545 plan->nplans = nrefs;
547 elog(
DEBUG4,
"check_foreign_key Debug Query is : %s ", sql);
554 if (newtuple != NULL && isequal)
563 for (r = 0; r < nrefs; r++)
569 int tcount = (
action ==
'r') ? 1 : 0;
580 (
errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
581 errmsg(
"SPI_execp returned %d", ret)));
589 (
errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
590 errmsg(
"\"%s\": tuple is referenced in \"%s\"",
595 #ifdef REFINT_VERBOSE
598 (
action ==
'c') ?
"deleted" :
"set to null");
624 for (
i = 0;
i < *nplans;
i++)
#define Assert(condition)
int errdetail(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
if(TABLE==NULL||TABLE_index==NULL)
char * pstrdup(const char *in)
MemoryContext TopMemoryContext
void * repalloc(void *pointer, Size size)
void * MemoryContextAlloc(MemoryContext context, Size size)
int32 pg_strtoint32(const char *s)
static Datum PointerGetDatum(const void *X)
MemoryContextSwitchTo(old_ctx)
Datum check_foreign_key(PG_FUNCTION_ARGS)
Datum check_primary_key(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(check_primary_key)
static EPlan * find_plan(char *ident, EPlan **eplan, int *nplans)
char * SPI_getrelname(Relation rel)
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
const char * SPI_result_code_string(int code)
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
int SPI_keepplan(SPIPlanPtr plan)
int SPI_execp(SPIPlanPtr plan, Datum *Values, const char *Nulls, long tcount)
char * SPI_gettype(TupleDesc tupdesc, int fnumber)
char * SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
static void * fn(void *arg)
#define TRIGGER_FIRED_BY_DELETE(event)
#define CALLED_AS_TRIGGER(fcinfo)
#define TRIGGER_FIRED_FOR_ROW(event)
#define TRIGGER_FIRED_BY_INSERT(event)
#define TRIGGER_FIRED_BY_UPDATE(event)