1 /*-------------------------------------------------------------------------
4 * Functions for quoting identifiers and literals
6 * Portions Copyright (c) 2000-2009, PostgreSQL Global Development Group
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
));
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.
46 quote_literal(PG_FUNCTION_ARGS
)
48 text
*t
= PG_GETARG_TEXT_P(0);
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
);
59 cp2
= VARDATA(result
);
61 for (; len
-- > 0; cp1
++)
65 *cp2
++ = ESCAPE_STRING_SYNTAX
;
70 len
= VARSIZE(t
) - VARHDRSZ
;
76 if (SQL_STR_DOUBLE(*cp1
, true))
82 SET_VARSIZE(result
, cp2
- ((char *) result
));
84 PG_RETURN_TEXT_P(result
);
89 * Returns a properly quoted literal, with null values returned
90 * as the text string 'NULL'.
93 quote_nullable(PG_FUNCTION_ARGS
)
96 PG_RETURN_TEXT_P(cstring_to_text("NULL"));
98 PG_RETURN_DATUM(DirectFunctionCall1(quote_literal
,