1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-20 05:03:10 +03:00

Set psql client encoding from locale by default

Add a new libpq connection option client_encoding (which includes the
existing PGCLIENTENCODING environment variable), which besides an
encoding name accepts a special value "auto" that tries to determine
the encoding from the locale in the client's environment, using the
mechanisms that have been in use in initdb.

psql sets this new connection option to "auto" when running from a
terminal and not overridden by setting PGCLIENTENCODING.

original code by Heikki Linnakangas, with subsequent contributions by
Jaime Casanova, Peter Eisentraut, Stephen Frost, Ibrar Ahmed
This commit is contained in:
Peter Eisentraut
2011-02-19 08:54:58 +02:00
parent 327e025071
commit 02e14562a8
10 changed files with 136 additions and 24 deletions

View File

@ -58,6 +58,7 @@ pqSetenvPoll(PGconn *conn)
switch (conn->setenv_state)
{
/* These are reading states */
case SETENV_STATE_CLIENT_ENCODING_WAIT:
case SETENV_STATE_OPTION_WAIT:
case SETENV_STATE_QUERY1_WAIT:
case SETENV_STATE_QUERY2_WAIT:
@ -74,6 +75,7 @@ pqSetenvPoll(PGconn *conn)
}
/* These are writing states, so we just proceed. */
case SETENV_STATE_CLIENT_ENCODING_SEND:
case SETENV_STATE_OPTION_SEND:
case SETENV_STATE_QUERY1_SEND:
case SETENV_STATE_QUERY2_SEND:
@ -98,6 +100,39 @@ pqSetenvPoll(PGconn *conn)
{
switch (conn->setenv_state)
{
/*
* The _CLIENT_ENCODING_SEND code is slightly different
* from _OPTION_SEND below (e.g., no getenv() call), which
* is why a different state is used.
*/
case SETENV_STATE_CLIENT_ENCODING_SEND:
{
char setQuery[100]; /* note length limit in
* sprintf below */
const char *val = conn->client_encoding_initial;
if (val)
{
if (pg_strcasecmp(val, "default") == 0)
sprintf(setQuery, "SET client_encoding = DEFAULT");
else
sprintf(setQuery, "SET client_encoding = '%.60s'",
val);
#ifdef CONNECTDEBUG
fprintf(stderr,
"Sending client_encoding with %s\n",
setQuery);
#endif
if (!PQsendQuery(conn, setQuery))
goto error_return;
conn->setenv_state = SETENV_STATE_CLIENT_ENCODING_WAIT;
}
else
conn->setenv_state = SETENV_STATE_OPTION_SEND;
break;
}
case SETENV_STATE_OPTION_SEND:
{
/*
@ -142,6 +177,31 @@ pqSetenvPoll(PGconn *conn)
break;
}
case SETENV_STATE_CLIENT_ENCODING_WAIT:
{
if (PQisBusy(conn))
return PGRES_POLLING_READING;
res = PQgetResult(conn);
if (res)
{
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
PQclear(res);
goto error_return;
}
PQclear(res);
/* Keep reading until PQgetResult returns NULL */
}
else
{
/* Query finished, so send the next option */
conn->setenv_state = SETENV_STATE_OPTION_SEND;
}
break;
}
case SETENV_STATE_OPTION_WAIT:
{
if (PQisBusy(conn))