From 5133dd786b0041114ba4da6b174ade8c33917e68 Mon Sep 17 00:00:00 2001 From: Andrew Dunstan Date: Tue, 19 Dec 2006 01:53:36 +0000 Subject: [PATCH] Interpret a dbName param to PQsetdbLogin as a conninfo string if it contains an = sign. Tom Lane and Andrew Dunstan. --- doc/src/sgml/libpq.sgml | 20 ++++++++----- doc/src/sgml/ref/psql-ref.sgml | 18 +++++++++++- src/interfaces/libpq/fe-connect.c | 49 +++++++++++++++++++------------ 3 files changed, 61 insertions(+), 26 deletions(-) diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 40d0bcb1fb..0fc0f76fd2 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -1,4 +1,4 @@ - + <application>libpq</application> - C Library @@ -324,13 +324,19 @@ PGconn *PQsetdbLogin(const char *pghost, const char *login, const char *pwd); - + - - This is the predecessor of PQconnectdb with a fixed - set of parameters. It has the same functionality except that the - missing parameters will always take on default values. Write NULL or an - empty string for any one of the fixed parameters that is to be defaulted. + + This is the predecessor of PQconnectdb with a fixed + set of parameters. It has the same functionality except that the + missing parameters will always take on default values. Write NULL or an + empty string for any one of the fixed parameters that is to be defaulted. + + + If the dbName contains an = sign, it + is taken as a conninfo string in exactly the same way as + if it had been passed to PQconnectdb, and the remaining + parameters are then applied as above. diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index c3071752c7..cd92a21729 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -1,5 +1,5 @@ @@ -113,6 +113,10 @@ PostgreSQL documentation class="parameter">dbname as the first non-option argument on the command line. + + If this parameter contains an = sign, it it treated as a + conninfo string. See for more information. + @@ -555,6 +559,18 @@ PostgreSQL documentation passwords. See for more information. + + An alternative way to specify connection parameters is in a + conninfo string, which is used instead of a + database name. This mechanism give you very wide control over the + connection. For example, + +$ psql "service=myservice sslmode=require" + + See for more information on all the + available connection options. + + If the connection could not be made for any reason (e.g., insufficient privileges, server is not running on the targeted host, etc.), diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index eb6ab6127d..5b29b60a6e 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -8,7 +8,7 @@ * * * 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, conn = makeEmptyPGconn(); if (conn == NULL) return NULL; - - /* - * Parse an empty conninfo string in order to set up the same defaults - * that PQconnectdb() would use. - */ - if (!connectOptions1(conn, "")) - return conn; - - /* - * Absorb specified options into conn structure, overriding defaults + /* + * If the dbName parameter contains '=', assume it's a conninfo + * 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, "")) + return conn; + + /* Insert dbName parameter value into struct */ + if (dbName && dbName[0] != '\0') + { + if (conn->dbName) + free(conn->dbName); + conn->dbName = strdup(dbName); + } + } + + /* + * Insert remaining parameters into struct, overriding defaults + * (as well as any conflicting data from dbName taken as a conninfo). */ if (pghost && pghost[0] != '\0') { @@ -613,13 +633,6 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions, 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 (conn->pguser) -- 2.40.0