diff options
author | Alvaro Herrera <alvherre@alvh.no-ip.org> | 2015-04-01 20:00:07 -0300 |
---|---|---|
committer | Alvaro Herrera <alvherre@alvh.no-ip.org> | 2015-04-01 20:00:07 -0300 |
commit | fcef1617295c074f2684c887627184d2fc26ac04 (patch) | |
tree | 8147bcb0483d07b79545fba4f6694791038f4a97 /src/interfaces/libpq/fe-connect.c | |
parent | a0efc714531d3dfd02fafd39e80d058cef6703b0 (diff) | |
download | postgresql-fcef1617295c074f2684c887627184d2fc26ac04.tar.gz postgresql-fcef1617295c074f2684c887627184d2fc26ac04.zip |
psql: fix \connect with URIs and conninfo strings
psql was already accepting conninfo strings as the first parameter in
\connect, but the way it worked wasn't sane; some of the other
parameters would get the previous connection's values, causing it to
connect to a completely unexpected server or, more likely, not finding
any server at all because of completely wrong combinations of
parameters.
Fix by explicitely checking for a conninfo-looking parameter in the
dbname position; if one is found, use its complete specification rather
than mix with the other arguments. Also, change tab-completion to not
try to complete conninfo/URI-looking "dbnames" and document that
conninfos are accepted as first argument.
There was a weak consensus to backpatch this, because while the behavior
of using the dbname as a conninfo is nowhere documented for \connect, it
is reasonable to expect that it works because it does work in many other
contexts. Therefore this is backpatched all the way back to 9.0.
To implement this, routines previously private to libpq have been
duplicated so that psql can decide what looks like a conninfo/URI
string. In back branches, just duplicate the same code all the way back
to 9.2, where URIs where introduced; 9.0 and 9.1 have a simpler version.
In master, the routines are moved to src/common and renamed.
Author: David Fetter, Andrew Dunstan. Some editorialization by me
(probably earning a Gierth's "Sloppy" badge in the process.)
Reviewers: Andrew Gierth, Erik Rijkers, Pavel Stěhule, Stephen Frost,
Robert Haas, Andrew Dunstan.
Diffstat (limited to 'src/interfaces/libpq/fe-connect.c')
-rw-r--r-- | src/interfaces/libpq/fe-connect.c | 46 |
1 files changed, 6 insertions, 40 deletions
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index e2a06b3d929..638d103f8c0 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -22,6 +22,7 @@ #include <time.h> #include <unistd.h> +#include "common/connstrings.h" #include "libpq-fe.h" #include "libpq-int.h" #include "fe-auth.h" @@ -339,8 +340,6 @@ static void closePGconn(PGconn *conn); static PQconninfoOption *conninfo_init(PQExpBuffer errorMessage); static PQconninfoOption *parse_connection_string(const char *conninfo, PQExpBuffer errorMessage, bool use_defaults); -static int uri_prefix_length(const char *connstr); -static bool recognized_connection_string(const char *connstr); static PQconninfoOption *conninfo_parse(const char *conninfo, PQExpBuffer errorMessage, bool use_defaults); static PQconninfoOption *conninfo_array_parse(const char *const * keywords, @@ -971,7 +970,7 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions, * If the dbName parameter contains what looks like a connection string, * parse it into conn struct using connectOptions1. */ - if (dbName && recognized_connection_string(dbName)) + if (dbName && libpq_connstring_is_recognized(dbName)) { if (!connectOptions1(conn, dbName)) return conn; @@ -4185,7 +4184,7 @@ parse_connection_string(const char *connstr, PQExpBuffer errorMessage, bool use_defaults) { /* Parse as URI if connection string matches URI prefix */ - if (uri_prefix_length(connstr) != 0) + if (libpq_connstring_uri_prefix_length(connstr) != 0) return conninfo_uri_parse(connstr, errorMessage, use_defaults); /* Parse as default otherwise */ @@ -4193,39 +4192,6 @@ parse_connection_string(const char *connstr, PQExpBuffer errorMessage, } /* - * Checks if connection string starts with either of the valid URI prefix - * designators. - * - * Returns the URI prefix length, 0 if the string doesn't contain a URI prefix. - */ -static int -uri_prefix_length(const char *connstr) -{ - if (strncmp(connstr, uri_designator, - sizeof(uri_designator) - 1) == 0) - return sizeof(uri_designator) - 1; - - if (strncmp(connstr, short_uri_designator, - sizeof(short_uri_designator) - 1) == 0) - return sizeof(short_uri_designator) - 1; - - return 0; -} - -/* - * Recognized connection string either starts with a valid URI prefix or - * contains a "=" in it. - * - * Must be consistent with parse_connection_string: anything for which this - * returns true should at least look like it's parseable by that routine. - */ -static bool -recognized_connection_string(const char *connstr) -{ - return uri_prefix_length(connstr) != 0 || strchr(connstr, '=') != NULL; -} - -/* * Subroutine for parse_connection_string * * Deal with a string containing key=value pairs. @@ -4400,7 +4366,7 @@ conninfo_parse(const char *conninfo, PQExpBuffer errorMessage, * * If expand_dbname is non-zero, and the value passed for the first occurrence * of "dbname" keyword is a connection string (as indicated by - * recognized_connection_string) then parse and process it, overriding any + * libpq_connstring_is_recognized) then parse and process it, overriding any * previously processed conflicting keywords. Subsequent keywords will take * precedence, however. */ @@ -4431,7 +4397,7 @@ conninfo_array_parse(const char *const * keywords, const char *const * values, * defaults here -- those get picked up later. We only want to * override for those parameters actually passed. */ - if (recognized_connection_string(pvalue)) + if (libpq_connstring_is_recognized(pvalue)) { dbname_options = parse_connection_string(pvalue, errorMessage, false); if (dbname_options == NULL) @@ -4722,7 +4688,7 @@ conninfo_uri_parse_options(PQconninfoOption *options, const char *uri, start = buf; /* Skip the URI prefix */ - prefix_len = uri_prefix_length(uri); + prefix_len = libpq_connstring_uri_prefix_length(uri); if (prefix_len == 0) { /* Should never happen */ |