LCOV - differential code coverage report
Current view: top level - src/backend/tcop - cmdtag.c (source / functions) Coverage Total Hit UNC UIC GBC GIC GNC CBC ECB
Current: Differential Code Coverage HEAD vs 15 Lines: 97.7 % 44 43 22 1 1 17 18 7 14
Current Date: 2023-04-08 15:15:32 Functions: 100.0 % 8 8 6 2 8
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

           TLA  Line data    Source code
       1                 : /*-------------------------------------------------------------------------
       2                 :  *
       3                 :  * cmdtag.c
       4                 :  *    Data and routines for commandtag names and enumeration.
       5                 :  *
       6                 :  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
       7                 :  * Portions Copyright (c) 1994, Regents of the University of California
       8                 :  *
       9                 :  * IDENTIFICATION
      10                 :  *    src/backend/tcop/cmdtag.c
      11                 :  *
      12                 :  *-------------------------------------------------------------------------
      13                 :  */
      14                 : #include "postgres.h"
      15                 : 
      16                 : #include "miscadmin.h"
      17                 : #include "tcop/cmdtag.h"
      18                 : #include "utils/builtins.h"
      19                 : 
      20                 : 
      21                 : typedef struct CommandTagBehavior
      22                 : {
      23                 :     const char *name;           /* tag name, e.g. "SELECT" */
      24                 :     const uint8 namelen;        /* set to strlen(name) */
      25                 :     const bool  event_trigger_ok;
      26                 :     const bool  table_rewrite_ok;
      27                 :     const bool  display_rowcount;   /* should the number of rows affected be
      28                 :                                      * shown in the command completion string */
      29                 : } CommandTagBehavior;
      30                 : 
      31                 : #define PG_CMDTAG(tag, name, evtrgok, rwrok, rowcnt) \
      32                 :     { name, (uint8) (sizeof(name) - 1), evtrgok, rwrok, rowcnt },
      33                 : 
      34                 : static const CommandTagBehavior tag_behavior[COMMAND_TAG_NEXTTAG] = {
      35                 : #include "tcop/cmdtaglist.h"
      36                 : };
      37                 : 
      38                 : #undef PG_CMDTAG
      39                 : 
      40                 : void
      41 GIC      516242 : InitializeQueryCompletion(QueryCompletion *qc)
      42                 : {
      43          516242 :     qc->commandTag = CMDTAG_UNKNOWN;
      44 CBC      516242 :     qc->nprocessed = 0;
      45 GIC      516242 : }
      46 ECB             : 
      47                 : const char *
      48 CBC        5768 : GetCommandTagName(CommandTag commandTag)
      49                 : {
      50 GIC        5768 :     return tag_behavior[commandTag].name;
      51 ECB             : }
      52                 : 
      53                 : const char *
      54 GNC      729542 : GetCommandTagNameAndLen(CommandTag commandTag, Size *len)
      55                 : {
      56          729542 :     *len = (Size) tag_behavior[commandTag].namelen;
      57          729542 :     return tag_behavior[commandTag].name;
      58                 : }
      59                 : 
      60 ECB             : bool
      61 GIC      237052 : command_tag_display_rowcount(CommandTag commandTag)
      62                 : {
      63          237052 :     return tag_behavior[commandTag].display_rowcount;
      64 ECB             : }
      65                 : 
      66                 : bool
      67 CBC       61274 : command_tag_event_trigger_ok(CommandTag commandTag)
      68                 : {
      69 GIC       61274 :     return tag_behavior[commandTag].event_trigger_ok;
      70                 : }
      71 ECB             : 
      72                 : bool
      73 CBC          41 : command_tag_table_rewrite_ok(CommandTag commandTag)
      74                 : {
      75 GIC          41 :     return tag_behavior[commandTag].table_rewrite_ok;
      76                 : }
      77 ECB             : 
      78                 : /*
      79                 :  * Search CommandTag by name
      80                 :  *
      81                 :  * Returns CommandTag, or CMDTAG_UNKNOWN if not recognized
      82                 :  */
      83                 : CommandTag
      84 GIC         156 : GetCommandTagEnum(const char *commandname)
      85 ECB             : {
      86                 :     const CommandTagBehavior *base,
      87                 :                *last,
      88                 :                *position;
      89                 :     int         result;
      90                 : 
      91 GIC         156 :     if (commandname == NULL || *commandname == '\0')
      92 UIC           0 :         return CMDTAG_UNKNOWN;
      93                 : 
      94 CBC         156 :     base = tag_behavior;
      95 GIC         156 :     last = tag_behavior + lengthof(tag_behavior) - 1;
      96            1176 :     while (last >= base)
      97                 :     {
      98            1170 :         position = base + ((last - base) >> 1);
      99            1170 :         result = pg_strcasecmp(commandname, position->name);
     100            1170 :         if (result == 0)
     101 CBC         150 :             return (CommandTag) (position - tag_behavior);
     102 GBC        1020 :         else if (result < 0)
     103 GIC         345 :             last = position - 1;
     104 ECB             :         else
     105 CBC         675 :             base = position + 1;
     106 ECB             :     }
     107 GIC           6 :     return CMDTAG_UNKNOWN;
     108 ECB             : }
     109                 : 
     110                 : /*
     111                 :  * BuildQueryCompletionString
     112                 :  *      Build a string containing the command tag name with the
     113                 :  *      QueryCompletion's nprocessed for command tags with display_rowcount
     114                 :  *      set.  Returns the strlen of the constructed string.
     115                 :  *
     116                 :  * The caller must ensure that buff is at least COMPLETION_TAG_BUFSIZE bytes.
     117                 :  *
     118                 :  * If nameonly is true, then the constructed string will contain only the tag
     119                 :  * name.
     120                 :  */
     121                 : Size
     122 GNC      237052 : BuildQueryCompletionString(char *buff, const QueryCompletion *qc,
     123                 :                            bool nameonly)
     124                 : {
     125          237052 :     CommandTag  tag = qc->commandTag;
     126                 :     Size        taglen;
     127          237052 :     const char *tagname = GetCommandTagNameAndLen(tag, &taglen);
     128                 :     char       *bufp;
     129                 : 
     130                 :     /*
     131                 :      * We assume the tagname is plain ASCII and therefore requires no encoding
     132                 :      * conversion.
     133                 :      */
     134          237052 :     memcpy(buff, tagname, taglen);
     135          237052 :     bufp = buff + taglen;
     136                 : 
     137                 :     /* ensure that the tagname isn't long enough to overrun the buffer */
     138          237052 :     Assert(taglen <= COMPLETION_TAG_BUFSIZE - MAXINT8LEN - 4);
     139                 : 
     140                 :     /*
     141                 :      * In PostgreSQL versions 11 and earlier, it was possible to create a
     142                 :      * table WITH OIDS.  When inserting into such a table, INSERT used to
     143                 :      * include the Oid of the inserted record in the completion tag.  To
     144                 :      * maintain compatibility in the wire protocol, we now write a "0" (for
     145                 :      * InvalidOid) in the location where we once wrote the new record's Oid.
     146                 :      */
     147          237052 :     if (command_tag_display_rowcount(tag) && !nameonly)
     148                 :     {
     149          140906 :         if (tag == CMDTAG_INSERT)
     150                 :         {
     151           24522 :             *bufp++ = ' ';
     152           24522 :             *bufp++ = '0';
     153                 :         }
     154          140906 :         *bufp++ = ' ';
     155          140906 :         bufp += pg_ulltoa_n(qc->nprocessed, bufp);
     156                 :     }
     157                 : 
     158                 :     /* and finally, NUL terminate the string */
     159          237052 :     *bufp = '\0';
     160                 : 
     161          237052 :     Assert((bufp - buff) == strlen(buff));
     162                 : 
     163          237052 :     return bufp - buff;
     164                 : }
        

Generated by: LCOV version v1.16-55-g56c0a2a