3 * $Modified: Thu Oct 16 08:13:42 1997 by brook $
6 * insert user name in response to a trigger
7 * usage: insert_username (column_name)
10 #include "executor/spi.h" /* this is what you need to work with SPI */
11 #include "commands/trigger.h" /* -"- and triggers */
12 #include "miscadmin.h" /* for GetUserName() */
16 extern Datum
insert_username(PG_FUNCTION_ARGS
);
18 PG_FUNCTION_INFO_V1(insert_username
);
21 insert_username(PG_FUNCTION_ARGS
)
23 TriggerData
*trigdata
= (TriggerData
*) fcinfo
->context
;
24 Trigger
*trigger
; /* to get trigger name */
25 int nargs
; /* # of arguments */
26 Datum newval
; /* new value of column */
27 char **args
; /* arguments */
28 char *relname
; /* triggered relation name */
29 Relation rel
; /* triggered relation */
30 HeapTuple rettuple
= NULL
;
31 TupleDesc tupdesc
; /* tuple description */
34 /* sanity checks from autoinc.c */
35 if (!CALLED_AS_TRIGGER(fcinfo
))
37 elog(ERROR
, "insert_username: not fired by trigger manager");
38 if (TRIGGER_FIRED_FOR_STATEMENT(trigdata
->tg_event
))
40 elog(ERROR
, "insert_username: cannot process STATEMENT events");
41 if (TRIGGER_FIRED_AFTER(trigdata
->tg_event
))
43 elog(ERROR
, "insert_username: must be fired before event");
45 if (TRIGGER_FIRED_BY_INSERT(trigdata
->tg_event
))
46 rettuple
= trigdata
->tg_trigtuple
;
47 else if (TRIGGER_FIRED_BY_UPDATE(trigdata
->tg_event
))
48 rettuple
= trigdata
->tg_newtuple
;
51 elog(ERROR
, "insert_username: cannot process DELETE events");
53 rel
= trigdata
->tg_relation
;
54 relname
= SPI_getrelname(rel
);
56 trigger
= trigdata
->tg_trigger
;
58 nargs
= trigger
->tgnargs
;
61 elog(ERROR
, "insert_username (%s): one argument was expected", relname
);
63 args
= trigger
->tgargs
;
64 tupdesc
= rel
->rd_att
;
66 attnum
= SPI_fnumber(tupdesc
, args
[0]);
70 (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION
),
71 errmsg("\"%s\" has no attribute \"%s\"", relname
, args
[0])));
73 if (SPI_gettypeid(tupdesc
, attnum
) != TEXTOID
)
75 (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION
),
76 errmsg("attribute \"%s\" of \"%s\" must be type TEXT",
79 /* create fields containing name */
80 newval
= CStringGetTextDatum(GetUserNameFromId(GetUserId()));
82 /* construct new tuple */
83 rettuple
= SPI_modifytuple(rel
, rettuple
, 1, &attnum
, &newval
, NULL
);
86 elog(ERROR
, "insert_username (\"%s\"): %d returned by SPI_modifytuple",
91 return PointerGetDatum(rettuple
);