From 4b99fed7541e330b669fe488a274c0c69490391c Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Wed, 18 Dec 2024 14:53:42 +0900 Subject: [PATCH] libpq: Add service name to PGconn and PQservice() 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 | 20 ++++++++++++++++++++ src/interfaces/libpq/exports.txt | 1 + src/interfaces/libpq/fe-connect.c | 11 ++++++++++- src/interfaces/libpq/libpq-fe.h | 1 + src/interfaces/libpq/libpq-int.h | 1 + 5 files changed, 33 insertions(+), 1 deletion(-) diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 01f259fd0d..105b22b317 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -2530,6 +2530,26 @@ char *PQport(const PGconn *conn); + + PQservicePQservice + + + + Returns the service of the active connection. + + +char *PQservice(const PGconn *conn); + + + + + returns NULL if the + conn argument is NULL. + Otherwise, if there was no service provided, it returns an empty string. + + + + PQttyPQtty diff --git a/src/interfaces/libpq/exports.txt b/src/interfaces/libpq/exports.txt index 5d8213e0b5..2ad2cbf5ca 100644 --- a/src/interfaces/libpq/exports.txt +++ b/src/interfaces/libpq/exports.txt @@ -205,3 +205,4 @@ PQcancelFinish 202 PQsocketPoll 203 PQsetChunkedRowsMode 204 PQgetCurrentTimeUSec 205 +PQservice 206 diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index aaf87e8e88..ddcc7b60ab 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -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) { diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h index 15012c770c..5947e7c766 100644 --- a/src/interfaces/libpq/libpq-fe.h +++ b/src/interfaces/libpq/libpq-fe.h @@ -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); diff --git a/src/interfaces/libpq/libpq-int.h b/src/interfaces/libpq/libpq-int.h index 08cc391cbd..dcebca9898 100644 --- a/src/interfaces/libpq/libpq-int.h +++ b/src/interfaces/libpq/libpq-int.h @@ -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) */ -- 2.39.5