1 /*-------------------------------------------------------------------------
4 * Functions for quoting identifiers and literals
6 * Portions Copyright (c) 2000-2008, PostgreSQL Global Development Group
10 * $PostgreSQL: pgsql/src/backend/utils/adt/quote.c,v 1.24 2008/03/23 00:24:19 tgl Exp $
12 *-------------------------------------------------------------------------
16 #include "utils/builtins.h"
21 * returns a properly quoted identifier
24 quote_ident(PG_FUNCTION_ARGS)
26 text *t = PG_GETARG_TEXT_P(0);
32 /* We have to convert to a C string to use quote_identifier */
33 len = VARSIZE(t) - VARHDRSZ;
34 str = (char *) palloc(len + 1);
35 memcpy(str, VARDATA(t), len);
38 qstr = quote_identifier(str);
41 result = (text *) palloc(len + VARHDRSZ);
42 SET_VARSIZE(result, len + VARHDRSZ);
43 memcpy(VARDATA(result), qstr, len);
45 PG_RETURN_TEXT_P(result);
50 * returns a properly quoted literal
52 * NOTE: think not to make this function's behavior change with
53 * standard_conforming_strings. We don't know where the result
54 * literal will be used, and so we must generate a result that
55 * will work with either setting. Take a look at what dblink
56 * uses this for before thinking you know better.
59 quote_literal(PG_FUNCTION_ARGS)
61 text *t = PG_GETARG_TEXT_P(0);
67 len = VARSIZE(t) - VARHDRSZ;
68 /* We make a worst-case result area; wasting a little space is OK */
69 result = (text *) palloc(len * 2 + 3 + VARHDRSZ);
72 cp2 = VARDATA(result);
74 for (; len-- > 0; cp1++)
78 *cp2++ = ESCAPE_STRING_SYNTAX;
83 len = VARSIZE(t) - VARHDRSZ;
89 if (SQL_STR_DOUBLE(*cp1, true))
95 SET_VARSIZE(result, cp2 - ((char *) result));
97 PG_RETURN_TEXT_P(result);
102 * Returns a properly quoted literal, with null values returned
103 * as the text string 'NULL'.
106 quote_nullable(PG_FUNCTION_ARGS)
109 PG_RETURN_DATUM(DirectFunctionCall1(textin,
110 CStringGetDatum("NULL")));
112 PG_RETURN_DATUM(DirectFunctionCall1(quote_literal,
113 PG_GETARG_DATUM(0)));