1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-16 06:01:02 +03:00

Simplify and standardize conversions between TEXT datums and ordinary C

strings.  This patch introduces four support functions cstring_to_text,
cstring_to_text_with_len, text_to_cstring, and text_to_cstring_buffer, and
two macros CStringGetTextDatum and TextDatumGetCString.  A number of
existing macros that provided variants on these themes were removed.

Most of the places that need to make such conversions now require just one
function or macro call, in place of the multiple notational layers that used
to be needed.  There are no longer any direct calls of textout or textin,
and we got most of the places that were using handmade conversions via
memcpy (there may be a few still lurking, though).

This commit doesn't make any serious effort to eliminate transient memory
leaks caused by detoasting toasted text objects before they reach
text_to_cstring.  We changed PG_GETARG_TEXT_P to PG_GETARG_TEXT_PP in a few
places where it was easy, but much more could be done.

Brendan Jurd and Tom Lane
This commit is contained in:
Tom Lane
2008-03-25 22:42:46 +00:00
parent f948197b40
commit 220db7ccd8
94 changed files with 771 additions and 1211 deletions

View File

@ -15,7 +15,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/selfuncs.c,v 1.246 2008/03/17 17:13:54 tgl Exp $
* $PostgreSQL: pgsql/src/backend/utils/adt/selfuncs.c,v 1.247 2008/03/25 22:42:44 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -1089,8 +1089,7 @@ patternsel(PG_FUNCTION_ARGS, Pattern_Type ptype, bool negate)
switch (prefix->consttype)
{
case TEXTOID:
prefixstr = DatumGetCString(DirectFunctionCall1(textout,
prefix->constvalue));
prefixstr = TextDatumGetCString(prefix->constvalue);
break;
case BYTEAOID:
prefixstr = DatumGetCString(DirectFunctionCall1(byteaout,
@ -3339,15 +3338,8 @@ convert_string_datum(Datum value, Oid typid)
case BPCHAROID:
case VARCHAROID:
case TEXTOID:
{
char *str = (char *) VARDATA(DatumGetPointer(value));
int strlength = VARSIZE(DatumGetPointer(value)) - VARHDRSZ;
val = (char *) palloc(strlength + 1);
memcpy(val, str, strlength);
val[strlength] = '\0';
break;
}
val = TextDatumGetCString(value);
break;
case NAMEOID:
{
NameData *nm = (NameData *) DatumGetPointer(value);
@ -4177,7 +4169,7 @@ like_fixed_prefix(Const *patt_const, bool case_insensitive,
if (typeid != BYTEAOID)
{
patt = DatumGetCString(DirectFunctionCall1(textout, patt_const->constvalue));
patt = TextDatumGetCString(patt_const->constvalue);
pattlen = strlen(patt);
}
else
@ -4282,7 +4274,7 @@ regex_fixed_prefix(Const *patt_const, bool case_insensitive,
errmsg("regular-expression matching not supported on type bytea")));
/* the right-hand const is type text for all of these */
patt = DatumGetCString(DirectFunctionCall1(textout, patt_const->constvalue));
patt = TextDatumGetCString(patt_const->constvalue);
/*
* Check for ARE director prefix. It's worth our trouble to recognize
@ -4618,7 +4610,7 @@ like_selectivity(Const *patt_const, bool case_insensitive)
if (typeid != BYTEAOID)
{
patt = DatumGetCString(DirectFunctionCall1(textout, patt_const->constvalue));
patt = TextDatumGetCString(patt_const->constvalue);
pattlen = strlen(patt);
}
else
@ -4777,7 +4769,7 @@ regex_selectivity(Const *patt_const, bool case_insensitive)
errmsg("regular-expression matching not supported on type bytea")));
/* the right-hand const is type text for all of these */
patt = DatumGetCString(DirectFunctionCall1(textout, patt_const->constvalue));
patt = TextDatumGetCString(patt_const->constvalue);
pattlen = strlen(patt);
/* If patt doesn't end with $, consider it to have a trailing wildcard */
@ -4892,8 +4884,7 @@ make_greater_string(const Const *str_const, FmgrInfo *ltproc)
}
else
{
workstr = DatumGetCString(DirectFunctionCall1(textout,
str_const->constvalue));
workstr = TextDatumGetCString(str_const->constvalue);
len = strlen(workstr);
if (lc_collate_is_c() || len == 0)
cmpstr = str_const->constvalue;
@ -5000,15 +4991,15 @@ string_to_datum(const char *str, Oid datatype)
Assert(str != NULL);
/*
* We cheat a little by assuming that textin() will do for bpchar and
* varchar constants too...
* We cheat a little by assuming that CStringGetTextDatum() will do for
* bpchar and varchar constants too...
*/
if (datatype == NAMEOID)
return DirectFunctionCall1(namein, CStringGetDatum(str));
else if (datatype == BYTEAOID)
return DirectFunctionCall1(byteain, CStringGetDatum(str));
else
return DirectFunctionCall1(textin, CStringGetDatum(str));
return CStringGetTextDatum(str);
}
/*