minor changes in psql's \encoding command - Mailing list pgsql-patches

From Weiping He
Subject minor changes in psql's \encoding command
Date
Msg-id 3E17A384.4070008@zhengmai.com.cn
Whole thread Raw
Responses Re: minor changes in psql's \encoding command
List pgsql-patches
Hi all,

   While developing one of our applicatins,
   we found that psql's meta command \encoding
   return different result to what
   "SHOW CLIENT_ENCODING" command return.
   And it make us somewhat difficult to determine
   our problem. After take a look at source of psql
   and libpq, I found that the \encoding command
   only use the encoding setting while connection was made.
   don't know why, but a few changes make \encoding command
   synchronized with "SHOW CLIENT_ENCODING" &
   "SET CLIENT_ENCODING TO 'something'"

   please apply it if it's useful.

   regards    laser
*** fe-connect.c.old    Sun Jan  5 09:34:00 2003
--- fe-connect.c    Sun Jan  5 09:46:25 2003
***************
*** 2730,2739 ****
  }

  int
! PQclientEncoding(const PGconn *conn)
  {
      if (!conn || conn->status != CONNECTION_OK)
          return -1;
      return conn->client_encoding;
  }

--- 2730,2754 ----
  }

  int
! PQclientEncoding(PGconn *conn)
  {
+     static char query[] = "show client_encoding";
+     PGresult    *res;
+     char    *encoding;
+
      if (!conn || conn->status != CONNECTION_OK)
          return -1;
+     res = PQexec(conn, query);
+
+     if(res == (PGresult *) NULL)
+         return -1;
+     if(res->resultStatus != PGRES_TUPLES_OK)
+         return -1;
+     else{
+         encoding = PQgetvalue(res, 0, 0);
+         conn->client_encoding = pg_char_to_encoding(encoding);
+     }
+     PQclear(res);
      return conn->client_encoding;
  }

*** command.c.old    Sun Jan  5 10:05:29 2003
--- command.c    Sun Jan  5 10:06:17 2003
***************
*** 455,463 ****
      {
          char       *encoding = scan_option(&string, OT_NORMAL, NULL, false);

!         if (!encoding)
              /* show encoding */
              puts(pg_encoding_to_char(pset.encoding));
          else
          {
              /* set encoding */
--- 455,465 ----
      {
          char       *encoding = scan_option(&string, OT_NORMAL, NULL, false);

!         if (!encoding){
!             pset.encoding = PQclientEncoding(pset.db);
              /* show encoding */
              puts(pg_encoding_to_char(pset.encoding));
+         }
          else
          {
              /* set encoding */
*** libpq-fe.h.old    Sun Jan  5 09:47:59 2003
--- libpq-fe.h    Sun Jan  5 09:48:22 2003
***************
*** 227,233 ****
  extern 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    PQsetClientEncoding(PGconn *conn, const char *encoding);

  #ifdef USE_SSL
--- 227,233 ----
  extern char *PQerrorMessage(const PGconn *conn);
  extern int    PQsocket(const PGconn *conn);
  extern int    PQbackendPID(const PGconn *conn);
! extern int    PQclientEncoding(PGconn *conn);
  extern int    PQsetClientEncoding(PGconn *conn, const char *encoding);

  #ifdef USE_SSL

pgsql-patches by date:

Previous
From: Tom Lane
Date:
Subject: Re: Fix for HP-UX shared library builds
Next
From: Tom Lane
Date:
Subject: Re: minor changes in psql's \encoding command