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

Commite146ca6

Browse files
committed
psql: fix \connect with URIs and conninfo strings
This is the second try at this, afterfcef161 failed miserably andhad to be reverted: as it turns out, libpq cannot depend on libpgcommonafter all. Instead of shuffling code in the master branch, make that onejust like 9.4 and accept the duplication. (This was all my own mistake,not the patch submitter's).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.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 parentf272098 commite146ca6

File tree

7 files changed

+142
-39
lines changed

7 files changed

+142
-39
lines changed

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

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

798798
<varlistentry>
799-
<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>
799+
<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>
800800
<listitem>
801801
<para>
802802
Establishes a new connection to a <productname>PostgreSQL</>
803-
server. If the new connection is successfully made, the
804-
previous connection is closed. If any of <replaceable
805-
class="parameter">dbname</replaceable>, <replaceable
806-
class="parameter">username</replaceable>, <replaceable
807-
class="parameter">host</replaceable> or <replaceable
808-
class="parameter">port</replaceable> are omitted or specified
809-
as <literal>-</literal>, the value of that parameter from the
810-
previous connection is used. If there is no previous
811-
connection, the <application>libpq</application> default for
812-
the parameter's value is used.
803+
server. The connection parameters to use can be specified either
804+
using a positional syntax, or using <literal>conninfo</> connection
805+
strings as detailed in <xref linkend="libpq-connstring">.
813806
</para>
814807

815808
<para>
809+
When using positional parameters, if any of
810+
<replaceable class="parameter">dbname</replaceable>,
811+
<replaceable class="parameter">username</replaceable>,
812+
<replaceable class="parameter">host</replaceable> or
813+
<replaceable class="parameter">port</replaceable> are omitted or
814+
specified as <literal>-</literal>, the value of that parameter from
815+
the previous connection is used; if there is no previous connection,
816+
the <application>libpq</application> default for the parameter's value
817+
is used. When using <literal>conninfo</> strings, no values from the
818+
previous connection are used for the new connection.
819+
</para>
820+
821+
<para>
822+
If the new connection is successfully made, the previous
823+
connection is closed.
816824
If the connection attempt failed (wrong user name, access
817825
denied, etc.), the previous connection will only be kept if
818826
<application>psql</application> is in interactive mode. When
@@ -822,6 +830,16 @@ testdb=&gt;
822830
mechanism that scripts are not accidentally acting on the
823831
wrong database on the other hand.
824832
</para>
833+
834+
<para>
835+
Examples:
836+
</para>
837+
<programlisting>
838+
=&gt; \c mydb myuser host.dom 6432
839+
=&gt; \c service=foo
840+
=&gt; \c "host=localhost port=5432 dbname=mydb connect_timeout=10 sslmode=disable"
841+
=&gt; \c postgresql://tom@localhost/mydb?application_name=myapp
842+
</programlisting>
825843
</listitem>
826844
</varlistentry>
827845

‎src/bin/psql/command.c

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,6 +1608,8 @@ do_connect(char *dbname, char *user, char *host, char *port)
16081608
PGconn*o_conn=pset.db,
16091609
*n_conn;
16101610
char*password=NULL;
1611+
boolkeep_password;
1612+
boolhas_connection_string;
16111613

16121614
if (!o_conn&& (!dbname|| !user|| !host|| !port))
16131615
{
@@ -1621,15 +1623,35 @@ do_connect(char *dbname, char *user, char *host, char *port)
16211623
return false;
16221624
}
16231625

1624-
if (!dbname)
1625-
dbname=PQdb(o_conn);
1626+
/* grab values from the old connection, unless supplied by caller */
16261627
if (!user)
16271628
user=PQuser(o_conn);
16281629
if (!host)
16291630
host=PQhost(o_conn);
16301631
if (!port)
16311632
port=PQport(o_conn);
16321633

1634+
has_connection_string=
1635+
dbname ?recognized_connection_string(dbname) : false;
1636+
1637+
/*
1638+
* Any change in the parameters read above makes us discard the password.
1639+
* We also discard it if we're to use a conninfo rather than the positional
1640+
* syntax.
1641+
*/
1642+
keep_password=
1643+
((strcmp(user,PQuser(o_conn))==0)&&
1644+
(!host||strcmp(host,PQhost(o_conn))==0)&&
1645+
(strcmp(port,PQport(o_conn))==0)&&
1646+
!has_connection_string);
1647+
1648+
/*
1649+
* Grab dbname from old connection unless supplied by caller. No password
1650+
* discard if this changes: passwords aren't (usually) database-specific.
1651+
*/
1652+
if (!dbname)
1653+
dbname=PQdb(o_conn);
1654+
16331655
/*
16341656
* If the user asked to be prompted for a password, ask for one now. If
16351657
* not, use the password from the old connection, provided the username
@@ -1644,42 +1666,53 @@ do_connect(char *dbname, char *user, char *host, char *port)
16441666
{
16451667
password=prompt_for_password(user);
16461668
}
1647-
elseif (o_conn&&user&&strcmp(PQuser(o_conn),user)==0)
1669+
elseif (o_conn&&keep_password)
16481670
{
1649-
password=pg_strdup(PQpass(o_conn));
1671+
password=PQpass(o_conn);
1672+
if (password&&*password)
1673+
password=pg_strdup(password);
1674+
else
1675+
password=NULL;
16501676
}
16511677

16521678
while (true)
16531679
{
16541680
#definePARAMS_ARRAY_SIZE8
16551681
constchar**keywords=pg_malloc(PARAMS_ARRAY_SIZE*sizeof(*keywords));
16561682
constchar**values=pg_malloc(PARAMS_ARRAY_SIZE*sizeof(*values));
1683+
intparamnum=0;
1684+
1685+
keywords[0]="dbname";
1686+
values[0]=dbname;
1687+
1688+
if (!has_connection_string)
1689+
{
1690+
keywords[++paramnum]="host";
1691+
values[paramnum]=host;
1692+
keywords[++paramnum]="port";
1693+
values[paramnum]=port;
1694+
keywords[++paramnum]="user";
1695+
values[paramnum]=user;
1696+
}
1697+
keywords[++paramnum]="password";
1698+
values[paramnum]=password;
1699+
keywords[++paramnum]="fallback_application_name";
1700+
values[paramnum]=pset.progname;
1701+
keywords[++paramnum]="client_encoding";
1702+
values[paramnum]= (pset.notty||getenv("PGCLIENTENCODING")) ?NULL :"auto";
16571703

1658-
keywords[0]="host";
1659-
values[0]=host;
1660-
keywords[1]="port";
1661-
values[1]=port;
1662-
keywords[2]="user";
1663-
values[2]=user;
1664-
keywords[3]="password";
1665-
values[3]=password;
1666-
keywords[4]="dbname";
1667-
values[4]=dbname;
1668-
keywords[5]="fallback_application_name";
1669-
values[5]=pset.progname;
1670-
keywords[6]="client_encoding";
1671-
values[6]= (pset.notty||getenv("PGCLIENTENCODING")) ?NULL :"auto";
1672-
keywords[7]=NULL;
1673-
values[7]=NULL;
1704+
/* add array terminator */
1705+
keywords[++paramnum]=NULL;
1706+
values[paramnum]=NULL;
16741707

16751708
n_conn=PQconnectdbParams(keywords,values, true);
16761709

1677-
free(keywords);
1678-
free(values);
1710+
pg_free(keywords);
1711+
pg_free(values);
16791712

16801713
/* We can immediately discard the password -- no longer needed */
16811714
if (password)
1682-
free(password);
1715+
pg_free(password);
16831716

16841717
if (PQstatus(n_conn)==CONNECTION_OK)
16851718
break;

‎src/bin/psql/common.c

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

18471847
return;
18481848
}
1849+
1850+
/*
1851+
* Checks if connection string starts with either of the valid URI prefix
1852+
* designators.
1853+
*
1854+
* Returns the URI prefix length, 0 if the string doesn't contain a URI prefix.
1855+
*
1856+
* XXX This is a duplicate of the eponymous libpq function.
1857+
*/
1858+
staticint
1859+
uri_prefix_length(constchar*connstr)
1860+
{
1861+
/* The connection URI must start with either of the following designators: */
1862+
staticconstcharuri_designator[]="postgresql://";
1863+
staticconstcharshort_uri_designator[]="postgres://";
1864+
1865+
if (strncmp(connstr,uri_designator,
1866+
sizeof(uri_designator)-1)==0)
1867+
returnsizeof(uri_designator)-1;
1868+
1869+
if (strncmp(connstr,short_uri_designator,
1870+
sizeof(short_uri_designator)-1)==0)
1871+
returnsizeof(short_uri_designator)-1;
1872+
1873+
return0;
1874+
}
1875+
1876+
/*
1877+
* Recognized connection string either starts with a valid URI prefix or
1878+
* contains a "=" in it.
1879+
*
1880+
* Must be consistent with parse_connection_string: anything for which this
1881+
* returns true should at least look like it's parseable by that routine.
1882+
*
1883+
* XXX This is a duplicate of the eponymous libpq function.
1884+
*/
1885+
bool
1886+
recognized_connection_string(constchar*connstr)
1887+
{
1888+
returnuri_prefix_length(connstr)!=0||strchr(connstr,'=')!=NULL;
1889+
}

‎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
@@ -260,11 +260,11 @@ slashUsage(unsigned short int pager)
260260

261261
fprintf(output,_("Connection\n"));
262262
if (currdb)
263-
fprintf(output,_(" \\c[onnect] [DBNAME|- USER|- HOST|- PORT|-]\n"
263+
fprintf(output,_(" \\c[onnect]{[DBNAME|- USER|- HOST|- PORT|-] | conninfo}\n"
264264
" connect to new database (currently \"%s\")\n"),
265265
currdb);
266266
else
267-
fprintf(output,_(" \\c[onnect] [DBNAME|- USER|- HOST|- PORT|-]\n"
267+
fprintf(output,_(" \\c[onnect]{[DBNAME|- USER|- HOST|- PORT|-] | conninfo}\n"
268268
" connect to new database (currently no connection)\n"));
269269
fprintf(output,_(" \\encoding [ENCODING] show or set client encoding\n"));
270270
fprintf(output,_(" \\password [USERNAME] securely change the password for a user\n"));

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3707,10 +3707,15 @@ psql_completion(const char *text, int start, int end)
37073707
COMPLETE_WITH_LIST_CS(my_list);
37083708
}
37093709
elseif (strcmp(prev_wd,"\\connect")==0||strcmp(prev_wd,"\\c")==0)
3710-
COMPLETE_WITH_QUERY(Query_for_list_of_databases);
3710+
{
3711+
if (!recognized_connection_string(text))
3712+
COMPLETE_WITH_QUERY(Query_for_list_of_databases);
3713+
}
37113714
elseif (strcmp(prev2_wd,"\\connect")==0||strcmp(prev2_wd,"\\c")==0)
3712-
COMPLETE_WITH_QUERY(Query_for_list_of_roles);
3713-
3715+
{
3716+
if (!recognized_connection_string(prev_wd))
3717+
COMPLETE_WITH_QUERY(Query_for_list_of_roles);
3718+
}
37143719
elseif (strncmp(prev_wd,"\\da",strlen("\\da"))==0)
37153720
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_aggregates,NULL);
37163721
elseif (strncmp(prev_wd,"\\db",strlen("\\db"))==0)

‎src/interfaces/libpq/fe-connect.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4197,6 +4197,8 @@ parse_connection_string(const char *connstr, PQExpBuffer errorMessage,
41974197
* designators.
41984198
*
41994199
* Returns the URI prefix length, 0 if the string doesn't contain a URI prefix.
4200+
*
4201+
* XXX this is duplicated in psql/common.c.
42004202
*/
42014203
staticint
42024204
uri_prefix_length(constchar*connstr)
@@ -4218,6 +4220,8 @@ uri_prefix_length(const char *connstr)
42184220
*
42194221
* Must be consistent with parse_connection_string: anything for which this
42204222
* returns true should at least look like it's parseable by that routine.
4223+
*
4224+
* XXX this is duplicated in psql/common.c
42214225
*/
42224226
staticbool
42234227
recognized_connection_string(constchar*connstr)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp