Commit 5133dd78 authored by Andrew Dunstan's avatar Andrew Dunstan

Interpret a dbName param to PQsetdbLogin as a conninfo string if it contains...

Interpret a dbName param to PQsetdbLogin as a conninfo string if it contains an = sign. Tom Lane and Andrew Dunstan.
parent 93b4f0ff
<!-- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.220 2006/11/10 22:15:26 tgl Exp $ --> <!-- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.221 2006/12/19 01:53:36 adunstan Exp $ -->
<chapter id="libpq"> <chapter id="libpq">
<title><application>libpq</application> - C Library</title> <title><application>libpq</application> - C Library</title>
...@@ -324,14 +324,20 @@ PGconn *PQsetdbLogin(const char *pghost, ...@@ -324,14 +324,20 @@ PGconn *PQsetdbLogin(const char *pghost,
const char *login, const char *login,
const char *pwd); const char *pwd);
</synopsis> </synopsis>
</para> </para>
<para> <para>
This is the predecessor of <function>PQconnectdb</function> with a fixed This is the predecessor of <function>PQconnectdb</function> with a fixed
set of parameters. It has the same functionality except that the set of parameters. It has the same functionality except that the
missing parameters will always take on default values. Write <symbol>NULL</symbol> or an missing parameters will always take on default values. Write <symbol>NULL</symbol> or an
empty string for any one of the fixed parameters that is to be defaulted. empty string for any one of the fixed parameters that is to be defaulted.
</para> </para>
<para>
If the <parameter>dbName</parameter> contains an <symbol>=</symbol> sign, it
is taken as a <parameter>conninfo</parameter> string in exactly the same way as
if it had been passed to <function>PQconnectdb</function>, and the remaining
parameters are then applied as above.
</para>
</listitem> </listitem>
</varlistentry> </varlistentry>
......
<!-- <!--
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.178 2006/12/06 15:47:22 momjian Exp $ $PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.179 2006/12/19 01:53:36 adunstan Exp $
PostgreSQL documentation PostgreSQL documentation
--> -->
...@@ -112,6 +112,10 @@ PostgreSQL documentation ...@@ -112,6 +112,10 @@ PostgreSQL documentation
equivalent to specifying <replaceable equivalent to specifying <replaceable
class="parameter">dbname</replaceable> as the first non-option class="parameter">dbname</replaceable> as the first non-option
argument on the command line. argument on the command line.
</para>
<para>
If this parameter contains an <symbol>=</symbol> sign, it it treated as a
<parameter>conninfo</parameter> string. See <xref linkend="libpq-connect"> for more information.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
...@@ -556,6 +560,18 @@ PostgreSQL documentation ...@@ -556,6 +560,18 @@ PostgreSQL documentation
</para> </para>
<para> <para>
An alternative way to specify connection parameters is in a
<parameter>conninfo</parameter> string, which is used instead of a
database name. This mechanism give you very wide control over the
connection. For example,
<programlisting>
$ <userinput>psql "service=myservice sslmode=require"</userinput>
</programlisting>
See <xref linkend="libpq-connect"> for more information on all the
available connection options.
</para>
<para>
If the connection could not be made for any reason (e.g., insufficient If the connection could not be made for any reason (e.g., insufficient
privileges, server is not running on the targeted host, etc.), privileges, server is not running on the targeted host, etc.),
<application>psql</application> will return an error and terminate. <application>psql</application> will return an error and terminate.
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.339 2006/11/21 16:28:00 tgl Exp $ * $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.340 2006/12/19 01:53:36 adunstan Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -574,16 +574,36 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions, ...@@ -574,16 +574,36 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions,
conn = makeEmptyPGconn(); conn = makeEmptyPGconn();
if (conn == NULL) if (conn == NULL)
return NULL; return NULL;
/* /*
* Parse an empty conninfo string in order to set up the same defaults * If the dbName parameter contains '=', assume it's a conninfo
* that PQconnectdb() would use. * string.
*/
if (dbName && strchr(dbName,'='))
{
if (!connectOptions1(conn, dbName))
return conn;
}
else
{
/*
* Old-style path: first, parse an empty conninfo string in
* order to set up the same defaults that PQconnectdb() would use.
*/ */
if (!connectOptions1(conn, "")) if (!connectOptions1(conn, ""))
return conn; return conn;
/* Insert dbName parameter value into struct */
if (dbName && dbName[0] != '\0')
{
if (conn->dbName)
free(conn->dbName);
conn->dbName = strdup(dbName);
}
}
/* /*
* Absorb specified options into conn structure, overriding defaults * Insert remaining parameters into struct, overriding defaults
* (as well as any conflicting data from dbName taken as a conninfo).
*/ */
if (pghost && pghost[0] != '\0') if (pghost && pghost[0] != '\0')
{ {
...@@ -613,13 +633,6 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions, ...@@ -613,13 +633,6 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions,
conn->pgtty = strdup(pgtty); conn->pgtty = strdup(pgtty);
} }
if (dbName && dbName[0] != '\0')
{
if (conn->dbName)
free(conn->dbName);
conn->dbName = strdup(dbName);
}
if (login && login[0] != '\0') if (login && login[0] != '\0')
{ {
if (conn->pguser) if (conn->pguser)
......
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