@@ -59,7 +59,8 @@ static backslashResult exec_command(const char *cmd,
59
59
PQExpBuffer query_buf );
60
60
static bool do_edit (const char * filename_arg , PQExpBuffer query_buf ,
61
61
int lineno , bool * edited );
62
- static bool do_connect (char * dbname , char * user , char * host , char * port );
62
+ static bool do_connect (enum trivalue reuse_previous_specification ,
63
+ char * dbname , char * user , char * host , char * port );
63
64
static bool do_shell (const char * command );
64
65
static bool do_watch (PQExpBuffer query_buf , long sleep );
65
66
static bool lookup_function_oid (PGconn * conn , const char * desc , Oid * foid );
@@ -218,12 +219,9 @@ exec_command(const char *cmd,
218
219
/*
219
220
* \c or \connect -- connect to database using the specified parameters.
220
221
*
221
- * \c dbname user host port
222
+ * \c [-reuse-previous=BOOL] dbname user host port
222
223
*
223
- * If any of these parameters are omitted or specified as '-', the current
224
- * value of the parameter will be used instead. If the parameter has no
225
- * current value, the default value for that parameter will be used. Some
226
- * examples:
224
+ * Specifying a parameter as '-' is equivalent to omitting it. Examples:
227
225
*
228
226
* \c - - hst Connect to current database on current port of host
229
227
* "hst" as current user. \c - usr - prt Connect to current database on
@@ -232,17 +230,31 @@ exec_command(const char *cmd,
232
230
*/
233
231
else if (strcmp (cmd , "c" ) == 0 || strcmp (cmd , "connect" ) == 0 )
234
232
{
233
+ static const char prefix [] = "-reuse-previous=" ;
235
234
char * opt1 ,
236
235
* opt2 ,
237
236
* opt3 ,
238
237
* opt4 ;
238
+ enum trivalue reuse_previous ;
239
239
240
240
opt1 = read_connect_arg (scan_state );
241
+ if (opt1 != NULL && strncmp (opt1 , prefix , sizeof (prefix ) - 1 ) == 0 )
242
+ {
243
+ reuse_previous =
244
+ ParseVariableBool (opt1 + sizeof (prefix ) - 1 , prefix ) ?
245
+ TRI_YES : TRI_NO ;
246
+
247
+ free (opt1 );
248
+ opt1 = read_connect_arg (scan_state );
249
+ }
250
+ else
251
+ reuse_previous = TRI_DEFAULT ;
252
+
241
253
opt2 = read_connect_arg (scan_state );
242
254
opt3 = read_connect_arg (scan_state );
243
255
opt4 = read_connect_arg (scan_state );
244
256
245
- success = do_connect (opt1 , opt2 , opt3 , opt4 );
257
+ success = do_connect (reuse_previous , opt1 , opt2 , opt3 , opt4 );
246
258
247
259
free (opt1 );
248
260
free (opt2 );
@@ -1545,22 +1557,25 @@ param_is_newly_set(const char *old_val, const char *new_val)
1545
1557
/*
1546
1558
* do_connect -- handler for \connect
1547
1559
*
1548
- * Connects to a database with given parameters. If there exists an
1549
- * established connection, NULL values will be replaced with the ones
1550
- * in the current connection. Otherwise NULL will be passed for that
1551
- * parameter to PQconnectdbParams(), so the libpq defaults will be used.
1560
+ * Connects to a database with given parameters. Absent an established
1561
+ * connection, all parameters are required. Given -reuse-previous=off or a
1562
+ * connection string without -reuse-previous=on, NULL values will pass through
1563
+ * to PQconnectdbParams(), so the libpq defaults will be used. Otherwise, NULL
1564
+ * values will be replaced with the ones in the current connection.
1552
1565
*
1553
1566
* In interactive mode, if connection fails with the given parameters,
1554
1567
* the old connection will be kept.
1555
1568
*/
1556
1569
static bool
1557
- do_connect (char * dbname , char * user , char * host , char * port )
1570
+ do_connect (enum trivalue reuse_previous_specification ,
1571
+ char * dbname , char * user , char * host , char * port )
1558
1572
{
1559
1573
PGconn * o_conn = pset .db ,
1560
1574
* n_conn ;
1561
1575
char * password = NULL ;
1562
1576
bool keep_password ;
1563
1577
bool has_connection_string ;
1578
+ bool reuse_previous ;
1564
1579
1565
1580
if (!o_conn && (!dbname || !user || !host || !port ))
1566
1581
{
@@ -1574,17 +1589,36 @@ do_connect(char *dbname, char *user, char *host, char *port)
1574
1589
return false;
1575
1590
}
1576
1591
1577
- /* grab values from the old connection, unless supplied by caller */
1578
- if (!user )
1592
+ has_connection_string = dbname ?
1593
+ recognized_connection_string (dbname ) : false;
1594
+ switch (reuse_previous_specification )
1595
+ {
1596
+ case TRI_YES :
1597
+ reuse_previous = true;
1598
+ break ;
1599
+ case TRI_NO :
1600
+ reuse_previous = false;
1601
+ break ;
1602
+ default :
1603
+ reuse_previous = !has_connection_string ;
1604
+ break ;
1605
+ }
1606
+ /* Silently ignore arguments subsequent to a connection string. */
1607
+ if (has_connection_string )
1608
+ {
1609
+ user = NULL ;
1610
+ host = NULL ;
1611
+ port = NULL ;
1612
+ }
1613
+
1614
+ /* grab missing values from the old connection */
1615
+ if (!user && reuse_previous )
1579
1616
user = PQuser (o_conn );
1580
- if (!host )
1617
+ if (!host && reuse_previous )
1581
1618
host = PQhost (o_conn );
1582
- if (!port )
1619
+ if (!port && reuse_previous )
1583
1620
port = PQport (o_conn );
1584
1621
1585
- has_connection_string =
1586
- dbname ? recognized_connection_string (dbname ) : false;
1587
-
1588
1622
/*
1589
1623
* Any change in the parameters read above makes us discard the password.
1590
1624
* We also discard it if we're to use a conninfo rather than the
@@ -1601,10 +1635,10 @@ do_connect(char *dbname, char *user, char *host, char *port)
1601
1635
(port && PQport (o_conn ) && strcmp (port , PQport (o_conn )) == 0 );
1602
1636
1603
1637
/*
1604
- * Grab dbname from old connection unless supplied by caller . No password
1605
- * discard if this changes: passwords aren't (usually) database-specific.
1638
+ * Grab missing dbname from old connection. No password discard if this
1639
+ * changes: passwords aren't (usually) database-specific.
1606
1640
*/
1607
- if (!dbname )
1641
+ if (!dbname && reuse_previous )
1608
1642
dbname = PQdb (o_conn );
1609
1643
1610
1644
/*
@@ -1635,20 +1669,27 @@ do_connect(char *dbname, char *user, char *host, char *port)
1635
1669
#define PARAMS_ARRAY_SIZE 8
1636
1670
const char * * keywords = pg_malloc (PARAMS_ARRAY_SIZE * sizeof (* keywords ));
1637
1671
const char * * values = pg_malloc (PARAMS_ARRAY_SIZE * sizeof (* values ));
1638
- int paramnum = 0 ;
1672
+ int paramnum = -1 ;
1639
1673
1640
- keywords [0 ] = "dbname" ;
1641
- values [0 ] = dbname ;
1674
+ keywords [++ paramnum ] = "host" ;
1675
+ values [paramnum ] = host ;
1676
+ keywords [++ paramnum ] = "port" ;
1677
+ values [paramnum ] = port ;
1678
+ keywords [++ paramnum ] = "user" ;
1679
+ values [paramnum ] = user ;
1642
1680
1643
- if (!has_connection_string )
1644
- {
1645
- keywords [++ paramnum ] = "host" ;
1646
- values [paramnum ] = host ;
1647
- keywords [++ paramnum ] = "port" ;
1648
- values [paramnum ] = port ;
1649
- keywords [++ paramnum ] = "user" ;
1650
- values [paramnum ] = user ;
1651
- }
1681
+ /*
1682
+ * Position in the array matters when the dbname is a connection
1683
+ * string, because settings in a connection string override earlier
1684
+ * array entries only. Thus, user= in the connection string always
1685
+ * takes effect, but client_encoding= often will not.
1686
+ *
1687
+ * If you change this code, also change the initial-connection code in
1688
+ * main(). For no good reason, a connection string password= takes
1689
+ * precedence in main() but not here.
1690
+ */
1691
+ keywords [++ paramnum ] = "dbname" ;
1692
+ values [paramnum ] = dbname ;
1652
1693
keywords [++ paramnum ] = "password" ;
1653
1694
values [paramnum ] = password ;
1654
1695
keywords [++ paramnum ] = "fallback_application_name" ;
0 commit comments