1 /*-------------------------------------------------------------------------
4 * Functions for quoting identifiers and literals
6 * Portions Copyright (c) 2000-2014, PostgreSQL Global Development Group
10 * src/backend/utils/adt/quote.c
12 *-------------------------------------------------------------------------
16 #include "utils/builtins.h"
21 * returns a properly quoted identifier
24 quote_ident(PG_FUNCTION_ARGS)
26 text *t = PG_GETARG_TEXT_PP(0);
30 str = text_to_cstring(t);
31 qstr = quote_identifier(str);
32 PG_RETURN_TEXT_P(cstring_to_text(qstr));
36 * quote_literal_internal -
37 * helper function for quote_literal and quote_literal_cstr
39 * NOTE: think not to make this function's behavior change with
40 * standard_conforming_strings. We don't know where the result
41 * literal will be used, and so we must generate a result that
42 * will work with either setting. Take a look at what dblink
43 * uses this for before thinking you know better.
46 quote_literal_internal(char *dst, const char *src, size_t len)
51 for (s = src; s < src + len; s++)
55 *dst++ = ESCAPE_STRING_SYNTAX;
63 if (SQL_STR_DOUBLE(*src, true))
74 * returns a properly quoted literal
77 quote_literal(PG_FUNCTION_ARGS)
79 text *t = PG_GETARG_TEXT_P(0);
85 len = VARSIZE(t) - VARHDRSZ;
86 /* We make a worst-case result area; wasting a little space is OK */
87 result = (text *) palloc(len * 2 + 3 + VARHDRSZ);
90 cp2 = VARDATA(result);
92 SET_VARSIZE(result, VARHDRSZ + quote_literal_internal(cp2, cp1, len));
94 PG_RETURN_TEXT_P(result);
98 * quote_literal_cstr -
99 * returns a properly quoted literal
102 quote_literal_cstr(const char *rawstr)
108 len = strlen(rawstr);
109 /* We make a worst-case result area; wasting a little space is OK */
110 result = palloc(len * 2 + 3);
112 newlen = quote_literal_internal(result, rawstr, len);
113 result[newlen] = '\0';
120 * Returns a properly quoted literal, with null values returned
121 * as the text string 'NULL'.
124 quote_nullable(PG_FUNCTION_ARGS)
127 PG_RETURN_TEXT_P(cstring_to_text("NULL"));
129 PG_RETURN_DATUM(DirectFunctionCall1(quote_literal,
130 PG_GETARG_DATUM(0)));