Commit 7c5a444f authored by Peter Eisentraut's avatar Peter Eisentraut

Treat empty connection parameters as is, in particular to enable overriding

environment variables with "nothing".  Empty host parameter indicates
Unix socket.
parent 3189e484
<!-- <!--
$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"> <chapter id="libpq-chapter">
...@@ -38,21 +38,22 @@ $Header: /cvsroot/pgsql/doc/src/sgml/libpq.sgml,v 1.41 2000/09/29 20:21:34 peter ...@@ -38,21 +38,22 @@ $Header: /cvsroot/pgsql/doc/src/sgml/libpq.sgml,v 1.41 2000/09/29 20:21:34 peter
<filename>libpq</filename> library. <filename>libpq</filename> library.
</para> </para>
<sect1 id="libpq-connect"> <sect1 id="libpq-connect">
<title>Database Connection Functions</title> <title>Database Connection Functions</title>
<para> <para>
The following routines deal with making a connection to The following routines deal with making a connection to a
a <productname>Postgres</productname> backend server. The application <productname>Postgres</productname> backend server. The
program can have several backend connections open at one time. application program can have several backend connections open at
(One reason to do that is to access more than one database.) one time. (One reason to do that is to access more than one
Each connection is represented by a PGconn object which is obtained database.) Each connection is represented by a
from PQconnectdb() or PQsetdbLogin(). Note that these functions <structname>PGconn</> object which is obtained from
will always return a non-null object pointer, unless perhaps <function>PQconnectdb</> or <function>PQsetdbLogin</>. Note that
there is too little memory even to allocate the PGconn object. these functions will always return a non-null object pointer,
The PQstatus function should be called unless perhaps there is too little memory even to allocate the
to check whether a connection was successfully made <structname>PGconn</> object. The <function>PQstatus</> function
before queries are sent via the connection object. should be called to check whether a connection was successfully
made before queries are sent via the connection object.
<itemizedlist> <itemizedlist>
<listitem> <listitem>
...@@ -87,9 +88,8 @@ PGconn *PQconnectdb(const char *conninfo) ...@@ -87,9 +88,8 @@ PGconn *PQconnectdb(const char *conninfo)
<listitem> <listitem>
<para> <para>
Name of host to connect to. If a non-zero-length string is Name of host to connect to. If a non-zero-length string is
specified, TCP/IP specified, TCP/IP communication is used, else Unix sockets.
communication is used. Using this parameter causes a hostname look-up. Using this parameter causes a hostname look-up. See hostaddr.
See hostaddr.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
...@@ -1930,22 +1930,22 @@ call <function>fe_setauthsvc</function> at all. ...@@ -1930,22 +1930,22 @@ call <function>fe_setauthsvc</function> at all.
</sect1> </sect1>
<sect1 id="libpq-sample">
<title>Sample Programs</title>
<sect2> <sect1 id="libpq-example">
<title>Sample Program 1</title> <title>Example Programs</title>
<example id="libpq-example-1">
<title>libpq Example Program 1</title>
<para>
<programlisting> <programlisting>
/* /*
* testlibpq.c Test the C version of Libpq, the Postgres frontend * testlibpq.c
* library.
*
* *
* Test the C version of libpq, the PostgreSQL frontend
* library.
*/ */
#include &lt;stdio.h&gt; #include &lt;stdio.h&gt;
#include "libpq-fe.h" #include &lt;libpq-fe.h&gt;
void void
exit_nicely(PGconn *conn) exit_nicely(PGconn *conn)
...@@ -2065,13 +2065,11 @@ main() ...@@ -2065,13 +2065,11 @@ main()
} }
</programlisting> </programlisting>
</para> </example>
</sect2>
<sect2> <example id="libpq-example-2">
<title>Sample Program 2</title> <title>libpq Example Program 2</title>
<para>
<programlisting> <programlisting>
/* /*
* testlibpq2.c * testlibpq2.c
...@@ -2187,13 +2185,11 @@ main() ...@@ -2187,13 +2185,11 @@ main()
return 0; return 0;
} }
</programlisting> </programlisting>
</para> </example>
</sect2>
<sect2> <example id="libpq-example-3">
<title>Sample Program 3</title> <title>libpq Example Program 3</>
<para>
<programlisting> <programlisting>
/* /*
* testlibpq3.c Test the C version of Libpq, the Postgres frontend * testlibpq3.c Test the C version of Libpq, the Postgres frontend
...@@ -2370,10 +2366,9 @@ main() ...@@ -2370,10 +2366,9 @@ main()
return 0; return 0;
} }
</programlisting> </programlisting>
</para> </example>
</sect2> </sect1>
</sect1>
</chapter> </chapter>
<!-- Keep this comment at the end of the file <!-- Keep this comment at the end of the file
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * 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 $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -416,15 +416,12 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions, ...@@ -416,15 +416,12 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions,
if (conn == NULL) if (conn == NULL)
return (PGconn *) NULL; return (PGconn *) NULL;
if ((pghost == NULL) || pghost[0] == '\0') if (pghost)
{
if ((tmp = getenv("PGHOST")) != NULL)
conn->pghost = strdup(tmp);
}
else
conn->pghost = strdup(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) if ((tmp = getenv("PGPORT")) == NULL)
tmp = DEF_PGPORT_STR; tmp = DEF_PGPORT_STR;
...@@ -433,7 +430,7 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions, ...@@ -433,7 +430,7 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions,
else else
conn->pgport = strdup(pgport); conn->pgport = strdup(pgport);
if ((pgtty == NULL) || pgtty[0] == '\0') if (pgtty == NULL)
{ {
if ((tmp = getenv("PGTTY")) == NULL) if ((tmp = getenv("PGTTY")) == NULL)
tmp = DefaultTty; tmp = DefaultTty;
...@@ -442,7 +439,7 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions, ...@@ -442,7 +439,7 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions,
else else
conn->pgtty = strdup(pgtty); conn->pgtty = strdup(pgtty);
if ((pgoptions == NULL) || pgoptions[0] == '\0') if (pgoptions == NULL)
{ {
if ((tmp = getenv("PGOPTIONS")) == NULL) if ((tmp = getenv("PGOPTIONS")) == NULL)
tmp = DefaultOption; tmp = DefaultOption;
...@@ -476,7 +473,7 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions, ...@@ -476,7 +473,7 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions,
else else
conn->pgpass = strdup(DefaultPassword); conn->pgpass = strdup(DefaultPassword);
if ((dbName == NULL) || dbName[0] == '\0') if (dbName == NULL)
{ {
if ((tmp = getenv("PGDATABASE")) != NULL) if ((tmp = getenv("PGDATABASE")) != NULL)
conn->dbName = strdup(tmp); conn->dbName = strdup(tmp);
...@@ -705,7 +702,7 @@ connectDBStart(PGconn *conn) ...@@ -705,7 +702,7 @@ connectDBStart(PGconn *conn)
MemSet((char *) &conn->raddr, 0, sizeof(conn->raddr)); 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 */ /* Using pghostaddr avoids a hostname lookup */
/* Note that this supports IPv4 only */ /* Note that this supports IPv4 only */
...@@ -724,7 +721,7 @@ connectDBStart(PGconn *conn) ...@@ -724,7 +721,7 @@ connectDBStart(PGconn *conn)
memmove((char *) &(conn->raddr.in.sin_addr), memmove((char *) &(conn->raddr.in.sin_addr),
(char *) &addr, sizeof(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 */ /* Using pghost, so we have to look-up the hostname */
struct hostent *hp; struct hostent *hp;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment