4contrib/spi/moddatetime.c
7It is a function to be called from a trigger for the purpose of updating
8a modification datetime stamp in a record when that record is UPDATEd.
11This is 95%+ based on autoinc.c, which I used as a starting point as I do
12not really know what I am doing. I also had help from
13Jan Wieck <jwieck@debis.com> who told me about the timestamp_in("now") function.
14OH, me, I'm Terry Mackintosh <terry@terrym.com>
22#include "utils/fmgrprotos.h"
26 .
name =
"moddatetime",
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 */
50 elog(
ERROR,
"moddatetime: not fired by trigger manager");
54 elog(
ERROR,
"moddatetime: must be fired for row");
58 elog(
ERROR,
"moddatetime: must be fired before event");
62 elog(
ERROR,
"moddatetime: cannot process INSERT events");
67 elog(
ERROR,
"moddatetime: cannot process DELETE events");
80 args = trigger->tgargs;
81 /* must be the field layout? */
82 tupdesc = rel->rd_att;
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.
91 * This is where we check to see if the field we are supposed to update
96 (
errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
97 errmsg(
"\"%s\" has no attribute \"%s\"",
101 * Check the target field has an allowed type, and get the current
102 * datetime as a value of that type.
105 if (atttypid == TIMESTAMPOID)
110 else if (atttypid == TIMESTAMPTZOID)
118 (
errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
119 errmsg(
"attribute \"%s\" of \"%s\" must be type TIMESTAMP or TIMESTAMPTZ",
121 newdt = (
Datum) 0;
/* keep compiler quiet */
125 /* Replace the attnum'th column with newdt */
127 1, &
attnum, &newdt, &newdtnull);
Datum timestamp_in(PG_FUNCTION_ARGS)
Datum timestamptz_in(PG_FUNCTION_ARGS)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
#define DirectFunctionCall3(func, arg1, arg2, arg3)
HeapTuple heap_modify_tuple_by_cols(HeapTuple tuple, TupleDesc tupleDesc, int nCols, const int *replCols, const Datum *replValues, const bool *replIsnull)
if(TABLE==NULL||TABLE_index==NULL)
void pfree(void *pointer)
PG_MODULE_MAGIC_EXT(.name="moddatetime",.version=PG_VERSION)
PG_FUNCTION_INFO_V1(moddatetime)
Datum moddatetime(PG_FUNCTION_ARGS)
static Datum PointerGetDatum(const void *X)
static Datum ObjectIdGetDatum(Oid X)
static Datum CStringGetDatum(const char *X)
static Datum Int32GetDatum(int32 X)
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
char * SPI_getrelname(Relation rel)
#define TRIGGER_FIRED_BEFORE(event)
#define CALLED_AS_TRIGGER(fcinfo)
#define TRIGGER_FIRED_FOR_ROW(event)
#define TRIGGER_FIRED_BY_INSERT(event)
#define TRIGGER_FIRED_BY_UPDATE(event)