77 *
88 *
99 * IDENTIFICATION
10- * $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.127 2001/01/03 20:04:10 tgl Exp $
10+ * $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.128 2001/01/06 03:33:17 ishii Exp $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
@@ -74,8 +74,8 @@ static bool fe_eof;
7474static StringInfoData attribute_buf ;
7575
7676#ifdef MULTIBYTE
77- static int encoding ;
78-
77+ static int client_encoding ;
78+ static int server_encoding ;
7979#endif
8080
8181
@@ -297,7 +297,8 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe,
297297 */
298298 initStringInfo (& attribute_buf );
299299#ifdef MULTIBYTE
300- encoding = pg_get_client_encoding ();
300+ client_encoding = pg_get_client_encoding ();
301+ server_encoding = GetDatabaseEncoding ();
301302#endif
302303
303304 if (from )
@@ -1114,29 +1115,35 @@ CopyReadAttribute(FILE *fp, bool *isnull, char *delim, int *newline, char *null_
11141115 }
11151116 appendStringInfoCharMacro (& attribute_buf , c );
11161117#ifdef MULTIBYTE
1117- /* get additional bytes of the char, if any */
1118- s [0 ] = c ;
1119- mblen = pg_encoding_mblen (encoding , s );
1120- for (j = 1 ; j < mblen ; j ++ )
1118+ if (client_encoding != server_encoding )
11211119 {
1122- c = CopyGetChar (fp );
1123- if (c == EOF )
1124- goto endOfFile ;
1125- appendStringInfoCharMacro (& attribute_buf , c );
1120+ /* get additional bytes of the char, if any */
1121+ s [0 ] = c ;
1122+ mblen = pg_encoding_mblen (client_encoding , s );
1123+ for (j = 1 ; j < mblen ; j ++ )
1124+ {
1125+ c = CopyGetChar (fp );
1126+ if (c == EOF )
1127+ goto endOfFile ;
1128+ appendStringInfoCharMacro (& attribute_buf , c );
1129+ }
11261130 }
11271131#endif
11281132 }
11291133
11301134#ifdef MULTIBYTE
1131- cvt = (char * ) pg_client_to_server ((unsigned char * ) attribute_buf .data ,
1132- attribute_buf .len );
1133- if (cvt != attribute_buf .data )
1135+ if (client_encoding != server_encoding )
11341136 {
1135- /* transfer converted data back to attribute_buf */
1136- attribute_buf .len = 0 ;
1137- attribute_buf .data [0 ] = '\0' ;
1138- appendBinaryStringInfo (& attribute_buf , cvt , strlen (cvt ));
1139- pfree (cvt );
1137+ cvt = (char * ) pg_client_to_server ((unsigned char * ) attribute_buf .data ,
1138+ attribute_buf .len );
1139+ if (cvt != attribute_buf .data )
1140+ {
1141+ /* transfer converted data back to attribute_buf */
1142+ attribute_buf .len = 0 ;
1143+ attribute_buf .data [0 ] = '\0' ;
1144+ appendBinaryStringInfo (& attribute_buf , cvt , strlen (cvt ));
1145+ pfree (cvt );
1146+ }
11401147 }
11411148#endif
11421149
@@ -1163,15 +1170,22 @@ CopyAttributeOut(FILE *fp, char *server_string, char *delim)
11631170#endif
11641171
11651172#ifdef MULTIBYTE
1166- string = (char * ) pg_server_to_client ((unsigned char * ) server_string ,
1167- strlen (server_string ));
1168- string_start = string ;
1173+ if (client_encoding != server_encoding )
1174+ {
1175+ string = (char * ) pg_server_to_client ((unsigned char * ) server_string ,
1176+ strlen (server_string ));
1177+ string_start = string ;
1178+ }
1179+ else
1180+ {
1181+ string = server_string ;
1182+ }
11691183#else
11701184 string = server_string ;
11711185#endif
11721186
11731187#ifdef MULTIBYTE
1174- for (; (mblen = pg_encoding_mblen (encoding , string )) &&
1188+ for (; (mblen = ( server_encoding == client_encoding ? 1 : pg_encoding_mblen (client_encoding , string ) )) &&
11751189 ((c = * string ) != '\0' ); string += mblen )
11761190#else
11771191 for (; (c = * string ) != '\0' ; string ++ )
@@ -1188,7 +1202,7 @@ CopyAttributeOut(FILE *fp, char *server_string, char *delim)
11881202 }
11891203
11901204#ifdef MULTIBYTE
1191- if (string_start != server_string )
1205+ if (client_encoding != server_encoding )
11921206 pfree (string_start ); /* pfree pg_server_to_client result */
11931207#endif
11941208}
0 commit comments