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

Commit557fcfa

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 parent2eb59ad commit557fcfa

File tree

6 files changed

+94
-32
lines changed

6 files changed

+94
-32
lines changed

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

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

768768
<varlistentry>
769-
<term><literal>\connect</literal> (or <literal>\c</literal>) <literal>[ <replaceable class="parameter">dbname</replaceable> [ <replaceable class="parameter">username</replaceable> ] [ <replaceable class="parameter">host</replaceable> ] [ <replaceable class="parameter">port</replaceable> ] ]</literal></term>
769+
<term><literal>\connect</literal> (or <literal>\c</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>
770770
<listitem>
771771
<para>
772772
Establishes a new connection to a <productname>PostgreSQL</>
773-
server. If the new connection is successfully made, the
774-
previous connection is closed. If any of <replaceable
775-
class="parameter">dbname</replaceable>, <replaceable
776-
class="parameter">username</replaceable>, <replaceable
777-
class="parameter">host</replaceable> or <replaceable
778-
class="parameter">port</replaceable> are omitted or specified
779-
as <literal>-</literal>, the value of that parameter from the
780-
previous connection is used. If there is no previous
781-
connection, the <application>libpq</application> default for
782-
the parameter's value is used.
773+
server. The connection parameters to use can be specified either
774+
using a positional syntax, or using <literal>conninfo</> connection
775+
strings as detailed in <xref linkend="libpq-connect">.
783776
</para>
784777

785778
<para>
779+
When using positional parameters, if any of
780+
<replaceable class="parameter">dbname</replaceable>,
781+
<replaceable class="parameter">username</replaceable>,
782+
<replaceable class="parameter">host</replaceable> or
783+
<replaceable class="parameter">port</replaceable> are omitted or
784+
specified as <literal>-</literal>, the value of that parameter from
785+
the previous connection is used; if there is no previous connection,
786+
the <application>libpq</application> default for the parameter's value
787+
is used. When using <literal>conninfo</> strings, no values from the
788+
previous connection are used for the new connection.
789+
</para>
790+
791+
<para>
792+
If the new connection is successfully made, the previous
793+
connection is closed.
786794
If the connection attempt failed (wrong user name, access
787795
denied, etc.), the previous connection will only be kept if
788796
<application>psql</application> is in interactive mode. When
@@ -792,6 +800,15 @@ testdb=&gt;
792800
mechanism that scripts are not accidentally acting on the
793801
wrong database on the other hand.
794802
</para>
803+
804+
<para>
805+
Examples:
806+
</para>
807+
<programlisting>
808+
=&gt; \c mydb myuser host.dom 6432
809+
=&gt; \c service=foo
810+
=&gt; \c "host=localhost port=5432 dbname=mydb connect_timeout=10 sslmode=disable"
811+
</programlisting>
795812
</listitem>
796813
</varlistentry>
797814

‎src/bin/psql/command.c

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,16 +1240,38 @@ do_connect(char *dbname, char *user, char *host, char *port)
12401240
PGconn*o_conn=pset.db,
12411241
*n_conn;
12421242
char*password=NULL;
1243+
boolkeep_password;
1244+
boolhas_connection_string;
12431245

1244-
if (!dbname)
1245-
dbname=PQdb(o_conn);
1246+
/* grab values from the old connection, unless supplied by caller */
12461247
if (!user)
12471248
user=PQuser(o_conn);
12481249
if (!host)
12491250
host=PQhost(o_conn);
12501251
if (!port)
12511252
port=PQport(o_conn);
12521253

1254+
has_connection_string=
1255+
dbname ?recognized_connection_string(dbname) : false;
1256+
1257+
/*
1258+
* Any change in the parameters read above makes us discard the password.
1259+
* We also discard it if we're to use a conninfo rather than the positional
1260+
* syntax.
1261+
*/
1262+
keep_password=
1263+
((strcmp(user,PQuser(o_conn))==0)&&
1264+
(!host||strcmp(host,PQhost(o_conn))==0)&&
1265+
(strcmp(port,PQport(o_conn))==0)&&
1266+
!has_connection_string);
1267+
1268+
/*
1269+
* Grab dbname from old connection unless supplied by caller. No password
1270+
* discard if this changes: passwords aren't (usually) database-specific.
1271+
*/
1272+
if (!dbname)
1273+
dbname=PQdb(o_conn);
1274+
12531275
/*
12541276
* If the user asked to be prompted for a password, ask for one now. If
12551277
* not, use the password from the old connection, provided the username
@@ -1264,7 +1286,7 @@ do_connect(char *dbname, char *user, char *host, char *port)
12641286
{
12651287
password=prompt_for_password(user);
12661288
}
1267-
elseif (o_conn&&user&&strcmp(PQuser(o_conn),user)==0)
1289+
elseif (o_conn&&keep_password)
12681290
{
12691291
password=strdup(PQpass(o_conn));
12701292
}
@@ -1274,21 +1296,28 @@ do_connect(char *dbname, char *user, char *host, char *port)
12741296
#definePARAMS_ARRAY_SIZE7
12751297
constchar**keywords=pg_malloc(PARAMS_ARRAY_SIZE*sizeof(*keywords));
12761298
constchar**values=pg_malloc(PARAMS_ARRAY_SIZE*sizeof(*values));
1299+
intparamnum=0;
12771300

1278-
keywords[0]="host";
1279-
values[0]=host;
1280-
keywords[1]="port";
1281-
values[1]=port;
1282-
keywords[2]="user";
1283-
values[2]=user;
1284-
keywords[3]="password";
1285-
values[3]=password;
1286-
keywords[4]="dbname";
1287-
values[4]=dbname;
1288-
keywords[5]="fallback_application_name";
1289-
values[5]=pset.progname;
1290-
keywords[6]=NULL;
1291-
values[6]=NULL;
1301+
keywords[0]="dbname";
1302+
values[0]=dbname;
1303+
1304+
if (!has_connection_string)
1305+
{
1306+
keywords[++paramnum]="host";
1307+
values[paramnum]=host;
1308+
keywords[++paramnum]="port";
1309+
values[paramnum]=port;
1310+
keywords[++paramnum]="user";
1311+
values[paramnum]=user;
1312+
}
1313+
keywords[++paramnum]="password";
1314+
values[paramnum]=password;
1315+
keywords[++paramnum]="fallback_application_name";
1316+
values[paramnum]=pset.progname;
1317+
1318+
/* add array terminator */
1319+
keywords[++paramnum]=NULL;
1320+
values[paramnum]=NULL;
12921321

12931322
n_conn=PQconnectdbParams(keywords,values, true);
12941323

‎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 returns true
1582+
* 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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ slashUsage(unsigned short int pager)
249249
fprintf(output,"\n");
250250

251251
fprintf(output,_("Connection\n"));
252-
fprintf(output,_(" \\c[onnect] [DBNAME|- USER|- HOST|- PORT|-]\n"
253-
" connect to new database (currently \"%s\")\n"),
252+
fprintf(output,_(" \\c[onnect]{[DBNAME|- USER|- HOST|- PORT|-] | conninfo}\n"
253+
" connect to new database (currently \"%s\")\n"),
254254
currdb);
255255
fprintf(output,_(" \\encoding [ENCODING] show or set client encoding\n"));
256256
fprintf(output,_(" \\password [USERNAME] securely change the password for a user\n"));

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2451,8 +2451,10 @@ psql_completion(char *text, int start, int end)
24512451
/* Backslash commands */
24522452
/* TODO: \dc \dd \dl */
24532453
elseif (strcmp(prev_wd,"\\connect")==0||strcmp(prev_wd,"\\c")==0)
2454-
COMPLETE_WITH_QUERY(Query_for_list_of_databases);
2455-
2454+
{
2455+
if (!recognized_connection_string(text))
2456+
COMPLETE_WITH_QUERY(Query_for_list_of_databases);
2457+
}
24562458
elseif (strncmp(prev_wd,"\\da",strlen("\\da"))==0)
24572459
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_aggregates,NULL);
24582460
elseif (strncmp(prev_wd,"\\db",strlen("\\db"))==0)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp