@@ -58,7 +58,8 @@ static backslashResult exec_command(const char *cmd,
58
58
PQExpBuffer query_buf );
59
59
static bool do_edit (const char * filename_arg , PQExpBuffer query_buf ,
60
60
int lineno , bool * edited );
61
- static bool do_connect (char * dbname , char * user , char * host , char * port );
61
+ static bool do_connect (enum trivalue reuse_previous_specification ,
62
+ char * dbname , char * user , char * host , char * port );
62
63
static bool do_shell (const char * command );
63
64
static bool lookup_function_oid (PGconn * conn , const char * desc , Oid * foid );
64
65
static bool get_create_function_cmd (PGconn * conn , Oid oid , PQExpBuffer buf );
@@ -216,12 +217,9 @@ exec_command(const char *cmd,
216
217
/*
217
218
* \c or \connect -- connect to database using the specified parameters.
218
219
*
219
- * \c dbname user host port
220
+ * \c [-reuse-previous=BOOL] dbname user host port
220
221
*
221
- * If any of these parameters are omitted or specified as '-', the current
222
- * value of the parameter will be used instead. If the parameter has no
223
- * current value, the default value for that parameter will be used. Some
224
- * examples:
222
+ * Specifying a parameter as '-' is equivalent to omitting it. Examples:
225
223
*
226
224
* \c - - hst Connect to current database on current port of host
227
225
* "hst" as current user. \c - usr - prt Connect to current database on
@@ -230,17 +228,31 @@ exec_command(const char *cmd,
230
228
*/
231
229
else if (strcmp (cmd , "c" ) == 0 || strcmp (cmd , "connect" ) == 0 )
232
230
{
231
+ static const char prefix [] = "-reuse-previous=" ;
233
232
char * opt1 ,
234
233
* opt2 ,
235
234
* opt3 ,
236
235
* opt4 ;
236
+ enum trivalue reuse_previous ;
237
237
238
238
opt1 = read_connect_arg (scan_state );
239
+ if (opt1 != NULL && strncmp (opt1 , prefix , sizeof (prefix ) - 1 ) == 0 )
240
+ {
241
+ reuse_previous =
242
+ ParseVariableBool (opt1 + sizeof (prefix ) - 1 , prefix ) ?
243
+ TRI_YES : TRI_NO ;
244
+
245
+ free (opt1 );
246
+ opt1 = read_connect_arg (scan_state );
247
+ }
248
+ else
249
+ reuse_previous = TRI_DEFAULT ;
250
+
239
251
opt2 = read_connect_arg (scan_state );
240
252
opt3 = read_connect_arg (scan_state );
241
253
opt4 = read_connect_arg (scan_state );
242
254
243
- success = do_connect (opt1 , opt2 , opt3 , opt4 );
255
+ success = do_connect (reuse_previous , opt1 , opt2 , opt3 , opt4 );
244
256
245
257
free (opt1 );
246
258
free (opt2 );
@@ -1492,34 +1504,57 @@ param_is_newly_set(const char *old_val, const char *new_val)
1492
1504
/*
1493
1505
* do_connect -- handler for \connect
1494
1506
*
1495
- * Connects to a database with given parameters. If there exists an
1496
- * established connection, NULL values will be replaced with the ones
1497
- * in the current connection. Otherwise NULL will be passed for that
1498
- * parameter to PQconnectdbParams(), so the libpq defaults will be used.
1507
+ * Connects to a database with given parameters. Absent an established
1508
+ * connection, all parameters are required. Given any of -reuse-previous=off,
1509
+ * a connection string without -reuse-previous=on, or lack of an established
1510
+ * connection, NULL values will pass through to PQconnectdbParams(), so the
1511
+ * libpq defaults will be used. Otherwise, NULL values will be replaced with
1512
+ * the ones in the current connection.
1499
1513
*
1500
1514
* In interactive mode, if connection fails with the given parameters,
1501
1515
* the old connection will be kept.
1502
1516
*/
1503
1517
static bool
1504
- do_connect (char * dbname , char * user , char * host , char * port )
1518
+ do_connect (enum trivalue reuse_previous_specification ,
1519
+ char * dbname , char * user , char * host , char * port )
1505
1520
{
1506
1521
PGconn * o_conn = pset .db ,
1507
1522
* n_conn ;
1508
1523
char * password = NULL ;
1509
1524
bool keep_password ;
1510
1525
bool has_connection_string ;
1526
+ bool reuse_previous ;
1511
1527
1512
- /* grab values from the old connection, unless supplied by caller */
1513
- if (!user )
1528
+ has_connection_string = dbname ?
1529
+ recognized_connection_string (dbname ) : false;
1530
+ switch (reuse_previous_specification )
1531
+ {
1532
+ case TRI_YES :
1533
+ reuse_previous = true;
1534
+ break ;
1535
+ case TRI_NO :
1536
+ reuse_previous = false;
1537
+ break ;
1538
+ default :
1539
+ reuse_previous = !has_connection_string ;
1540
+ break ;
1541
+ }
1542
+ /* Silently ignore arguments subsequent to a connection string. */
1543
+ if (has_connection_string )
1544
+ {
1545
+ user = NULL ;
1546
+ host = NULL ;
1547
+ port = NULL ;
1548
+ }
1549
+
1550
+ /* grab missing values from the old connection */
1551
+ if (!user && reuse_previous )
1514
1552
user = PQuser (o_conn );
1515
- if (!host )
1553
+ if (!host && reuse_previous )
1516
1554
host = PQhost (o_conn );
1517
- if (!port )
1555
+ if (!port && reuse_previous )
1518
1556
port = PQport (o_conn );
1519
1557
1520
- has_connection_string =
1521
- dbname ? recognized_connection_string (dbname ) : false;
1522
-
1523
1558
/*
1524
1559
* Any change in the parameters read above makes us discard the password.
1525
1560
* We also discard it if we're to use a conninfo rather than the
@@ -1536,10 +1571,10 @@ do_connect(char *dbname, char *user, char *host, char *port)
1536
1571
(port && PQport (o_conn ) && strcmp (port , PQport (o_conn )) == 0 );
1537
1572
1538
1573
/*
1539
- * Grab dbname from old connection unless supplied by caller . No password
1540
- * discard if this changes: passwords aren't (usually) database-specific.
1574
+ * Grab missing dbname from old connection. No password discard if this
1575
+ * changes: passwords aren't (usually) database-specific.
1541
1576
*/
1542
- if (!dbname )
1577
+ if (!dbname && reuse_previous )
1543
1578
dbname = PQdb (o_conn );
1544
1579
1545
1580
/*
@@ -1566,20 +1601,27 @@ do_connect(char *dbname, char *user, char *host, char *port)
1566
1601
#define PARAMS_ARRAY_SIZE 8
1567
1602
const char * * keywords = pg_malloc (PARAMS_ARRAY_SIZE * sizeof (* keywords ));
1568
1603
const char * * values = pg_malloc (PARAMS_ARRAY_SIZE * sizeof (* values ));
1569
- int paramnum = 0 ;
1604
+ int paramnum = -1 ;
1570
1605
1571
- keywords [0 ] = "dbname" ;
1572
- values [0 ] = dbname ;
1606
+ keywords [++ paramnum ] = "host" ;
1607
+ values [paramnum ] = host ;
1608
+ keywords [++ paramnum ] = "port" ;
1609
+ values [paramnum ] = port ;
1610
+ keywords [++ paramnum ] = "user" ;
1611
+ values [paramnum ] = user ;
1573
1612
1574
- if (!has_connection_string )
1575
- {
1576
- keywords [++ paramnum ] = "host" ;
1577
- values [paramnum ] = host ;
1578
- keywords [++ paramnum ] = "port" ;
1579
- values [paramnum ] = port ;
1580
- keywords [++ paramnum ] = "user" ;
1581
- values [paramnum ] = user ;
1582
- }
1613
+ /*
1614
+ * Position in the array matters when the dbname is a connection
1615
+ * string, because settings in a connection string override earlier
1616
+ * array entries only. Thus, user= in the connection string always
1617
+ * takes effect, but client_encoding= often will not.
1618
+ *
1619
+ * If you change this code, also change the initial-connection code in
1620
+ * main(). For no good reason, a connection string password= takes
1621
+ * precedence in main() but not here.
1622
+ */
1623
+ keywords [++ paramnum ] = "dbname" ;
1624
+ values [paramnum ] = dbname ;
1583
1625
keywords [++ paramnum ] = "password" ;
1584
1626
values [paramnum ] = password ;
1585
1627
keywords [++ paramnum ] = "fallback_application_name" ;
0 commit comments