<!--
-$Header: /cvsroot/pgsql/doc/src/sgml/libpq.sgml,v 1.41 2000/09/29 20:21:34 petere Exp $
+$Header: /cvsroot/pgsql/doc/src/sgml/libpq.sgml,v 1.42 2000/10/03 19:16:16 petere Exp $
-->
<chapter id="libpq-chapter">
<filename>libpq</filename> library.
</para>
- <sect1 id="libpq-connect">
- <title>Database Connection Functions</title>
+ <sect1 id="libpq-connect">
+ <title>Database Connection Functions</title>
- <para>
- The following routines deal with making a connection to
- a <productname>Postgres</productname> backend server. The application
- program can have several backend connections open at one time.
- (One reason to do that is to access more than one database.)
- Each connection is represented by a PGconn object which is obtained
- from PQconnectdb() or PQsetdbLogin(). Note that these functions
- will always return a non-null object pointer, unless perhaps
- there is too little memory even to allocate the PGconn object.
- The PQstatus function should be called
- to check whether a connection was successfully made
- before queries are sent via the connection object.
+ <para>
+ The following routines deal with making a connection to a
+ <productname>Postgres</productname> backend server. The
+ application program can have several backend connections open at
+ one time. (One reason to do that is to access more than one
+ database.) Each connection is represented by a
+ <structname>PGconn</> object which is obtained from
+ <function>PQconnectdb</> or <function>PQsetdbLogin</>. Note that
+ these functions will always return a non-null object pointer,
+ unless perhaps there is too little memory even to allocate the
+ <structname>PGconn</> object. The <function>PQstatus</> function
+ should be called to check whether a connection was successfully
+ made before queries are sent via the connection object.
<itemizedlist>
<listitem>
<listitem>
<para>
Name of host to connect to. If a non-zero-length string is
- specified, TCP/IP
- communication is used. Using this parameter causes a hostname look-up.
- See hostaddr.
+ specified, TCP/IP communication is used, else Unix sockets.
+ Using this parameter causes a hostname look-up. See hostaddr.
</para>
</listitem>
</varlistentry>
</sect1>
-<sect1 id="libpq-sample">
-<title>Sample Programs</title>
-<sect2>
-<title>Sample Program 1</title>
+ <sect1 id="libpq-example">
+ <title>Example Programs</title>
+
+ <example id="libpq-example-1">
+ <title>libpq Example Program 1</title>
-<para>
<programlisting>
/*
- * testlibpq.c Test the C version of Libpq, the Postgres frontend
- * library.
- *
+ * testlibpq.c
*
+ * Test the C version of libpq, the PostgreSQL frontend
+ * library.
*/
#include <stdio.h>
-#include "libpq-fe.h"
+#include <libpq-fe.h>
void
exit_nicely(PGconn *conn)
}
</programlisting>
-</para>
-</sect2>
+ </example>
-<sect2>
-<title>Sample Program 2</title>
+ <example id="libpq-example-2">
+ <title>libpq Example Program 2</title>
-<para>
<programlisting>
/*
* testlibpq2.c
return 0;
}
</programlisting>
-</para>
-</sect2>
+ </example>
-<sect2>
-<title>Sample Program 3</title>
+ <example id="libpq-example-3">
+ <title>libpq Example Program 3</>
-<para>
<programlisting>
/*
* testlibpq3.c Test the C version of Libpq, the Postgres frontend
return 0;
}
</programlisting>
-</para>
+ </example>
-</sect2>
-</sect1>
+ </sect1>
</chapter>
<!-- Keep this comment at the end of the file
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.136 2000/10/03 03:39:46 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.137 2000/10/03 19:16:17 petere Exp $
*
*-------------------------------------------------------------------------
*/
if (conn == NULL)
return (PGconn *) NULL;
- if ((pghost == NULL) || pghost[0] == '\0')
- {
- if ((tmp = getenv("PGHOST")) != NULL)
- conn->pghost = strdup(tmp);
- }
- else
+ if (pghost)
conn->pghost = strdup(pghost);
+ else if ((tmp = getenv("PGHOST")) != NULL)
+ conn->pghost = strdup(tmp);
- if ((pgport == NULL) || pgport[0] == '\0')
+ if (pgport == NULL)
{
if ((tmp = getenv("PGPORT")) == NULL)
tmp = DEF_PGPORT_STR;
else
conn->pgport = strdup(pgport);
- if ((pgtty == NULL) || pgtty[0] == '\0')
+ if (pgtty == NULL)
{
if ((tmp = getenv("PGTTY")) == NULL)
tmp = DefaultTty;
else
conn->pgtty = strdup(pgtty);
- if ((pgoptions == NULL) || pgoptions[0] == '\0')
+ if (pgoptions == NULL)
{
if ((tmp = getenv("PGOPTIONS")) == NULL)
tmp = DefaultOption;
else
conn->pgpass = strdup(DefaultPassword);
- if ((dbName == NULL) || dbName[0] == '\0')
+ if (dbName == NULL)
{
if ((tmp = getenv("PGDATABASE")) != NULL)
conn->dbName = strdup(tmp);
MemSet((char *) &conn->raddr, 0, sizeof(conn->raddr));
- if (conn->pghostaddr != NULL)
+ if (conn->pghostaddr != NULL && conn->pghostaddr[0] != '\0')
{
/* Using pghostaddr avoids a hostname lookup */
/* Note that this supports IPv4 only */
memmove((char *) &(conn->raddr.in.sin_addr),
(char *) &addr, sizeof(addr));
}
- else if (conn->pghost != NULL)
+ else if (conn->pghost != NULL && conn->pghost[0] != '\0')
{
/* Using pghost, so we have to look-up the hostname */
struct hostent *hp;