88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.209 2002/10/14 17:15:11 momjian Exp $
11+ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.210 2002/10/15 01:48:25 tgl Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
@@ -1583,8 +1583,6 @@ PQsetenvPoll(PGconn *conn)
15831583{
15841584PGresult * res ;
15851585
1586- static const char envname []= "PGCLIENTENCODING" ;
1587-
15881586if (conn == NULL || conn -> status == CONNECTION_BAD )
15891587return PGRES_POLLING_FAILED ;
15901588
@@ -1625,25 +1623,23 @@ PQsetenvPoll(PGconn *conn)
16251623gotoerror_return ;
16261624}
16271625
1628-
1629- keep_going :/* We will come back to here until there
1630- * is nothing left to parse. */
1631- switch (conn -> setenv_state )
1626+ /* We will loop here until there is nothing left to do in this call. */
1627+ for (;;)
16321628{
1633-
1634- case SETENV_STATE_ENCODINGS_SEND :
1629+ switch (conn -> setenv_state )
1630+ {
1631+ case SETENV_STATE_ENCODINGS_SEND :
16351632{
1636- const char * env ;
1633+ const char * env = getenv ( "PGCLIENTENCODING" ) ;
16371634
1638- env = getenv (envname );
16391635if (!env || * env == '\0' )
16401636{
16411637/*
1642- * query server encoding if PGCLIENTENCODING is not
1643- * specified
1638+ * PGCLIENTENCODING is not specified, so query server
1639+ * for it. We must use begin/commit in case autocommit
1640+ * is off by default.
16441641 */
1645- if (!PQsendQuery (conn ,
1646- "select getdatabaseencoding()" ))
1642+ if (!PQsendQuery (conn ,"begin; select getdatabaseencoding(); commit" ))
16471643gotoerror_return ;
16481644
16491645conn -> setenv_state = SETENV_STATE_ENCODINGS_WAIT ;
@@ -1662,11 +1658,14 @@ PQsetenvPoll(PGconn *conn)
16621658gotoerror_return ;
16631659}
16641660conn -> client_encoding = encoding ;
1665- }
16661661
1662+ /* Move on to setting the environment options */
1663+ conn -> setenv_state = SETENV_STATE_OPTION_SEND ;
1664+ }
1665+ break ;
16671666}
16681667
1669- case SETENV_STATE_ENCODINGS_WAIT :
1668+ case SETENV_STATE_ENCODINGS_WAIT :
16701669{
16711670if (PQisBusy (conn ))
16721671return PGRES_POLLING_READING ;
@@ -1675,37 +1674,35 @@ PQsetenvPoll(PGconn *conn)
16751674
16761675if (res )
16771676{
1678- char * encoding ;
1677+ if (PQresultStatus (res )== PGRES_TUPLES_OK )
1678+ {
1679+ /* set client encoding in pg_conn struct */
1680+ char * encoding ;
16791681
1680- if (PQresultStatus (res )!= PGRES_TUPLES_OK )
1682+ encoding = PQgetvalue (res ,0 ,0 );
1683+ if (!encoding )/* this should not happen */
1684+ conn -> client_encoding = PG_SQL_ASCII ;
1685+ else
1686+ conn -> client_encoding = pg_char_to_encoding (encoding );
1687+ }
1688+ else if (PQresultStatus (res )!= PGRES_COMMAND_OK )
16811689{
16821690PQclear (res );
16831691gotoerror_return ;
16841692}
1685-
1686- /* set client encoding in pg_conn struct */
1687- encoding = PQgetvalue (res ,0 ,0 );
1688- if (!encoding )/* this should not happen */
1689- conn -> client_encoding = PG_SQL_ASCII ;
1690- else
1691- conn -> client_encoding = pg_char_to_encoding (encoding );
16921693PQclear (res );
1693-
1694- /*
1695- * We have to keep going in order to clear up the
1696- * query
1697- */
1698- gotokeep_going ;
1694+ /* Keep reading until PQgetResult returns NULL */
16991695}
1700-
1701- /* NULL result indicates that the query is finished */
1702-
1703- /* Move on to setting the environment options */
1704- conn -> setenv_state = SETENV_STATE_OPTION_SEND ;
1705- gotokeep_going ;
1696+ else
1697+ {
1698+ /* NULL result indicates that the query is finished */
1699+ /* Move on to setting the environment options */
1700+ conn -> setenv_state = SETENV_STATE_OPTION_SEND ;
1701+ }
1702+ break ;
17061703}
17071704
1708- case SETENV_STATE_OPTION_SEND :
1705+ case SETENV_STATE_OPTION_SEND :
17091706{
17101707/* Send an Environment Option */
17111708char setQuery [100 ];/* note length limits in
@@ -1740,11 +1737,10 @@ PQsetenvPoll(PGconn *conn)
17401737/* No more options to send, so we are done. */
17411738conn -> setenv_state = SETENV_STATE_IDLE ;
17421739}
1743-
1744- gotokeep_going ;
1740+ break ;
17451741}
17461742
1747- case SETENV_STATE_OPTION_WAIT :
1743+ case SETENV_STATE_OPTION_WAIT :
17481744{
17491745if (PQisBusy (conn ))
17501746return PGRES_POLLING_READING ;
@@ -1758,33 +1754,29 @@ PQsetenvPoll(PGconn *conn)
17581754PQclear (res );
17591755gotoerror_return ;
17601756}
1761- /* Don't need the result */
17621757PQclear (res );
1763-
1764- /*
1765- * We have to keep going in order to clear up the
1766- * query
1767- */
1768- gotokeep_going ;
1758+ /* Keep reading until PQgetResult returns NULL */
17691759}
1770-
1771- /* NULL result indicates that the query is finished */
1772-
1773- /* Send the next option */
1774- conn -> next_eo ++ ;
1775- conn -> setenv_state = SETENV_STATE_OPTION_SEND ;
1776- gotokeep_going ;
1760+ else
1761+ {
1762+ /* NULL result indicates that the query is finished */
1763+ /* Send the next option */
1764+ conn -> next_eo ++ ;
1765+ conn -> setenv_state = SETENV_STATE_OPTION_SEND ;
1766+ }
1767+ break ;
17771768}
17781769
1779- case SETENV_STATE_IDLE :
1780- return PGRES_POLLING_OK ;
1770+ case SETENV_STATE_IDLE :
1771+ return PGRES_POLLING_OK ;
17811772
1782- default :
1783- printfPQExpBuffer (& conn -> errorMessage ,
1784- libpq_gettext ("invalid state %c, "
1785- "probably indicative of memory corruption\n" ),
1786- conn -> setenv_state );
1787- gotoerror_return ;
1773+ default :
1774+ printfPQExpBuffer (& conn -> errorMessage ,
1775+ libpq_gettext ("invalid state %c, "
1776+ "probably indicative of memory corruption\n" ),
1777+ conn -> setenv_state );
1778+ gotoerror_return ;
1779+ }
17881780}
17891781
17901782/* Unreachable */