PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
moddatetime.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 "utils/fmgrprotos.h"
#include "utils/rel.h"
Include dependency graph for moddatetime.c:

Go to the source code of this file.

Functions

 PG_MODULE_MAGIC_EXT (.name="moddatetime",.version=PG_VERSION)
 
 PG_FUNCTION_INFO_V1 (moddatetime)
 
Datum moddatetime (PG_FUNCTION_ARGS)
 

Function Documentation

◆ moddatetime()

Datum moddatetime ( PG_FUNCTION_ARGS  )

Definition at line 33 of file moddatetime.c.

34{
35 TriggerData *trigdata = (TriggerData *) fcinfo->context;
36 Trigger *trigger; /* to get trigger name */
37 int nargs; /* # of arguments */
38 int attnum; /* positional number of field to change */
39 Oid atttypid; /* type OID of field to change */
40 Datum newdt; /* The current datetime. */
41 bool newdtnull; /* null flag for it */
42 char **args; /* arguments */
43 char *relname; /* triggered relation name */
44 Relation rel; /* triggered relation */
45 HeapTuple rettuple = NULL;
46 TupleDesc tupdesc; /* tuple description */
47
48 if (!CALLED_AS_TRIGGER(fcinfo))
49 /* internal error */
50 elog(ERROR, "moddatetime: not fired by trigger manager");
51
52 if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
53 /* internal error */
54 elog(ERROR, "moddatetime: must be fired for row");
55
56 if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))
57 /* internal error */
58 elog(ERROR, "moddatetime: must be fired before event");
59
60 if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
61 /* internal error */
62 elog(ERROR, "moddatetime: cannot process INSERT events");
63 else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
64 rettuple = trigdata->tg_newtuple;
65 else
66 /* internal error */
67 elog(ERROR, "moddatetime: cannot process DELETE events");
68
69 rel = trigdata->tg_relation;
71
72 trigger = trigdata->tg_trigger;
73
74 nargs = trigger->tgnargs;
75
76 if (nargs != 1)
77 /* internal error */
78 elog(ERROR, "moddatetime (%s): A single argument was expected", relname);
79
80 args = trigger->tgargs;
81 /* must be the field layout? */
82 tupdesc = rel->rd_att;
83
84 /*
85 * This gets the position in the tuple of the field we want. args[0] being
86 * the name of the field to update, as passed in from the trigger.
87 */
88 attnum = SPI_fnumber(tupdesc, args[0]);
89
90 /*
91 * This is where we check to see if the field we are supposed to update
92 * even exists.
93 */
94 if (attnum <= 0)
96 (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
97 errmsg("\"%s\" has no attribute \"%s\"",
98 relname, args[0])));
99
100 /*
101 * Check the target field has an allowed type, and get the current
102 * datetime as a value of that type.
103 */
104 atttypid = SPI_gettypeid(tupdesc, attnum);
105 if (atttypid == TIMESTAMPOID)
107 CStringGetDatum("now"),
109 Int32GetDatum(-1));
110 else if (atttypid == TIMESTAMPTZOID)
112 CStringGetDatum("now"),
114 Int32GetDatum(-1));
115 else
116 {
118 (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
119 errmsg("attribute \"%s\" of \"%s\" must be type TIMESTAMP or TIMESTAMPTZ",
120 args[0], relname)));
121 newdt = (Datum) 0; /* keep compiler quiet */
122 }
123 newdtnull = false;
124
125 /* Replace the attnum'th column with newdt */
126 rettuple = heap_modify_tuple_by_cols(rettuple, tupdesc,
127 1, &attnum, &newdt, &newdtnull);
128
129 /* Clean up */
130 pfree(relname);
131
132 return PointerGetDatum(rettuple);
133}
Datum timestamp_in(PG_FUNCTION_ARGS)
Definition: timestamp.c:166
Datum timestamptz_in(PG_FUNCTION_ARGS)
Definition: timestamp.c:418
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:686
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:81
void pfree(void *pointer)
Definition: mcxt.c:2150
int16 attnum
Definition: pg_attribute.h:74
NameData relname
Definition: pg_class.h:38
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
uintptr_t Datum
Definition: postgres.h:69
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:355
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:217
#define InvalidOid
Definition: postgres_ext.h:35
unsigned int Oid
Definition: postgres_ext.h:30
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:1176
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
Definition: spi.c:1309
char * SPI_getrelname(Relation rel)
Definition: spi.c:1327
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
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, CStringGetDatum(), DirectFunctionCall3, elog, ereport, errcode(), errmsg(), ERROR, heap_modify_tuple_by_cols(), if(), Int32GetDatum(), InvalidOid, ObjectIdGetDatum(), pfree(), PointerGetDatum(), relname, SPI_fnumber(), SPI_getrelname(), SPI_gettypeid(), TriggerData::tg_event, TriggerData::tg_newtuple, TriggerData::tg_relation, TriggerData::tg_trigger, Trigger::tgnargs, timestamp_in(), timestamptz_in(), TRIGGER_FIRED_BEFORE, TRIGGER_FIRED_BY_INSERT, TRIGGER_FIRED_BY_UPDATE, and TRIGGER_FIRED_FOR_ROW.

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( moddatetime  )

◆ PG_MODULE_MAGIC_EXT()

PG_MODULE_MAGIC_EXT ( name = "moddatetime",
version = PG_VERSION 
)