LCOV - differential code coverage report
Current view: top level - contrib/spi - insert_username.c (source / functions) Coverage Total Hit UBC
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 0.0 % 34 0 34
Current Date: 2024-04-14 14:21:10 Functions: 0.0 % 3 0 3
Baseline: 16@8cea358b128 Branches: 0.0 % 32 0 32
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed (240..) days: 0.0 % 34 0 34
Function coverage date bins:
(240..) days: 0.0 % 3 0 3
Branch coverage date bins:
(240..) days: 0.0 % 32 0 32

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*
                                  2                 :                :  * contrib/spi/insert_username.c
                                  3                 :                :  *
                                  4                 :                :  * insert user name in response to a trigger
                                  5                 :                :  * usage:  insert_username (column_name)
                                  6                 :                :  */
                                  7                 :                : #include "postgres.h"
                                  8                 :                : 
                                  9                 :                : #include "access/htup_details.h"
                                 10                 :                : #include "catalog/pg_type.h"
                                 11                 :                : #include "commands/trigger.h"
                                 12                 :                : #include "executor/spi.h"
                                 13                 :                : #include "miscadmin.h"
                                 14                 :                : #include "utils/builtins.h"
                                 15                 :                : #include "utils/rel.h"
                                 16                 :                : 
 6529 tgl@sss.pgh.pa.us          17                 :UBC           0 : PG_MODULE_MAGIC;
                                 18                 :                : 
 8546                            19                 :              0 : PG_FUNCTION_INFO_V1(insert_username);
                                 20                 :                : 
                                 21                 :                : Datum
 8721                            22                 :              0 : insert_username(PG_FUNCTION_ARGS)
                                 23                 :                : {
                                 24                 :              0 :     TriggerData *trigdata = (TriggerData *) fcinfo->context;
                                 25                 :                :     Trigger    *trigger;        /* to get trigger name */
                                 26                 :                :     int         nargs;          /* # of arguments */
                                 27                 :                :     Datum       newval;         /* new value of column */
                                 28                 :                :     bool        newnull;        /* null flag */
                                 29                 :                :     char      **args;           /* arguments */
                                 30                 :                :     char       *relname;        /* triggered relation name */
                                 31                 :                :     Relation    rel;            /* triggered relation */
 9676 vadim4o@yahoo.com          32                 :              0 :     HeapTuple   rettuple = NULL;
                                 33                 :                :     TupleDesc   tupdesc;        /* tuple description */
                                 34                 :                :     int         attnum;
                                 35                 :                : 
                                 36                 :                :     /* sanity checks from autoinc.c */
 8721 tgl@sss.pgh.pa.us          37   [ #  #  #  # ]:              0 :     if (!CALLED_AS_TRIGGER(fcinfo))
                                 38                 :                :         /* internal error */
                                 39         [ #  # ]:              0 :         elog(ERROR, "insert_username: not fired by trigger manager");
 4937                            40         [ #  # ]:              0 :     if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
                                 41                 :                :         /* internal error */
                                 42         [ #  # ]:              0 :         elog(ERROR, "insert_username: must be fired for row");
                                 43         [ #  # ]:              0 :     if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))
                                 44                 :                :         /* internal error */
 9595 bruce@momjian.us           45         [ #  # ]:              0 :         elog(ERROR, "insert_username: must be fired before event");
                                 46                 :                : 
 8721 tgl@sss.pgh.pa.us          47         [ #  # ]:              0 :     if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
                                 48                 :              0 :         rettuple = trigdata->tg_trigtuple;
                                 49         [ #  # ]:              0 :     else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
                                 50                 :              0 :         rettuple = trigdata->tg_newtuple;
                                 51                 :                :     else
                                 52                 :                :         /* internal error */
 6282 bruce@momjian.us           53         [ #  # ]:              0 :         elog(ERROR, "insert_username: cannot process DELETE events");
                                 54                 :                : 
 8721 tgl@sss.pgh.pa.us          55                 :              0 :     rel = trigdata->tg_relation;
 9676 vadim4o@yahoo.com          56                 :              0 :     relname = SPI_getrelname(rel);
                                 57                 :                : 
 8721 tgl@sss.pgh.pa.us          58                 :              0 :     trigger = trigdata->tg_trigger;
                                 59                 :                : 
 9676 vadim4o@yahoo.com          60                 :              0 :     nargs = trigger->tgnargs;
                                 61         [ #  # ]:              0 :     if (nargs != 1)
                                 62                 :                :         /* internal error */
 9595 bruce@momjian.us           63         [ #  # ]:              0 :         elog(ERROR, "insert_username (%s): one argument was expected", relname);
                                 64                 :                : 
 9676 vadim4o@yahoo.com          65                 :              0 :     args = trigger->tgargs;
                                 66                 :              0 :     tupdesc = rel->rd_att;
                                 67                 :                : 
 9544 bruce@momjian.us           68                 :              0 :     attnum = SPI_fnumber(tupdesc, args[0]);
                                 69                 :                : 
 2714 tgl@sss.pgh.pa.us          70         [ #  # ]:              0 :     if (attnum <= 0)
 7570                            71         [ #  # ]:              0 :         ereport(ERROR,
                                 72                 :                :                 (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
                                 73                 :                :                  errmsg("\"%s\" has no attribute \"%s\"", relname, args[0])));
                                 74                 :                : 
 9544 bruce@momjian.us           75         [ #  # ]:              0 :     if (SPI_gettypeid(tupdesc, attnum) != TEXTOID)
 7570 tgl@sss.pgh.pa.us          76         [ #  # ]:              0 :         ereport(ERROR,
                                 77                 :                :                 (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
                                 78                 :                :                  errmsg("attribute \"%s\" of \"%s\" must be type TEXT",
                                 79                 :                :                         args[0], relname)));
                                 80                 :                : 
                                 81                 :                :     /* create fields containing name */
 3263 andrew@dunslane.net        82                 :              0 :     newval = CStringGetTextDatum(GetUserNameFromId(GetUserId(), false));
 2714 tgl@sss.pgh.pa.us          83                 :              0 :     newnull = false;
                                 84                 :                : 
                                 85                 :                :     /* construct new tuple */
                                 86                 :              0 :     rettuple = heap_modify_tuple_by_cols(rettuple, tupdesc,
                                 87                 :                :                                          1, &attnum, &newval, &newnull);
                                 88                 :                : 
 9544 bruce@momjian.us           89                 :              0 :     pfree(relname);
                                 90                 :                : 
 8721 tgl@sss.pgh.pa.us          91                 :              0 :     return PointerGetDatum(rettuple);
                                 92                 :                : }
        

Generated by: LCOV version 2.1-beta2-3-g6141622