1997-10-17 13:55:34 +04:00
|
|
|
/*
|
|
|
|
* insert_username.c
|
|
|
|
* $Modified: Thu Oct 16 08:13:42 1997 by brook $
|
|
|
|
*
|
|
|
|
* insert user name in response to a trigger
|
|
|
|
* usage: insert_username (column_name)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "executor/spi.h" /* this is what you need to work with SPI */
|
1998-02-26 07:46:47 +03:00
|
|
|
#include "commands/trigger.h" /* -"- and triggers */
|
2000-12-04 04:25:35 +03:00
|
|
|
#include "miscadmin.h" /* for GetUserName() */
|
1997-10-17 13:55:34 +04:00
|
|
|
|
2001-03-22 07:01:46 +03:00
|
|
|
extern Datum insert_username(PG_FUNCTION_ARGS);
|
1997-10-17 13:55:34 +04:00
|
|
|
|
2000-11-20 23:36:57 +03:00
|
|
|
PG_FUNCTION_INFO_V1(insert_username);
|
|
|
|
|
2000-05-29 05:59:17 +04:00
|
|
|
Datum
|
|
|
|
insert_username(PG_FUNCTION_ARGS)
|
1997-10-17 13:55:34 +04:00
|
|
|
{
|
2000-05-29 05:59:17 +04:00
|
|
|
TriggerData *trigdata = (TriggerData *) fcinfo->context;
|
1998-02-26 07:46:47 +03:00
|
|
|
Trigger *trigger; /* to get trigger name */
|
1997-10-17 13:55:34 +04:00
|
|
|
int nargs; /* # of arguments */
|
|
|
|
Datum newval; /* new value of column */
|
1998-02-26 07:46:47 +03:00
|
|
|
char **args; /* arguments */
|
|
|
|
char *relname; /* triggered relation name */
|
1997-10-17 13:55:34 +04:00
|
|
|
Relation rel; /* triggered relation */
|
|
|
|
HeapTuple rettuple = NULL;
|
|
|
|
TupleDesc tupdesc; /* tuple description */
|
|
|
|
int attnum;
|
|
|
|
|
1998-02-26 07:46:47 +03:00
|
|
|
/* sanity checks from autoinc.c */
|
2000-05-29 05:59:17 +04:00
|
|
|
if (!CALLED_AS_TRIGGER(fcinfo))
|
|
|
|
elog(ERROR, "insert_username: not fired by trigger manager");
|
|
|
|
if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
|
1998-01-06 21:53:02 +03:00
|
|
|
elog(ERROR, "insert_username: can't process STATEMENT events");
|
2000-05-29 05:59:17 +04:00
|
|
|
if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
|
1998-01-06 21:53:02 +03:00
|
|
|
elog(ERROR, "insert_username: 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-17 13:55:34 +04:00
|
|
|
else
|
1998-01-06 21:53:02 +03:00
|
|
|
elog(ERROR, "insert_username: can't 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-17 13:55:34 +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-17 13:55:34 +04:00
|
|
|
|
|
|
|
nargs = trigger->tgnargs;
|
|
|
|
if (nargs != 1)
|
1998-01-06 21:53:02 +03:00
|
|
|
elog(ERROR, "insert_username (%s): one argument was expected", relname);
|
1998-02-26 07:46:47 +03:00
|
|
|
|
1997-10-17 13:55:34 +04:00
|
|
|
args = trigger->tgargs;
|
|
|
|
tupdesc = rel->rd_att;
|
1998-02-26 07:46:47 +03:00
|
|
|
|
|
|
|
attnum = SPI_fnumber(tupdesc, args[0]);
|
|
|
|
|
|
|
|
if (attnum < 0)
|
1998-01-06 21:53:02 +03:00
|
|
|
elog(ERROR, "insert_username (%s): there is no attribute %s", relname, args[0]);
|
1998-02-26 07:46:47 +03:00
|
|
|
if (SPI_gettypeid(tupdesc, attnum) != TEXTOID)
|
|
|
|
elog(ERROR, "insert_username (%s): attribute %s must be of TEXT type",
|
|
|
|
relname, args[0]);
|
|
|
|
|
|
|
|
/* create fields containing name */
|
2000-12-04 04:25:35 +03:00
|
|
|
newval = DirectFunctionCall1(textin,
|
2001-03-22 07:01:46 +03:00
|
|
|
CStringGetDatum(GetUserName(GetUserId())));
|
1998-02-26 07:46:47 +03:00
|
|
|
|
|
|
|
/* construct new tuple */
|
|
|
|
rettuple = SPI_modifytuple(rel, rettuple, 1, &attnum, &newval, NULL);
|
|
|
|
if (rettuple == NULL)
|
|
|
|
elog(ERROR, "insert_username (%s): %d returned by SPI_modifytuple",
|
|
|
|
relname, SPI_result);
|
|
|
|
|
|
|
|
pfree(relname);
|
1997-10-17 13:55:34 +04:00
|
|
|
|
2000-05-29 05:59:17 +04:00
|
|
|
return PointerGetDatum(rettuple);
|
1997-10-17 13:55:34 +04:00
|
|
|
}
|