@@ -32,6 +32,7 @@ moddatetime(PG_FUNCTION_ARGS)
32
32
Trigger * trigger ; /* to get trigger name */
33
33
int nargs ; /* # of arguments */
34
34
int attnum ; /* positional number of field to change */
35
+ Oid atttypid ; /* type OID of field to change */
35
36
Datum newdt ; /* The current datetime. */
36
37
char * * args ; /* arguments */
37
38
char * relname ; /* triggered relation name */
@@ -75,21 +76,15 @@ moddatetime(PG_FUNCTION_ARGS)
75
76
/* must be the field layout? */
76
77
tupdesc = rel -> rd_att ;
77
78
78
- /* Get the current datetime. */
79
- newdt = DirectFunctionCall3 (timestamp_in ,
80
- CStringGetDatum ("now" ),
81
- ObjectIdGetDatum (InvalidOid ),
82
- Int32GetDatum (-1 ));
83
-
84
79
/*
85
80
* This gets the position in the tuple of the field we want. args[0] being
86
81
* the name of the field to update, as passed in from the trigger.
87
82
*/
88
83
attnum = SPI_fnumber (tupdesc , args [0 ]);
89
84
90
85
/*
91
- * This is were we check to see if the field we are supposed to update
92
- * even exits . The above function must return -1 if name not found?
86
+ * This is where we check to see if the field we are supposed to update
87
+ * even exists . The above function must return -1 if name not found?
93
88
*/
94
89
if (attnum < 0 )
95
90
ereport (ERROR ,
@@ -98,20 +93,33 @@ moddatetime(PG_FUNCTION_ARGS)
98
93
relname , args [0 ])));
99
94
100
95
/*
101
- * OK, this is where we make sure the timestamp field that we are
102
- * modifying is really a timestamp field. Hay, error checking, what a
103
- * novel idea !-)
96
+ * Check the target field has an allowed type, and get the current
97
+ * datetime as a value of that type.
104
98
*/
105
- if (SPI_gettypeid (tupdesc , attnum ) != TIMESTAMPOID )
99
+ atttypid = SPI_gettypeid (tupdesc , attnum );
100
+ if (atttypid == TIMESTAMPOID )
101
+ newdt = DirectFunctionCall3 (timestamp_in ,
102
+ CStringGetDatum ("now" ),
103
+ ObjectIdGetDatum (InvalidOid ),
104
+ Int32GetDatum (-1 ));
105
+ else if (atttypid == TIMESTAMPTZOID )
106
+ newdt = DirectFunctionCall3 (timestamptz_in ,
107
+ CStringGetDatum ("now" ),
108
+ ObjectIdGetDatum (InvalidOid ),
109
+ Int32GetDatum (-1 ));
110
+ else
111
+ {
106
112
ereport (ERROR ,
107
113
(errcode (ERRCODE_TRIGGERED_ACTION_EXCEPTION ),
108
- errmsg ("attribute \"%s\" of \"%s\" must be type TIMESTAMP" ,
114
+ errmsg ("attribute \"%s\" of \"%s\" must be type TIMESTAMP or TIMESTAMPTZ " ,
109
115
args [0 ], relname )));
116
+ newdt = (Datum ) 0 ; /* keep compiler quiet */
117
+ }
110
118
111
119
/* 1 is the number of items in the arrays attnum and newdt.
112
120
attnum is the positional number of the field to be updated.
113
121
newdt is the new datetime stamp.
114
- NOTE that attnum and newdt are not arrays, but then a 1 ellement array
122
+ NOTE that attnum and newdt are not arrays, but then a 1 element array
115
123
is not an array any more then they are. Thus, they can be considered a
116
124
one element array.
117
125
*/
0 commit comments