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

Commita44e54c

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 parent68a8f26 commita44e54c

File tree

6 files changed

+134
-37
lines changed

6 files changed

+134
-37
lines changed

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

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -778,23 +778,31 @@ testdb=>
778778
</varlistentry>
779779

780780
<varlistentry>
781-
<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>
781+
<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>
782782
<listitem>
783783
<para>
784784
Establishes a new connection to a <productname>PostgreSQL</>
785-
server. If the new connection is successfully made, the
786-
previous connection is closed. If any of <replaceable
787-
class="parameter">dbname</replaceable>, <replaceable
788-
class="parameter">username</replaceable>, <replaceable
789-
class="parameter">host</replaceable> or <replaceable
790-
class="parameter">port</replaceable> are omitted or specified
791-
as <literal>-</literal>, the value of that parameter from the
792-
previous connection is used. If there is no previous
793-
connection, the <application>libpq</application> default for
794-
the parameter's value is used.
785+
server. The connection parameters to use can be specified either
786+
using a positional syntax, or using <literal>conninfo</> connection
787+
strings as detailed in <xref linkend="libpq-connstring">.
795788
</para>
796789

797790
<para>
791+
When using positional parameters, if any of
792+
<replaceable class="parameter">dbname</replaceable>,
793+
<replaceable class="parameter">username</replaceable>,
794+
<replaceable class="parameter">host</replaceable> or
795+
<replaceable class="parameter">port</replaceable> are omitted or
796+
specified as <literal>-</literal>, the value of that parameter from
797+
the previous connection is used; if there is no previous connection,
798+
the <application>libpq</application> default for the parameter's value
799+
is used. When using <literal>conninfo</> strings, no values from the
800+
previous connection are used for the new connection.
801+
</para>
802+
803+
<para>
804+
If the new connection is successfully made, the previous
805+
connection is closed.
798806
If the connection attempt failed (wrong user name, access
799807
denied, etc.), the previous connection will only be kept if
800808
<application>psql</application> is in interactive mode. When
@@ -804,6 +812,16 @@ testdb=&gt;
804812
mechanism that scripts are not accidentally acting on the
805813
wrong database on the other hand.
806814
</para>
815+
816+
<para>
817+
Examples:
818+
</para>
819+
<programlisting>
820+
=&gt; \c mydb myuser host.dom 6432
821+
=&gt; \c service=foo
822+
=&gt; \c "host=localhost port=5432 dbname=mydb connect_timeout=10 sslmode=disable"
823+
=&gt; \c postgresql://tom@localhost/mydb?application_name=myapp
824+
</programlisting>
807825
</listitem>
808826
</varlistentry>
809827

‎src/bin/psql/command.c

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,6 +1595,8 @@ do_connect(char *dbname, char *user, char *host, char *port)
15951595
PGconn*o_conn=pset.db,
15961596
*n_conn;
15971597
char*password=NULL;
1598+
boolkeep_password;
1599+
boolhas_connection_string;
15981600

15991601
if (!o_conn&& (!dbname|| !user|| !host|| !port))
16001602
{
@@ -1608,15 +1610,35 @@ do_connect(char *dbname, char *user, char *host, char *port)
16081610
return false;
16091611
}
16101612

1611-
if (!dbname)
1612-
dbname=PQdb(o_conn);
1613+
/* grab values from the old connection, unless supplied by caller */
16131614
if (!user)
16141615
user=PQuser(o_conn);
16151616
if (!host)
16161617
host=PQhost(o_conn);
16171618
if (!port)
16181619
port=PQport(o_conn);
16191620

1621+
has_connection_string=
1622+
dbname ?recognized_connection_string(dbname) : false;
1623+
1624+
/*
1625+
* Any change in the parameters read above makes us discard the password.
1626+
* We also discard it if we're to use a conninfo rather than the positional
1627+
* syntax.
1628+
*/
1629+
keep_password=
1630+
((strcmp(user,PQuser(o_conn))==0)&&
1631+
(!host||strcmp(host,PQhost(o_conn))==0)&&
1632+
(strcmp(port,PQport(o_conn))==0)&&
1633+
!has_connection_string);
1634+
1635+
/*
1636+
* Grab dbname from old connection unless supplied by caller. No password
1637+
* discard if this changes: passwords aren't (usually) database-specific.
1638+
*/
1639+
if (!dbname)
1640+
dbname=PQdb(o_conn);
1641+
16201642
/*
16211643
* If the user asked to be prompted for a password, ask for one now. If
16221644
* not, use the password from the old connection, provided the username
@@ -1631,42 +1653,53 @@ do_connect(char *dbname, char *user, char *host, char *port)
16311653
{
16321654
password=prompt_for_password(user);
16331655
}
1634-
elseif (o_conn&&user&&strcmp(PQuser(o_conn),user)==0)
1656+
elseif (o_conn&&keep_password)
16351657
{
1636-
password=pg_strdup(PQpass(o_conn));
1658+
password=PQpass(o_conn);
1659+
if (password&&*password)
1660+
password=pg_strdup(password);
1661+
else
1662+
password=NULL;
16371663
}
16381664

16391665
while (true)
16401666
{
16411667
#definePARAMS_ARRAY_SIZE8
16421668
constchar**keywords=pg_malloc(PARAMS_ARRAY_SIZE*sizeof(*keywords));
16431669
constchar**values=pg_malloc(PARAMS_ARRAY_SIZE*sizeof(*values));
1670+
intparamnum=0;
1671+
1672+
keywords[0]="dbname";
1673+
values[0]=dbname;
1674+
1675+
if (!has_connection_string)
1676+
{
1677+
keywords[++paramnum]="host";
1678+
values[paramnum]=host;
1679+
keywords[++paramnum]="port";
1680+
values[paramnum]=port;
1681+
keywords[++paramnum]="user";
1682+
values[paramnum]=user;
1683+
}
1684+
keywords[++paramnum]="password";
1685+
values[paramnum]=password;
1686+
keywords[++paramnum]="fallback_application_name";
1687+
values[paramnum]=pset.progname;
1688+
keywords[++paramnum]="client_encoding";
1689+
values[paramnum]= (pset.notty||getenv("PGCLIENTENCODING")) ?NULL :"auto";
16441690

1645-
keywords[0]="host";
1646-
values[0]=host;
1647-
keywords[1]="port";
1648-
values[1]=port;
1649-
keywords[2]="user";
1650-
values[2]=user;
1651-
keywords[3]="password";
1652-
values[3]=password;
1653-
keywords[4]="dbname";
1654-
values[4]=dbname;
1655-
keywords[5]="fallback_application_name";
1656-
values[5]=pset.progname;
1657-
keywords[6]="client_encoding";
1658-
values[6]= (pset.notty||getenv("PGCLIENTENCODING")) ?NULL :"auto";
1659-
keywords[7]=NULL;
1660-
values[7]=NULL;
1691+
/* add array terminator */
1692+
keywords[++paramnum]=NULL;
1693+
values[paramnum]=NULL;
16611694

16621695
n_conn=PQconnectdbParams(keywords,values, true);
16631696

1664-
free(keywords);
1665-
free(values);
1697+
pg_free(keywords);
1698+
pg_free(values);
16661699

16671700
/* We can immediately discard the password -- no longer needed */
16681701
if (password)
1669-
free(password);
1702+
pg_free(password);
16701703

16711704
if (PQstatus(n_conn)==CONNECTION_OK)
16721705
break;

‎src/bin/psql/common.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,3 +1748,44 @@ expand_tilde(char **filename)
17481748

17491749
return;
17501750
}
1751+
1752+
/*
1753+
* Checks if connection string starts with either of the valid URI prefix
1754+
* designators.
1755+
*
1756+
* Returns the URI prefix length, 0 if the string doesn't contain a URI prefix.
1757+
*
1758+
* XXX This is a duplicate of the eponymous libpq function.
1759+
*/
1760+
staticint
1761+
uri_prefix_length(constchar*connstr)
1762+
{
1763+
/* The connection URI must start with either of the following designators: */
1764+
staticconstcharuri_designator[]="postgresql://";
1765+
staticconstcharshort_uri_designator[]="postgres://";
1766+
1767+
if (strncmp(connstr,uri_designator,
1768+
sizeof(uri_designator)-1)==0)
1769+
returnsizeof(uri_designator)-1;
1770+
1771+
if (strncmp(connstr,short_uri_designator,
1772+
sizeof(short_uri_designator)-1)==0)
1773+
returnsizeof(short_uri_designator)-1;
1774+
1775+
return0;
1776+
}
1777+
1778+
/*
1779+
* Recognized connection string either starts with a valid URI prefix or
1780+
* contains a "=" in it.
1781+
*
1782+
* Must be consistent with parse_connection_string: anything for which this
1783+
* returns true should at least look like it's parseable by that routine.
1784+
*
1785+
* XXX This is a duplicate of the eponymous libpq function.
1786+
*/
1787+
bool
1788+
recognized_connection_string(constchar*connstr)
1789+
{
1790+
returnuri_prefix_length(connstr)!=0||strchr(connstr,'=')!=NULL;
1791+
}

‎src/bin/psql/common.h

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

4747
externvoidexpand_tilde(char**filename);
4848

49+
externboolrecognized_connection_string(constchar*connstr);
50+
4951
#endif/* COMMON_H */

‎src/bin/psql/help.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,11 @@ slashUsage(unsigned short int pager)
244244

245245
fprintf(output,_("Connection\n"));
246246
if (currdb)
247-
fprintf(output,_(" \\c[onnect] [DBNAME|- USER|- HOST|- PORT|-]\n"
247+
fprintf(output,_(" \\c[onnect]{[DBNAME|- USER|- HOST|- PORT|-] | conninfo}\n"
248248
" connect to new database (currently \"%s\")\n"),
249249
currdb);
250250
else
251-
fprintf(output,_(" \\c[onnect] [DBNAME|- USER|- HOST|- PORT|-]\n"
251+
fprintf(output,_(" \\c[onnect]{[DBNAME|- USER|- HOST|- PORT|-] | conninfo}\n"
252252
" connect to new database (currently no connection)\n"));
253253
fprintf(output,_(" \\encoding [ENCODING] show or set client encoding\n"));
254254
fprintf(output,_(" \\password [USERNAME] securely change the password for a user\n"));

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3413,7 +3413,10 @@ psql_completion(const char *text, int start, int end)
34133413
/* Backslash commands */
34143414
/* TODO: \dc \dd \dl */
34153415
elseif (strcmp(prev_wd,"\\connect")==0||strcmp(prev_wd,"\\c")==0)
3416-
COMPLETE_WITH_QUERY(Query_for_list_of_databases);
3416+
{
3417+
if (!recognized_connection_string(text))
3418+
COMPLETE_WITH_QUERY(Query_for_list_of_databases);
3419+
}
34173420

34183421
elseif (strncmp(prev_wd,"\\da",strlen("\\da"))==0)
34193422
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_aggregates,NULL);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp