postgresql/src/backend/utils/adt/quote.c

92 lines
1.9 KiB
C
Raw Normal View History

/*-------------------------------------------------------------------------
*
* quote.c
* Functions for quoting identifiers and literals
*
* Portions Copyright (c) 2000-2006, PostgreSQL Global Development Group
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/quote.c,v 1.19 2006/05/26 23:48:54 momjian Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "utils/builtins.h"
#include "parser/gramparse.h"
/*
* quote_ident -
* returns a properly quoted identifier
*/
Datum
quote_ident(PG_FUNCTION_ARGS)
{
text *t = PG_GETARG_TEXT_P(0);
text *result;
const char *qstr;
char *str;
int len;
/* We have to convert to a C string to use quote_identifier */
len = VARSIZE(t) - VARHDRSZ;
str = (char *) palloc(len + 1);
memcpy(str, VARDATA(t), len);
str[len] = '\0';
qstr = quote_identifier(str);
len = strlen(qstr);
result = (text *) palloc(len + VARHDRSZ);
VARATT_SIZEP(result) = len + VARHDRSZ;
memcpy(VARDATA(result), qstr, len);
PG_RETURN_TEXT_P(result);
}
/*
* quote_literal -
* returns a properly quoted literal
*/
Datum
quote_literal(PG_FUNCTION_ARGS)
{
text *t = PG_GETARG_TEXT_P(0);
text *result;
2001-03-22 05:01:46 +01:00
char *cp1;
char *cp2;
int len;
len = VARSIZE(t) - VARHDRSZ;
/* We make a worst-case result area; wasting a little space is OK */
result = (text *) palloc(len * 2 + 3 + VARHDRSZ);
cp1 = VARDATA(t);
cp2 = VARDATA(result);
if (!standard_conforming_strings)
for (; len-- > 0; cp1++)
if (*cp1 == '\\')
{
*cp2++ = ESCAPE_STRING_SYNTAX;
break;
}
2005-10-15 04:49:52 +02:00
len = VARSIZE(t) - VARHDRSZ;
cp1 = VARDATA(t);
*cp2++ = '\'';
while (len-- > 0)
{
if (SQL_STR_DOUBLE(*cp1, !standard_conforming_strings))
*cp2++ = *cp1;
*cp2++ = *cp1++;
}
*cp2++ = '\'';
2001-03-22 05:01:46 +01:00
VARATT_SIZEP(result) = cp2 - ((char *) result);
PG_RETURN_TEXT_P(result);
}