8
8
*
9
9
*
10
10
* 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 $
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
@@ -1583,8 +1583,6 @@ PQsetenvPoll(PGconn *conn)
1583
1583
{
1584
1584
PGresult * res ;
1585
1585
1586
- static const char envname [] = "PGCLIENTENCODING" ;
1587
-
1588
1586
if (conn == NULL || conn -> status == CONNECTION_BAD )
1589
1587
return PGRES_POLLING_FAILED ;
1590
1588
@@ -1625,25 +1623,23 @@ PQsetenvPoll(PGconn *conn)
1625
1623
goto error_return ;
1626
1624
}
1627
1625
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 (;;)
1632
1628
{
1633
-
1634
- case SETENV_STATE_ENCODINGS_SEND :
1629
+ switch (conn -> setenv_state )
1630
+ {
1631
+ case SETENV_STATE_ENCODINGS_SEND :
1635
1632
{
1636
- const char * env ;
1633
+ const char * env = getenv ( "PGCLIENTENCODING" ) ;
1637
1634
1638
- env = getenv (envname );
1639
1635
if (!env || * env == '\0' )
1640
1636
{
1641
1637
/*
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.
1644
1641
*/
1645
- if (!PQsendQuery (conn ,
1646
- "select getdatabaseencoding()" ))
1642
+ if (!PQsendQuery (conn , "begin; select getdatabaseencoding(); commit" ))
1647
1643
goto error_return ;
1648
1644
1649
1645
conn -> setenv_state = SETENV_STATE_ENCODINGS_WAIT ;
@@ -1662,11 +1658,14 @@ PQsetenvPoll(PGconn *conn)
1662
1658
goto error_return ;
1663
1659
}
1664
1660
conn -> client_encoding = encoding ;
1665
- }
1666
1661
1662
+ /* Move on to setting the environment options */
1663
+ conn -> setenv_state = SETENV_STATE_OPTION_SEND ;
1664
+ }
1665
+ break ;
1667
1666
}
1668
1667
1669
- case SETENV_STATE_ENCODINGS_WAIT :
1668
+ case SETENV_STATE_ENCODINGS_WAIT :
1670
1669
{
1671
1670
if (PQisBusy (conn ))
1672
1671
return PGRES_POLLING_READING ;
@@ -1675,37 +1674,35 @@ PQsetenvPoll(PGconn *conn)
1675
1674
1676
1675
if (res )
1677
1676
{
1678
- char * encoding ;
1677
+ if (PQresultStatus (res ) == PGRES_TUPLES_OK )
1678
+ {
1679
+ /* set client encoding in pg_conn struct */
1680
+ char * encoding ;
1679
1681
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 )
1681
1689
{
1682
1690
PQclear (res );
1683
1691
goto error_return ;
1684
1692
}
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 );
1692
1693
PQclear (res );
1693
-
1694
- /*
1695
- * We have to keep going in order to clear up the
1696
- * query
1697
- */
1698
- goto keep_going ;
1694
+ /* Keep reading until PQgetResult returns NULL */
1699
1695
}
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
- goto keep_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 ;
1706
1703
}
1707
1704
1708
- case SETENV_STATE_OPTION_SEND :
1705
+ case SETENV_STATE_OPTION_SEND :
1709
1706
{
1710
1707
/* Send an Environment Option */
1711
1708
char setQuery [100 ]; /* note length limits in
@@ -1740,11 +1737,10 @@ PQsetenvPoll(PGconn *conn)
1740
1737
/* No more options to send, so we are done. */
1741
1738
conn -> setenv_state = SETENV_STATE_IDLE ;
1742
1739
}
1743
-
1744
- goto keep_going ;
1740
+ break ;
1745
1741
}
1746
1742
1747
- case SETENV_STATE_OPTION_WAIT :
1743
+ case SETENV_STATE_OPTION_WAIT :
1748
1744
{
1749
1745
if (PQisBusy (conn ))
1750
1746
return PGRES_POLLING_READING ;
@@ -1758,33 +1754,29 @@ PQsetenvPoll(PGconn *conn)
1758
1754
PQclear (res );
1759
1755
goto error_return ;
1760
1756
}
1761
- /* Don't need the result */
1762
1757
PQclear (res );
1763
-
1764
- /*
1765
- * We have to keep going in order to clear up the
1766
- * query
1767
- */
1768
- goto keep_going ;
1758
+ /* Keep reading until PQgetResult returns NULL */
1769
1759
}
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
- goto keep_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 ;
1777
1768
}
1778
1769
1779
- case SETENV_STATE_IDLE :
1780
- return PGRES_POLLING_OK ;
1770
+ case SETENV_STATE_IDLE :
1771
+ return PGRES_POLLING_OK ;
1781
1772
1782
- default :
1783
- printfPQExpBuffer (& conn -> errorMessage ,
1784
- libpq_gettext ("invalid state %c, "
1785
- "probably indicative of memory corruption\n" ),
1786
- conn -> setenv_state );
1787
- goto error_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
+ goto error_return ;
1779
+ }
1788
1780
}
1789
1781
1790
1782
/* Unreachable */
0 commit comments