| 1 |
/* |
| 2 |
* contrib/spi/autoinc.c |
| 3 |
*/ |
| 4 |
#include "postgres.h" |
| 5 |
|
| 6 |
#include "access/htup_details.h" |
| 7 |
#include "catalog/pg_type.h" |
| 8 |
#include "commands/sequence.h" |
| 9 |
#include "commands/trigger.h" |
| 10 |
#include "executor/spi.h" |
| 11 |
#include "utils/builtins.h" |
| 12 |
#include "utils/rel.h" |
| 13 |
|
| 14 |
PG_MODULE_MAGIC_EXT( |
| 15 |
.name = "autoinc", |
| 16 |
.version = PG_VERSION |
| 17 |
); |
| 18 |
|
| 19 |
PG_FUNCTION_INFO_V1(autoinc); |
| 20 |
|
| 21 |
Datum |
| 22 |
autoinc(PG_FUNCTION_ARGS) |
| 23 |
{ |
| 24 |
TriggerData *trigdata = (TriggerData *) fcinfo->context; |
| 25 |
Trigger *trigger; /* to get trigger name */ |
| 26 |
int nargs; /* # of arguments */ |
| 27 |
int *chattrs; /* attnums of attributes to change */ |
| 28 |
int chnattrs = 0; /* # of above */ |
| 29 |
Datum *newvals; /* vals of above */ |
| 30 |
bool *newnulls; /* null flags for above */ |
| 31 |
char **args; /* arguments */ |
| 32 |
char *relname; /* triggered relation name */ |
| 33 |
Relation rel; /* triggered relation */ |
| 34 |
HeapTuple rettuple = NULL; |
| 35 |
TupleDesc tupdesc; /* tuple description */ |
| 36 |
bool isnull; |
| 37 |
int i; |
| 38 |
|
| 39 |
if (!CALLED_AS_TRIGGER(fcinfo)) |
| 40 |
/* internal error */ |
| 41 |
elog(ERROR, "not fired by trigger manager"); |
| 42 |
if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event)) |
| 43 |
/* internal error */ |
| 44 |
elog(ERROR, "must be fired for row"); |
| 45 |
if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event)) |
| 46 |
/* internal error */ |
| 47 |
elog(ERROR, "must be fired before event"); |
| 48 |
|
| 49 |
if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event)) |
| 50 |
rettuple = trigdata->tg_trigtuple; |
| 51 |
else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event)) |
| 52 |
rettuple = trigdata->tg_newtuple; |
| 53 |
else |
| 54 |
/* internal error */ |
| 55 |
elog(ERROR, "cannot process DELETE events"); |
| 56 |
|
| 57 |
rel = trigdata->tg_relation; |
| 58 |
relname = SPI_getrelname(rel); |
| 59 |
|
| 60 |
trigger = trigdata->tg_trigger; |
| 61 |
|
| 62 |
nargs = trigger->tgnargs; |
| 63 |
if (nargs <= 0 || nargs % 2 != 0) |
| 64 |
/* internal error */ |
| 65 |
elog(ERROR, "autoinc (%s): even number gt 0 of arguments was expected", relname); |
| 66 |
|
| 67 |
args = trigger->tgargs; |
| 68 |
tupdesc = rel->rd_att; |
| 69 |
|
| 70 |
chattrs = (int *) palloc(nargs / 2 * sizeof(int)); |
| 71 |
newvals = (Datum *) palloc(nargs / 2 * sizeof(Datum)); |
| 72 |
newnulls = (bool *) palloc(nargs / 2 * sizeof(bool)); |
| 73 |
|
| 74 |
for (i = 0; i < nargs;) |
| 75 |
{ |
| 76 |
int attnum = SPI_fnumber(tupdesc, args[i]); |
| 77 |
int32 val; |
| 78 |
Datum seqname; |
| 79 |
|
| 80 |
if (attnum <= 0) |
| 81 |
ereport(ERROR, |
| 82 |
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION), |
| 83 |
errmsg("\"%s\" has no attribute \"%s\"", |
| 84 |
relname, args[i]))); |
| 85 |
|
| 86 |
if (SPI_gettypeid(tupdesc, attnum) != INT4OID) |
| 87 |
ereport(ERROR, |
| 88 |
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION), |
| 89 |
errmsg("attribute \"%s\" of \"%s\" must be type INT4", |
| 90 |
args[i], relname))); |
| 91 |
|
| 92 |
val = DatumGetInt32(SPI_getbinval(rettuple, tupdesc, attnum, &isnull)); |
| 93 |
|
| 94 |
if (!isnull && val != 0) |
| 95 |
{ |
| 96 |
i += 2; |
| 97 |
continue; |
| 98 |
} |
| 99 |
|
| 100 |
i++; |
| 101 |
chattrs[chnattrs] = attnum; |
| 102 |
seqname = CStringGetTextDatum(args[i]); |
| 103 |
newvals[chnattrs] = DirectFunctionCall1(nextval, seqname); |
| 104 |
/* nextval now returns int64; coerce down to int32 */ |
| 105 |
newvals[chnattrs] = Int32GetDatum((int32) DatumGetInt64(newvals[chnattrs])); |
| 106 |
if (DatumGetInt32(newvals[chnattrs]) == 0) |
| 107 |
{ |
| 108 |
newvals[chnattrs] = DirectFunctionCall1(nextval, seqname); |
| 109 |
newvals[chnattrs] = Int32GetDatum((int32) DatumGetInt64(newvals[chnattrs])); |
| 110 |
} |
| 111 |
newnulls[chnattrs] = false; |
| 112 |
pfree(DatumGetTextPP(seqname)); |
| 113 |
chnattrs++; |
| 114 |
i++; |
| 115 |
} |
| 116 |
|
| 117 |
if (chnattrs > 0) |
| 118 |
{ |
| 119 |
rettuple = heap_modify_tuple_by_cols(rettuple, tupdesc, |
| 120 |
chnattrs, chattrs, |
| 121 |
newvals, newnulls); |
| 122 |
} |
| 123 |
|
| 124 |
pfree(relname); |
| 125 |
pfree(chattrs); |
| 126 |
pfree(newvals); |
| 127 |
pfree(newnulls); |
| 128 |
|
| 129 |
return PointerGetDatum(rettuple); |
| 130 |
} |