libpq: Add service name to PGconn and PQservice()
authorMichael Paquier <michael@paquier.xyz>
Wed, 18 Dec 2024 05:53:42 +0000 (14:53 +0900)
committerMichael Paquier <michael@paquier.xyz>
Wed, 18 Dec 2024 05:53:42 +0000 (14:53 +0900)
This commit adds one field to PGconn for the database service name (if
any), with PQservice() as routine to retrieve it.  Like the other
routines of this area, NULL is returned as result if the connection is
NULL.

A follow-up patch will make use of this feature to be able to display
the service name in the psql prompt.

Author: Michael Banck
Reviewed-by: Greg Sabino Mullane
Discusion: https://postgr.es/m/6723c612.050a0220.1567f4.b94a@mx.google.com

doc/src/sgml/libpq.sgml
src/interfaces/libpq/exports.txt
src/interfaces/libpq/fe-connect.c
src/interfaces/libpq/libpq-fe.h
src/interfaces/libpq/libpq-int.h

index 01f259fd0dc40f54b3d3fd5349d1d3148611f646..105b22b31715176cf804b5b60be8a706619a18d9 100644 (file)
@@ -2530,6 +2530,26 @@ char *PQport(const PGconn *conn);
      </listitem>
     </varlistentry>
 
+    <varlistentry id="libpq-PQservice">
+     <term><function>PQservice</function><indexterm><primary>PQservice</primary></indexterm></term>
+
+     <listitem>
+      <para>
+       Returns the service of the active connection.
+
+<synopsis>
+char *PQservice(const PGconn *conn);
+</synopsis>
+      </para>
+
+      <para>
+       <xref linkend="libpq-PQservice"/> returns <symbol>NULL</symbol> if the
+       <parameter>conn</parameter> argument is <symbol>NULL</symbol>.
+       Otherwise, if there was no service provided, it returns an empty string.
+      </para>
+     </listitem>
+    </varlistentry>
+
     <varlistentry id="libpq-PQtty">
      <term><function>PQtty</function><indexterm><primary>PQtty</primary></indexterm></term>
 
index 5d8213e0b571a95e5163b9d42590206c988d043d..2ad2cbf5ca3cc4ddd5d9ef6b6565e935022a19e8 100644 (file)
@@ -205,3 +205,4 @@ PQcancelFinish            202
 PQsocketPoll              203
 PQsetChunkedRowsMode      204
 PQgetCurrentTimeUSec      205
+PQservice                 206
index aaf87e8e885fb2dcb98718ccb4f98b15f7861eca..ddcc7b60ab0a0998b426710b82af1b89fac7b466 100644 (file)
@@ -190,7 +190,8 @@ typedef struct _internalPQconninfoOption
 
 static const internalPQconninfoOption PQconninfoOptions[] = {
        {"service", "PGSERVICE", NULL, NULL,
-       "Database-Service", "", 20, -1},
+               "Database-Service", "", 20,
+       offsetof(struct pg_conn, pgservice)},
 
        {"user", "PGUSER", NULL, NULL,
                "Database-User", "", 20,
@@ -7040,6 +7041,14 @@ PQdb(const PGconn *conn)
        return conn->dbName;
 }
 
+char *
+PQservice(const PGconn *conn)
+{
+       if (!conn)
+               return NULL;
+       return conn->pgservice;
+}
+
 char *
 PQuser(const PGconn *conn)
 {
index 15012c770c4a6a146f71383fb6ea4944c46758ba..5947e7c766f009ca73310a3b514ceba956574b2e 100644 (file)
@@ -385,6 +385,7 @@ extern int  PQrequestCancel(PGconn *conn);
 
 /* Accessor functions for PGconn objects */
 extern char *PQdb(const PGconn *conn);
+extern char *PQservice(const PGconn *conn);
 extern char *PQuser(const PGconn *conn);
 extern char *PQpass(const PGconn *conn);
 extern char *PQhost(const PGconn *conn);
index 08cc391cbd4bb9203a9d15f7fed6db84887593b8..dcebca98988097c1defe9a4f7203a8426a37df1a 100644 (file)
@@ -394,6 +394,7 @@ struct pg_conn
        char       *fbappname;          /* fallback application name */
        char       *dbName;                     /* database name */
        char       *replication;        /* connect as the replication standby? */
+       char       *pgservice;          /* Postgres service, if any */
        char       *pguser;                     /* Postgres username and password, if any */
        char       *pgpass;
        char       *pgpassfile;         /* path to a file containing password(s) */