PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
autoinc.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "catalog/pg_type.h"
#include "commands/sequence.h"
#include "commands/trigger.h"
#include "executor/spi.h"
#include "utils/builtins.h"
#include "utils/rel.h"
Include dependency graph for autoinc.c:

Go to the source code of this file.

Functions

 PG_FUNCTION_INFO_V1 (autoinc)
 
Datum autoinc (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 

Function Documentation

◆ autoinc()

Datum autoinc ( PG_FUNCTION_ARGS  )

Definition at line 19 of file autoinc.c.

20{
21 TriggerData *trigdata = (TriggerData *) fcinfo->context;
22 Trigger *trigger; /* to get trigger name */
23 int nargs; /* # of arguments */
24 int *chattrs; /* attnums of attributes to change */
25 int chnattrs = 0; /* # of above */
26 Datum *newvals; /* vals of above */
27 bool *newnulls; /* null flags for above */
28 char **args; /* arguments */
29 char *relname; /* triggered relation name */
30 Relation rel; /* triggered relation */
31 HeapTuple rettuple = NULL;
32 TupleDesc tupdesc; /* tuple description */
33 bool isnull;
34 int i;
35
36 if (!CALLED_AS_TRIGGER(fcinfo))
37 /* internal error */
38 elog(ERROR, "not fired by trigger manager");
39 if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
40 /* internal error */
41 elog(ERROR, "must be fired for row");
42 if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))
43 /* internal error */
44 elog(ERROR, "must be fired before event");
45
46 if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
47 rettuple = trigdata->tg_trigtuple;
48 else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
49 rettuple = trigdata->tg_newtuple;
50 else
51 /* internal error */
52 elog(ERROR, "cannot process DELETE events");
53
54 rel = trigdata->tg_relation;
56
57 trigger = trigdata->tg_trigger;
58
59 nargs = trigger->tgnargs;
60 if (nargs <= 0 || nargs % 2 != 0)
61 /* internal error */
62 elog(ERROR, "autoinc (%s): even number gt 0 of arguments was expected", relname);
63
64 args = trigger->tgargs;
65 tupdesc = rel->rd_att;
66
67 chattrs = (int *) palloc(nargs / 2 * sizeof(int));
68 newvals = (Datum *) palloc(nargs / 2 * sizeof(Datum));
69 newnulls = (bool *) palloc(nargs / 2 * sizeof(bool));
70
71 for (i = 0; i < nargs;)
72 {
73 int attnum = SPI_fnumber(tupdesc, args[i]);
74 int32 val;
75 Datum seqname;
76
77 if (attnum <= 0)
79 (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
80 errmsg("\"%s\" has no attribute \"%s\"",
81 relname, args[i])));
82
83 if (SPI_gettypeid(tupdesc, attnum) != INT4OID)
85 (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
86 errmsg("attribute \"%s\" of \"%s\" must be type INT4",
87 args[i], relname)));
88
89 val = DatumGetInt32(SPI_getbinval(rettuple, tupdesc, attnum, &isnull));
90
91 if (!isnull && val != 0)
92 {
93 i += 2;
94 continue;
95 }
96
97 i++;
98 chattrs[chnattrs] = attnum;
99 seqname = CStringGetTextDatum(args[i]);
100 newvals[chnattrs] = DirectFunctionCall1(nextval, seqname);
101 /* nextval now returns int64; coerce down to int32 */
102 newvals[chnattrs] = Int32GetDatum((int32) DatumGetInt64(newvals[chnattrs]));
103 if (DatumGetInt32(newvals[chnattrs]) == 0)
104 {
105 newvals[chnattrs] = DirectFunctionCall1(nextval, seqname);
106 newvals[chnattrs] = Int32GetDatum((int32) DatumGetInt64(newvals[chnattrs]));
107 }
108 newnulls[chnattrs] = false;
109 pfree(DatumGetTextPP(seqname));
110 chnattrs++;
111 i++;
112 }
113
114 if (chnattrs > 0)
115 {
116 rettuple = heap_modify_tuple_by_cols(rettuple, tupdesc,
117 chnattrs, chattrs,
118 newvals, newnulls);
119 }
120
121 pfree(relname);
122 pfree(chattrs);
123 pfree(newvals);
124 pfree(newnulls);
125
126 return PointerGetDatum(rettuple);
127}
#define CStringGetTextDatum(s)
Definition: builtins.h:97
int32_t int32
Definition: c.h:481
Datum nextval(PG_FUNCTION_ARGS)
Definition: sequence.c:593
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
#define DatumGetTextPP(X)
Definition: fmgr.h:292
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:641
HeapTuple heap_modify_tuple_by_cols(HeapTuple tuple, TupleDesc tupleDesc, int nCols, const int *replCols, const Datum *replValues, const bool *replIsnull)
Definition: heaptuple.c:1278
long val
Definition: informix.c:689
int i
Definition: isn.c:72
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:76
void pfree(void *pointer)
Definition: mcxt.c:1521
void * palloc(Size size)
Definition: mcxt.c:1317
int16 attnum
Definition: pg_attribute.h:74
NameData relname
Definition: pg_class.h:38
static int64 DatumGetInt64(Datum X)
Definition: postgres.h:385
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uintptr_t Datum
Definition: postgres.h:64
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:202
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:1175
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
Definition: spi.c:1308
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
Definition: spi.c:1252
char * SPI_getrelname(Relation rel)
Definition: spi.c:1326
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
HeapTuple tg_trigtuple
Definition: trigger.h:36
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, CStringGetTextDatum, DatumGetInt32(), DatumGetInt64(), DatumGetTextPP, DirectFunctionCall1, elog, ereport, errcode(), errmsg(), ERROR, heap_modify_tuple_by_cols(), i, if(), Int32GetDatum(), nextval(), palloc(), pfree(), PointerGetDatum(), relname, SPI_fnumber(), SPI_getbinval(), SPI_getrelname(), SPI_gettypeid(), TriggerData::tg_event, TriggerData::tg_newtuple, TriggerData::tg_relation, TriggerData::tg_trigger, TriggerData::tg_trigtuple, Trigger::tgnargs, TRIGGER_FIRED_BEFORE, TRIGGER_FIRED_BY_INSERT, TRIGGER_FIRED_BY_UPDATE, TRIGGER_FIRED_FOR_ROW, and val.

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( autoinc  )

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 14 of file autoinc.c.