LCOV - differential code coverage report
Current view: top level - contrib/spi - autoinc.c (source / functions) Coverage Total Hit UBC CBC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 87.7 % 57 50 7 50
Current Date: 2025-09-06 07:49:51 +0900 Functions: 100.0 % 3 3 3
Baseline: lcov-20250906-005545-baseline Branches: 47.7 % 44 21 23 21
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 100.0 % 1 1 1
(360..) days: 87.5 % 56 49 7 49
Function coverage date bins:
(30,360] days: 100.0 % 1 1 1
(360..) days: 100.0 % 2 2 2
Branch coverage date bins:
(360..) days: 47.7 % 44 21 23 21

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*
                                  2                 :                :  * contrib/spi/autoinc.c
                                  3                 :                :  */
                                  4                 :                : #include "postgres.h"
                                  5                 :                : 
                                  6                 :                : #include "access/htup_details.h"
                                  7                 :                : #include "catalog/pg_type.h"
                                  8                 :                : #include "commands/sequence.h"
                                  9                 :                : #include "commands/trigger.h"
                                 10                 :                : #include "executor/spi.h"
                                 11                 :                : #include "utils/builtins.h"
                                 12                 :                : #include "utils/rel.h"
                                 13                 :                : 
  164 tgl@sss.pgh.pa.us          14                 :CBC           1 : PG_MODULE_MAGIC_EXT(
                                 15                 :                :                     .name = "autoinc",
                                 16                 :                :                     .version = PG_VERSION
                                 17                 :                : );
                                 18                 :                : 
 9056                            19                 :              2 : PG_FUNCTION_INFO_V1(autoinc);
                                 20                 :                : 
                                 21                 :                : Datum
 9231                            22                 :             12 : autoinc(PG_FUNCTION_ARGS)
                                 23                 :                : {
                                 24                 :             12 :     TriggerData *trigdata = (TriggerData *) fcinfo->context;
                                 25                 :                :     Trigger    *trigger;        /* to get trigger name */
                                 26                 :                :     int         nargs;          /* # of arguments */
                                 27                 :                :     int        *chattrs;        /* attnums of attributes to change */
10201 vadim4o@yahoo.com          28                 :             12 :     int         chnattrs = 0;   /* # of above */
                                 29                 :                :     Datum      *newvals;        /* vals of above */
                                 30                 :                :     bool       *newnulls;       /* null flags for above */
                                 31                 :                :     char      **args;           /* arguments */
                                 32                 :                :     char       *relname;        /* triggered relation name */
                                 33                 :                :     Relation    rel;            /* triggered relation */
                                 34                 :             12 :     HeapTuple   rettuple = NULL;
                                 35                 :                :     TupleDesc   tupdesc;        /* tuple description */
                                 36                 :                :     bool        isnull;
                                 37                 :                :     int         i;
                                 38                 :                : 
 9231 tgl@sss.pgh.pa.us          39   [ +  -  -  + ]:             12 :     if (!CALLED_AS_TRIGGER(fcinfo))
                                 40                 :                :         /* internal error */
 8080 tgl@sss.pgh.pa.us          41         [ #  # ]:UBC           0 :         elog(ERROR, "not fired by trigger manager");
 5447 tgl@sss.pgh.pa.us          42         [ -  + ]:CBC          12 :     if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
                                 43                 :                :         /* internal error */
 5447 tgl@sss.pgh.pa.us          44         [ #  # ]:UBC           0 :         elog(ERROR, "must be fired for row");
 5447 tgl@sss.pgh.pa.us          45         [ -  + ]:CBC          12 :     if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))
                                 46                 :                :         /* internal error */
 8080 tgl@sss.pgh.pa.us          47         [ #  # ]:UBC           0 :         elog(ERROR, "must be fired before event");
                                 48                 :                : 
 9231 tgl@sss.pgh.pa.us          49         [ +  + ]:CBC          12 :     if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
                                 50                 :              3 :         rettuple = trigdata->tg_trigtuple;
                                 51         [ +  - ]:              9 :     else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
                                 52                 :              9 :         rettuple = trigdata->tg_newtuple;
                                 53                 :                :     else
                                 54                 :                :         /* internal error */
 6792 bruce@momjian.us           55         [ #  # ]:UBC           0 :         elog(ERROR, "cannot process DELETE events");
                                 56                 :                : 
 9231 tgl@sss.pgh.pa.us          57                 :CBC          12 :     rel = trigdata->tg_relation;
10201 vadim4o@yahoo.com          58                 :             12 :     relname = SPI_getrelname(rel);
                                 59                 :                : 
 9231 tgl@sss.pgh.pa.us          60                 :             12 :     trigger = trigdata->tg_trigger;
                                 61                 :                : 
10201 vadim4o@yahoo.com          62                 :             12 :     nargs = trigger->tgnargs;
                                 63   [ +  -  -  + ]:             12 :     if (nargs <= 0 || nargs % 2 != 0)
                                 64                 :                :         /* internal error */
10105 bruce@momjian.us           65         [ #  # ]:UBC           0 :         elog(ERROR, "autoinc (%s): even number gt 0 of arguments was expected", relname);
                                 66                 :                : 
10201 vadim4o@yahoo.com          67                 :CBC          12 :     args = trigger->tgargs;
                                 68                 :             12 :     tupdesc = rel->rd_att;
                                 69                 :                : 
10054 bruce@momjian.us           70                 :             12 :     chattrs = (int *) palloc(nargs / 2 * sizeof(int));
                                 71                 :             12 :     newvals = (Datum *) palloc(nargs / 2 * sizeof(Datum));
 3224 tgl@sss.pgh.pa.us          72                 :             12 :     newnulls = (bool *) palloc(nargs / 2 * sizeof(bool));
                                 73                 :                : 
10054 bruce@momjian.us           74         [ +  + ]:             24 :     for (i = 0; i < nargs;)
                                 75                 :                :     {
                                 76                 :             12 :         int         attnum = SPI_fnumber(tupdesc, args[i]);
                                 77                 :                :         int32       val;
                                 78                 :                :         Datum       seqname;
                                 79                 :                : 
 3224 tgl@sss.pgh.pa.us          80         [ -  + ]:             12 :         if (attnum <= 0)
 8080 tgl@sss.pgh.pa.us          81         [ #  # ]:UBC           0 :             ereport(ERROR,
                                 82                 :                :                     (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
                                 83                 :                :                      errmsg("\"%s\" has no attribute \"%s\"",
                                 84                 :                :                             relname, args[i])));
                                 85                 :                : 
10054 bruce@momjian.us           86         [ -  + ]:CBC          12 :         if (SPI_gettypeid(tupdesc, attnum) != INT4OID)
 8080 tgl@sss.pgh.pa.us          87         [ #  # ]:UBC           0 :             ereport(ERROR,
                                 88                 :                :                     (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
                                 89                 :                :                      errmsg("attribute \"%s\" of \"%s\" must be type INT4",
                                 90                 :                :                             args[i], relname)));
                                 91                 :                : 
10054 bruce@momjian.us           92                 :CBC          12 :         val = DatumGetInt32(SPI_getbinval(rettuple, tupdesc, attnum, &isnull));
                                 93                 :                : 
10201 vadim4o@yahoo.com          94   [ +  +  +  + ]:             12 :         if (!isnull && val != 0)
                                 95                 :                :         {
                                 96                 :              4 :             i += 2;
                                 97                 :              4 :             continue;
                                 98                 :                :         }
                                 99                 :                : 
                                100                 :              8 :         i++;
                                101                 :              8 :         chattrs[chnattrs] = attnum;
 6374 tgl@sss.pgh.pa.us         102                 :              8 :         seqname = CStringGetTextDatum(args[i]);
 9194                           103                 :              8 :         newvals[chnattrs] = DirectFunctionCall1(nextval, seqname);
                                104                 :                :         /* nextval now returns int64; coerce down to int32 */
 8787                           105                 :              8 :         newvals[chnattrs] = Int32GetDatum((int32) DatumGetInt64(newvals[chnattrs]));
10054 bruce@momjian.us          106         [ +  + ]:              8 :         if (DatumGetInt32(newvals[chnattrs]) == 0)
                                107                 :                :         {
 9194 tgl@sss.pgh.pa.us         108                 :              1 :             newvals[chnattrs] = DirectFunctionCall1(nextval, seqname);
 8787                           109                 :              1 :             newvals[chnattrs] = Int32GetDatum((int32) DatumGetInt64(newvals[chnattrs]));
                                110                 :                :         }
 3224                           111                 :              8 :         newnulls[chnattrs] = false;
 3100 noah@leadboat.com         112                 :              8 :         pfree(DatumGetTextPP(seqname));
10201 vadim4o@yahoo.com         113                 :              8 :         chnattrs++;
                                114                 :              8 :         i++;
                                115                 :                :     }
                                116                 :                : 
                                117         [ +  + ]:             12 :     if (chnattrs > 0)
                                118                 :                :     {
 3224 tgl@sss.pgh.pa.us         119                 :              8 :         rettuple = heap_modify_tuple_by_cols(rettuple, tupdesc,
                                120                 :                :                                              chnattrs, chattrs,
                                121                 :                :                                              newvals, newnulls);
                                122                 :                :     }
                                123                 :                : 
10054 bruce@momjian.us          124                 :             12 :     pfree(relname);
                                125                 :             12 :     pfree(chattrs);
                                126                 :             12 :     pfree(newvals);
 3224 tgl@sss.pgh.pa.us         127                 :             12 :     pfree(newnulls);
                                128                 :                : 
 9231                           129                 :             12 :     return PointerGetDatum(rettuple);
                                130                 :                : }
        

Generated by: LCOV version 2.4-beta