Hi
2015-08-25 17:21 GMT+02:00 Tom Lane <[email protected]>:
> Jim Nasby <[email protected]> writes:
> > What I've had problems with is trying to correlate psql specified
> > connection attributes with things like DBI. It would be nice if there
> > was a way to get a fully formed connection URI for the current
> connection.
>
> Yeah, although I'd think the capability to create such a URI is libpq's
> province not psql's. Maybe a PQgetConnectionURI(PGConn) function in
> libpq, and some psql backslash command to access that? Or maybe a nicer
> API would be that there's a magic psql variable containing the URI; not
> sure.
>
proof concept of PQGetConnectionUri and \uri command.
missing:
connection options
uri encoding
>
> regards, tom lane
>
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
new file mode 100644
index 6181a61..47e27cd
*** a/src/bin/psql/command.c
--- b/src/bin/psql/command.c
*************** exec_command(const char *cmd,
*** 1505,1510 ****
--- 1505,1530 ----
free(opt);
}
+ /* \uri */
+ else if (strcmp(cmd, "uri") == 0)
+ {
+ char *db = PQdb(pset.db);
+
+ if (db == NULL)
+ printf(_("You are currently not connected to a database.\n"));
+ else
+ {
+ char *uri = PQgetConnectionUri(pset.db);
+ if (uri == NULL)
+ {
+ psql_error("out of memory\n");
+ exit(EXIT_FAILURE);
+ }
+ printf("%s\n", uri);
+ free(uri);
+ }
+ }
+
/* \w -- write query buffer to file */
else if (strcmp(cmd, "w") == 0 || strcmp(cmd, "write") == 0)
{
diff --git a/src/interfaces/libpq/exports.txt b/src/interfaces/libpq/exports.txt
new file mode 100644
index 4a21bf1..c1165c9
*** a/src/interfaces/libpq/exports.txt
--- b/src/interfaces/libpq/exports.txt
*************** PQsslInUse 166
*** 169,171 ****
--- 169,172 ----
PQsslStruct 167
PQsslAttributes 168
PQsslAttribute 169
+ PQgetConnectionUri 170
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
new file mode 100644
index a45f4cb..c70e25e
*** a/src/interfaces/libpq/fe-connect.c
--- b/src/interfaces/libpq/fe-connect.c
*************** PQport(const PGconn *conn)
*** 5361,5366 ****
--- 5361,5402 ----
return conn->pgport;
}
+ #define isdef(strptr) ((strptr) && (strptr[0] != '\0'))
+
+ /*
+ * Returns string uri - returned string should be released
+ */
+ char *
+ PQgetConnectionUri(const PGconn *conn)
+ {
+ PQExpBufferData buf;
+ char *host;
+
+ if (!conn)
+ return NULL;
+
+ host = PQhost(conn);
+
+ initPQExpBuffer(&buf);
+
+ appendPQExpBuffer(&buf, "postgresql://%s", conn->pguser);
+
+ if (isdef(conn->pgpass))
+ appendPQExpBuffer(&buf, ":%s", conn->pgpass);
+ appendPQExpBufferStr(&buf, "@");
+ if (isdef(host))
+ appendPQExpBufferStr(&buf, host);
+ if (isdef(conn->pgport))
+ appendPQExpBuffer(&buf, ":%s", conn->pgport);
+ if (isdef(conn->dbName))
+ appendPQExpBuffer(&buf, "/%s", conn->dbName);
+
+ if (isdef(conn->pgoptions))
+ appendPQExpBuffer(&buf, "?%s", conn->pgoptions);
+
+ return buf.data;
+ }
+
char *
PQtty(const PGconn *conn)
{
diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h
new file mode 100644
index a73eae2..7c8a212
*** a/src/interfaces/libpq/libpq-fe.h
--- b/src/interfaces/libpq/libpq-fe.h
*************** extern char *PQhost(const PGconn *conn);
*** 304,309 ****
--- 304,310 ----
extern char *PQport(const PGconn *conn);
extern char *PQtty(const PGconn *conn);
extern char *PQoptions(const PGconn *conn);
+ extern char *PQgetConnectionUri(const PGconn *conn);
extern ConnStatusType PQstatus(const PGconn *conn);
extern PGTransactionStatusType PQtransactionStatus(const PGconn *conn);
extern const char *PQparameterStatus(const PGconn *conn,
--
Sent via pgsql-hackers mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers