PostgreSQL Source Code  git master
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/builtins.h"
#include "utils/rel.h"
Include dependency graph for moddatetime.c:

Go to the source code of this file.

Functions

 PG_FUNCTION_INFO_V1 (moddatetime)
 
Datum moddatetime (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 

Function Documentation

◆ moddatetime()

Datum moddatetime ( PG_FUNCTION_ARGS  )

Definition at line 30 of file moddatetime.c.

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

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 25 of file moddatetime.c.