From d4e62e5deda4e067c335736780fbdcfbbd9ce140 Mon Sep 17 00:00:00 2001
From: Tatsuo Ishii <ishii@postgresql.org>
Date: Sat, 5 Feb 2000 12:33:22 +0000
Subject: [PATCH] Change function name PQclientencoding to PQclientEncoding
 since it seems more suitable for the naming convention in libpq.

New function PQsetClientEncoding added. It makes possible to change
the client encoding on the fly without setting PGCLIENTENCODING.
---
 src/interfaces/libpq/fe-connect.c | 46 +++++++++++++++++++++++++++++--
 src/interfaces/libpq/fe-print.c   |  4 +--
 src/interfaces/libpq/libpq-fe.h   |  5 ++--
 3 files changed, 49 insertions(+), 6 deletions(-)

diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index f899aaff624..92d32917ef2 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.116 2000/01/26 05:58:45 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.117 2000/02/05 12:33:22 ishii Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -41,6 +41,7 @@
 #endif
 
 #ifdef MULTIBYTE
+#include "miscadmin.h"
 #include "mb/pg_wchar.h"
 #endif
 
@@ -2381,13 +2382,54 @@ PQbackendPID(const PGconn *conn)
 }
 
 int
-PQclientencoding(const PGconn *conn)
+PQclientEncoding(const PGconn *conn)
 {
 	if (!conn || conn->status != CONNECTION_OK)
 		return -1;
 	return conn->client_encoding;
 }
 
+#ifdef MULTIBYTE
+int
+PQsetClientEncoding(PGconn *conn, const char *encoding)
+{
+	char qbuf[128];
+	static char query[] = "set client_encoding to '%s'";
+	PGresult *res;
+	int status;
+
+	if (!conn || conn->status != CONNECTION_OK)
+		return -1;
+
+	/* check query buffer overflow */
+	if (sizeof(qbuf) < (sizeof(query) + strlen(encoding)))
+		return -1;
+
+	/* ok, now send a query */
+	sprintf(qbuf, query, encoding);
+	res = PQexec(conn, qbuf);
+
+	if (res == (PGresult *)NULL)
+		return -1;
+	if (res->resultStatus != PGRES_COMMAND_OK)
+		status = -1;
+	else
+	{
+		/* change libpq internal encoding */
+		conn->client_encoding = pg_char_to_encoding(encoding);
+		status = 0;	/* everything is ok */
+	}
+	PQclear(res);
+	return(status);
+}
+#else
+int
+PQsetClientEncoding(PGconn *conn, const char *encoding)
+{
+	return -1;
+}
+#endif
+
 void
 PQtrace(PGconn *conn, FILE *debug_port)
 {
diff --git a/src/interfaces/libpq/fe-print.c b/src/interfaces/libpq/fe-print.c
index 7c0e655f2a8..e54167cc1fe 100644
--- a/src/interfaces/libpq/fe-print.c
+++ b/src/interfaces/libpq/fe-print.c
@@ -10,7 +10,7 @@
  * didn't really belong there.
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-print.c,v 1.33 2000/01/29 16:58:51 petere Exp $
+ *	  $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-print.c,v 1.34 2000/02/05 12:33:22 ishii Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -347,7 +347,7 @@ do_field(const PQprintOpt *po, const PGresult *res,
 			char		ch = '0';
 
 #ifdef MULTIBYTE
-			for (p = pval; *p; p += PQmblen(p, PQclientencoding(res->conn)))
+			for (p = pval; *p; p += PQmblen(p, PQclientEncoding(res->conn)))
 #else
 			for (p = pval; *p; p++)
 #endif
diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h
index 35bbb6eff3f..8acb622ad16 100644
--- a/src/interfaces/libpq/libpq-fe.h
+++ b/src/interfaces/libpq/libpq-fe.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: libpq-fe.h,v 1.58 2000/01/29 16:58:51 petere Exp $
+ * $Id: libpq-fe.h,v 1.59 2000/02/05 12:33:22 ishii Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -218,7 +218,8 @@ extern		"C"
 	extern const char *PQerrorMessage(const PGconn *conn);
 	extern int	PQsocket(const PGconn *conn);
 	extern int	PQbackendPID(const PGconn *conn);
-	extern int	PQclientencoding(const PGconn *conn);
+	extern int	PQclientEncoding(const PGconn *conn);
+	extern int	PQsetClientEncoding(PGconn *conn, const char *encoding);
 
 	/* Enable/disable tracing */
 	extern void PQtrace(PGconn *conn, FILE *debug_port);
-- 
GitLab