Attached is a patch which cleans up that major connection kludge in dbpgsql.c. As well, I changed 2 things to inline, whereas the whole bottom set of functions should probably be inline or function pointers.

Dan
Index: dbmail/pgsql/dbpgsql.c
===================================================================
--- dbmail.orig/pgsql/dbpgsql.c 2004-08-02 07:27:42.607051528 -0400
+++ dbmail/pgsql/dbpgsql.c      2004-08-03 12:06:59.188470368 -0400
@@ -49,24 +49,16 @@
 
 int db_connect(void)
 {
-       char connectionstring[255];
 
-       /* use the standard port for postgresql if none is given. This looks a 
bit
-          dirty.. can't we get this info from somewhere else? */
+       // This code isn't dirty any more :)
+       // Copyright Dan Weber 2004 GPL 
        if (_db_params.port != 0)
-               snprintf(connectionstring, 255,
-                        "host='%s' user='%s' password='%s' dbname='%s' "
-                        "port='%u'",
-                        _db_params.host, _db_params.user, _db_params.pass,
-                        _db_params.db, _db_params.port);
+               conn = PQsetdblogin(_db_params.host, _db_params.port, NULL, 
+                       NULL, _db_params.db,_db_params.user,_db_params.pass);
        else
-               snprintf(connectionstring, 255,
-                        "host='%s' user='%s' password='%s' dbname='%s' ",
-                        _db_params.host, _db_params.user, _db_params.pass,
-                        _db_params.db);
-
-       conn = PQconnectdb(connectionstring);
-
+               conn = PQsetdblogin(_db_params.host, NULL, NULL, 
+                       NULL, _db_params.db,_db_params.user,_db_params.pass);
+       
        if (PQstatus(conn) == CONNECTION_BAD) {
                trace(TRACE_ERROR,
                      "%si,%s: PQconnectdb failed: %s",
@@ -88,6 +80,8 @@
 }
 
 long long db_num_rows(void)
+// This typecase is evil.  Its not always true
+// if this function returns 0
 {
        int num_rows;
        if (!res)
@@ -228,8 +222,10 @@
        return 0;
 }
 
-unsigned long db_escape_string(char *to,
+inline unsigned long db_escape_string(char *to,
                               const char *from, unsigned long length)
+// inline will be better because I can't see why wasting a function call is a 
+// good idea.  As well, the type checking stays.
 {
        return PQescapeString(to, from, length);
 }
@@ -269,7 +265,7 @@
        return PQgetlength(res, row, field);
 }
 
-u64_t db_get_affected_rows(void)
+inline u64_t db_get_affected_rows(void) // See above comment on inline
 {
        return affected_rows;
 }
Index: dbmail/db.h
===================================================================
--- dbmail.orig/db.h    2004-08-02 07:27:42.697037848 -0400
+++ dbmail/db.h 2004-08-03 12:09:15.627728448 -0400
@@ -155,7 +155,7 @@
  * \return length of escaped string
  * \attention behaviour is undefined if to and from overlap
  */
-unsigned long db_escape_string(char *to,
+inline unsigned long db_escape_string(char *to,
                               const char *from, unsigned long length);
 
 /**
@@ -174,7 +174,7 @@
  *    -  -1 on error (e.g. no result set)
  *    -  number of affected rows otherwise
  */
-u64_t db_get_affected_rows(void);
+inline u64_t db_get_affected_rows(void);
 
 /**
  * \brief switch from the normal result set to the msgbuf
Index: dbmail/mysql/dbmysql.c
===================================================================
--- dbmail.orig/mysql/dbmysql.c 2004-08-02 07:27:42.751029640 -0400
+++ dbmail/mysql/dbmysql.c      2004-08-03 12:11:21.473596968 -0400
@@ -242,7 +242,7 @@
        return 0;
 }
 
-unsigned long db_escape_string(char *to,
+inline unsigned long db_escape_string(char *to,
                               const char *from, unsigned long length)
 {
        return mysql_real_escape_string(&conn, to, from, length);
@@ -295,7 +295,7 @@
        return (u64_t) mysql_fetch_lengths(res)[field];
 }
 
-u64_t db_get_affected_rows(void)
+inline u64_t db_get_affected_rows(void)
 {
        return (u64_t) mysql_affected_rows(&conn);
 }

Attachment: signature.asc
Description: OpenPGP digital signature



Reply via email to