From c82d74778b195689e85c56d67b212d2804b62854 Mon Sep 17 00:00:00 2001 From: Hari Babu Date: Mon, 26 Mar 2018 17:38:21 +1100 Subject: [PATCH] PQhost to return active connected host and hostaddr details Earlier PQhost doesn't return the connected host details when the connection type is CHT_HOST_ADDRESS instead it returns the provided all connection host parameter values or the default host details, this can lead to confusion. It is better to provide the active host or hostaddr details of the connected server host irrespective of the connection type. when both host and hostaddr are specified in the connection string, active host parameter value is returned. PQhost function returns NULL when the input conn is NULL, otherwise returns an empty string when the input conn structure cannot be evaluated. Similarly PQPort is also changed to return the active connection port or NULL or an empty string. --- doc/src/sgml/libpq.sgml | 38 ++++++++++++++++++++++++++++++++++++-- src/interfaces/libpq/fe-connect.c | 25 +++++++++++++------------ 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 1fd5dd9fca..4166aad1c8 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -1692,7 +1692,7 @@ char *PQpass(const PGconn *conn); - Returns the server host name of the connection. + Returns the server host name of the active connection. This can be a host name, an IP address, or a directory path if the connection is via Unix socket. (The path case can be distinguished because it will always be an absolute path, beginning @@ -1701,6 +1701,25 @@ char *PQpass(const PGconn *conn); char *PQhost(const PGconn *conn); + + + The PQhost function returns NULL + when the input conn parameter is NULL + or an empty string if conn cannot be evaluated. + Otherwise, the return value depends on the properties of the conn: + specifically host and hostaddr must not + be NULL or empty string. Furthermore, if both host + and hostaddr properties exist on conn + the return value will contain only the host. Callers of this + function must carefully evaluate the return value, depending on the status of the connection. + + + + If multiple hosts were specified in the connection string, It is not possible to + rely on the output of the function PQhost until the connection + is established properly. The status of the connection can be checked by calling + PQstatus function. + @@ -1714,12 +1733,27 @@ char *PQhost(const PGconn *conn); - Returns the port of the connection. + Returns the port of the active connection. char *PQport(const PGconn *conn); + + + The PQport function returns NULL + when the input conn parameter is NULL + or an empty string if conn cannot be evaluated. + Otherwise, returns the active connection port. Callers of this function + must carefully evaluate the return value, depending on the status of the connection. + + + + If multiple ports were specified in the connection string, It is not possible to + rely on the output of the function PQport until the connection + is established properly. The status of the connection can be checked by calling + PQstatus function. + diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index 39c19998c2..33cf844bb3 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -6017,19 +6017,18 @@ PQhost(const PGconn *conn) { if (!conn) return NULL; - if (conn->connhost != NULL && - conn->connhost[conn->whichhost].type != CHT_HOST_ADDRESS) - return conn->connhost[conn->whichhost].host; - else if (conn->pghost != NULL && conn->pghost[0] != '\0') - return conn->pghost; - else + + if (conn->connhost != NULL) { -#ifdef HAVE_UNIX_SOCKETS - return DEFAULT_PGSOCKET_DIR; -#else - return DefaultHost; -#endif + if (conn->connhost[conn->whichhost].host != NULL && + conn->connhost[conn->whichhost].host[0] != '\0') + return conn->connhost[conn->whichhost].host; + else if ((conn->connhost[conn->whichhost].hostaddr != NULL) && + (conn->connhost[conn->whichhost].hostaddr[0] != '\0')) + return conn->connhost[conn->whichhost].hostaddr; } + + return ""; } char * @@ -6037,9 +6036,11 @@ PQport(const PGconn *conn) { if (!conn) return NULL; + if (conn->connhost != NULL) return conn->connhost[conn->whichhost].port; - return conn->pgport; + + return ""; } char * -- 2.16.1.windows.4