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.5 2001/10/25 05:49:45 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);
133 VARATT_SIZEP(result) = cp2 - ((char *) result);
138 /* Return a properly quoted literal value */
140 do_quote_literal(text *lptr)
147 len = VARSIZE(lptr) - VARHDRSZ;
148 result = (text *) palloc(len * 2 + VARHDRSZ + 2);
151 cp2 = VARDATA(result);
164 VARATT_SIZEP(result) = cp2 - ((char *) result);
171 /* Check if a given identifier needs quoting (MULTIBYTE version) */
173 quote_ident_required(text *iptr)
179 ep = VARDATA(iptr) + VARSIZE(iptr) - VARHDRSZ;
184 if (pg_mblen(cp) != 1)
186 if (!(*cp == '_' || (*cp >= 'a' && *cp <= 'z')))
191 if (pg_mblen(cp) != 1)
194 if (*cp >= 'a' && *cp <= 'z')
196 if (*cp >= '0' && *cp <= '9')
207 /* Return a properly quoted identifier (MULTIBYTE version) */
209 do_quote_ident(text *iptr)
217 len = VARSIZE(iptr) - VARHDRSZ;
218 result = (text *) palloc(len * 2 + VARHDRSZ + 2);
221 cp2 = VARDATA(result);
226 if ((wl = pg_mblen(cp1)) != 1)
245 VARATT_SIZEP(result) = cp2 - ((char *) result);
250 /* Return a properly quoted literal value (MULTIBYTE version) */
252 do_quote_literal(text *lptr)
260 len = VARSIZE(lptr) - VARHDRSZ;
261 result = (text *) palloc(len * 2 + VARHDRSZ + 2);
264 cp2 = VARDATA(result);
269 if ((wl = pg_mblen(cp1)) != 1)
288 VARATT_SIZEP(result) = cp2 - ((char *) result);