1 /*-------------------------------------------------------------------------
4 * Functions for quoting identifiers and literals
6 * Portions Copyright (c) 2000, PostgreSQL Global Development Group
10 * $Header: /cvsroot/pgsql/src/backend/utils/adt/quote.c,v 1.7 2002/04/04 04:25:49 momjian Exp $
12 *-------------------------------------------------------------------------
18 #include "mb/pg_wchar.h"
19 #include "utils/builtins.h"
22 static bool quote_ident_required(text *iptr);
23 static text *do_quote_ident(text *iptr);
24 static text *do_quote_literal(text *iptr);
29 * returns a properly quoted identifier
32 quote_ident(PG_FUNCTION_ARGS)
34 text *t = PG_GETARG_TEXT_P(0);
37 if (quote_ident_required(t))
38 result = do_quote_ident(t);
41 result = (text *) palloc(VARSIZE(t));
42 memcpy(result, t, VARSIZE(t));
45 PG_FREE_IF_COPY(t, 0);
47 PG_RETURN_TEXT_P(result);
52 * returns a properly quoted literal
55 quote_literal(PG_FUNCTION_ARGS)
57 text *t = PG_GETARG_TEXT_P(0);
60 result = do_quote_literal(t);
62 PG_FREE_IF_COPY(t, 0);
64 PG_RETURN_TEXT_P(result);
69 * MULTIBYTE dependant internal functions follow
76 /* Check if a given identifier needs quoting */
78 quote_ident_required(text *iptr)
84 ep = VARDATA(iptr) + VARSIZE(iptr) - VARHDRSZ;
89 if (!(*cp == '_' || (*cp >= 'a' && *cp <= 'z')))
94 if (*cp >= 'a' && *cp <= 'z')
96 if (*cp >= '0' && *cp <= '9')
107 /* Return a properly quoted identifier */
109 do_quote_ident(text *iptr)
116 len = VARSIZE(iptr) - VARHDRSZ;
117 result = (text *) palloc(len * 2 + VARHDRSZ + 2);
120 cp2 = VARDATA(result);
131 VARATT_SIZEP(result) = cp2 - ((char *) result);
136 /* Return a properly quoted literal value */
138 do_quote_literal(text *lptr)
145 len = VARSIZE(lptr) - VARHDRSZ;
146 result = (text *) palloc(len * 2 + VARHDRSZ + 2);
149 cp2 = VARDATA(result);
162 VARATT_SIZEP(result) = cp2 - ((char *) result);
169 /* Check if a given identifier needs quoting (MULTIBYTE version) */
171 quote_ident_required(text *iptr)
177 ep = VARDATA(iptr) + VARSIZE(iptr) - VARHDRSZ;
182 if (pg_mblen(cp) != 1)
184 if (!(*cp == '_' || (*cp >= 'a' && *cp <= 'z')))
189 if (pg_mblen(cp) != 1)
192 if (*cp >= 'a' && *cp <= 'z')
194 if (*cp >= '0' && *cp <= '9')
205 /* Return a properly quoted identifier (MULTIBYTE version) */
207 do_quote_ident(text *iptr)
215 len = VARSIZE(iptr) - VARHDRSZ;
216 result = (text *) palloc(len * 2 + VARHDRSZ + 2);
219 cp2 = VARDATA(result);
224 if ((wl = pg_mblen(cp1)) != 1)
241 VARATT_SIZEP(result) = cp2 - ((char *) result);
246 /* Return a properly quoted literal value (MULTIBYTE version) */
248 do_quote_literal(text *lptr)
256 len = VARSIZE(lptr) - VARHDRSZ;
257 result = (text *) palloc(len * 2 + VARHDRSZ + 2);
260 cp2 = VARDATA(result);
265 if ((wl = pg_mblen(cp1)) != 1)
284 VARATT_SIZEP(result) = cp2 - ((char *) result);