Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f419a82

Browse files
committed
Modify recently added PQconnectdbParams() with new argument, expand_dbname.
If expand_dbname is non-zero and dbname contains an = sign, it is taken as a conninfo string in exactly the same way as if it had been passed to PQconnectdb. This is equivalent to the way PQsetdbLogin() works, allowing PQconnectdbParams() to be a complete alternative. Also improve the way the new function is called from psql and replace a previously missed call to PQsetdbLogin() in psql. Additionally use PQconnectdbParams() for pg_dump and friends, and the bin/scripts command line utilities such as vacuumdb, createdb, etc. Finally, update the documentation for the new parameter, as well as the nuances of precedence in cases where key words are repeated or duplicated in the conninfo string.
1 parent a141ec1 commit f419a82

File tree

8 files changed

+295
-59
lines changed

8 files changed

+295
-59
lines changed

doc/src/sgml/libpq.sgml

+29-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.296 2010/01/28 06:28:26 joe Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.297 2010/02/05 03:09:04 joe Exp $ -->
22

33
<chapter id="libpq">
44
<title><application>libpq</application> - C Library</title>
@@ -98,7 +98,7 @@
9898
Makes a new connection to the database server.
9999

100100
<synopsis>
101-
PGconn *PQconnectdbParams(const char **keywords, const char **values);
101+
PGconn *PQconnectdbParams(const char **keywords, const char **values, int expand_dbname);
102102
</synopsis>
103103
</para>
104104

@@ -114,6 +114,12 @@
114114
programming.
115115
</para>
116116

117+
<para>
118+
When <literal>expand_dbname</literal> is non-zero, the
119+
<parameter>dbname</parameter> key word value is allowed to be recognized
120+
as a <parameter>conninfo</parameter> string. See below for details.
121+
</para>
122+
117123
<para>
118124
The passed arrays can be empty to use all default parameters, or can
119125
contain one or more parameter settings. They should be matched in length.
@@ -473,6 +479,24 @@
473479
is checked. If the environment variable is not set either,
474480
then the indicated built-in defaults are used.
475481
</para>
482+
483+
<para>
484+
If <literal>expand_dbname</literal> is non-zero and
485+
<parameter>dbname</parameter> contains an <symbol>=</symbol> sign, it
486+
is taken as a <parameter>conninfo</parameter> string in exactly the same way as
487+
if it had been passed to <function>PQconnectdb</function>(see below). Previously
488+
processed key words will be overridden by key words in the
489+
<parameter>conninfo</parameter> string.
490+
</para>
491+
492+
<para>
493+
In general key words are processed from the beginning of these arrays in index
494+
order. The effect of this is that when key words are repeated, the last processed
495+
value is retained. Therefore, through careful placement of the
496+
<parameter>dbname</parameter> key word, it is possible to determine what may
497+
be overridden by a <parameter>conninfo</parameter> string, and what may not.
498+
</para>
499+
476500
</listitem>
477501
</varlistentry>
478502

@@ -573,7 +597,7 @@ PGconn *PQsetdb(char *pghost,
573597
Make a connection to the database server in a nonblocking manner.
574598

575599
<synopsis>
576-
PGconn *PQconnectStartParams(const char **keywords, const char **values);
600+
PGconn *PQconnectStartParams(const char **keywords, const char **values, int expand_dbname);
577601
</synopsis>
578602

579603
<synopsis>
@@ -597,8 +621,8 @@ PGconn *PQsetdb(char *pghost,
597621
<para>
598622
With <function>PQconnectStartParams</function>, the database connection is made
599623
using the parameters taken from the <literal>keywords</literal> and
600-
<literal>values</literal> arrays, as described above for
601-
<function>PQconnectdbParams</function>.
624+
<literal>values</literal> arrays, and controlled by <literal>expand_dbname</literal>,
625+
as described above for <function>PQconnectdbParams</function>.
602626
</para>
603627

604628
<para>

src/bin/pg_dump/pg_backup_db.c

+55-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Implements the basic DB functions used by the archiver.
66
*
77
* IDENTIFICATION
8-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.85 2009/12/14 00:39:11 itagaki Exp $
8+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.86 2010/02/05 03:09:05 joe Exp $
99
*
1010
*-------------------------------------------------------------------------
1111
*/
@@ -154,10 +154,34 @@ _connectDB(ArchiveHandle *AH, const char *reqdb, const char *requser)
154154

155155
do
156156
{
157+
#define PARAMS_ARRAY_SIZE 7
158+
const char **keywords = malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
159+
const char **values = malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
160+
161+
if (!keywords || !values)
162+
die_horribly(AH, modulename, "out of memory\n");
163+
164+
keywords[0] = "host";
165+
values[0] = PQhost(AH->connection);
166+
keywords[1] = "port";
167+
values[1] = PQport(AH->connection);
168+
keywords[2] = "user";
169+
values[2] = newuser;
170+
keywords[3] = "password";
171+
values[3] = password;
172+
keywords[4] = "dbname";
173+
values[4] = newdb;
174+
keywords[5] = "fallback_application_name";
175+
values[5] = progname;
176+
keywords[6] = NULL;
177+
values[6] = NULL;
178+
157179
new_pass = false;
158-
newConn = PQsetdbLogin(PQhost(AH->connection), PQport(AH->connection),
159-
NULL, NULL, newdb,
160-
newuser, password);
180+
newConn = PQconnectdbParams(keywords, values, true);
181+
182+
free(keywords);
183+
free(values);
184+
161185
if (!newConn)
162186
die_horribly(AH, modulename, "failed to reconnect to database\n");
163187

@@ -237,9 +261,33 @@ ConnectDatabase(Archive *AHX,
237261
*/
238262
do
239263
{
264+
#define PARAMS_ARRAY_SIZE 7
265+
const char **keywords = malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
266+
const char **values = malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
267+
268+
if (!keywords || !values)
269+
die_horribly(AH, modulename, "out of memory\n");
270+
271+
keywords[0] = "host";
272+
values[0] = pghost;
273+
keywords[1] = "port";
274+
values[1] = pgport;
275+
keywords[2] = "user";
276+
values[2] = username;
277+
keywords[3] = "password";
278+
values[3] = password;
279+
keywords[4] = "dbname";
280+
values[4] = dbname;
281+
keywords[5] = "fallback_application_name";
282+
values[5] = progname;
283+
keywords[6] = NULL;
284+
values[6] = NULL;
285+
240286
new_pass = false;
241-
AH->connection = PQsetdbLogin(pghost, pgport, NULL, NULL,
242-
dbname, username, password);
287+
AH->connection = PQconnectdbParams(keywords, values, true);
288+
289+
free(keywords);
290+
free(values);
243291

244292
if (!AH->connection)
245293
die_horribly(AH, modulename, "failed to connect to database\n");
@@ -697,3 +745,4 @@ _isDQChar(unsigned char c, bool atStart)
697745
else
698746
return false;
699747
}
748+

src/bin/pg_dump/pg_dumpall.c

+30-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1994, Regents of the University of California
77
*
88
*
9-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.131 2010/01/06 03:34:41 momjian Exp $
9+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.132 2010/02/05 03:09:05 joe Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -1618,8 +1618,36 @@ connectDatabase(const char *dbname, const char *pghost, const char *pgport,
16181618
*/
16191619
do
16201620
{
1621+
#define PARAMS_ARRAY_SIZE 7
1622+
const char **keywords = malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
1623+
const char **values = malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
1624+
1625+
if (!keywords || !values)
1626+
{
1627+
fprintf(stderr, _("%s: out of memory\n"), progname);
1628+
exit(1);
1629+
}
1630+
1631+
keywords[0] = "host";
1632+
values[0] = pghost;
1633+
keywords[1] = "port";
1634+
values[1] = pgport;
1635+
keywords[2] = "user";
1636+
values[2] = pguser;
1637+
keywords[3] = "password";
1638+
values[3] = password;
1639+
keywords[4] = "dbname";
1640+
values[4] = dbname;
1641+
keywords[5] = "fallback_application_name";
1642+
values[5] = progname;
1643+
keywords[6] = NULL;
1644+
values[6] = NULL;
1645+
16211646
new_pass = false;
1622-
conn = PQsetdbLogin(pghost, pgport, NULL, NULL, dbname, pguser, password);
1647+
conn = PQconnectdbParams(keywords, values, true);
1648+
1649+
free(keywords);
1650+
free(values);
16231651

16241652
if (!conn)
16251653
{

src/bin/psql/command.c

+25-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2010, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.213 2010/01/02 16:57:59 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.214 2010/02/05 03:09:05 joe Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "command.h"
@@ -1213,7 +1213,7 @@ param_is_newly_set(const char *old_val, const char *new_val)
12131213
* Connects to a database with given parameters. If there exists an
12141214
* established connection, NULL values will be replaced with the ones
12151215
* in the current connection. Otherwise NULL will be passed for that
1216-
* parameter to PQsetdbLogin(), so the libpq defaults will be used.
1216+
* parameter to PQconnectdbParams(), so the libpq defaults will be used.
12171217
*
12181218
* In interactive mode, if connection fails with the given parameters,
12191219
* the old connection will be kept.
@@ -1255,8 +1255,29 @@ do_connect(char *dbname, char *user, char *host, char *port)
12551255

12561256
while (true)
12571257
{
1258-
n_conn = PQsetdbLogin(host, port, NULL, NULL,
1259-
dbname, user, password);
1258+
#define PARAMS_ARRAY_SIZE 7
1259+
const char **keywords = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
1260+
const char **values = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
1261+
1262+
keywords[0] = "host";
1263+
values[0] = host;
1264+
keywords[1] = "port";
1265+
values[1] = port;
1266+
keywords[2] = "user";
1267+
values[2] = user;
1268+
keywords[3] = "password";
1269+
values[3] = password;
1270+
keywords[4] = "dbname";
1271+
values[4] = dbname;
1272+
keywords[5] = "fallback_application_name";
1273+
values[5] = pset.progname;
1274+
keywords[6] = NULL;
1275+
values[6] = NULL;
1276+
1277+
n_conn = PQconnectdbParams(keywords, values, true);
1278+
1279+
free(keywords);
1280+
free(values);
12601281

12611282
/* We can immediately discard the password -- no longer needed */
12621283
if (password)

src/bin/psql/startup.c

+26-17
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2010, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/startup.c,v 1.159 2010/01/28 06:28:26 joe Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/startup.c,v 1.160 2010/02/05 03:09:05 joe Exp $
77
*/
88
#include "postgres_fe.h"
99

@@ -90,8 +90,6 @@ main(int argc, char *argv[])
9090
char *password = NULL;
9191
char *password_prompt = NULL;
9292
bool new_pass;
93-
const char *keywords[] = {"host","port","dbname","user",
94-
"password","application_name",NULL};
9593

9694
set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("psql"));
9795

@@ -173,20 +171,31 @@ main(int argc, char *argv[])
173171
/* loop until we have a password if requested by backend */
174172
do
175173
{
176-
const char *values[] = {
177-
options.host,
178-
options.port,
179-
(options.action == ACT_LIST_DB &&
180-
options.dbname == NULL) ? "postgres" : options.dbname,
181-
options.username,
182-
password,
183-
pset.progname,
184-
NULL
185-
};
186-
187-
new_pass = false;
188-
189-
pset.db = PQconnectdbParams(keywords, values);
174+
#define PARAMS_ARRAY_SIZE 7
175+
const char **keywords = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
176+
const char **values = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
177+
178+
keywords[0] = "host";
179+
values[0] = options.host;
180+
keywords[1] = "port";
181+
values[1] = options.port;
182+
keywords[2] = "user";
183+
values[2] = options.username;
184+
keywords[3] = "password";
185+
values[3] = password;
186+
keywords[4] = "dbname";
187+
values[4] = (options.action == ACT_LIST_DB &&
188+
options.dbname == NULL) ?
189+
"postgres" : options.dbname;
190+
keywords[5] = "fallback_application_name";
191+
values[5] = pset.progname;
192+
keywords[6] = NULL;
193+
values[6] = NULL;
194+
195+
new_pass = false;
196+
pset.db = PQconnectdbParams(keywords, values, true);
197+
free(keywords);
198+
free(values);
190199

191200
if (PQstatus(pset.db) == CONNECTION_BAD &&
192201
PQconnectionNeedsPassword(pset.db) &&

src/bin/scripts/common.c

+30-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/bin/scripts/common.c,v 1.38 2010/01/02 16:58:00 momjian Exp $
10+
* $PostgreSQL: pgsql/src/bin/scripts/common.c,v 1.39 2010/02/05 03:09:05 joe Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -108,8 +108,36 @@ connectDatabase(const char *dbname, const char *pghost, const char *pgport,
108108
*/
109109
do
110110
{
111+
#define PARAMS_ARRAY_SIZE 7
112+
const char **keywords = malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
113+
const char **values = malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
114+
115+
if (!keywords || !values)
116+
{
117+
fprintf(stderr, _("%s: out of memory\n"), progname);
118+
exit(1);
119+
}
120+
121+
keywords[0] = "host";
122+
values[0] = pghost;
123+
keywords[1] = "port";
124+
values[1] = pgport;
125+
keywords[2] = "user";
126+
values[2] = pguser;
127+
keywords[3] = "password";
128+
values[3] = password;
129+
keywords[4] = "dbname";
130+
values[4] = dbname;
131+
keywords[5] = "fallback_application_name";
132+
values[5] = progname;
133+
keywords[6] = NULL;
134+
values[6] = NULL;
135+
111136
new_pass = false;
112-
conn = PQsetdbLogin(pghost, pgport, NULL, NULL, dbname, pguser, password);
137+
conn = PQconnectdbParams(keywords, values, true);
138+
139+
free(keywords);
140+
free(values);
113141

114142
if (!conn)
115143
{

0 commit comments

Comments
 (0)