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

Prefer pg_any_to_server/pg_server_to_any over pg_do_encoding_conversion.

A large majority of the callers of pg_do_encoding_conversion were
specifying the database encoding as either source or target of the
conversion, meaning that we can use the less general functions
pg_any_to_server/pg_server_to_any instead.

The main advantage of using the latter functions is that they can make use
of a cached conversion-function lookup in the common case that the other
encoding is the current client_encoding.  It's notationally cleaner too in
most cases, not least because of the historical artifact that the latter
functions use "char *" rather than "unsigned char *" in their APIs.

Note that pg_any_to_server will apply an encoding verification step in
some cases where pg_do_encoding_conversion would have just done nothing.
This seems to me to be a good idea at most of these call sites, though
it partially negates the performance benefit.

Per discussion of bug #9210.
This commit is contained in:
Tom Lane
2014-02-23 16:59:05 -05:00
parent 49c817eab7
commit 769065c1b2
12 changed files with 50 additions and 99 deletions

View File

@ -345,10 +345,7 @@ xml_recv(PG_FUNCTION_ARGS)
xmlFreeDoc(doc);
/* Now that we know what we're dealing with, convert to server encoding */
newstr = (char *) pg_do_encoding_conversion((unsigned char *) str,
nbytes,
encoding,
GetDatabaseEncoding());
newstr = pg_any_to_server(str, nbytes, encoding);
if (newstr != str)
{
@ -1793,10 +1790,8 @@ sqlchar_to_unicode(char *s)
char *utf8string;
pg_wchar ret[2]; /* need space for trailing zero */
utf8string = (char *) pg_do_encoding_conversion((unsigned char *) s,
pg_mblen(s),
GetDatabaseEncoding(),
PG_UTF8);
/* note we're not assuming s is null-terminated */
utf8string = pg_server_to_any(s, pg_mblen(s), PG_UTF8);
pg_encoding_mb2wchar_with_len(PG_UTF8, utf8string, ret,
pg_encoding_mblen(PG_UTF8, utf8string));
@ -1892,19 +1887,15 @@ map_sql_identifier_to_xml_name(char *ident, bool fully_escaped,
static char *
unicode_to_sqlchar(pg_wchar c)
{
unsigned char utf8string[5]; /* need room for trailing zero */
char utf8string[8]; /* need room for trailing zero */
char *result;
memset(utf8string, 0, sizeof(utf8string));
unicode_to_utf8(c, utf8string);
unicode_to_utf8(c, (unsigned char *) utf8string);
result = (char *) pg_do_encoding_conversion(utf8string,
pg_encoding_mblen(PG_UTF8,
(char *) utf8string),
PG_UTF8,
GetDatabaseEncoding());
/* if pg_do_encoding_conversion didn't strdup, we must */
if (result == (char *) utf8string)
result = pg_any_to_server(utf8string, strlen(utf8string), PG_UTF8);
/* if pg_any_to_server didn't strdup, we must */
if (result == utf8string)
result = pstrdup(result);
return result;
}