Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit276591b

Browse files
committed
psql: fix \connect with URIs and conninfo strings
psql was already accepting conninfo strings as the first parameter in\connect, but the way it worked wasn't sane; some of the otherparameters would get the previous connection's values, causing it toconnect to a completely unexpected server or, more likely, not findingany server at all because of completely wrong combinations ofparameters.Fix by explicitely checking for a conninfo-looking parameter in thedbname position; if one is found, use its complete specification ratherthan mix with the other arguments. Also, change tab-completion to nottry to complete conninfo/URI-looking "dbnames" and document thatconninfos are accepted as first argument.There was a weak consensus to backpatch this, because while the behaviorof using the dbname as a conninfo is nowhere documented for \connect, itis reasonable to expect that it works because it does work in many othercontexts. Therefore this is backpatched all the way back to 9.0.To implement this, routines previously private to libpq have beenduplicated so that psql can decide what looks like a conninfo/URIstring. In back branches, just duplicate the same code all the way backto 9.2, where URIs where introduced; 9.0 and 9.1 have a simpler version.In master, the routines are moved to src/common and renamed.Author: David Fetter, Andrew Dunstan. Some editorialization by me(probably earning a Gierth's "Sloppy" badge in the process.)Reviewers: Andrew Gierth, Erik Rijkers, Pavel Stěhule, Stephen Frost,Robert Haas, Andrew Dunstan.
1 parent41d2cb8 commit276591b

File tree

6 files changed

+96
-34
lines changed

6 files changed

+96
-34
lines changed

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

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -745,23 +745,31 @@ testdb=>
745745
</varlistentry>
746746

747747
<varlistentry>
748-
<term><literal>\c</literal> or <literal>\connect</literal> <literal>[ <replaceable class="parameter">dbname</replaceable> [ <replaceable class="parameter">username</replaceable> ] [ <replaceable class="parameter">host</replaceable> ] [ <replaceable class="parameter">port</replaceable> ] ]</literal></term>
748+
<term><literal>\c</literal> or <literal>\connect</literal> <literal>[ <replaceable class="parameter">dbname</replaceable> [ <replaceable class="parameter">username</replaceable> ] [ <replaceable class="parameter">host</replaceable> ] [ <replaceable class="parameter">port</replaceable> ] ] | <replaceable class="parameter">conninfo</replaceable></literal></term>
749749
<listitem>
750750
<para>
751751
Establishes a new connection to a <productname>PostgreSQL</>
752-
server. If the new connection is successfully made, the
753-
previous connection is closed. If any of <replaceable
754-
class="parameter">dbname</replaceable>, <replaceable
755-
class="parameter">username</replaceable>, <replaceable
756-
class="parameter">host</replaceable> or <replaceable
757-
class="parameter">port</replaceable> are omitted or specified
758-
as <literal>-</literal>, the value of that parameter from the
759-
previous connection is used. If there is no previous
760-
connection, the <application>libpq</application> default for
761-
the parameter's value is used.
752+
server. The connection parameters to use can be specified either
753+
using a positional syntax, or using <literal>conninfo</> connection
754+
strings as detailed in <xref linkend="libpq-connect">.
762755
</para>
763756

764757
<para>
758+
When using positional parameters, if any of
759+
<replaceable class="parameter">dbname</replaceable>,
760+
<replaceable class="parameter">username</replaceable>,
761+
<replaceable class="parameter">host</replaceable> or
762+
<replaceable class="parameter">port</replaceable> are omitted or
763+
specified as <literal>-</literal>, the value of that parameter from
764+
the previous connection is used; if there is no previous connection,
765+
the <application>libpq</application> default for the parameter's value
766+
is used. When using <literal>conninfo</> strings, no values from the
767+
previous connection are used for the new connection.
768+
</para>
769+
770+
<para>
771+
If the new connection is successfully made, the previous
772+
connection is closed.
765773
If the connection attempt failed (wrong user name, access
766774
denied, etc.), the previous connection will only be kept if
767775
<application>psql</application> is in interactive mode. When
@@ -771,6 +779,15 @@ testdb=&gt;
771779
mechanism that scripts are not accidentally acting on the
772780
wrong database on the other hand.
773781
</para>
782+
783+
<para>
784+
Examples:
785+
</para>
786+
<programlisting>
787+
=&gt; \c mydb myuser host.dom 6432
788+
=&gt; \c service=foo
789+
=&gt; \c "host=localhost port=5432 dbname=mydb connect_timeout=10 sslmode=disable"
790+
</programlisting>
774791
</listitem>
775792
</varlistentry>
776793

‎src/bin/psql/command.c

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,16 +1467,38 @@ do_connect(char *dbname, char *user, char *host, char *port)
14671467
PGconn*o_conn=pset.db,
14681468
*n_conn;
14691469
char*password=NULL;
1470+
boolkeep_password;
1471+
boolhas_connection_string;
14701472

1471-
if (!dbname)
1472-
dbname=PQdb(o_conn);
1473+
/* grab values from the old connection, unless supplied by caller */
14731474
if (!user)
14741475
user=PQuser(o_conn);
14751476
if (!host)
14761477
host=PQhost(o_conn);
14771478
if (!port)
14781479
port=PQport(o_conn);
14791480

1481+
has_connection_string=
1482+
dbname ?recognized_connection_string(dbname) : false;
1483+
1484+
/*
1485+
* Any change in the parameters read above makes us discard the password.
1486+
* We also discard it if we're to use a conninfo rather than the positional
1487+
* syntax.
1488+
*/
1489+
keep_password=
1490+
((strcmp(user,PQuser(o_conn))==0)&&
1491+
(!host||strcmp(host,PQhost(o_conn))==0)&&
1492+
(strcmp(port,PQport(o_conn))==0)&&
1493+
!has_connection_string);
1494+
1495+
/*
1496+
* Grab dbname from old connection unless supplied by caller. No password
1497+
* discard if this changes: passwords aren't (usually) database-specific.
1498+
*/
1499+
if (!dbname)
1500+
dbname=PQdb(o_conn);
1501+
14801502
/*
14811503
* If the user asked to be prompted for a password, ask for one now. If
14821504
* not, use the password from the old connection, provided the username
@@ -1491,7 +1513,7 @@ do_connect(char *dbname, char *user, char *host, char *port)
14911513
{
14921514
password=prompt_for_password(user);
14931515
}
1494-
elseif (o_conn&&user&&strcmp(PQuser(o_conn),user)==0)
1516+
elseif (o_conn&&keep_password)
14951517
{
14961518
password=strdup(PQpass(o_conn));
14971519
}
@@ -1501,23 +1523,30 @@ do_connect(char *dbname, char *user, char *host, char *port)
15011523
#definePARAMS_ARRAY_SIZE8
15021524
constchar**keywords=pg_malloc(PARAMS_ARRAY_SIZE*sizeof(*keywords));
15031525
constchar**values=pg_malloc(PARAMS_ARRAY_SIZE*sizeof(*values));
1504-
1505-
keywords[0]="host";
1506-
values[0]=host;
1507-
keywords[1]="port";
1508-
values[1]=port;
1509-
keywords[2]="user";
1510-
values[2]=user;
1511-
keywords[3]="password";
1512-
values[3]=password;
1513-
keywords[4]="dbname";
1514-
values[4]=dbname;
1515-
keywords[5]="fallback_application_name";
1516-
values[5]=pset.progname;
1517-
keywords[6]="client_encoding";
1518-
values[6]= (pset.notty||getenv("PGCLIENTENCODING")) ?NULL :"auto";
1519-
keywords[7]=NULL;
1520-
values[7]=NULL;
1526+
intparamnum=0;
1527+
1528+
keywords[0]="dbname";
1529+
values[0]=dbname;
1530+
1531+
if (!has_connection_string)
1532+
{
1533+
keywords[++paramnum]="host";
1534+
values[paramnum]=host;
1535+
keywords[++paramnum]="port";
1536+
values[paramnum]=port;
1537+
keywords[++paramnum]="user";
1538+
values[paramnum]=user;
1539+
}
1540+
keywords[++paramnum]="password";
1541+
values[paramnum]=password;
1542+
keywords[++paramnum]="fallback_application_name";
1543+
values[paramnum]=pset.progname;
1544+
keywords[++paramnum]="client_encoding";
1545+
values[paramnum]= (pset.notty||getenv("PGCLIENTENCODING")) ?NULL :"auto";
1546+
1547+
/* add array terminator */
1548+
keywords[++paramnum]=NULL;
1549+
values[paramnum]=NULL;
15211550

15221551
n_conn=PQconnectdbParams(keywords,values, true);
15231552

‎src/bin/psql/common.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,3 +1574,15 @@ expand_tilde(char **filename)
15741574

15751575
return*filename;
15761576
}
1577+
1578+
/*
1579+
* Recognized connection string contains a "=" in it.
1580+
*
1581+
* Must be consistent with conninfo_parse: anything for which this
1582+
* returns true should at least look like it's parseable by that routine.
1583+
*/
1584+
bool
1585+
recognized_connection_string(constchar*connstr)
1586+
{
1587+
returnstrchr(connstr,'=')!=NULL;
1588+
}

‎src/bin/psql/common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,6 @@ extern const char *session_username(void);
6363

6464
externchar*expand_tilde(char**filename);
6565

66+
externboolrecognized_connection_string(constchar*connstr);
67+
6668
#endif/* COMMON_H */

‎src/bin/psql/help.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ slashUsage(unsigned short int pager)
251251
fprintf(output,"\n");
252252

253253
fprintf(output,_("Connection\n"));
254-
fprintf(output,_(" \\c[onnect] [DBNAME|- USER|- HOST|- PORT|-]\n"
254+
fprintf(output,_(" \\c[onnect]{[DBNAME|- USER|- HOST|- PORT|-] | conninfo}\n"
255255
" connect to new database (currently \"%s\")\n"),
256256
currdb);
257257
fprintf(output,_(" \\encoding [ENCODING] show or set client encoding\n"));

‎src/bin/psql/tab-complete.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2776,8 +2776,10 @@ psql_completion(char *text, int start, int end)
27762776
/* Backslash commands */
27772777
/* TODO: \dc \dd \dl */
27782778
elseif (strcmp(prev_wd,"\\connect")==0||strcmp(prev_wd,"\\c")==0)
2779-
COMPLETE_WITH_QUERY(Query_for_list_of_databases);
2780-
2779+
{
2780+
if (!recognized_connection_string(text))
2781+
COMPLETE_WITH_QUERY(Query_for_list_of_databases);
2782+
}
27812783
elseif (strncmp(prev_wd,"\\da",strlen("\\da"))==0)
27822784
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_aggregates,NULL);
27832785
elseif (strncmp(prev_wd,"\\db",strlen("\\db"))==0)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp