Fix pg_dump bug in the database-level collation patch. "datcollate" and
[PostgreSQL.git] / contrib / spi / moddatetime.c
blobb42dcaa48cd9f975bd552c0abdebffc48f6ff1b4
1 /*
2 moddatetime.c
4 $PostgreSQL$
6 What is this?
7 It is a function to be called from a trigger for the purpose of updating
8 a modification datetime stamp in a record when that record is UPDATEd.
10 Credits
11 This is 95%+ based on autoinc.c, which I used as a starting point as I do
12 not really know what I am doing. I also had help from
13 Jan Wieck <jwieck@debis.com> who told me about the timestamp_in("now") function.
14 OH, me, I'm Terry Mackintosh <terry@terrym.com>
17 #include "executor/spi.h" /* this is what you need to work with SPI */
18 #include "commands/trigger.h" /* -"- and triggers */
20 PG_MODULE_MAGIC;
22 extern Datum moddatetime(PG_FUNCTION_ARGS);
24 PG_FUNCTION_INFO_V1(moddatetime);
26 Datum
27 moddatetime(PG_FUNCTION_ARGS)
29 TriggerData *trigdata = (TriggerData *) fcinfo->context;
30 Trigger *trigger; /* to get trigger name */
31 int nargs; /* # of arguments */
32 int attnum; /* positional number of field to change */
33 Datum newdt; /* The current datetime. */
34 char **args; /* arguments */
35 char *relname; /* triggered relation name */
36 Relation rel; /* triggered relation */
37 HeapTuple rettuple = NULL;
38 TupleDesc tupdesc; /* tuple description */
40 if (!CALLED_AS_TRIGGER(fcinfo))
41 /* internal error */
42 elog(ERROR, "moddatetime: not fired by trigger manager");
44 if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
45 /* internal error */
46 elog(ERROR, "moddatetime: cannot process STATEMENT events");
48 if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
49 /* internal error */
50 elog(ERROR, "moddatetime: must be fired before event");
52 if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
53 /* internal error */
54 elog(ERROR, "moddatetime: must be fired before event");
55 else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
56 rettuple = trigdata->tg_newtuple;
57 else
58 /* internal error */
59 elog(ERROR, "moddatetime: cannot process DELETE events");
61 rel = trigdata->tg_relation;
62 relname = SPI_getrelname(rel);
64 trigger = trigdata->tg_trigger;
66 nargs = trigger->tgnargs;
68 if (nargs != 1)
69 /* internal error */
70 elog(ERROR, "moddatetime (%s): A single argument was expected", relname);
72 args = trigger->tgargs;
73 /* must be the field layout? */
74 tupdesc = rel->rd_att;
76 /* Get the current datetime. */
77 newdt = DirectFunctionCall3(timestamp_in,
78 CStringGetDatum("now"),
79 ObjectIdGetDatum(InvalidOid),
80 Int32GetDatum(-1));
83 * This gets the position in the tuple of the field we want. args[0] being
84 * the name of the field to update, as passed in from the trigger.
86 attnum = SPI_fnumber(tupdesc, args[0]);
89 * This is were we check to see if the field we are supposed to update
90 * even exits. The above function must return -1 if name not found?
92 if (attnum < 0)
93 ereport(ERROR,
94 (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
95 errmsg("\"%s\" has no attribute \"%s\"",
96 relname, args[0])));
99 * OK, this is where we make sure the timestamp field that we are
100 * modifying is really a timestamp field. Hay, error checking, what a
101 * novel idea !-)
103 if (SPI_gettypeid(tupdesc, attnum) != TIMESTAMPOID)
104 ereport(ERROR,
105 (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
106 errmsg("attribute \"%s\" of \"%s\" must be type TIMESTAMP",
107 args[0], relname)));
109 /* 1 is the number of items in the arrays attnum and newdt.
110 attnum is the positional number of the field to be updated.
111 newdt is the new datetime stamp.
112 NOTE that attnum and newdt are not arrays, but then a 1 ellement array
113 is not an array any more then they are. Thus, they can be considered a
114 one element array.
116 rettuple = SPI_modifytuple(rel, rettuple, 1, &attnum, &newdt, NULL);
118 if (rettuple == NULL)
119 /* internal error */
120 elog(ERROR, "moddatetime (%s): %d returned by SPI_modifytuple",
121 relname, SPI_result);
123 /* Clean up */
124 pfree(relname);
126 return PointerGetDatum(rettuple);