Age Owner TLA Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * quote.c
4 : * Functions for quoting identifiers and literals
5 : *
6 : * Portions Copyright (c) 2000-2023, 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
8251 JanWieck 25 GIC 3742 : quote_ident(PG_FUNCTION_ARGS)
8251 JanWieck 26 ECB : {
5493 tgl 27 GIC 3742 : text *t = PG_GETARG_TEXT_PP(0);
6593 tgl 28 ECB : const char *qstr;
29 : char *str;
30 :
5493 tgl 31 GIC 3742 : str = text_to_cstring(t);
6593 tgl 32 CBC 3742 : qstr = quote_identifier(str);
5493 33 3742 : PG_RETURN_TEXT_P(cstring_to_text(qstr));
8251 JanWieck 34 ECB : }
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
4429 peter_e 47 GIC 4474 : quote_literal_internal(char *dst, const char *src, size_t len)
4523 rhaas 48 ECB : {
49 : const char *s;
4523 rhaas 50 GIC 4474 : char *savedst = dst;
4523 rhaas 51 ECB :
4523 rhaas 52 GIC 340717 : for (s = src; s < src + len; s++)
4523 rhaas 53 ECB : {
4523 rhaas 54 GIC 336248 : if (*s == '\\')
4523 rhaas 55 ECB : {
4523 rhaas 56 GIC 5 : *dst++ = ESCAPE_STRING_SYNTAX;
4523 rhaas 57 CBC 5 : break;
4523 rhaas 58 ECB : }
59 : }
60 :
4523 rhaas 61 GIC 4474 : *dst++ = '\'';
4523 rhaas 62 CBC 340724 : while (len-- > 0)
4523 rhaas 63 ECB : {
4523 rhaas 64 GIC 336250 : if (SQL_STR_DOUBLE(*src, true))
4523 rhaas 65 CBC 22 : *dst++ = *src;
66 336250 : *dst++ = *src++;
4523 rhaas 67 ECB : }
4523 rhaas 68 GIC 4474 : *dst++ = '\'';
4523 rhaas 69 ECB :
4523 rhaas 70 GIC 4474 : return dst - savedst;
4523 rhaas 71 ECB : }
72 :
73 : /*
74 : * quote_literal -
75 : * returns a properly quoted literal
76 : */
77 : Datum
8251 JanWieck 78 GIC 1772 : quote_literal(PG_FUNCTION_ARGS)
8251 JanWieck 79 ECB : {
2219 noah 80 GIC 1772 : text *t = PG_GETARG_TEXT_PP(0);
8251 JanWieck 81 ECB : text *result;
82 : char *cp1;
83 : char *cp2;
84 : int len;
85 :
2219 noah 86 GIC 1772 : len = VARSIZE_ANY_EXHDR(t);
6593 tgl 87 ECB : /* We make a worst-case result area; wasting a little space is OK */
6490 bruce 88 GIC 1772 : result = (text *) palloc(len * 2 + 3 + VARHDRSZ);
8251 JanWieck 89 ECB :
2219 noah 90 GIC 1772 : cp1 = VARDATA_ANY(t);
8251 JanWieck 91 CBC 1772 : cp2 = VARDATA(result);
8251 JanWieck 92 ECB :
4523 rhaas 93 GIC 1772 : SET_VARSIZE(result, VARHDRSZ + quote_literal_internal(cp2, cp1, len));
6385 bruce 94 ECB :
4523 rhaas 95 GIC 1772 : PG_RETURN_TEXT_P(result);
4523 rhaas 96 ECB : }
97 :
98 : /*
99 : * quote_literal_cstr -
100 : * returns a properly quoted literal
101 : */
102 : char *
4429 peter_e 103 GIC 2702 : quote_literal_cstr(const char *rawstr)
4523 rhaas 104 ECB : {
105 : char *result;
106 : int len;
107 : int newlen;
108 :
4523 rhaas 109 GIC 2702 : len = strlen(rawstr);
4523 rhaas 110 ECB : /* We make a worst-case result area; wasting a little space is OK */
2305 heikki.linnakangas 111 GIC 2702 : result = palloc(len * 2 + 3 + 1);
8251 JanWieck 112 ECB :
4523 rhaas 113 GIC 2702 : newlen = quote_literal_internal(result, rawstr, len);
4523 rhaas 114 CBC 2702 : result[newlen] = '\0';
4523 rhaas 115 ECB :
4523 rhaas 116 GIC 2702 : return result;
8251 JanWieck 117 ECB : }
118 :
119 : /*
120 : * quote_nullable -
121 : * Returns a properly quoted literal, with null values returned
122 : * as the text string 'NULL'.
123 : */
124 : Datum
5495 tgl 125 GIC 790 : quote_nullable(PG_FUNCTION_ARGS)
5495 tgl 126 ECB : {
5495 tgl 127 GIC 790 : if (PG_ARGISNULL(0))
5493 tgl 128 CBC 42 : PG_RETURN_TEXT_P(cstring_to_text("NULL"));
5495 tgl 129 ECB : else
5495 tgl 130 GIC 748 : PG_RETURN_DATUM(DirectFunctionCall1(quote_literal,
5495 tgl 131 ECB : PG_GETARG_DATUM(0)));
132 : }
|