PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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 */
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)
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 {
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 */
127 1, &attnum, &newdt, &newdtnull);
128
129 /* Clean up */
130 pfree(relname);
131
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:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition fmgr.h:688
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
void pfree(void *pointer)
Definition mcxt.c:1616
int16 attnum
NameData relname
Definition pg_class.h:38
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
uint64_t Datum
Definition postgres.h:70
static Datum CStringGetDatum(const char *X)
Definition postgres.h:380
static Datum Int32GetDatum(int32 X)
Definition postgres.h:222
#define InvalidOid
unsigned int Oid
static int fb(int x)
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
TupleDesc rd_att
Definition rel.h:112
#define TRIGGER_FIRED_BEFORE(event)
Definition trigger.h:130
#define CALLED_AS_TRIGGER(fcinfo)
Definition trigger.h:26
#define TRIGGER_FIRED_FOR_ROW(event)
Definition trigger.h:124
#define TRIGGER_FIRED_BY_INSERT(event)
Definition trigger.h:112
#define TRIGGER_FIRED_BY_UPDATE(event)
Definition trigger.h:118

References attnum, CALLED_AS_TRIGGER, CStringGetDatum(), DirectFunctionCall3, elog, ereport, errcode(), errmsg(), ERROR, fb(), heap_modify_tuple_by_cols(), Int32GetDatum(), InvalidOid, ObjectIdGetDatum(), pfree(), PointerGetDatum(), RelationData::rd_att, 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 
)