1997-10-03 02:01:57 +08:00
|
|
|
|
|
|
|
#include "executor/spi.h" /* this is what you need to work with SPI */
|
|
|
|
#include "commands/trigger.h" /* -"- and triggers */
|
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
extern Datum autoinc(PG_FUNCTION_ARGS);
|
1998-02-26 12:46:47 +08:00
|
|
|
extern int4 nextval(struct varlena * seqin);
|
1997-10-03 02:01:57 +08:00
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
Datum
|
|
|
|
autoinc(PG_FUNCTION_ARGS)
|
1997-10-03 02:01:57 +08:00
|
|
|
{
|
2000-05-29 09:59:17 +08:00
|
|
|
TriggerData *trigdata = (TriggerData *) fcinfo->context;
|
1997-10-03 02:01:57 +08:00
|
|
|
Trigger *trigger; /* to get trigger name */
|
|
|
|
int nargs; /* # of arguments */
|
|
|
|
int *chattrs; /* attnums of attributes to change */
|
|
|
|
int chnattrs = 0; /* # of above */
|
|
|
|
Datum *newvals; /* vals of above */
|
|
|
|
char **args; /* arguments */
|
|
|
|
char *relname; /* triggered relation name */
|
|
|
|
Relation rel; /* triggered relation */
|
|
|
|
HeapTuple rettuple = NULL;
|
|
|
|
TupleDesc tupdesc; /* tuple description */
|
|
|
|
bool isnull;
|
|
|
|
int i;
|
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
if (!CALLED_AS_TRIGGER(fcinfo))
|
|
|
|
elog(ERROR, "autoinc: not fired by trigger manager");
|
|
|
|
if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
|
1998-01-07 02:53:02 +08:00
|
|
|
elog(ERROR, "autoinc: can't process STATEMENT events");
|
2000-05-29 09:59:17 +08:00
|
|
|
if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
|
1998-01-07 02:53:02 +08:00
|
|
|
elog(ERROR, "autoinc: must be fired before event");
|
1998-02-26 12:46:47 +08:00
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
|
|
|
|
rettuple = trigdata->tg_trigtuple;
|
|
|
|
else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
|
|
|
|
rettuple = trigdata->tg_newtuple;
|
1997-10-03 02:01:57 +08:00
|
|
|
else
|
1998-01-07 02:53:02 +08:00
|
|
|
elog(ERROR, "autoinc: can't process DELETE events");
|
1998-02-26 12:46:47 +08:00
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
rel = trigdata->tg_relation;
|
1997-10-03 02:01:57 +08:00
|
|
|
relname = SPI_getrelname(rel);
|
1998-02-26 12:46:47 +08:00
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
trigger = trigdata->tg_trigger;
|
1997-10-03 02:01:57 +08:00
|
|
|
|
|
|
|
nargs = trigger->tgnargs;
|
|
|
|
if (nargs <= 0 || nargs % 2 != 0)
|
1998-01-07 02:53:02 +08:00
|
|
|
elog(ERROR, "autoinc (%s): even number gt 0 of arguments was expected", relname);
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-10-03 02:01:57 +08:00
|
|
|
args = trigger->tgargs;
|
|
|
|
tupdesc = rel->rd_att;
|
1998-02-26 12:46:47 +08:00
|
|
|
|
|
|
|
chattrs = (int *) palloc(nargs / 2 * sizeof(int));
|
|
|
|
newvals = (Datum *) palloc(nargs / 2 * sizeof(Datum));
|
|
|
|
|
|
|
|
for (i = 0; i < nargs;)
|
1997-10-03 02:01:57 +08:00
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
struct varlena *seqname;
|
|
|
|
int attnum = SPI_fnumber(tupdesc, args[i]);
|
|
|
|
int32 val;
|
|
|
|
|
|
|
|
if (attnum < 0)
|
1998-01-07 02:53:02 +08:00
|
|
|
elog(ERROR, "autoinc (%s): there is no attribute %s", relname, args[i]);
|
1998-02-26 12:46:47 +08:00
|
|
|
if (SPI_gettypeid(tupdesc, attnum) != INT4OID)
|
|
|
|
elog(ERROR, "autoinc (%s): attribute %s must be of INT4 type",
|
|
|
|
relname, args[i]);
|
|
|
|
|
|
|
|
val = DatumGetInt32(SPI_getbinval(rettuple, tupdesc, attnum, &isnull));
|
|
|
|
|
1997-10-03 02:01:57 +08:00
|
|
|
if (!isnull && val != 0)
|
|
|
|
{
|
|
|
|
i += 2;
|
|
|
|
continue;
|
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-10-03 02:01:57 +08:00
|
|
|
i++;
|
|
|
|
chattrs[chnattrs] = attnum;
|
1998-02-26 12:46:47 +08:00
|
|
|
seqname = textin(args[i]);
|
|
|
|
newvals[chnattrs] = Int32GetDatum(nextval(seqname));
|
|
|
|
if (DatumGetInt32(newvals[chnattrs]) == 0)
|
|
|
|
newvals[chnattrs] = Int32GetDatum(nextval(seqname));
|
|
|
|
pfree(seqname);
|
1997-10-03 02:01:57 +08:00
|
|
|
chnattrs++;
|
|
|
|
i++;
|
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
|
1997-10-03 02:01:57 +08:00
|
|
|
if (chnattrs > 0)
|
|
|
|
{
|
1998-02-26 12:46:47 +08:00
|
|
|
rettuple = SPI_modifytuple(rel, rettuple, chnattrs, chattrs, newvals, NULL);
|
|
|
|
if (rettuple == NULL)
|
|
|
|
elog(ERROR, "autoinc (%s): %d returned by SPI_modifytuple",
|
|
|
|
relname, SPI_result);
|
1997-10-03 02:01:57 +08:00
|
|
|
}
|
1998-02-26 12:46:47 +08:00
|
|
|
|
|
|
|
pfree(relname);
|
|
|
|
pfree(chattrs);
|
|
|
|
pfree(newvals);
|
1997-10-03 02:01:57 +08:00
|
|
|
|
2000-05-29 09:59:17 +08:00
|
|
|
return PointerGetDatum(rettuple);
|
1997-10-03 02:01:57 +08:00
|
|
|
}
|