2008-05-17 05:28:26 +04:00
|
|
|
/*
|
|
|
|
* $PostgreSQL: pgsql/contrib/spi/autoinc.c,v 1.15 2008/05/17 01:28:22 adunstan Exp $
|
|
|
|
*/
|
1997-10-02 22:01:57 +04:00
|
|
|
|
|
|
|
#include "executor/spi.h" /* this is what you need to work with SPI */
|
|
|
|
#include "commands/trigger.h" /* -"- and triggers */
|
2000-06-12 00:08:01 +04:00
|
|
|
#include "commands/sequence.h" /* for nextval() */
|
1997-10-02 22:01:57 +04:00
|
|
|
|
2006-05-31 02:12:16 +04:00
|
|
|
PG_MODULE_MAGIC;
|
|
|
|
|
2000-05-29 05:59:17 +04:00
|
|
|
extern Datum autoinc(PG_FUNCTION_ARGS);
|
1997-10-02 22:01:57 +04:00
|
|
|
|
2000-11-20 23:36:57 +03:00
|
|
|
PG_FUNCTION_INFO_V1(autoinc);
|
|
|
|
|
2000-05-29 05:59:17 +04:00
|
|
|
Datum
|
|
|
|
autoinc(PG_FUNCTION_ARGS)
|
1997-10-02 22:01:57 +04:00
|
|
|
{
|
2000-05-29 05:59:17 +04:00
|
|
|
TriggerData *trigdata = (TriggerData *) fcinfo->context;
|
1997-10-02 22:01:57 +04: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 05:59:17 +04:00
|
|
|
if (!CALLED_AS_TRIGGER(fcinfo))
|
2003-07-24 21:52:50 +04:00
|
|
|
/* internal error */
|
|
|
|
elog(ERROR, "not fired by trigger manager");
|
2000-05-29 05:59:17 +04:00
|
|
|
if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
|
2003-07-24 21:52:50 +04:00
|
|
|
/* internal error */
|
Wording cleanup for error messages. Also change can't -> cannot.
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
2007-02-01 22:10:30 +03:00
|
|
|
elog(ERROR, "cannot process STATEMENT events");
|
2000-05-29 05:59:17 +04:00
|
|
|
if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
|
2003-07-24 21:52:50 +04:00
|
|
|
/* internal error */
|
|
|
|
elog(ERROR, "must be fired before event");
|
1998-02-26 07:46:47 +03:00
|
|
|
|
2000-05-29 05:59:17 +04: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-02 22:01:57 +04:00
|
|
|
else
|
2003-07-24 21:52:50 +04:00
|
|
|
/* internal error */
|
Wording cleanup for error messages. Also change can't -> cannot.
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
2007-02-01 22:10:30 +03:00
|
|
|
elog(ERROR, "cannot process DELETE events");
|
1998-02-26 07:46:47 +03:00
|
|
|
|
2000-05-29 05:59:17 +04:00
|
|
|
rel = trigdata->tg_relation;
|
1997-10-02 22:01:57 +04:00
|
|
|
relname = SPI_getrelname(rel);
|
1998-02-26 07:46:47 +03:00
|
|
|
|
2000-05-29 05:59:17 +04:00
|
|
|
trigger = trigdata->tg_trigger;
|
1997-10-02 22:01:57 +04:00
|
|
|
|
|
|
|
nargs = trigger->tgnargs;
|
|
|
|
if (nargs <= 0 || nargs % 2 != 0)
|
2003-07-24 21:52:50 +04:00
|
|
|
/* internal error */
|
1998-01-06 21:53:02 +03:00
|
|
|
elog(ERROR, "autoinc (%s): even number gt 0 of arguments was expected", relname);
|
1998-02-26 07:46:47 +03:00
|
|
|
|
1997-10-02 22:01:57 +04:00
|
|
|
args = trigger->tgargs;
|
|
|
|
tupdesc = rel->rd_att;
|
1998-02-26 07:46:47 +03:00
|
|
|
|
|
|
|
chattrs = (int *) palloc(nargs / 2 * sizeof(int));
|
|
|
|
newvals = (Datum *) palloc(nargs / 2 * sizeof(Datum));
|
|
|
|
|
|
|
|
for (i = 0; i < nargs;)
|
1997-10-02 22:01:57 +04:00
|
|
|
{
|
1998-02-26 07:46:47 +03:00
|
|
|
int attnum = SPI_fnumber(tupdesc, args[i]);
|
|
|
|
int32 val;
|
2000-07-06 03:12:09 +04:00
|
|
|
Datum seqname;
|
1998-02-26 07:46:47 +03:00
|
|
|
|
|
|
|
if (attnum < 0)
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
|
|
|
|
errmsg("\"%s\" has no attribute \"%s\"",
|
|
|
|
relname, args[i])));
|
|
|
|
|
1998-02-26 07:46:47 +03:00
|
|
|
if (SPI_gettypeid(tupdesc, attnum) != INT4OID)
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
|
2005-10-15 06:49:52 +04:00
|
|
|
errmsg("attribute \"%s\" of \"%s\" must be type INT4",
|
|
|
|
args[i], relname)));
|
1998-02-26 07:46:47 +03:00
|
|
|
|
|
|
|
val = DatumGetInt32(SPI_getbinval(rettuple, tupdesc, attnum, &isnull));
|
|
|
|
|
1997-10-02 22:01:57 +04:00
|
|
|
if (!isnull && val != 0)
|
|
|
|
{
|
|
|
|
i += 2;
|
|
|
|
continue;
|
|
|
|
}
|
1998-02-26 07:46:47 +03:00
|
|
|
|
1997-10-02 22:01:57 +04:00
|
|
|
i++;
|
|
|
|
chattrs[chnattrs] = attnum;
|
2008-03-26 01:42:46 +03:00
|
|
|
seqname = CStringGetTextDatum(args[i]);
|
2000-07-06 03:12:09 +04:00
|
|
|
newvals[chnattrs] = DirectFunctionCall1(nextval, seqname);
|
2001-08-17 00:38:56 +04:00
|
|
|
/* nextval now returns int64; coerce down to int32 */
|
|
|
|
newvals[chnattrs] = Int32GetDatum((int32) DatumGetInt64(newvals[chnattrs]));
|
1998-02-26 07:46:47 +03:00
|
|
|
if (DatumGetInt32(newvals[chnattrs]) == 0)
|
2001-08-17 00:38:56 +04:00
|
|
|
{
|
2000-07-06 03:12:09 +04:00
|
|
|
newvals[chnattrs] = DirectFunctionCall1(nextval, seqname);
|
2001-08-17 00:38:56 +04:00
|
|
|
newvals[chnattrs] = Int32GetDatum((int32) DatumGetInt64(newvals[chnattrs]));
|
|
|
|
}
|
2000-07-06 03:12:09 +04:00
|
|
|
pfree(DatumGetTextP(seqname));
|
1997-10-02 22:01:57 +04:00
|
|
|
chnattrs++;
|
|
|
|
i++;
|
|
|
|
}
|
1998-02-26 07:46:47 +03:00
|
|
|
|
1997-10-02 22:01:57 +04:00
|
|
|
if (chnattrs > 0)
|
|
|
|
{
|
1998-02-26 07:46:47 +03:00
|
|
|
rettuple = SPI_modifytuple(rel, rettuple, chnattrs, chattrs, newvals, NULL);
|
|
|
|
if (rettuple == NULL)
|
2003-07-24 21:52:50 +04:00
|
|
|
/* internal error */
|
1998-02-26 07:46:47 +03:00
|
|
|
elog(ERROR, "autoinc (%s): %d returned by SPI_modifytuple",
|
|
|
|
relname, SPI_result);
|
1997-10-02 22:01:57 +04:00
|
|
|
}
|
1998-02-26 07:46:47 +03:00
|
|
|
|
|
|
|
pfree(relname);
|
|
|
|
pfree(chattrs);
|
|
|
|
pfree(newvals);
|
1997-10-02 22:01:57 +04:00
|
|
|
|
2000-05-29 05:59:17 +04:00
|
|
|
return PointerGetDatum(rettuple);
|
1997-10-02 22:01:57 +04:00
|
|
|
}
|