1997-10-17 13:55:34 +04:00
|
|
|
/*
|
2010-09-21 00:08:53 +04:00
|
|
|
* contrib/spi/insert_username.c
|
1997-10-17 13:55:34 +04:00
|
|
|
*
|
|
|
|
* insert user name in response to a trigger
|
|
|
|
* usage: insert_username (column_name)
|
|
|
|
*/
|
2009-01-07 16:44:37 +03:00
|
|
|
#include "postgres.h"
|
1997-10-17 13:55:34 +04:00
|
|
|
|
2016-11-08 23:36:36 +03:00
|
|
|
#include "access/htup_details.h"
|
2009-01-07 16:44:37 +03:00
|
|
|
#include "catalog/pg_type.h"
|
|
|
|
#include "commands/trigger.h"
|
|
|
|
#include "executor/spi.h"
|
|
|
|
#include "miscadmin.h"
|
|
|
|
#include "utils/builtins.h"
|
2011-02-23 20:18:09 +03:00
|
|
|
#include "utils/rel.h"
|
1997-10-17 13:55:34 +04:00
|
|
|
|
2006-05-31 02:12:16 +04:00
|
|
|
PG_MODULE_MAGIC;
|
|
|
|
|
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;
|
1997-10-17 13:55:34 +04:00
|
|
|
Trigger *trigger; /* to get trigger name */
|
|
|
|
int nargs; /* # of arguments */
|
|
|
|
Datum newval; /* new value of column */
|
2016-11-08 23:36:36 +03:00
|
|
|
bool newnull; /* null flag */
|
1997-10-17 13:55:34 +04:00
|
|
|
char **args; /* arguments */
|
|
|
|
char *relname; /* triggered relation name */
|
|
|
|
Relation rel; /* triggered relation */
|
|
|
|
HeapTuple rettuple = NULL;
|
|
|
|
TupleDesc tupdesc; /* tuple description */
|
|
|
|
int attnum;
|
|
|
|
|
|
|
|
/* sanity checks from autoinc.c */
|
2000-05-29 05:59:17 +04:00
|
|
|
if (!CALLED_AS_TRIGGER(fcinfo))
|
2003-07-24 21:52:50 +04:00
|
|
|
/* internal error */
|
2000-05-29 05:59:17 +04:00
|
|
|
elog(ERROR, "insert_username: not fired by trigger manager");
|
2010-10-08 21:27:31 +04:00
|
|
|
if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
|
2003-07-24 21:52:50 +04:00
|
|
|
/* internal error */
|
2010-10-08 21:27:31 +04:00
|
|
|
elog(ERROR, "insert_username: must be fired for row");
|
|
|
|
if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))
|
2003-07-24 21:52:50 +04:00
|
|
|
/* internal error */
|
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
|
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, "insert_username: 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-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)
|
2003-07-24 21:52:50 +04:00
|
|
|
/* internal error */
|
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
|
|
|
attnum = SPI_fnumber(tupdesc, args[0]);
|
1998-02-26 07:46:47 +03:00
|
|
|
|
2016-11-08 21:11:15 +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[0])));
|
|
|
|
|
1997-10-17 13:55:34 +04:00
|
|
|
if (SPI_gettypeid(tupdesc, attnum) != TEXTOID)
|
2003-07-24 21:52:50 +04:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
|
|
|
|
errmsg("attribute \"%s\" of \"%s\" must be type TEXT",
|
|
|
|
args[0], relname)));
|
1998-02-26 07:46:47 +03:00
|
|
|
|
1997-10-17 13:55:34 +04:00
|
|
|
/* create fields containing name */
|
2015-05-09 20:06:49 +03:00
|
|
|
newval = CStringGetTextDatum(GetUserNameFromId(GetUserId(), false));
|
2016-11-08 23:36:36 +03:00
|
|
|
newnull = false;
|
1998-02-26 07:46:47 +03:00
|
|
|
|
1997-10-17 13:55:34 +04:00
|
|
|
/* construct new tuple */
|
2016-11-08 23:36:36 +03:00
|
|
|
rettuple = heap_modify_tuple_by_cols(rettuple, tupdesc,
|
|
|
|
1, &attnum, &newval, &newnull);
|
1998-02-26 07:46:47 +03:00
|
|
|
|
1997-10-17 13:55:34 +04:00
|
|
|
pfree(relname);
|
|
|
|
|
2000-05-29 05:59:17 +04:00
|
|
|
return PointerGetDatum(rettuple);
|
1997-10-17 13:55:34 +04:00
|
|
|
}
|