Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * quote.c
4 : : * Functions for quoting identifiers and literals
5 : : *
6 : : * Portions Copyright (c) 2000-2025, 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
9132 JanWieck@Yahoo.com 25 :CBC 4363 : quote_ident(PG_FUNCTION_ARGS)
26 : : {
6374 tgl@sss.pgh.pa.us 27 : 4363 : text *t = PG_GETARG_TEXT_PP(0);
28 : : const char *qstr;
29 : : char *str;
30 : :
31 : 4363 : str = text_to_cstring(t);
7474 32 : 4363 : qstr = quote_identifier(str);
6374 33 : 4363 : 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
5310 peter_e@gmx.net 47 : 4716 : quote_literal_internal(char *dst, const char *src, size_t len)
48 : : {
49 : : const char *s;
5404 rhaas@postgresql.org 50 : 4716 : char *savedst = dst;
51 : :
52 [ + + ]: 349414 : for (s = src; s < src + len; s++)
53 : : {
54 [ + + ]: 344703 : if (*s == '\\')
55 : : {
56 : 5 : *dst++ = ESCAPE_STRING_SYNTAX;
57 : 5 : break;
58 : : }
59 : : }
60 : :
61 : 4716 : *dst++ = '\'';
62 [ + + ]: 349421 : while (len-- > 0)
63 : : {
64 [ + + + + ]: 344705 : if (SQL_STR_DOUBLE(*src, true))
65 : 22 : *dst++ = *src;
66 : 344705 : *dst++ = *src++;
67 : : }
68 : 4716 : *dst++ = '\'';
69 : :
70 : 4716 : return dst - savedst;
71 : : }
72 : :
73 : : /*
74 : : * quote_literal -
75 : : * returns a properly quoted literal
76 : : */
77 : : Datum
9132 JanWieck@Yahoo.com 78 : 1778 : quote_literal(PG_FUNCTION_ARGS)
79 : : {
3100 noah@leadboat.com 80 : 1778 : text *t = PG_GETARG_TEXT_PP(0);
81 : : text *result;
82 : : char *cp1;
83 : : char *cp2;
84 : : int len;
85 : :
86 [ - + - - : 1778 : len = VARSIZE_ANY_EXHDR(t);
- - - - +
+ ]
87 : : /* We make a worst-case result area; wasting a little space is OK */
7371 bruce@momjian.us 88 : 1778 : result = (text *) palloc(len * 2 + 3 + VARHDRSZ);
89 : :
3100 noah@leadboat.com 90 [ + + ]: 1778 : cp1 = VARDATA_ANY(t);
9132 JanWieck@Yahoo.com 91 : 1778 : cp2 = VARDATA(result);
92 : :
5404 rhaas@postgresql.org 93 : 1778 : SET_VARSIZE(result, VARHDRSZ + quote_literal_internal(cp2, cp1, len));
94 : :
95 : 1778 : PG_RETURN_TEXT_P(result);
96 : : }
97 : :
98 : : /*
99 : : * quote_literal_cstr -
100 : : * returns a properly quoted literal
101 : : */
102 : : char *
5310 peter_e@gmx.net 103 : 2938 : quote_literal_cstr(const char *rawstr)
104 : : {
105 : : char *result;
106 : : int len;
107 : : int newlen;
108 : :
5404 rhaas@postgresql.org 109 : 2938 : len = strlen(rawstr);
110 : : /* We make a worst-case result area; wasting a little space is OK */
152 michael@paquier.xyz 111 : 2938 : result = palloc(
112 : : (len * 2) /* doubling for every character if each one is
113 : : * a quote */
114 : : + 3 /* two outer quotes + possibly 'E' if needed */
115 : 2938 : + 1 /* null terminator */
116 : : );
117 : :
5404 rhaas@postgresql.org 118 : 2938 : newlen = quote_literal_internal(result, rawstr, len);
119 : 2938 : result[newlen] = '\0';
120 : :
121 : 2938 : return result;
122 : : }
123 : :
124 : : /*
125 : : * quote_nullable -
126 : : * Returns a properly quoted literal, with null values returned
127 : : * as the text string 'NULL'.
128 : : */
129 : : Datum
6376 tgl@sss.pgh.pa.us 130 : 790 : quote_nullable(PG_FUNCTION_ARGS)
131 : : {
132 [ + + ]: 790 : if (PG_ARGISNULL(0))
6374 133 : 42 : PG_RETURN_TEXT_P(cstring_to_text("NULL"));
134 : : else
6376 135 : 748 : PG_RETURN_DATUM(DirectFunctionCall1(quote_literal,
136 : : PG_GETARG_DATUM(0)));
137 : : }
|