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.

Macros

#define atooid(x)   ((Oid) strtoul((x), NULL, 10))
 

Functions

 PG_FUNCTION_INFO_V1 (lo_manage)
 
Datum lo_manage (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 

Macro Definition Documentation

Function Documentation

Datum lo_manage ( PG_FUNCTION_ARGS  )

Definition at line 26 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.

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