mirror of
https://github.com/postgres/postgres.git
synced 2025-11-21 00:42:43 +03:00
Add ENCODING option to COPY TO/FROM and file_fdw.
File encodings can be specified separately from client encoding. If not specified, client encoding is used for backward compatibility. Cases when the encoding doesn't match client encoding are slower than matched cases because we don't have conversion procs for other encodings. Performance improvement would be be a future work. Original patch by Hitoshi Harada, and modified by me.
This commit is contained in:
@@ -496,6 +496,17 @@ pg_encoding_max_length_sql(PG_FUNCTION_ARGS)
|
||||
*/
|
||||
char *
|
||||
pg_client_to_server(const char *s, int len)
|
||||
{
|
||||
Assert(ClientEncoding);
|
||||
|
||||
return pg_any_to_server(s, len, ClientEncoding->encoding);
|
||||
}
|
||||
|
||||
/*
|
||||
* convert any encoding to server encoding.
|
||||
*/
|
||||
char *
|
||||
pg_any_to_server(const char *s, int len, int encoding)
|
||||
{
|
||||
Assert(DatabaseEncoding);
|
||||
Assert(ClientEncoding);
|
||||
@@ -503,8 +514,8 @@ pg_client_to_server(const char *s, int len)
|
||||
if (len <= 0)
|
||||
return (char *) s;
|
||||
|
||||
if (ClientEncoding->encoding == DatabaseEncoding->encoding ||
|
||||
ClientEncoding->encoding == PG_SQL_ASCII)
|
||||
if (encoding == DatabaseEncoding->encoding ||
|
||||
encoding == PG_SQL_ASCII)
|
||||
{
|
||||
/*
|
||||
* No conversion is needed, but we must still validate the data.
|
||||
@@ -524,8 +535,8 @@ pg_client_to_server(const char *s, int len)
|
||||
* to the parser but we have no way to convert it. We compromise by
|
||||
* rejecting the data if it contains any non-ASCII characters.
|
||||
*/
|
||||
if (PG_VALID_BE_ENCODING(ClientEncoding->encoding))
|
||||
(void) pg_verify_mbstr(ClientEncoding->encoding, s, len, false);
|
||||
if (PG_VALID_BE_ENCODING(encoding))
|
||||
(void) pg_verify_mbstr(encoding, s, len, false);
|
||||
else
|
||||
{
|
||||
int i;
|
||||
@@ -543,7 +554,11 @@ pg_client_to_server(const char *s, int len)
|
||||
return (char *) s;
|
||||
}
|
||||
|
||||
return perform_default_encoding_conversion(s, len, true);
|
||||
if (ClientEncoding->encoding == encoding)
|
||||
return perform_default_encoding_conversion(s, len, true);
|
||||
else
|
||||
return (char *) pg_do_encoding_conversion(
|
||||
(unsigned char *) s, len, encoding, DatabaseEncoding->encoding);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -551,6 +566,17 @@ pg_client_to_server(const char *s, int len)
|
||||
*/
|
||||
char *
|
||||
pg_server_to_client(const char *s, int len)
|
||||
{
|
||||
Assert(ClientEncoding);
|
||||
|
||||
return pg_any_to_server(s, len, ClientEncoding->encoding);
|
||||
}
|
||||
|
||||
/*
|
||||
* convert server encoding to any encoding.
|
||||
*/
|
||||
char *
|
||||
pg_server_to_any(const char *s, int len, int encoding)
|
||||
{
|
||||
Assert(DatabaseEncoding);
|
||||
Assert(ClientEncoding);
|
||||
@@ -558,12 +584,16 @@ pg_server_to_client(const char *s, int len)
|
||||
if (len <= 0)
|
||||
return (char *) s;
|
||||
|
||||
if (ClientEncoding->encoding == DatabaseEncoding->encoding ||
|
||||
ClientEncoding->encoding == PG_SQL_ASCII ||
|
||||
if (encoding == DatabaseEncoding->encoding ||
|
||||
encoding == PG_SQL_ASCII ||
|
||||
DatabaseEncoding->encoding == PG_SQL_ASCII)
|
||||
return (char *) s; /* assume data is valid */
|
||||
|
||||
return perform_default_encoding_conversion(s, len, false);
|
||||
if (ClientEncoding->encoding == encoding)
|
||||
return perform_default_encoding_conversion(s, len, false);
|
||||
else
|
||||
return (char *) pg_do_encoding_conversion(
|
||||
(unsigned char *) s, len, DatabaseEncoding->encoding, encoding);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user