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

Commit 07bae9c

Browse files
committed
Please find enclosed a patch that lets you use \c to connect
(optionally) to a new host and port without exiting psql. This eliminates, IMHO, a surprise in that you can now connect to PostgreSQL on a differnt machine from the one where you started your session. This should help people who use psql as an administrative tool. David Fetter
1 parent d52a57f commit 07bae9c

File tree

3 files changed

+94
-28
lines changed

3 files changed

+94
-28
lines changed

doc/src/sgml/ref/psql-ref.sgml

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.157 2005/12/20 00:51:45 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.158 2006/02/12 02:54:30 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -693,13 +693,15 @@ testdb=&gt;
693693
</varlistentry>
694694

695695
<varlistentry>
696-
<term><literal>\connect</literal> (or <literal>\c</literal>) <literal>[ <replaceable class="parameter">dbname</replaceable> [ <replaceable class="parameter">username</replaceable> ] ]</literal></term>
696+
<term><literal>\connect</literal> (or <literal>\c</literal>) <literal>[ <replaceable class="parameter">dbname</replaceable> [ <replaceable class="parameter">username</replaceable> ] [ <replaceable class="parameter">host</replaceable> ] [ <replaceable class="parameter">port</replaceable> ] ]</literal></term>
697697
<listitem>
698698
<para>
699699
Establishes a connection to a new database and/or under a user
700700
name. The previous connection is closed. If <replaceable
701701
class="parameter">dbname</replaceable> is <literal>-</literal>
702-
the current database name is assumed.
702+
the current database name is assumed. Similar consideration
703+
applies to <replaceable class="parameter">host</replaceable> and
704+
<replaceable class="parameter">port</replaceable>.
703705
</para>
704706

705707
<para>

src/bin/psql/command.c

+87-23
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.158 2005/12/26 14:58:04 petere Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.159 2006/02/12 02:54:30 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "command.h"
@@ -55,7 +55,7 @@ static backslashResult exec_command(const char *cmd,
5555
PsqlScanState scan_state,
5656
PQExpBuffer query_buf);
5757
static bool do_edit(const char *filename_arg, PQExpBuffer query_buf);
58-
static bool do_connect(const char *new_dbname, const char *new_user);
58+
static bool do_connect(const char *new_dbname, const char *new_user, const char *new_host, const char *new_port);
5959
static bool do_shell(const char *command);
6060

6161

@@ -189,20 +189,27 @@ exec_command(const char *cmd,
189189
}
190190

191191
/*----------
192-
* \c or \connect -- connect to new database or as different user
192+
* \c or \connect -- connect to new database or as different user,
193+
* and/or new host and/or port
193194
*
194-
* \c foo bar connect to db "foo" as user "bar"
195-
* \c foo [-] connect to db "foo" as current user
196-
* \c - bar connect to current db as user "bar"
195+
* \c foo bar [-] [-] connect to db "foo" as user "bar" on current host and port
196+
* \c foo [-] [-] [-] connect to db "foo" as current user on current host and port
197+
* \c - bar [-] [-] connect to current db as user "bar" on current host and port
198+
* \c - - host.domain.tld [-] connect to default db as default user on host.domain.tld on default port
199+
* \c - - - 5555 connect to default db as default user on default host at port 5555
197200
* \c connect to default db as default user
198201
*----------
199202
*/
200203
else if (strcmp(cmd, "c") == 0 || strcmp(cmd, "connect") == 0)
201204
{
202205
char *opt1,
203-
*opt2;
206+
*opt2,
207+
*opt3,
208+
*opt4;
204209
char opt1q,
205-
opt2q;
210+
opt2q,
211+
opt3q,
212+
opt4q;
206213

207214
/*
208215
* Ideally we should treat the arguments as SQL identifiers. But for
@@ -217,20 +224,53 @@ exec_command(const char *cmd,
217224
OT_SQLIDHACK, &opt1q, true);
218225
opt2 = psql_scan_slash_option(scan_state,
219226
OT_SQLIDHACK, &opt2q, true);
220-
227+
opt3 = psql_scan_slash_option(scan_state,
228+
OT_SQLIDHACK, &opt3q, true);
229+
opt4 = psql_scan_slash_option(scan_state,
230+
OT_SQLIDHACK, &opt4q, true);
231+
232+
if (opt4)
233+
/* gave port */
234+
success = do_connect(!opt1q && (strcmp(opt1, "-") == 0 ||
235+
strcmp(opt1, "") == 0) ? "" : opt1,
236+
!opt2q && (strcmp(opt2, "-") == 0 ||
237+
strcmp(opt2, "") == 0) ? "" : opt2,
238+
!opt3q && (strcmp(opt3, "-") == 0 ||
239+
strcmp(opt3, "") == 0) ? "" : opt3,
240+
!opt3q && (strcmp(opt3, "-") == 0 ||
241+
strcmp(opt3, "") == 0) ? "" : opt3);
242+
if (opt3)
243+
/* gave host */
244+
success = do_connect(!opt1q && (strcmp(opt1, "-") == 0 ||
245+
strcmp(opt1, "") == 0) ? "" : opt1,
246+
!opt2q && (strcmp(opt2, "-") == 0 ||
247+
strcmp(opt2, "") == 0) ? "" : opt2,
248+
!opt3q && (strcmp(opt3, "-") == 0 ||
249+
strcmp(opt3, "") == 0) ? "" : opt3,
250+
NULL);
221251
if (opt2)
222252
/* gave username */
223-
success = do_connect(!opt1q && (strcmp(opt1, "-") == 0 || strcmp(opt1, "") == 0) ? "" : opt1,
224-
!opt2q && (strcmp(opt2, "-") == 0 || strcmp(opt2, "") == 0) ? "" : opt2);
253+
success = do_connect(!opt1q && (strcmp(opt1, "-") == 0 ||
254+
strcmp(opt1, "") == 0) ? "" : opt1,
255+
!opt2q && (strcmp(opt2, "-") == 0 ||
256+
strcmp(opt2, "") == 0) ? "" : opt2,
257+
NULL,
258+
NULL);
225259
else if (opt1)
226260
/* gave database name */
227-
success = do_connect(!opt1q && (strcmp(opt1, "-") == 0 || strcmp(opt1, "") == 0) ? "" : opt1, "");
261+
success = do_connect(!opt1q && (strcmp(opt1, "-") == 0 ||
262+
strcmp(opt1, "") == 0) ? "" : opt1,
263+
"",
264+
NULL,
265+
NULL);
228266
else
229267
/* connect to default db as default user */
230-
success = do_connect(NULL, NULL);
268+
success = do_connect(NULL, NULL, NULL, NULL);
231269

232270
free(opt1);
233271
free(opt2);
272+
free(opt3);
273+
free(opt4);
234274
}
235275

236276
/* \cd */
@@ -959,11 +999,13 @@ exec_command(const char *cmd,
959999
* The old connection will be kept if the session is interactive.
9601000
*/
9611001
static bool
962-
do_connect(const char *new_dbname, const char *new_user)
1002+
do_connect(const char *new_dbname, const char *new_user, const char *new_host, const char *new_port)
9631003
{
9641004
PGconn *oldconn = pset.db;
9651005
const char *dbparam = NULL;
9661006
const char *userparam = NULL;
1007+
const char *hostparam = NULL;
1008+
const char *portparam = NULL;
9671009
const char *pwparam = NULL;
9681010
char *password_prompt = NULL;
9691011
char *prompted_password = NULL;
@@ -985,6 +1027,18 @@ do_connect(const char *new_dbname, const char *new_user)
9851027
else
9861028
userparam = new_user;
9871029

1030+
/* If host is "" then use the old one */
1031+
if (new_host && PQhost(oldconn) && strcmp(new_host, "") == 0)
1032+
hostparam = PQhost(oldconn);
1033+
else
1034+
hostparam = new_host;
1035+
1036+
/* If port is "" then use the old one */
1037+
if (new_port && PQport(oldconn) && strcmp(new_port, "") == 0)
1038+
portparam = PQport(oldconn);
1039+
else
1040+
portparam = new_port;
1041+
9881042
if (userparam == NULL)
9891043
password_prompt = strdup("Password: ");
9901044
else
@@ -1009,7 +1063,7 @@ do_connect(const char *new_dbname, const char *new_user)
10091063
do
10101064
{
10111065
need_pass = false;
1012-
pset.db = PQsetdbLogin(PQhost(oldconn), PQport(oldconn),
1066+
pset.db = PQsetdbLogin(hostparam, portparam,
10131067
NULL, NULL, dbparam, userparam, pwparam);
10141068

10151069
if (PQstatus(pset.db) == CONNECTION_BAD &&
@@ -1061,14 +1115,24 @@ do_connect(const char *new_dbname, const char *new_user)
10611115
{
10621116
if (!QUIET())
10631117
{
1064-
if (userparam != new_user) /* no new user */
1065-
printf(_("You are now connected to database \"%s\".\n"), dbparam);
1066-
else if (dbparam != new_dbname) /* no new db */
1067-
printf(_("You are now connected as new user \"%s\".\n"), new_user);
1068-
else
1069-
/* both new */
1070-
printf(_("You are now connected to database \"%s\" as user \"%s\".\n"),
1071-
PQdb(pset.db), PQuser(pset.db));
1118+
if ((hostparam == new_host) && (portparam == new_port)) /* no new host or port */
1119+
{
1120+
if (userparam != new_user) /* no new user */
1121+
printf(_("You are now connected to database \"%s\".\n"), dbparam);
1122+
else if (dbparam != new_dbname) /* no new db */
1123+
printf(_("You are now connected as new user \"%s\".\n"), new_user);
1124+
else
1125+
/* both new */
1126+
printf(_("You are now connected to database \"%s\" as user \"%s\".\n"),
1127+
PQdb(pset.db), PQuser(pset.db));
1128+
}
1129+
else /* At least one of host and port are new */
1130+
{
1131+
printf(
1132+
_("You are now connected to database \"%s\" as user \"%s\" on host \"%s\" at port %s.\n"),
1133+
PQdb(pset.db), PQuser(pset.db), PQhost(pset.db),
1134+
PQport(pset.db));
1135+
}
10721136
}
10731137

10741138
if (oldconn)

src/bin/psql/help.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/help.c,v 1.107 2006/02/11 21:55:35 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/help.c,v 1.108 2006/02/12 02:54:30 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "common.h"
@@ -173,7 +173,7 @@ slashUsage(unsigned short int pager)
173173
* in 80 columns >> "
174174
*/
175175
fprintf(output, _("General\n"));
176-
fprintf(output, _(" \\c[onnect] [DBNAME|- [USER]]\n"
176+
fprintf(output, _(" \\c[onnect] [DBNAME|- USER|- HOST|- PORT|-]\n"
177177
" connect to new database (currently \"%s\")\n"),
178178
PQdb(pset.db));
179179
fprintf(output, _(" \\cd [DIR] change the current working directory\n"));

0 commit comments

Comments
 (0)