2000-09-05 22:25:14 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* quote.c
|
|
|
|
* Functions for quoting identifiers and literals
|
|
|
|
*
|
2004-08-29 06:13:13 +02:00
|
|
|
* Portions Copyright (c) 2000-2004, PostgreSQL Global Development Group
|
2000-09-05 22:25:14 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2004-08-29 06:13:13 +02:00
|
|
|
* $PostgreSQL: pgsql/src/backend/utils/adt/quote.c,v 1.13 2004/08/29 04:12:52 momjian Exp $
|
2000-09-05 22:25:14 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2002-09-05 02:43:07 +02:00
|
|
|
#include <ctype.h>
|
|
|
|
|
2000-09-05 22:25:14 +02:00
|
|
|
#include "mb/pg_wchar.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
|
|
|
|
|
|
|
|
static bool quote_ident_required(text *iptr);
|
|
|
|
static text *do_quote_ident(text *iptr);
|
|
|
|
static text *do_quote_literal(text *iptr);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* quote_ident -
|
|
|
|
* returns a properly quoted identifier
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
quote_ident(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *t = PG_GETARG_TEXT_P(0);
|
|
|
|
text *result;
|
|
|
|
|
|
|
|
if (quote_ident_required(t))
|
2001-03-22 05:01:46 +01:00
|
|
|
result = do_quote_ident(t);
|
2000-09-05 22:25:14 +02:00
|
|
|
else
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
result = (text *) palloc(VARSIZE(t));
|
2000-09-05 22:25:14 +02:00
|
|
|
memcpy(result, t, VARSIZE(t));
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(t, 0);
|
|
|
|
|
2000-09-12 21:39:42 +02:00
|
|
|
PG_RETURN_TEXT_P(result);
|
2000-09-05 22:25:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* quote_literal -
|
|
|
|
* returns a properly quoted literal
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
quote_literal(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *t = PG_GETARG_TEXT_P(0);
|
|
|
|
text *result;
|
|
|
|
|
|
|
|
result = do_quote_literal(t);
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(t, 0);
|
|
|
|
|
2000-09-12 21:39:42 +02:00
|
|
|
PG_RETURN_TEXT_P(result);
|
2000-09-05 22:25:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-09-03 23:45:44 +02:00
|
|
|
* Check if a given identifier needs quoting
|
2000-09-05 22:25:14 +02:00
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
quote_ident_required(text *iptr)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
char *cp;
|
|
|
|
char *ep;
|
2000-09-05 22:25:14 +02:00
|
|
|
|
|
|
|
cp = VARDATA(iptr);
|
2001-03-22 05:01:46 +01:00
|
|
|
ep = VARDATA(iptr) + VARSIZE(iptr) - VARHDRSZ;
|
2000-09-05 22:25:14 +02:00
|
|
|
|
|
|
|
if (cp >= ep)
|
|
|
|
return true;
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
if (pg_mblen(cp) != 1)
|
2000-09-05 22:25:14 +02:00
|
|
|
return true;
|
2001-03-22 05:01:46 +01:00
|
|
|
if (!(*cp == '_' || (*cp >= 'a' && *cp <= 'z')))
|
2000-09-05 22:25:14 +02:00
|
|
|
return true;
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
while ((++cp) < ep)
|
2000-09-05 22:25:14 +02:00
|
|
|
{
|
|
|
|
if (pg_mblen(cp) != 1)
|
|
|
|
return true;
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
if (*cp >= 'a' && *cp <= 'z')
|
|
|
|
continue;
|
|
|
|
if (*cp >= '0' && *cp <= '9')
|
|
|
|
continue;
|
|
|
|
if (*cp == '_')
|
|
|
|
continue;
|
2000-09-05 22:25:14 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-09-03 23:45:44 +02:00
|
|
|
/*
|
|
|
|
* Return a properly quoted identifier
|
|
|
|
*/
|
2000-09-05 22:25:14 +02:00
|
|
|
static text *
|
|
|
|
do_quote_ident(text *iptr)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
text *result;
|
|
|
|
char *cp1;
|
|
|
|
char *cp2;
|
|
|
|
int len;
|
|
|
|
int wl;
|
2000-09-05 22:25:14 +02:00
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
len = VARSIZE(iptr) - VARHDRSZ;
|
|
|
|
result = (text *) palloc(len * 2 + VARHDRSZ + 2);
|
2000-09-05 22:25:14 +02:00
|
|
|
|
|
|
|
cp1 = VARDATA(iptr);
|
|
|
|
cp2 = VARDATA(result);
|
|
|
|
|
|
|
|
*cp2++ = '"';
|
2001-03-22 05:01:46 +01:00
|
|
|
while (len > 0)
|
2000-09-05 22:25:14 +02:00
|
|
|
{
|
|
|
|
if ((wl = pg_mblen(cp1)) != 1)
|
|
|
|
{
|
|
|
|
len -= wl;
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
while (wl-- > 0)
|
2000-09-05 22:25:14 +02:00
|
|
|
*cp2++ = *cp1++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
if (*cp1 == '"')
|
2000-09-05 22:25:14 +02:00
|
|
|
*cp2++ = '"';
|
2001-03-22 05:01:46 +01:00
|
|
|
*cp2++ = *cp1++;
|
2000-09-05 22:25:14 +02:00
|
|
|
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
*cp2++ = '"';
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
VARATT_SIZEP(result) = cp2 - ((char *) result);
|
2000-09-05 22:25:14 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2002-09-03 23:45:44 +02:00
|
|
|
/*
|
|
|
|
* Return a properly quoted literal value
|
|
|
|
*/
|
2000-09-05 22:25:14 +02:00
|
|
|
static text *
|
|
|
|
do_quote_literal(text *lptr)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
text *result;
|
|
|
|
char *cp1;
|
|
|
|
char *cp2;
|
|
|
|
int len;
|
|
|
|
int wl;
|
2000-09-05 22:25:14 +02:00
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
len = VARSIZE(lptr) - VARHDRSZ;
|
|
|
|
result = (text *) palloc(len * 2 + VARHDRSZ + 2);
|
2000-09-05 22:25:14 +02:00
|
|
|
|
|
|
|
cp1 = VARDATA(lptr);
|
|
|
|
cp2 = VARDATA(result);
|
|
|
|
|
|
|
|
*cp2++ = '\'';
|
2001-03-22 05:01:46 +01:00
|
|
|
while (len > 0)
|
2000-09-05 22:25:14 +02:00
|
|
|
{
|
|
|
|
if ((wl = pg_mblen(cp1)) != 1)
|
|
|
|
{
|
|
|
|
len -= wl;
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
while (wl-- > 0)
|
2000-09-05 22:25:14 +02:00
|
|
|
*cp2++ = *cp1++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
if (*cp1 == '\'')
|
2000-09-05 22:25:14 +02:00
|
|
|
*cp2++ = '\'';
|
2001-03-22 05:01:46 +01:00
|
|
|
if (*cp1 == '\\')
|
2000-09-05 22:25:14 +02:00
|
|
|
*cp2++ = '\\';
|
2001-03-22 05:01:46 +01:00
|
|
|
*cp2++ = *cp1++;
|
2000-09-05 22:25:14 +02:00
|
|
|
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
*cp2++ = '\'';
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
VARATT_SIZEP(result) = cp2 - ((char *) result);
|
2000-09-05 22:25:14 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|