Commit add1b052 authored by Heikki Linnakangas's avatar Heikki Linnakangas

Allow "dbname" from connection string to be overridden in PQconnectDBParams

If the "dbname" attribute in PQconnectDBParams contained a connection string
or URI (and expand_dbname = TRUE), the database name from the connection
string could not be overridden by a subsequent "dbname" keyword in the
array. That was not intentional; all other options can be overridden.
Furthermore, any subsequent "dbname" caused the connection string from the
first dbname value to be processed again, overriding any values for the same
options that were given between the connection string and the second dbname
option.

In the passing, clarify in the docs that only the first dbname option in the
array is parsed as a connection string.

Alex Shulgin. Backpatch to all supported versions.
parent 81d815dc
...@@ -124,7 +124,10 @@ PGconn *PQconnectdbParams(const char * const *keywords, ...@@ -124,7 +124,10 @@ PGconn *PQconnectdbParams(const char * const *keywords,
<para> <para>
When <literal>expand_dbname</literal> is non-zero, the When <literal>expand_dbname</literal> is non-zero, the
<parameter>dbname</parameter> key word value is allowed to be recognized <parameter>dbname</parameter> key word value is allowed to be recognized
as a connection string. More details on the possible formats appear in as a connection string. Only the first occurrence of
<parameter>dbname</parameter> is expanded this way, any subsequent
<parameter>dbname</parameter> value is processed as plain database name. More
details on the possible connection string formats appear in
<xref linkend="libpq-connstring">. <xref linkend="libpq-connstring">.
</para> </para>
......
...@@ -4379,10 +4379,11 @@ conninfo_parse(const char *conninfo, PQExpBuffer errorMessage, ...@@ -4379,10 +4379,11 @@ conninfo_parse(const char *conninfo, PQExpBuffer errorMessage,
* Defaults are supplied (from a service file, environment variables, etc) * Defaults are supplied (from a service file, environment variables, etc)
* for unspecified options, but only if use_defaults is TRUE. * for unspecified options, but only if use_defaults is TRUE.
* *
* If expand_dbname is non-zero, and the value passed for keyword "dbname" is a * If expand_dbname is non-zero, and the value passed for the first occurrence
* connection string (as indicated by recognized_connection_string) then parse * of "dbname" keyword is a connection string (as indicated by
* and process it, overriding any previously processed conflicting * recognized_connection_string) then parse and process it, overriding any
* keywords. Subsequent keywords will take precedence, however. * previously processed conflicting keywords. Subsequent keywords will take
* precedence, however.
*/ */
static PQconninfoOption * static PQconninfoOption *
conninfo_array_parse(const char *const * keywords, const char *const * values, conninfo_array_parse(const char *const * keywords, const char *const * values,
...@@ -4458,7 +4459,7 @@ conninfo_array_parse(const char *const * keywords, const char *const * values, ...@@ -4458,7 +4459,7 @@ conninfo_array_parse(const char *const * keywords, const char *const * values,
} }
/* /*
* If we are on the dbname parameter, and we have a parsed * If we are on the first dbname parameter, and we have a parsed
* connection string, copy those parameters across, overriding any * connection string, copy those parameters across, overriding any
* existing previous settings. * existing previous settings.
*/ */
...@@ -4492,6 +4493,12 @@ conninfo_array_parse(const char *const * keywords, const char *const * values, ...@@ -4492,6 +4493,12 @@ conninfo_array_parse(const char *const * keywords, const char *const * values,
} }
} }
} }
/*
* Forget the parsed connection string, so that any subsequent
* dbname parameters will not be expanded.
*/
PQconninfoFree(dbname_options);
dbname_options = NULL;
} }
else else
{ {
......
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