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

Commit6203881

Browse files
committed
libpq: PQhost to return active connected host or hostaddr
Previously, PQhost didn't return the connected host details when theconnection type was CHT_HOST_ADDRESS (i.e., via hostaddr). Instead, itreturned the complete host connection parameter (which could containmultiple hosts) or the default host details, which was confusing andarguably incorrect.Change this to return the actually connected host or hostaddrirrespective of the connection type. When hostaddr but no host wasspecified, hostaddr is now returned. Never return the original hostconnection parameter, and document that PQhost cannot be relied onbefore the connection is established.PQport is similarly changed to always return the active connection portand never the original connection parameter.Back-patch of commit1944cdcinto the v10 branch.Author: Hari Babu <kommi.haribabu@gmail.com>Reviewed-by: Michael Paquier <michael@paquier.xyz>Reviewed-by: Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp>Reviewed-by: David G. Johnston <david.g.johnston@gmail.com>
1 parentb805b63 commit6203881

File tree

2 files changed

+59
-14
lines changed

2 files changed

+59
-14
lines changed

‎doc/src/sgml/libpq.sgml

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,7 +1619,7 @@ char *PQpass(const PGconn *conn);
16191619

16201620
<listitem>
16211621
<para>
1622-
Returns the server host name of the connection.
1622+
Returns the server host name of theactiveconnection.
16231623
This can be a host name, an IP address, or a directory path if the
16241624
connection is via Unix socket. (The path case can be distinguished
16251625
because it will always be an absolute path, beginning
@@ -1628,6 +1628,30 @@ char *PQpass(const PGconn *conn);
16281628
char *PQhost(const PGconn *conn);
16291629
</synopsis>
16301630
</para>
1631+
1632+
<para>
1633+
If the connection parameters specified both <literal>host</literal> and
1634+
<literal>hostaddr</literal>, then <function>PQhost</function> will
1635+
return the <literal>host</literal> information. If only
1636+
<literal>hostaddr</literal> was specified, then that is returned.
1637+
If multiple hosts were specified in the connection parameters,
1638+
<function>PQhost</function> returns the host actually connected to.
1639+
</para>
1640+
1641+
<para>
1642+
<function>PQhost</function> returns <symbol>NULL</symbol> if the
1643+
<parameter>conn</parameter> argument is <symbol>NULL</symbol>.
1644+
Otherwise, if there is an error producing the host information (perhaps
1645+
if the connection has not been fully established or there was an
1646+
error), it returns an empty string.
1647+
</para>
1648+
1649+
<para>
1650+
If multiple hosts were specified in the connection parameters, it is
1651+
not possible to rely on the result of <function>PQhost</function> until
1652+
the connection is established. The status of the connection can be
1653+
checked using the function <function>PQstatus</function>.
1654+
</para>
16311655
</listitem>
16321656
</varlistentry>
16331657

@@ -1641,12 +1665,32 @@ char *PQhost(const PGconn *conn);
16411665

16421666
<listitem>
16431667
<para>
1644-
Returns the port of the connection.
1668+
Returns the port of theactiveconnection.
16451669

16461670
<synopsis>
16471671
char *PQport(const PGconn *conn);
16481672
</synopsis>
16491673
</para>
1674+
1675+
<para>
1676+
If multiple ports were specified in the connection parameters,
1677+
<function>PQport</function> returns the port actually connected to.
1678+
</para>
1679+
1680+
<para>
1681+
<function>PQport</function> returns <symbol>NULL</symbol> if the
1682+
<parameter>conn</parameter> argument is <symbol>NULL</symbol>.
1683+
Otherwise, if there is an error producing the port information (perhaps
1684+
if the connection has not been fully established or there was an
1685+
error), it returns an empty string.
1686+
</para>
1687+
1688+
<para>
1689+
If multiple ports were specified in the connection parameters, it is
1690+
not possible to rely on the result of <function>PQport</function> until
1691+
the connection is established. The status of the connection can be
1692+
checked using the function <function>PQstatus</function>.
1693+
</para>
16501694
</listitem>
16511695
</varlistentry>
16521696

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6013,29 +6013,30 @@ PQhost(const PGconn *conn)
60136013
{
60146014
if (!conn)
60156015
returnNULL;
6016-
if (conn->connhost!=NULL&&
6017-
conn->connhost[conn->whichhost].type!=CHT_HOST_ADDRESS)
6018-
returnconn->connhost[conn->whichhost].host;
6019-
elseif (conn->pghost!=NULL&&conn->pghost[0]!='\0')
6020-
returnconn->pghost;
6021-
else
6016+
6017+
if (conn->connhost!=NULL)
60226018
{
6023-
#ifdefHAVE_UNIX_SOCKETS
6024-
returnDEFAULT_PGSOCKET_DIR;
6025-
#else
6026-
returnDefaultHost;
6027-
#endif
6019+
if (conn->connhost[conn->whichhost].host!=NULL&&
6020+
conn->connhost[conn->whichhost].host[0]!='\0')
6021+
returnconn->connhost[conn->whichhost].host;
6022+
elseif (conn->connhost[conn->whichhost].hostaddr!=NULL&&
6023+
conn->connhost[conn->whichhost].hostaddr[0]!='\0')
6024+
returnconn->connhost[conn->whichhost].hostaddr;
60286025
}
6026+
6027+
return"";
60296028
}
60306029

60316030
char*
60326031
PQport(constPGconn*conn)
60336032
{
60346033
if (!conn)
60356034
returnNULL;
6035+
60366036
if (conn->connhost!=NULL)
60376037
returnconn->connhost[conn->whichhost].port;
6038-
returnconn->pgport;
6038+
6039+
return"";
60396040
}
60406041

60416042
char*

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp