]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/quote.c
Update CVS HEAD for 2007 copyright. Back branches are typically not
[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-2007, PostgreSQL Global Development Group
7  *
8  *
9  * IDENTIFICATION
10  *        $PostgreSQL: pgsql/src/backend/utils/adt/quote.c,v 1.21 2007/01/05 22:19:41 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_P(0);
27         text       *result;
28         const char *qstr;
29         char       *str;
30         int                     len;
31
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);
36         str[len] = '\0';
37
38         qstr = quote_identifier(str);
39
40         len = strlen(qstr);
41         result = (text *) palloc(len + VARHDRSZ);
42         VARATT_SIZEP(result) = len + VARHDRSZ;
43         memcpy(VARDATA(result), qstr, len);
44
45         PG_RETURN_TEXT_P(result);
46 }
47
48 /*
49  * quote_literal -
50  *        returns a properly quoted literal
51  *
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.
57  */
58 Datum
59 quote_literal(PG_FUNCTION_ARGS)
60 {
61         text       *t = PG_GETARG_TEXT_P(0);
62         text       *result;
63         char       *cp1;
64         char       *cp2;
65         int                     len;
66
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);
70
71         cp1 = VARDATA(t);
72         cp2 = VARDATA(result);
73
74         for (; len-- > 0; cp1++)
75         {
76                 if (*cp1 == '\\')
77                 {
78                         *cp2++ = ESCAPE_STRING_SYNTAX;
79                         break;
80                 }
81         }
82
83         len = VARSIZE(t) - VARHDRSZ;
84         cp1 = VARDATA(t);
85
86         *cp2++ = '\'';
87         while (len-- > 0)
88         {
89                 if (SQL_STR_DOUBLE(*cp1, true))
90                         *cp2++ = *cp1;
91                 *cp2++ = *cp1++;
92         }
93         *cp2++ = '\'';
94
95         VARATT_SIZEP(result) = cp2 - ((char *) result);
96
97         PG_RETURN_TEXT_P(result);
98 }