LCOV - code coverage report
Current view: top level - contrib/lo - lo.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 32 36 88.9 %
Date: 2019-09-19 17:07:13 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  *  PostgreSQL definitions for managed Large Objects.
       3             :  *
       4             :  *  contrib/lo/lo.c
       5             :  *
       6             :  */
       7             : 
       8             : #include "postgres.h"
       9             : 
      10             : #include "commands/trigger.h"
      11             : #include "executor/spi.h"
      12             : #include "utils/builtins.h"
      13             : #include "utils/rel.h"
      14             : 
      15           2 : PG_MODULE_MAGIC;
      16             : 
      17             : 
      18             : /*
      19             :  * This is the trigger that protects us from orphaned large objects
      20             :  */
      21           4 : PG_FUNCTION_INFO_V1(lo_manage);
      22             : 
      23             : Datum
      24           4 : lo_manage(PG_FUNCTION_ARGS)
      25             : {
      26           4 :     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           4 :     if (!CALLED_AS_TRIGGER(fcinfo)) /* internal error */
      36           0 :         elog(ERROR, "%s: not fired by trigger manager",
      37             :              trigdata->tg_trigger->tgname);
      38             : 
      39           4 :     if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event)) /* internal error */
      40           0 :         elog(ERROR, "%s: must be fired for row",
      41             :              trigdata->tg_trigger->tgname);
      42             : 
      43             :     /*
      44             :      * Fetch some values from trigdata
      45             :      */
      46           4 :     newtuple = trigdata->tg_newtuple;
      47           4 :     trigtuple = trigdata->tg_trigtuple;
      48           4 :     tupdesc = trigdata->tg_relation->rd_att;
      49           4 :     args = trigdata->tg_trigger->tgargs;
      50             : 
      51           4 :     if (args == NULL)           /* internal error */
      52           0 :         elog(ERROR, "%s: no column name provided in the trigger definition",
      53             :              trigdata->tg_trigger->tgname);
      54             : 
      55             :     /* tuple to return to Executor */
      56           4 :     if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
      57           2 :         rettuple = newtuple;
      58             :     else
      59           2 :         rettuple = trigtuple;
      60             : 
      61             :     /* Are we deleting the row? */
      62           4 :     isdelete = TRIGGER_FIRED_BY_DELETE(trigdata->tg_event);
      63             : 
      64             :     /* Get the column we're interested in */
      65           4 :     attnum = SPI_fnumber(tupdesc, args[0]);
      66             : 
      67           4 :     if (attnum <= 0)
      68           0 :         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           4 :     if (newtuple != NULL)
      78             :     {
      79           2 :         char       *orig = SPI_getvalue(trigtuple, tupdesc, attnum);
      80           2 :         char       *newv = SPI_getvalue(newtuple, tupdesc, attnum);
      81             : 
      82           2 :         if (orig != NULL && (newv == NULL || strcmp(orig, newv) != 0))
      83           2 :             DirectFunctionCall1(be_lo_unlink,
      84             :                                 ObjectIdGetDatum(atooid(orig)));
      85             : 
      86           2 :         if (newv)
      87           2 :             pfree(newv);
      88           2 :         if (orig)
      89           2 :             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           4 :     if (isdelete)
      98             :     {
      99           2 :         char       *orig = SPI_getvalue(trigtuple, tupdesc, attnum);
     100             : 
     101           2 :         if (orig != NULL)
     102             :         {
     103           2 :             DirectFunctionCall1(be_lo_unlink,
     104             :                                 ObjectIdGetDatum(atooid(orig)));
     105             : 
     106           2 :             pfree(orig);
     107             :         }
     108             :     }
     109             : 
     110           4 :     return PointerGetDatum(rettuple);
     111             : }

Generated by: LCOV version 1.13