1 /*-------------------------------------------------------------------------
4 * Functions for quoting identifiers and literals
6 * Portions Copyright (c) 2000-2003, PostgreSQL Global Development Group
10 * $Header: /cvsroot/pgsql/src/backend/utils/adt/quote.c,v 1.11 2003/08/04 23:59:39 tgl 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);
68 * Check if a given identifier needs quoting
71 quote_ident_required(text *iptr)
77 ep = VARDATA(iptr) + VARSIZE(iptr) - VARHDRSZ;
82 if (pg_mblen(cp) != 1)
84 if (!(*cp == '_' || (*cp >= 'a' && *cp <= 'z')))
89 if (pg_mblen(cp) != 1)
92 if (*cp >= 'a' && *cp <= 'z')
94 if (*cp >= '0' && *cp <= '9')
106 * Return a properly quoted identifier
109 do_quote_ident(text *iptr)
117 len = VARSIZE(iptr) - VARHDRSZ;
118 result = (text *) palloc(len * 2 + VARHDRSZ + 2);
121 cp2 = VARDATA(result);
126 if ((wl = pg_mblen(cp1)) != 1)
143 VARATT_SIZEP(result) = cp2 - ((char *) result);
149 * Return a properly quoted literal value
152 do_quote_literal(text *lptr)
160 len = VARSIZE(lptr) - VARHDRSZ;
161 result = (text *) palloc(len * 2 + VARHDRSZ + 2);
164 cp2 = VARDATA(result);
169 if ((wl = pg_mblen(cp1)) != 1)
188 VARATT_SIZEP(result) = cp2 - ((char *) result);