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

Go to the source code of this file.

Functions

 PG_FUNCTION_INFO_V1 (lo_manage)
 
Datum lo_manage (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 

Function Documentation

Datum lo_manage ( PG_FUNCTION_ARGS  )

Definition at line 24 of file lo.c.

References generate_unaccent_rules::args, atooid, be_lo_unlink(), CALLED_AS_TRIGGER, DirectFunctionCall1, elog, ERROR, NULL, ObjectIdGetDatum, pfree(), PointerGetDatum, RelationData::rd_att, SPI_fnumber(), SPI_getvalue(), TriggerData::tg_event, TriggerData::tg_newtuple, TriggerData::tg_relation, TriggerData::tg_trigger, TriggerData::tg_trigtuple, Trigger::tgargs, Trigger::tgname, TRIGGER_FIRED_BY_DELETE, TRIGGER_FIRED_BY_UPDATE, and TRIGGER_FIRED_FOR_ROW.

25 {
26  TriggerData *trigdata = (TriggerData *) fcinfo->context;
27  int attnum; /* attribute number to monitor */
28  char **args; /* Args containing attr name */
29  TupleDesc tupdesc; /* Tuple Descriptor */
30  HeapTuple rettuple; /* Tuple to be returned */
31  bool isdelete; /* are we deleting? */
32  HeapTuple newtuple; /* The new value for tuple */
33  HeapTuple trigtuple; /* The original value of tuple */
34 
35  if (!CALLED_AS_TRIGGER(fcinfo)) /* internal error */
36  elog(ERROR, "%s: not fired by trigger manager",
37  trigdata->tg_trigger->tgname);
38 
39  if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event)) /* internal error */
40  elog(ERROR, "%s: must be fired for row",
41  trigdata->tg_trigger->tgname);
42 
43  /*
44  * Fetch some values from trigdata
45  */
46  newtuple = trigdata->tg_newtuple;
47  trigtuple = trigdata->tg_trigtuple;
48  tupdesc = trigdata->tg_relation->rd_att;
49  args = trigdata->tg_trigger->tgargs;
50 
51  if (args == NULL) /* internal error */
52  elog(ERROR, "%s: no column name provided in the trigger definition",
53  trigdata->tg_trigger->tgname);
54 
55  /* tuple to return to Executor */
56  if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
57  rettuple = newtuple;
58  else
59  rettuple = trigtuple;
60 
61  /* Are we deleting the row? */
62  isdelete = TRIGGER_FIRED_BY_DELETE(trigdata->tg_event);
63 
64  /* Get the column we're interested in */
65  attnum = SPI_fnumber(tupdesc, args[0]);
66 
67  if (attnum <= 0)
68  elog(ERROR, "%s: column \"%s\" does not exist",
69  trigdata->tg_trigger->tgname, args[0]);
70 
71  /*
72  * Handle updates
73  *
74  * Here, if the value of the monitored attribute changes, then the large
75  * object associated with the original value is unlinked.
76  */
77  if (newtuple != NULL)
78  {
79  char *orig = SPI_getvalue(trigtuple, tupdesc, attnum);
80  char *newv = SPI_getvalue(newtuple, tupdesc, attnum);
81 
82  if (orig != NULL && (newv == NULL || strcmp(orig, newv) != 0))
84  ObjectIdGetDatum(atooid(orig)));
85 
86  if (newv)
87  pfree(newv);
88  if (orig)
89  pfree(orig);
90  }
91 
92  /*
93  * Handle deleting of rows
94  *
95  * Here, we unlink the large object associated with the managed attribute
96  */
97  if (isdelete)
98  {
99  char *orig = SPI_getvalue(trigtuple, tupdesc, attnum);
100 
101  if (orig != NULL)
102  {
104  ObjectIdGetDatum(atooid(orig)));
105 
106  pfree(orig);
107  }
108  }
109 
110  return PointerGetDatum(rettuple);
111 }
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:761
#define PointerGetDatum(X)
Definition: postgres.h:562
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
HeapTuple tg_trigtuple
Definition: trigger.h:35
char * SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
Definition: spi.c:804
void pfree(void *pointer)
Definition: mcxt.c:950
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
char * tgname
Definition: reltrigger.h:27
#define atooid(x)
Definition: postgres_ext.h:42
char ** tgargs
Definition: reltrigger.h:40
#define TRIGGER_FIRED_BY_DELETE(event)
Definition: trigger.h:73
Trigger * tg_trigger
Definition: trigger.h:37
TupleDesc rd_att
Definition: rel.h:115
HeapTuple tg_newtuple
Definition: trigger.h:36
Datum be_lo_unlink(PG_FUNCTION_ARGS)
Definition: be-fsstubs.c:341
#define NULL
Definition: c.h:229
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:25
TriggerEvent tg_event
Definition: trigger.h:33
#define elog
Definition: elog.h:219
#define TRIGGER_FIRED_FOR_ROW(event)
Definition: trigger.h:82
#define TRIGGER_FIRED_BY_UPDATE(event)
Definition: trigger.h:76
Relation tg_relation
Definition: trigger.h:34
PG_FUNCTION_INFO_V1 ( lo_manage  )

Variable Documentation

PG_MODULE_MAGIC

Definition at line 15 of file lo.c.