LCOV - code coverage report
Current view: top level - src/backend/utils/adt - quote.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 36 36 100.0 %
Date: 2024-04-26 14:11:25 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * quote.c
       4             :  *    Functions for quoting identifiers and literals
       5             :  *
       6             :  * Portions Copyright (c) 2000-2024, PostgreSQL Global Development Group
       7             :  *
       8             :  *
       9             :  * IDENTIFICATION
      10             :  *    src/backend/utils/adt/quote.c
      11             :  *
      12             :  *-------------------------------------------------------------------------
      13             :  */
      14             : #include "postgres.h"
      15             : 
      16             : #include "utils/builtins.h"
      17             : #include "varatt.h"
      18             : 
      19             : 
      20             : /*
      21             :  * quote_ident -
      22             :  *    returns a properly quoted identifier
      23             :  */
      24             : Datum
      25        7540 : quote_ident(PG_FUNCTION_ARGS)
      26             : {
      27        7540 :     text       *t = PG_GETARG_TEXT_PP(0);
      28             :     const char *qstr;
      29             :     char       *str;
      30             : 
      31        7540 :     str = text_to_cstring(t);
      32        7540 :     qstr = quote_identifier(str);
      33        7540 :     PG_RETURN_TEXT_P(cstring_to_text(qstr));
      34             : }
      35             : 
      36             : /*
      37             :  * quote_literal_internal -
      38             :  *    helper function for quote_literal and quote_literal_cstr
      39             :  *
      40             :  * NOTE: think not to make this function's behavior change with
      41             :  * standard_conforming_strings.  We don't know where the result
      42             :  * literal will be used, and so we must generate a result that
      43             :  * will work with either setting.  Take a look at what dblink
      44             :  * uses this for before thinking you know better.
      45             :  */
      46             : static size_t
      47        9796 : quote_literal_internal(char *dst, const char *src, size_t len)
      48             : {
      49             :     const char *s;
      50        9796 :     char       *savedst = dst;
      51             : 
      52      703594 :     for (s = src; s < src + len; s++)
      53             :     {
      54      693808 :         if (*s == '\\')
      55             :         {
      56          10 :             *dst++ = ESCAPE_STRING_SYNTAX;
      57          10 :             break;
      58             :         }
      59             :     }
      60             : 
      61        9796 :     *dst++ = '\'';
      62      703608 :     while (len-- > 0)
      63             :     {
      64      693812 :         if (SQL_STR_DOUBLE(*src, true))
      65          44 :             *dst++ = *src;
      66      693812 :         *dst++ = *src++;
      67             :     }
      68        9796 :     *dst++ = '\'';
      69             : 
      70        9796 :     return dst - savedst;
      71             : }
      72             : 
      73             : /*
      74             :  * quote_literal -
      75             :  *    returns a properly quoted literal
      76             :  */
      77             : Datum
      78        3524 : quote_literal(PG_FUNCTION_ARGS)
      79             : {
      80        3524 :     text       *t = PG_GETARG_TEXT_PP(0);
      81             :     text       *result;
      82             :     char       *cp1;
      83             :     char       *cp2;
      84             :     int         len;
      85             : 
      86        3524 :     len = VARSIZE_ANY_EXHDR(t);
      87             :     /* We make a worst-case result area; wasting a little space is OK */
      88        3524 :     result = (text *) palloc(len * 2 + 3 + VARHDRSZ);
      89             : 
      90        3524 :     cp1 = VARDATA_ANY(t);
      91        3524 :     cp2 = VARDATA(result);
      92             : 
      93        3524 :     SET_VARSIZE(result, VARHDRSZ + quote_literal_internal(cp2, cp1, len));
      94             : 
      95        3524 :     PG_RETURN_TEXT_P(result);
      96             : }
      97             : 
      98             : /*
      99             :  * quote_literal_cstr -
     100             :  *    returns a properly quoted literal
     101             :  */
     102             : char *
     103        6272 : quote_literal_cstr(const char *rawstr)
     104             : {
     105             :     char       *result;
     106             :     int         len;
     107             :     int         newlen;
     108             : 
     109        6272 :     len = strlen(rawstr);
     110             :     /* We make a worst-case result area; wasting a little space is OK */
     111        6272 :     result = palloc(len * 2 + 3 + 1);
     112             : 
     113        6272 :     newlen = quote_literal_internal(result, rawstr, len);
     114        6272 :     result[newlen] = '\0';
     115             : 
     116        6272 :     return result;
     117             : }
     118             : 
     119             : /*
     120             :  * quote_nullable -
     121             :  *    Returns a properly quoted literal, with null values returned
     122             :  *    as the text string 'NULL'.
     123             :  */
     124             : Datum
     125        1580 : quote_nullable(PG_FUNCTION_ARGS)
     126             : {
     127        1580 :     if (PG_ARGISNULL(0))
     128          84 :         PG_RETURN_TEXT_P(cstring_to_text("NULL"));
     129             :     else
     130        1496 :         PG_RETURN_DATUM(DirectFunctionCall1(quote_literal,
     131             :                                             PG_GETARG_DATUM(0)));
     132             : }

Generated by: LCOV version 1.14