1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-27 12:41:57 +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

@ -8,7 +8,7 @@
* Darko Prenosil <Darko.Prenosil@finteh.hr>
* Shridhar Daithankar <shridhar_daithankar@persistent.co.in>
*
* $PostgreSQL: pgsql/contrib/dblink/dblink.c,v 1.69 2008/01/14 02:49:47 tgl Exp $
* $PostgreSQL: pgsql/contrib/dblink/dblink.c,v 1.70 2008/03/25 22:42:41 tgl Exp $
* Copyright (c) 2001-2008, PostgreSQL Global Development Group
* ALL RIGHTS RESERVED;
*
@ -116,8 +116,6 @@ typedef struct remoteConnHashEnt
#define NUMCONN 16
/* general utility */
#define GET_TEXT(cstrp) DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(cstrp)))
#define GET_STR(textp) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp)))
#define xpfree(var_) \
do { \
if (var_ != NULL) \
@ -171,7 +169,7 @@ typedef struct remoteConnHashEnt
#define DBLINK_GET_CONN \
do { \
char *conname_or_str = GET_STR(PG_GETARG_TEXT_P(0)); \
char *conname_or_str = text_to_cstring(PG_GETARG_TEXT_PP(0)); \
rconn = getConnectionByName(conname_or_str); \
if(rconn) \
{ \
@ -197,7 +195,7 @@ typedef struct remoteConnHashEnt
#define DBLINK_GET_NAMED_CONN \
do { \
char *conname = GET_STR(PG_GETARG_TEXT_P(0)); \
char *conname = text_to_cstring(PG_GETARG_TEXT_PP(0)); \
rconn = getConnectionByName(conname); \
if(rconn) \
conn = rconn->conn; \
@ -234,11 +232,11 @@ dblink_connect(PG_FUNCTION_ARGS)
if (PG_NARGS() == 2)
{
connstr = GET_STR(PG_GETARG_TEXT_P(1));
connname = GET_STR(PG_GETARG_TEXT_P(0));
connstr = text_to_cstring(PG_GETARG_TEXT_PP(1));
connname = text_to_cstring(PG_GETARG_TEXT_PP(0));
}
else if (PG_NARGS() == 1)
connstr = GET_STR(PG_GETARG_TEXT_P(0));
connstr = text_to_cstring(PG_GETARG_TEXT_PP(0));
oldcontext = MemoryContextSwitchTo(TopMemoryContext);
@ -272,7 +270,7 @@ dblink_connect(PG_FUNCTION_ARGS)
else
pconn->conn = conn;
PG_RETURN_TEXT_P(GET_TEXT("OK"));
PG_RETURN_TEXT_P(cstring_to_text("OK"));
}
/*
@ -290,7 +288,7 @@ dblink_disconnect(PG_FUNCTION_ARGS)
if (PG_NARGS() == 1)
{
conname = GET_STR(PG_GETARG_TEXT_P(0));
conname = text_to_cstring(PG_GETARG_TEXT_PP(0));
rconn = getConnectionByName(conname);
if (rconn)
conn = rconn->conn;
@ -310,7 +308,7 @@ dblink_disconnect(PG_FUNCTION_ARGS)
else
pconn->conn = NULL;
PG_RETURN_TEXT_P(GET_TEXT("OK"));
PG_RETURN_TEXT_P(cstring_to_text("OK"));
}
/*
@ -336,8 +334,8 @@ dblink_open(PG_FUNCTION_ARGS)
if (PG_NARGS() == 2)
{
/* text,text */
curname = GET_STR(PG_GETARG_TEXT_P(0));
sql = GET_STR(PG_GETARG_TEXT_P(1));
curname = text_to_cstring(PG_GETARG_TEXT_PP(0));
sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
rconn = pconn;
}
else if (PG_NARGS() == 3)
@ -345,25 +343,25 @@ dblink_open(PG_FUNCTION_ARGS)
/* might be text,text,text or text,text,bool */
if (get_fn_expr_argtype(fcinfo->flinfo, 2) == BOOLOID)
{
curname = GET_STR(PG_GETARG_TEXT_P(0));
sql = GET_STR(PG_GETARG_TEXT_P(1));
curname = text_to_cstring(PG_GETARG_TEXT_PP(0));
sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
fail = PG_GETARG_BOOL(2);
rconn = pconn;
}
else
{
conname = GET_STR(PG_GETARG_TEXT_P(0));
curname = GET_STR(PG_GETARG_TEXT_P(1));
sql = GET_STR(PG_GETARG_TEXT_P(2));
conname = text_to_cstring(PG_GETARG_TEXT_PP(0));
curname = text_to_cstring(PG_GETARG_TEXT_PP(1));
sql = text_to_cstring(PG_GETARG_TEXT_PP(2));
rconn = getConnectionByName(conname);
}
}
else if (PG_NARGS() == 4)
{
/* text,text,text,bool */
conname = GET_STR(PG_GETARG_TEXT_P(0));
curname = GET_STR(PG_GETARG_TEXT_P(1));
sql = GET_STR(PG_GETARG_TEXT_P(2));
conname = text_to_cstring(PG_GETARG_TEXT_PP(0));
curname = text_to_cstring(PG_GETARG_TEXT_PP(1));
sql = text_to_cstring(PG_GETARG_TEXT_PP(2));
fail = PG_GETARG_BOOL(3);
rconn = getConnectionByName(conname);
}
@ -403,12 +401,12 @@ dblink_open(PG_FUNCTION_ARGS)
else
{
DBLINK_RES_ERROR_AS_NOTICE("sql error");
PG_RETURN_TEXT_P(GET_TEXT("ERROR"));
PG_RETURN_TEXT_P(cstring_to_text("ERROR"));
}
}
PQclear(res);
PG_RETURN_TEXT_P(GET_TEXT("OK"));
PG_RETURN_TEXT_P(cstring_to_text("OK"));
}
/*
@ -433,7 +431,7 @@ dblink_close(PG_FUNCTION_ARGS)
if (PG_NARGS() == 1)
{
/* text */
curname = GET_STR(PG_GETARG_TEXT_P(0));
curname = text_to_cstring(PG_GETARG_TEXT_PP(0));
rconn = pconn;
}
else if (PG_NARGS() == 2)
@ -441,22 +439,22 @@ dblink_close(PG_FUNCTION_ARGS)
/* might be text,text or text,bool */
if (get_fn_expr_argtype(fcinfo->flinfo, 1) == BOOLOID)
{
curname = GET_STR(PG_GETARG_TEXT_P(0));
curname = text_to_cstring(PG_GETARG_TEXT_PP(0));
fail = PG_GETARG_BOOL(1);
rconn = pconn;
}
else
{
conname = GET_STR(PG_GETARG_TEXT_P(0));
curname = GET_STR(PG_GETARG_TEXT_P(1));
conname = text_to_cstring(PG_GETARG_TEXT_PP(0));
curname = text_to_cstring(PG_GETARG_TEXT_PP(1));
rconn = getConnectionByName(conname);
}
}
if (PG_NARGS() == 3)
{
/* text,text,bool */
conname = GET_STR(PG_GETARG_TEXT_P(0));
curname = GET_STR(PG_GETARG_TEXT_P(1));
conname = text_to_cstring(PG_GETARG_TEXT_PP(0));
curname = text_to_cstring(PG_GETARG_TEXT_PP(1));
fail = PG_GETARG_BOOL(2);
rconn = getConnectionByName(conname);
}
@ -477,7 +475,7 @@ dblink_close(PG_FUNCTION_ARGS)
else
{
DBLINK_RES_ERROR_AS_NOTICE("sql error");
PG_RETURN_TEXT_P(GET_TEXT("ERROR"));
PG_RETURN_TEXT_P(cstring_to_text("ERROR"));
}
}
@ -500,7 +498,7 @@ dblink_close(PG_FUNCTION_ARGS)
}
}
PG_RETURN_TEXT_P(GET_TEXT("OK"));
PG_RETURN_TEXT_P(cstring_to_text("OK"));
}
/*
@ -535,8 +533,8 @@ dblink_fetch(PG_FUNCTION_ARGS)
if (PG_NARGS() == 4)
{
/* text,text,int,bool */
conname = GET_STR(PG_GETARG_TEXT_P(0));
curname = GET_STR(PG_GETARG_TEXT_P(1));
conname = text_to_cstring(PG_GETARG_TEXT_PP(0));
curname = text_to_cstring(PG_GETARG_TEXT_PP(1));
howmany = PG_GETARG_INT32(2);
fail = PG_GETARG_BOOL(3);
@ -549,15 +547,15 @@ dblink_fetch(PG_FUNCTION_ARGS)
/* text,text,int or text,int,bool */
if (get_fn_expr_argtype(fcinfo->flinfo, 2) == BOOLOID)
{
curname = GET_STR(PG_GETARG_TEXT_P(0));
curname = text_to_cstring(PG_GETARG_TEXT_PP(0));
howmany = PG_GETARG_INT32(1);
fail = PG_GETARG_BOOL(2);
conn = pconn->conn;
}
else
{
conname = GET_STR(PG_GETARG_TEXT_P(0));
curname = GET_STR(PG_GETARG_TEXT_P(1));
conname = text_to_cstring(PG_GETARG_TEXT_PP(0));
curname = text_to_cstring(PG_GETARG_TEXT_PP(1));
howmany = PG_GETARG_INT32(2);
rconn = getConnectionByName(conname);
@ -568,7 +566,7 @@ dblink_fetch(PG_FUNCTION_ARGS)
else if (PG_NARGS() == 2)
{
/* text,int */
curname = GET_STR(PG_GETARG_TEXT_P(0));
curname = text_to_cstring(PG_GETARG_TEXT_PP(0));
howmany = PG_GETARG_INT32(1);
conn = pconn->conn;
}
@ -769,7 +767,7 @@ dblink_record_internal(FunctionCallInfo fcinfo, bool is_async, bool do_get)
{
/* text,text,bool */
DBLINK_GET_CONN;
sql = GET_STR(PG_GETARG_TEXT_P(1));
sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
fail = PG_GETARG_BOOL(2);
}
else if (PG_NARGS() == 2)
@ -778,20 +776,20 @@ dblink_record_internal(FunctionCallInfo fcinfo, bool is_async, bool do_get)
if (get_fn_expr_argtype(fcinfo->flinfo, 1) == BOOLOID)
{
conn = pconn->conn;
sql = GET_STR(PG_GETARG_TEXT_P(0));
sql = text_to_cstring(PG_GETARG_TEXT_PP(0));
fail = PG_GETARG_BOOL(1);
}
else
{
DBLINK_GET_CONN;
sql = GET_STR(PG_GETARG_TEXT_P(1));
sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
}
}
else if (PG_NARGS() == 1)
{
/* text */
conn = pconn->conn;
sql = GET_STR(PG_GETARG_TEXT_P(0));
sql = text_to_cstring(PG_GETARG_TEXT_PP(0));
}
else
/* shouldn't happen */
@ -821,7 +819,7 @@ dblink_record_internal(FunctionCallInfo fcinfo, bool is_async, bool do_get)
if (PG_NARGS() == 2)
{
DBLINK_GET_CONN;
sql = GET_STR(PG_GETARG_TEXT_P(1));
sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
}
else
/* shouldn't happen */
@ -1024,7 +1022,7 @@ dblink_get_connections(PG_FUNCTION_ARGS)
{
/* stash away current value */
astate = accumArrayResult(astate,
PointerGetDatum(GET_TEXT(hentry->name)),
CStringGetTextDatum(hentry->name),
false, TEXTOID, CurrentMemoryContext);
}
}
@ -1087,9 +1085,9 @@ dblink_cancel_query(PG_FUNCTION_ARGS)
PQfreeCancel(cancel);
if (res == 1)
PG_RETURN_TEXT_P(GET_TEXT("OK"));
PG_RETURN_TEXT_P(cstring_to_text("OK"));
else
PG_RETURN_TEXT_P(GET_TEXT(errbuf));
PG_RETURN_TEXT_P(cstring_to_text(errbuf));
}
@ -1116,9 +1114,9 @@ dblink_error_message(PG_FUNCTION_ARGS)
msg = PQerrorMessage(conn);
if (msg == NULL || msg[0] == '\0')
PG_RETURN_TEXT_P(GET_TEXT("OK"));
PG_RETURN_TEXT_P(cstring_to_text("OK"));
else
PG_RETURN_TEXT_P(GET_TEXT(msg));
PG_RETURN_TEXT_P(cstring_to_text(msg));
}
/*
@ -1146,7 +1144,7 @@ dblink_exec(PG_FUNCTION_ARGS)
{
/* must be text,text,bool */
DBLINK_GET_CONN;
sql = GET_STR(PG_GETARG_TEXT_P(1));
sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
fail = PG_GETARG_BOOL(2);
}
else if (PG_NARGS() == 2)
@ -1155,20 +1153,20 @@ dblink_exec(PG_FUNCTION_ARGS)
if (get_fn_expr_argtype(fcinfo->flinfo, 1) == BOOLOID)
{
conn = pconn->conn;
sql = GET_STR(PG_GETARG_TEXT_P(0));
sql = text_to_cstring(PG_GETARG_TEXT_PP(0));
fail = PG_GETARG_BOOL(1);
}
else
{
DBLINK_GET_CONN;
sql = GET_STR(PG_GETARG_TEXT_P(1));
sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
}
}
else if (PG_NARGS() == 1)
{
/* must be single text argument */
conn = pconn->conn;
sql = GET_STR(PG_GETARG_TEXT_P(0));
sql = text_to_cstring(PG_GETARG_TEXT_PP(0));
}
else
/* shouldn't happen */
@ -1196,7 +1194,7 @@ dblink_exec(PG_FUNCTION_ARGS)
* and save a copy of the command status string to return as our
* result tuple
*/
sql_cmd_status = GET_TEXT("ERROR");
sql_cmd_status = cstring_to_text("ERROR");
}
else if (PQresultStatus(res) == PGRES_COMMAND_OK)
@ -1210,7 +1208,7 @@ dblink_exec(PG_FUNCTION_ARGS)
* and save a copy of the command status string to return as our
* result tuple
*/
sql_cmd_status = GET_TEXT(PQcmdStatus(res));
sql_cmd_status = cstring_to_text(PQcmdStatus(res));
PQclear(res);
}
else
@ -1267,7 +1265,7 @@ dblink_get_pkey(PG_FUNCTION_ARGS)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_TABLE),
errmsg("relation \"%s\" does not exist",
GET_STR(PG_GETARG_TEXT_P(0)))));
text_to_cstring(PG_GETARG_TEXT_PP(0)))));
/*
* need a tuple descriptor representing one INT and one TEXT column
@ -1387,7 +1385,7 @@ dblink_build_sql_insert(PG_FUNCTION_ARGS)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_TABLE),
errmsg("relation \"%s\" does not exist",
GET_STR(relname_text))));
text_to_cstring(relname_text))));
/*
* There should be at least one key attribute
@ -1443,7 +1441,7 @@ dblink_build_sql_insert(PG_FUNCTION_ARGS)
/*
* And send it
*/
PG_RETURN_TEXT_P(GET_TEXT(sql));
PG_RETURN_TEXT_P(cstring_to_text(sql));
}
@ -1484,7 +1482,7 @@ dblink_build_sql_delete(PG_FUNCTION_ARGS)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_TABLE),
errmsg("relation \"%s\" does not exist",
GET_STR(relname_text))));
text_to_cstring(relname_text))));
/*
* There should be at least one key attribute
@ -1525,7 +1523,7 @@ dblink_build_sql_delete(PG_FUNCTION_ARGS)
/*
* And send it
*/
PG_RETURN_TEXT_P(GET_TEXT(sql));
PG_RETURN_TEXT_P(cstring_to_text(sql));
}
@ -1573,7 +1571,7 @@ dblink_build_sql_update(PG_FUNCTION_ARGS)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_TABLE),
errmsg("relation \"%s\" does not exist",
GET_STR(relname_text))));
text_to_cstring(relname_text))));
/*
* There should be one source array key values for each key attnum
@ -1629,7 +1627,7 @@ dblink_build_sql_update(PG_FUNCTION_ARGS)
/*
* And send it
*/
PG_RETURN_TEXT_P(GET_TEXT(sql));
PG_RETURN_TEXT_P(cstring_to_text(sql));
}
/*
@ -1643,7 +1641,7 @@ Datum
dblink_current_query(PG_FUNCTION_ARGS)
{
if (debug_query_string)
PG_RETURN_TEXT_P(GET_TEXT(debug_query_string));
PG_RETURN_TEXT_P(cstring_to_text(debug_query_string));
else
PG_RETURN_NULL();
}
@ -1763,8 +1761,7 @@ get_text_array_contents(ArrayType *array, int *numitems)
}
else
{
values[i] = DatumGetCString(DirectFunctionCall1(textout,
PointerGetDatum(ptr)));
values[i] = TextDatumGetCString(PointerGetDatum(ptr));
ptr = att_addlength_pointer(ptr, typlen, ptr);
ptr = (char *) att_align_nominal(ptr, typalign);
}
@ -2026,9 +2023,10 @@ quote_literal_cstr(char *rawstr)
text *result_text;
char *result;
rawstr_text = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(rawstr)));
result_text = DatumGetTextP(DirectFunctionCall1(quote_literal, PointerGetDatum(rawstr_text)));
result = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(result_text)));
rawstr_text = cstring_to_text(rawstr);
result_text = DatumGetTextP(DirectFunctionCall1(quote_literal,
PointerGetDatum(rawstr_text)));
result = text_to_cstring(result_text);
return result;
}
@ -2044,9 +2042,10 @@ quote_ident_cstr(char *rawstr)
text *result_text;
char *result;
rawstr_text = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(rawstr)));
result_text = DatumGetTextP(DirectFunctionCall1(quote_ident, PointerGetDatum(rawstr_text)));
result = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(result_text)));
rawstr_text = cstring_to_text(rawstr);
result_text = DatumGetTextP(DirectFunctionCall1(quote_ident,
PointerGetDatum(rawstr_text)));
result = text_to_cstring(result_text);
return result;
}