LCOV - code coverage report
Current view: top level - src/backend/access/common - printsimple.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 41 42 97.6 %
Date: 2019-11-13 21:06:57 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * printsimple.c
       4             :  *    Routines to print out tuples containing only a limited range of
       5             :  *    builtin types without catalog access.  This is intended for
       6             :  *    backends that don't have catalog access because they are not bound
       7             :  *    to a specific database, such as some walsender processes.  It
       8             :  *    doesn't handle standalone backends or protocol versions other than
       9             :  *    3.0, because we don't need such handling for current applications.
      10             :  *
      11             :  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
      12             :  * Portions Copyright (c) 1994, Regents of the University of California
      13             :  *
      14             :  * IDENTIFICATION
      15             :  *    src/backend/access/common/printsimple.c
      16             :  *
      17             :  *-------------------------------------------------------------------------
      18             :  */
      19             : #include "postgres.h"
      20             : 
      21             : #include "access/printsimple.h"
      22             : #include "catalog/pg_type.h"
      23             : #include "libpq/pqformat.h"
      24             : #include "utils/builtins.h"
      25             : 
      26             : /*
      27             :  * At startup time, send a RowDescription message.
      28             :  */
      29             : void
      30         858 : printsimple_startup(DestReceiver *self, int operation, TupleDesc tupdesc)
      31             : {
      32             :     StringInfoData buf;
      33             :     int         i;
      34             : 
      35         858 :     pq_beginmessage(&buf, 'T'); /* RowDescription */
      36         858 :     pq_sendint16(&buf, tupdesc->natts);
      37             : 
      38        3312 :     for (i = 0; i < tupdesc->natts; ++i)
      39             :     {
      40        2454 :         Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
      41             : 
      42        2454 :         pq_sendstring(&buf, NameStr(attr->attname));
      43        2454 :         pq_sendint32(&buf, 0);  /* table oid */
      44        2454 :         pq_sendint16(&buf, 0);  /* attnum */
      45        2454 :         pq_sendint32(&buf, (int) attr->atttypid);
      46        2454 :         pq_sendint16(&buf, attr->attlen);
      47        2454 :         pq_sendint32(&buf, attr->atttypmod);
      48        2454 :         pq_sendint16(&buf, 0);  /* format code */
      49             :     }
      50             : 
      51         858 :     pq_endmessage(&buf);
      52         858 : }
      53             : 
      54             : /*
      55             :  * For each tuple, send a DataRow message.
      56             :  */
      57             : bool
      58        2122 : printsimple(TupleTableSlot *slot, DestReceiver *self)
      59             : {
      60        2122 :     TupleDesc   tupdesc = slot->tts_tupleDescriptor;
      61             :     StringInfoData buf;
      62             :     int         i;
      63             : 
      64             :     /* Make sure the tuple is fully deconstructed */
      65        2122 :     slot_getallattrs(slot);
      66             : 
      67             :     /* Prepare and send message */
      68        2122 :     pq_beginmessage(&buf, 'D');
      69        2122 :     pq_sendint16(&buf, tupdesc->natts);
      70             : 
      71        8368 :     for (i = 0; i < tupdesc->natts; ++i)
      72             :     {
      73        6246 :         Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
      74             :         Datum       value;
      75             : 
      76        6246 :         if (slot->tts_isnull[i])
      77             :         {
      78         562 :             pq_sendint32(&buf, -1);
      79         562 :             continue;
      80             :         }
      81             : 
      82        5684 :         value = slot->tts_values[i];
      83             : 
      84             :         /*
      85             :          * We can't call the regular type output functions here because we
      86             :          * might not have catalog access.  Instead, we must hard-wire
      87             :          * knowledge of the required types.
      88             :          */
      89        5684 :         switch (attr->atttypid)
      90             :         {
      91             :             case TEXTOID:
      92             :                 {
      93        5344 :                     text       *t = DatumGetTextPP(value);
      94             : 
      95       21376 :                     pq_sendcountedtext(&buf,
      96        5344 :                                        VARDATA_ANY(t),
      97       16032 :                                        VARSIZE_ANY_EXHDR(t),
      98             :                                        false);
      99             :                 }
     100        5344 :                 break;
     101             : 
     102             :             case INT4OID:
     103             :                 {
     104         330 :                     int32       num = DatumGetInt32(value);
     105             :                     char        str[12];    /* sign, 10 digits and '\0' */
     106             : 
     107         330 :                     pg_ltoa(num, str);
     108         330 :                     pq_sendcountedtext(&buf, str, strlen(str), false);
     109             :                 }
     110         330 :                 break;
     111             : 
     112             :             case INT8OID:
     113             :                 {
     114          10 :                     int64       num = DatumGetInt64(value);
     115             :                     char        str[23];    /* sign, 21 digits and '\0' */
     116             : 
     117          10 :                     pg_lltoa(num, str);
     118          10 :                     pq_sendcountedtext(&buf, str, strlen(str), false);
     119             :                 }
     120          10 :                 break;
     121             : 
     122             :             default:
     123           0 :                 elog(ERROR, "unsupported type OID: %u", attr->atttypid);
     124             :         }
     125             :     }
     126             : 
     127        2122 :     pq_endmessage(&buf);
     128             : 
     129        2122 :     return true;
     130             : }

Generated by: LCOV version 1.13