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 */
|
1997-10-17 13:55:34 +04:00
|
|
|
#include "miscadmin.h" /* for GetPgUserName() */
|
|
|
|
|
1998-02-26 07:46:47 +03:00
|
|
|
HeapTuple insert_username(void);
|
1997-10-17 13:55:34 +04:00
|
|
|
|
|
|
|
HeapTuple
|
1998-02-26 07:46:47 +03:00
|
|
|
insert_username()
|
1997-10-17 13:55:34 +04:00
|
|
|
{
|
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 */
|
1997-10-17 13:55:34 +04:00
|
|
|
if (!CurrentTriggerData)
|
1998-01-06 21:53:02 +03:00
|
|
|
elog(ERROR, "insert_username: triggers are not initialized");
|
1997-10-17 13:55:34 +04:00
|
|
|
if (TRIGGER_FIRED_FOR_STATEMENT(CurrentTriggerData->tg_event))
|
1998-01-06 21:53:02 +03:00
|
|
|
elog(ERROR, "insert_username: can't process STATEMENT events");
|
1997-10-17 13:55:34 +04:00
|
|
|
if (TRIGGER_FIRED_AFTER(CurrentTriggerData->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
|
|
|
|
1997-10-17 13:55:34 +04:00
|
|
|
if (TRIGGER_FIRED_BY_INSERT(CurrentTriggerData->tg_event))
|
|
|
|
rettuple = CurrentTriggerData->tg_trigtuple;
|
|
|
|
else if (TRIGGER_FIRED_BY_UPDATE(CurrentTriggerData->tg_event))
|
|
|
|
rettuple = CurrentTriggerData->tg_newtuple;
|
|
|
|
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
|
|
|
|
1997-10-17 13:55:34 +04:00
|
|
|
rel = CurrentTriggerData->tg_relation;
|
|
|
|
relname = SPI_getrelname(rel);
|
1998-02-26 07:46:47 +03:00
|
|
|
|
1997-10-17 13:55:34 +04:00
|
|
|
trigger = CurrentTriggerData->tg_trigger;
|
|
|
|
|
|
|
|
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
|
|
|
|
1997-10-17 13:55:34 +04:00
|
|
|
CurrentTriggerData = NULL;
|
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 */
|
|
|
|
newval = PointerGetDatum(textin(GetPgUserName()));
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
|
|
return (rettuple);
|
|
|
|
}
|