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,
5555PsqlScanState scan_state ,
5656PQExpBuffer query_buf );
5757static 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 );
5959static 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 */
200203else if (strcmp (cmd ,"c" )== 0 || strcmp (cmd ,"connect" )== 0 )
201204{
202205char * opt1 ,
203- * opt2 ;
206+ * opt2 ,
207+ * opt3 ,
208+ * opt4 ;
204209char 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,
217224OT_SQLIDHACK ,& opt1q , true);
218225opt2 = psql_scan_slash_option (scan_state ,
219226OT_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 );
221251if (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 );
225259else 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 );
228266else
229267/* connect to default db as default user */
230- success = do_connect (NULL ,NULL );
268+ success = do_connect (NULL ,NULL , NULL , NULL );
231269
232270free (opt1 );
233271free (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 */
9611001static 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{
9641004PGconn * oldconn = pset .db ;
9651005const char * dbparam = NULL ;
9661006const char * userparam = NULL ;
1007+ const char * hostparam = NULL ;
1008+ const char * portparam = NULL ;
9671009const char * pwparam = NULL ;
9681010char * password_prompt = NULL ;
9691011char * prompted_password = NULL ;
@@ -985,6 +1027,18 @@ do_connect(const char *new_dbname, const char *new_user)
9851027else
9861028userparam = 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+
9881042if (userparam == NULL )
9891043password_prompt = strdup ("Password: " );
9901044else
@@ -1009,7 +1063,7 @@ do_connect(const char *new_dbname, const char *new_user)
10091063do
10101064{
10111065need_pass = false;
1012- pset .db = PQsetdbLogin (PQhost ( oldconn ), PQport ( oldconn ) ,
1066+ pset .db = PQsetdbLogin (hostparam , portparam ,
10131067NULL ,NULL ,dbparam ,userparam ,pwparam );
10141068
10151069if (PQstatus (pset .db )== CONNECTION_BAD &&
@@ -1061,14 +1115,24 @@ do_connect(const char *new_dbname, const char *new_user)
10611115{
10621116if (!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
10741138if (oldconn )