88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.215 2002/12/06 04:37:05 momjian Exp $
11+ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.216 2002/12/19 19:30:24 momjian Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
@@ -186,7 +186,7 @@ static int parseServiceInfo(PQconninfoOption *options,
186186PQExpBuffer errorMessage );
187187char * pwdfMatchesString (char * buf ,char * token );
188188char * PasswordFromFile (char * hostname ,char * port ,char * dbname ,
189- char * username );
189+ char * username );
190190
191191/*
192192 *Connecting to a Database
@@ -1054,7 +1054,7 @@ static int
10541054connectDBComplete (PGconn * conn )
10551055{
10561056PostgresPollingStatusType flag = PGRES_POLLING_WRITING ;
1057- time_t finish_time = ((time_t )- 1 );
1057+ time_t finish_time = ((time_t )- 1 );
10581058
10591059if (conn == NULL || conn -> status == CONNECTION_BAD )
10601060return 0 ;
@@ -1064,11 +1064,14 @@ connectDBComplete(PGconn *conn)
10641064 */
10651065if (conn -> connect_timeout != NULL )
10661066{
1067- int timeout = atoi (conn -> connect_timeout );
1067+ int timeout = atoi (conn -> connect_timeout );
10681068
10691069if (timeout > 0 )
10701070{
1071- /* Rounding could cause connection to fail; need at least 2 secs */
1071+ /*
1072+ * Rounding could cause connection to fail; need at least 2
1073+ * secs
1074+ */
10721075if (timeout < 2 )
10731076timeout = 2 ;
10741077/* calculate the finish time based on start + timeout */
@@ -1611,151 +1614,157 @@ PQsetenvPoll(PGconn *conn)
16111614switch (conn -> setenv_state )
16121615{
16131616case SETENV_STATE_ENCODINGS_SEND :
1614- {
1615- const char * env = getenv ("PGCLIENTENCODING" );
1616-
1617- if (!env || * env == '\0' )
1618- {
1619- /*
1620- * PGCLIENTENCODING is not specified, so query server
1621- * for it. We must use begin/commit in case autocommit
1622- * is off by default.
1623- */
1624- if (!PQsendQuery (conn ,"begin; select getdatabaseencoding(); commit" ))
1625- gotoerror_return ;
1626-
1627- conn -> setenv_state = SETENV_STATE_ENCODINGS_WAIT ;
1628- return PGRES_POLLING_READING ;
1629- }
1630- else
16311617{
1632- /* otherwise set client encoding in pg_conn struct */
1633- int encoding = pg_char_to_encoding (env );
1618+ const char * env = getenv ("PGCLIENTENCODING" );
16341619
1635- if (encoding < 0 )
1620+ if (! env || * env == '\0' )
16361621{
1637- printfPQExpBuffer (& conn -> errorMessage ,
1638- libpq_gettext ("invalid encoding name in PGCLIENTENCODING: %s\n" ),
1639- env );
1640- gotoerror_return ;
1622+ /*
1623+ * PGCLIENTENCODING is not specified, so query
1624+ * server for it. We must use begin/commit in
1625+ * case autocommit is off by default.
1626+ */
1627+ if (!PQsendQuery (conn ,"begin; select getdatabaseencoding(); commit" ))
1628+ gotoerror_return ;
1629+
1630+ conn -> setenv_state = SETENV_STATE_ENCODINGS_WAIT ;
1631+ return PGRES_POLLING_READING ;
16411632}
1642- conn -> client_encoding = encoding ;
1633+ else
1634+ {
1635+ /* otherwise set client encoding in pg_conn struct */
1636+ int encoding = pg_char_to_encoding (env );
16431637
1644- /* Move on to setting the environment options */
1645- conn -> setenv_state = SETENV_STATE_OPTION_SEND ;
1638+ if (encoding < 0 )
1639+ {
1640+ printfPQExpBuffer (& conn -> errorMessage ,
1641+ libpq_gettext ("invalid encoding name in PGCLIENTENCODING: %s\n" ),
1642+ env );
1643+ gotoerror_return ;
1644+ }
1645+ conn -> client_encoding = encoding ;
1646+
1647+ /* Move on to setting the environment options */
1648+ conn -> setenv_state = SETENV_STATE_OPTION_SEND ;
1649+ }
1650+ break ;
16461651}
1647- break ;
1648- }
16491652
16501653case SETENV_STATE_ENCODINGS_WAIT :
1651- {
1652- if (PQisBusy (conn ))
1653- return PGRES_POLLING_READING ;
1654+ {
1655+ if (PQisBusy (conn ))
1656+ return PGRES_POLLING_READING ;
16541657
1655- res = PQgetResult (conn );
1658+ res = PQgetResult (conn );
16561659
1657- if (res )
1658- {
1659- if (PQresultStatus (res )== PGRES_TUPLES_OK )
1660+ if (res )
16601661{
1661- /* set client encoding in pg_conn struct */
1662- char * encoding ;
1663-
1664- encoding = PQgetvalue (res ,0 ,0 );
1665- if (!encoding )/* this should not happen */
1666- conn -> client_encoding = PG_SQL_ASCII ;
1667- else
1668- conn -> client_encoding = pg_char_to_encoding (encoding );
1662+ if (PQresultStatus (res )== PGRES_TUPLES_OK )
1663+ {
1664+ /* set client encoding in pg_conn struct */
1665+ char * encoding ;
1666+
1667+ encoding = PQgetvalue (res ,0 ,0 );
1668+ if (!encoding )/* this should not happen */
1669+ conn -> client_encoding = PG_SQL_ASCII ;
1670+ else
1671+ conn -> client_encoding = pg_char_to_encoding (encoding );
1672+ }
1673+ else if (PQresultStatus (res )!= PGRES_COMMAND_OK )
1674+ {
1675+ PQclear (res );
1676+ gotoerror_return ;
1677+ }
1678+ PQclear (res );
1679+ /* Keep reading until PQgetResult returns NULL */
16691680}
1670- else if ( PQresultStatus ( res ) != PGRES_COMMAND_OK )
1681+ else
16711682{
1672- PQclear (res );
1673- gotoerror_return ;
1683+ /*
1684+ * NULL result indicates that the query is
1685+ * finished
1686+ */
1687+ /* Move on to setting the environment options */
1688+ conn -> setenv_state = SETENV_STATE_OPTION_SEND ;
16741689}
1675- PQclear (res );
1676- /* Keep reading until PQgetResult returns NULL */
1677- }
1678- else
1679- {
1680- /* NULL result indicates that the query is finished */
1681- /* Move on to setting the environment options */
1682- conn -> setenv_state = SETENV_STATE_OPTION_SEND ;
1690+ break ;
16831691}
1684- break ;
1685- }
16861692
16871693case SETENV_STATE_OPTION_SEND :
1688- {
1689- /* Send an Environment Option */
1690- char setQuery [100 ];/* note length limits in
1691- * sprintf's below */
1692-
1693- if (conn -> next_eo -> envName )
16941694{
1695- const char * val ;
1695+ /* Send an Environment Option */
1696+ char setQuery [100 ];/* note length limits in
1697+ * sprintf's below */
16961698
1697- if (( val = getenv ( conn -> next_eo -> envName )) )
1699+ if (conn -> next_eo -> envName )
16981700{
1699- if (strcasecmp (val ,"default" )== 0 )
1700- sprintf (setQuery ,"SET %s = %.60s" ,
1701- conn -> next_eo -> pgName ,val );
1702- else
1703- sprintf (setQuery ,"SET %s = '%.60s'" ,
1704- conn -> next_eo -> pgName ,val );
1701+ const char * val ;
1702+
1703+ if ((val = getenv (conn -> next_eo -> envName )))
1704+ {
1705+ if (strcasecmp (val ,"default" )== 0 )
1706+ sprintf (setQuery ,"SET %s = %.60s" ,
1707+ conn -> next_eo -> pgName ,val );
1708+ else
1709+ sprintf (setQuery ,"SET %s = '%.60s'" ,
1710+ conn -> next_eo -> pgName ,val );
17051711#ifdef CONNECTDEBUG
1706- printf ("Use environment variable %s to send %s\n" ,
1707- conn -> next_eo -> envName ,setQuery );
1712+ printf ("Use environment variable %s to send %s\n" ,
1713+ conn -> next_eo -> envName ,setQuery );
17081714#endif
1709- if (!PQsendQuery (conn ,setQuery ))
1710- gotoerror_return ;
1715+ if (!PQsendQuery (conn ,setQuery ))
1716+ gotoerror_return ;
17111717
1712- conn -> setenv_state = SETENV_STATE_OPTION_WAIT ;
1718+ conn -> setenv_state = SETENV_STATE_OPTION_WAIT ;
1719+ }
1720+ else
1721+ conn -> next_eo ++ ;
17131722}
17141723else
1715- conn -> next_eo ++ ;
1716- }
1717- else
1718- {
1719- /* No more options to send, so we are done. */
1720- conn -> setenv_state = SETENV_STATE_IDLE ;
1724+ {
1725+ /* No more options to send, so we are done. */
1726+ conn -> setenv_state = SETENV_STATE_IDLE ;
1727+ }
1728+ break ;
17211729}
1722- break ;
1723- }
17241730
17251731case SETENV_STATE_OPTION_WAIT :
1726- {
1727- if (PQisBusy (conn ))
1728- return PGRES_POLLING_READING ;
1732+ {
1733+ if (PQisBusy (conn ))
1734+ return PGRES_POLLING_READING ;
17291735
1730- res = PQgetResult (conn );
1736+ res = PQgetResult (conn );
17311737
1732- if (res )
1733- {
1734- if (PQresultStatus (res )!= PGRES_COMMAND_OK )
1738+ if (res )
17351739{
1740+ if (PQresultStatus (res )!= PGRES_COMMAND_OK )
1741+ {
1742+ PQclear (res );
1743+ gotoerror_return ;
1744+ }
17361745PQclear (res );
1737- goto error_return ;
1746+ /* Keep reading until PQgetResult returns NULL */
17381747}
1739- PQclear (res );
1740- /* Keep reading until PQgetResult returns NULL */
1741- }
1742- else
1743- {
1744- /* NULL result indicates that the query is finished */
1745- /* Send the next option */
1746- conn -> next_eo ++ ;
1747- conn -> setenv_state = SETENV_STATE_OPTION_SEND ;
1748+ else
1749+ {
1750+ /*
1751+ * NULL result indicates that the query is
1752+ * finished
1753+ */
1754+ /* Send the next option */
1755+ conn -> next_eo ++ ;
1756+ conn -> setenv_state = SETENV_STATE_OPTION_SEND ;
1757+ }
1758+ break ;
17481759}
1749- break ;
1750- }
17511760
17521761case SETENV_STATE_IDLE :
17531762return PGRES_POLLING_OK ;
17541763
17551764default :
17561765printfPQExpBuffer (& conn -> errorMessage ,
17571766libpq_gettext ("invalid state %c, "
1758- "probably indicative of memory corruption\n" ),
1767+ "probably indicative of memory corruption\n" ),
17591768conn -> setenv_state );
17601769gotoerror_return ;
17611770}