3
3
*
4
4
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
5
5
*
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 $
7
7
*/
8
8
#include "postgres_fe.h"
9
9
#include "command.h"
@@ -55,7 +55,7 @@ static backslashResult exec_command(const char *cmd,
55
55
PsqlScanState scan_state ,
56
56
PQExpBuffer query_buf );
57
57
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 );
59
59
static bool do_shell (const char * command );
60
60
61
61
@@ -189,20 +189,27 @@ exec_command(const char *cmd,
189
189
}
190
190
191
191
/*----------
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
193
194
*
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
197
200
* \c connect to default db as default user
198
201
*----------
199
202
*/
200
203
else if (strcmp (cmd , "c" ) == 0 || strcmp (cmd , "connect" ) == 0 )
201
204
{
202
205
char * opt1 ,
203
- * opt2 ;
206
+ * opt2 ,
207
+ * opt3 ,
208
+ * opt4 ;
204
209
char opt1q ,
205
- opt2q ;
210
+ opt2q ,
211
+ opt3q ,
212
+ opt4q ;
206
213
207
214
/*
208
215
* Ideally we should treat the arguments as SQL identifiers. But for
@@ -217,20 +224,53 @@ exec_command(const char *cmd,
217
224
OT_SQLIDHACK , & opt1q , true);
218
225
opt2 = psql_scan_slash_option (scan_state ,
219
226
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 );
221
251
if (opt2 )
222
252
/* 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 );
225
259
else if (opt1 )
226
260
/* 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 );
228
266
else
229
267
/* connect to default db as default user */
230
- success = do_connect (NULL , NULL );
268
+ success = do_connect (NULL , NULL , NULL , NULL );
231
269
232
270
free (opt1 );
233
271
free (opt2 );
272
+ free (opt3 );
273
+ free (opt4 );
234
274
}
235
275
236
276
/* \cd */
@@ -959,11 +999,13 @@ exec_command(const char *cmd,
959
999
* The old connection will be kept if the session is interactive.
960
1000
*/
961
1001
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 )
963
1003
{
964
1004
PGconn * oldconn = pset .db ;
965
1005
const char * dbparam = NULL ;
966
1006
const char * userparam = NULL ;
1007
+ const char * hostparam = NULL ;
1008
+ const char * portparam = NULL ;
967
1009
const char * pwparam = NULL ;
968
1010
char * password_prompt = NULL ;
969
1011
char * prompted_password = NULL ;
@@ -985,6 +1027,18 @@ do_connect(const char *new_dbname, const char *new_user)
985
1027
else
986
1028
userparam = new_user ;
987
1029
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
+
988
1042
if (userparam == NULL )
989
1043
password_prompt = strdup ("Password: " );
990
1044
else
@@ -1009,7 +1063,7 @@ do_connect(const char *new_dbname, const char *new_user)
1009
1063
do
1010
1064
{
1011
1065
need_pass = false;
1012
- pset .db = PQsetdbLogin (PQhost ( oldconn ), PQport ( oldconn ) ,
1066
+ pset .db = PQsetdbLogin (hostparam , portparam ,
1013
1067
NULL , NULL , dbparam , userparam , pwparam );
1014
1068
1015
1069
if (PQstatus (pset .db ) == CONNECTION_BAD &&
@@ -1061,14 +1115,24 @@ do_connect(const char *new_dbname, const char *new_user)
1061
1115
{
1062
1116
if (!QUIET ())
1063
1117
{
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
+ }
1072
1136
}
1073
1137
1074
1138
if (oldconn )
0 commit comments