PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
insert_username.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
#include "executor/spi.h"
#include "miscadmin.h"
#include "utils/builtins.h"
#include "utils/rel.h"
Include dependency graph for insert_username.c:

Go to the source code of this file.

Functions

 PG_FUNCTION_INFO_V1 (insert_username)
 
Datum insert_username (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 

Function Documentation

◆ insert_username()

Datum insert_username ( PG_FUNCTION_ARGS  )

Definition at line 22 of file insert_username.c.

23{
24 TriggerData *trigdata = (TriggerData *) fcinfo->context;
25 Trigger *trigger; /* to get trigger name */
26 int nargs; /* # of arguments */
27 Datum newval; /* new value of column */
28 bool newnull; /* null flag */
29 char **args; /* arguments */
30 char *relname; /* triggered relation name */
31 Relation rel; /* triggered relation */
32 HeapTuple rettuple = NULL;
33 TupleDesc tupdesc; /* tuple description */
34 int attnum;
35
36 /* sanity checks from autoinc.c */
37 if (!CALLED_AS_TRIGGER(fcinfo))
38 /* internal error */
39 elog(ERROR, "insert_username: not fired by trigger manager");
40 if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
41 /* internal error */
42 elog(ERROR, "insert_username: must be fired for row");
43 if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))
44 /* internal error */
45 elog(ERROR, "insert_username: must be fired before event");
46
47 if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
48 rettuple = trigdata->tg_trigtuple;
49 else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
50 rettuple = trigdata->tg_newtuple;
51 else
52 /* internal error */
53 elog(ERROR, "insert_username: cannot process DELETE events");
54
55 rel = trigdata->tg_relation;
57
58 trigger = trigdata->tg_trigger;
59
60 nargs = trigger->tgnargs;
61 if (nargs != 1)
62 /* internal error */
63 elog(ERROR, "insert_username (%s): one argument was expected", relname);
64
65 args = trigger->tgargs;
66 tupdesc = rel->rd_att;
67
68 attnum = SPI_fnumber(tupdesc, args[0]);
69
70 if (attnum <= 0)
72 (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
73 errmsg("\"%s\" has no attribute \"%s\"", relname, args[0])));
74
75 if (SPI_gettypeid(tupdesc, attnum) != TEXTOID)
77 (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
78 errmsg("attribute \"%s\" of \"%s\" must be type TEXT",
79 args[0], relname)));
80
81 /* create fields containing name */
83 newnull = false;
84
85 /* construct new tuple */
86 rettuple = heap_modify_tuple_by_cols(rettuple, tupdesc,
87 1, &attnum, &newval, &newnull);
88
90
91 return PointerGetDatum(rettuple);
92}
#define CStringGetTextDatum(s)
Definition: builtins.h:97
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
#define newval
HeapTuple heap_modify_tuple_by_cols(HeapTuple tuple, TupleDesc tupleDesc, int nCols, const int *replCols, const Datum *replValues, const bool *replIsnull)
Definition: heaptuple.c:1278
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:76
void pfree(void *pointer)
Definition: mcxt.c:1521
Oid GetUserId(void)
Definition: miscinit.c:517
char * GetUserNameFromId(Oid roleid, bool noerr)
Definition: miscinit.c:1036
int16 attnum
Definition: pg_attribute.h:74
NameData relname
Definition: pg_class.h:38
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uintptr_t Datum
Definition: postgres.h:64
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:1175
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
Definition: spi.c:1308
char * SPI_getrelname(Relation rel)
Definition: spi.c:1326
Relation tg_relation
Definition: trigger.h:35
TriggerEvent tg_event
Definition: trigger.h:34
HeapTuple tg_newtuple
Definition: trigger.h:37
Trigger * tg_trigger
Definition: trigger.h:38
HeapTuple tg_trigtuple
Definition: trigger.h:36
int16 tgnargs
Definition: reltrigger.h:38
#define TRIGGER_FIRED_BEFORE(event)
Definition: trigger.h:128
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:26
#define TRIGGER_FIRED_FOR_ROW(event)
Definition: trigger.h:122
#define TRIGGER_FIRED_BY_INSERT(event)
Definition: trigger.h:110
#define TRIGGER_FIRED_BY_UPDATE(event)
Definition: trigger.h:116

References generate_unaccent_rules::args, attnum, CALLED_AS_TRIGGER, CStringGetTextDatum, elog, ereport, errcode(), errmsg(), ERROR, GetUserId(), GetUserNameFromId(), heap_modify_tuple_by_cols(), if(), newval, pfree(), PointerGetDatum(), relname, SPI_fnumber(), SPI_getrelname(), SPI_gettypeid(), TriggerData::tg_event, TriggerData::tg_newtuple, TriggerData::tg_relation, TriggerData::tg_trigger, TriggerData::tg_trigtuple, Trigger::tgnargs, TRIGGER_FIRED_BEFORE, TRIGGER_FIRED_BY_INSERT, TRIGGER_FIRED_BY_UPDATE, and TRIGGER_FIRED_FOR_ROW.

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( insert_username  )

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 17 of file insert_username.c.