On Thu, Sep 10, 2009 at 12:01 AM, Jaime Casanova
<[email protected]> wrote:
> On Mon, Jul 6, 2009 at 10:00 AM, Heikki
> Linnakangas<[email protected]> wrote:
>>
>> Could we
>> have a version of PQconnectdb() with an API more suited for setting the
>> params programmatically? The PQsetdbLogin() approach doesn't scale as
>> parameters are added/removed in future versions, but we could have
>> something like this:
>>
>> PGconn *PQconnectParams(const char **params)
>>
>> Where "params" is an array with an even number of parameters, forming
>> key/value pairs. Usage example:
>>
i extracted the functions to connect that Heikki put on psql in his
patch for determining client_encoding from client locale and put it in
libpq so i follow the PQconnectdbParams(* params[]) approach.
i put the new function at the end of the exports.txt file, there's a
reason to renumber the exports to put it at the beginning with the
other PQconnectdb function?
this patch still lacks documentation, i will add it in the next days
but want to know if you have any comments about this...
--
Atentamente,
Jaime Casanova
Soporte y capacitación de PostgreSQL
AsesorÃa y desarrollo de sistemas
Guayaquil - Ecuador
Cel. +59387171157
Index: src/bin/psql/command.c
===================================================================
RCS file: /home/postgres/pgrepo/pgsql/src/bin/psql/command.c,v
retrieving revision 1.206
diff -c -r1.206 command.c
*** src/bin/psql/command.c 11 Jun 2009 14:49:07 -0000 1.206
--- src/bin/psql/command.c 14 Sep 2009 17:34:00 -0000
***************
*** 1239,1246 ****
while (true)
{
! n_conn = PQsetdbLogin(host, port, NULL, NULL,
! dbname, user, password);
/* We can immediately discard the password -- no longer needed */
if (password)
--- 1239,1254 ----
while (true)
{
! const char *params[] = {
! "host", host,
! "port", port,
! "dbname", dbname,
! "user", user,
! "password", password,
! NULL, NULL
! };
!
! n_conn = PQconnectdbParams(params);
/* We can immediately discard the password -- no longer needed */
if (password)
Index: src/bin/psql/startup.c
===================================================================
RCS file: /home/postgres/pgrepo/pgsql/src/bin/psql/startup.c,v
retrieving revision 1.156
diff -c -r1.156 startup.c
*** src/bin/psql/startup.c 5 Apr 2009 04:19:58 -0000 1.156
--- src/bin/psql/startup.c 14 Sep 2009 17:33:43 -0000
***************
*** 171,181 ****
/* loop until we have a password if requested by backend */
do
{
new_pass = false;
! pset.db = PQsetdbLogin(options.host, options.port, NULL, NULL,
! options.action == ACT_LIST_DB && options.dbname == NULL ?
! "postgres" : options.dbname,
! options.username, password);
if (PQstatus(pset.db) == CONNECTION_BAD &&
PQconnectionNeedsPassword(pset.db) &&
--- 171,189 ----
/* loop until we have a password if requested by backend */
do
{
+ const char *params[] = {
+ "host", options.host,
+ "port", options.port,
+ "dbname", (options.action == ACT_LIST_DB &&
+ options.dbname == NULL) ? "postgres" : options.dbname,
+ "user", options.username,
+ "password", password,
+ NULL, NULL
+ };
+
new_pass = false;
!
! pset.db = PQconnectdbParams(params);
if (PQstatus(pset.db) == CONNECTION_BAD &&
PQconnectionNeedsPassword(pset.db) &&
Index: src/interfaces/libpq/exports.txt
===================================================================
RCS file: /home/postgres/pgrepo/pgsql/src/interfaces/libpq/exports.txt,v
retrieving revision 1.23
diff -c -r1.23 exports.txt
*** src/interfaces/libpq/exports.txt 31 Mar 2009 01:41:27 -0000 1.23
--- src/interfaces/libpq/exports.txt 14 Sep 2009 17:33:03 -0000
***************
*** 153,155 ****
--- 153,156 ----
PQfireResultCreateEvents 151
PQconninfoParse 152
PQinitOpenSSL 153
+ PQconnectdbParams 154
Index: src/interfaces/libpq/fe-connect.c
===================================================================
RCS file: /home/postgres/pgrepo/pgsql/src/interfaces/libpq/fe-connect.c,v
retrieving revision 1.376
diff -c -r1.376 fe-connect.c
*** src/interfaces/libpq/fe-connect.c 24 Jul 2009 17:58:31 -0000 1.376
--- src/interfaces/libpq/fe-connect.c 14 Sep 2009 17:34:49 -0000
***************
*** 211,216 ****
--- 211,219 ----
"GSS-library", "", 7}, /* sizeof("gssapi") = 7 */
#endif
+ {"appname", NULL, NULL, NULL,
+ "Client-application", "", 45},
+
/* Terminating entry --- MUST BE LAST */
{NULL, NULL, NULL, NULL,
NULL, NULL, 0}
***************
*** 283,288 ****
--- 286,333 ----
*/
/*
+ * PQconnectdbParams
+ */
+ PGconn *
+ PQconnectdbParams(const char * const *params)
+ {
+ PGconn *ret;
+ PQExpBufferData buf;
+
+ initPQExpBuffer(&buf);
+
+ while(*params)
+ {
+ const char *option = params[0];
+ const char *value = params[1];
+
+ if (value != NULL)
+ {
+ /* write option name */
+ appendPQExpBuffer(&buf, "%s = '", option);
+ /* write value, escaping single quotes and backslashes */
+ while(*value)
+ {
+ if (*value == '\'' || *value == '\\')
+ appendPQExpBufferChar(&buf, '\\');
+ appendPQExpBufferChar(&buf, *(value++));
+ }
+
+ appendPQExpBufferStr(&buf, "' ");
+ }
+
+ params+=2;
+ }
+
+ ret = PQconnectdb(buf.data);
+
+ termPQExpBuffer(&buf);
+
+ return ret;
+
+ }
+
+ /*
* PQconnectdb
*
* establishes a connection to a postgres backend through the postmaster
Index: src/interfaces/libpq/libpq-fe.h
===================================================================
RCS file: /home/postgres/pgrepo/pgsql/src/interfaces/libpq/libpq-fe.h,v
retrieving revision 1.147
diff -c -r1.147 libpq-fe.h
*** src/interfaces/libpq/libpq-fe.h 11 Jun 2009 14:49:14 -0000 1.147
--- src/interfaces/libpq/libpq-fe.h 14 Sep 2009 17:34:28 -0000
***************
*** 230,235 ****
--- 230,236 ----
/* Synchronous (blocking) */
extern PGconn *PQconnectdb(const char *conninfo);
+ extern PGconn *PQconnectdbParams(const char * const *params);
extern PGconn *PQsetdbLogin(const char *pghost, const char *pgport,
const char *pgoptions, const char *pgtty,
const char *dbName,
--
Sent via pgsql-hackers mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers