Fix xslt_process() to ensure that it inserts a NULL terminator after the
[PostgreSQL.git] / src / backend / utils / adt / quote.c
blob54c51d692555f29f2bdcc80106164b4cff61ec52
1 /*-------------------------------------------------------------------------
3 * quote.c
4 * Functions for quoting identifiers and literals
6 * Portions Copyright (c) 2000-2009, PostgreSQL Global Development Group
9 * IDENTIFICATION
10 * $PostgreSQL$
12 *-------------------------------------------------------------------------
14 #include "postgres.h"
16 #include "utils/builtins.h"
20 * quote_ident -
21 * returns a properly quoted identifier
23 Datum
24 quote_ident(PG_FUNCTION_ARGS)
26 text *t = PG_GETARG_TEXT_PP(0);
27 const char *qstr;
28 char *str;
30 str = text_to_cstring(t);
31 qstr = quote_identifier(str);
32 PG_RETURN_TEXT_P(cstring_to_text(qstr));
36 * quote_literal -
37 * returns a properly quoted literal
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.
45 Datum
46 quote_literal(PG_FUNCTION_ARGS)
48 text *t = PG_GETARG_TEXT_P(0);
49 text *result;
50 char *cp1;
51 char *cp2;
52 int len;
54 len = VARSIZE(t) - VARHDRSZ;
55 /* We make a worst-case result area; wasting a little space is OK */
56 result = (text *) palloc(len * 2 + 3 + VARHDRSZ);
58 cp1 = VARDATA(t);
59 cp2 = VARDATA(result);
61 for (; len-- > 0; cp1++)
63 if (*cp1 == '\\')
65 *cp2++ = ESCAPE_STRING_SYNTAX;
66 break;
70 len = VARSIZE(t) - VARHDRSZ;
71 cp1 = VARDATA(t);
73 *cp2++ = '\'';
74 while (len-- > 0)
76 if (SQL_STR_DOUBLE(*cp1, true))
77 *cp2++ = *cp1;
78 *cp2++ = *cp1++;
80 *cp2++ = '\'';
82 SET_VARSIZE(result, cp2 - ((char *) result));
84 PG_RETURN_TEXT_P(result);
88 * quote_nullable -
89 * Returns a properly quoted literal, with null values returned
90 * as the text string 'NULL'.
92 Datum
93 quote_nullable(PG_FUNCTION_ARGS)
95 if (PG_ARGISNULL(0))
96 PG_RETURN_TEXT_P(cstring_to_text("NULL"));
97 else
98 PG_RETURN_DATUM(DirectFunctionCall1(quote_literal,
99 PG_GETARG_DATUM(0)));