]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/quote.c
Update copyright for 2009.
[postgresql] / src / backend / utils / adt / quote.c
1 /*-------------------------------------------------------------------------
2  *
3  * quote.c
4  *        Functions for quoting identifiers and literals
5  *
6  * Portions Copyright (c) 2000-2009, PostgreSQL Global Development Group
7  *
8  *
9  * IDENTIFICATION
10  *        $PostgreSQL: pgsql/src/backend/utils/adt/quote.c,v 1.26 2009/01/01 17:23:49 momjian Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14 #include "postgres.h"
15
16 #include "utils/builtins.h"
17
18
19 /*
20  * quote_ident -
21  *        returns a properly quoted identifier
22  */
23 Datum
24 quote_ident(PG_FUNCTION_ARGS)
25 {
26         text       *t = PG_GETARG_TEXT_PP(0);
27         const char *qstr;
28         char       *str;
29
30         str = text_to_cstring(t);
31         qstr = quote_identifier(str);
32         PG_RETURN_TEXT_P(cstring_to_text(qstr));
33 }
34
35 /*
36  * quote_literal -
37  *        returns a properly quoted literal
38  *
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.
44  */
45 Datum
46 quote_literal(PG_FUNCTION_ARGS)
47 {
48         text       *t = PG_GETARG_TEXT_P(0);
49         text       *result;
50         char       *cp1;
51         char       *cp2;
52         int                     len;
53
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);
57
58         cp1 = VARDATA(t);
59         cp2 = VARDATA(result);
60
61         for (; len-- > 0; cp1++)
62         {
63                 if (*cp1 == '\\')
64                 {
65                         *cp2++ = ESCAPE_STRING_SYNTAX;
66                         break;
67                 }
68         }
69
70         len = VARSIZE(t) - VARHDRSZ;
71         cp1 = VARDATA(t);
72
73         *cp2++ = '\'';
74         while (len-- > 0)
75         {
76                 if (SQL_STR_DOUBLE(*cp1, true))
77                         *cp2++ = *cp1;
78                 *cp2++ = *cp1++;
79         }
80         *cp2++ = '\'';
81
82         SET_VARSIZE(result, cp2 - ((char *) result));
83
84         PG_RETURN_TEXT_P(result);
85 }
86
87 /*
88  * quote_nullable -
89  *    Returns a properly quoted literal, with null values returned
90  *    as the text string 'NULL'.
91  */
92 Datum
93 quote_nullable(PG_FUNCTION_ARGS)
94 {
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)));
100 }