Here is the second part of the cleanup patch, introducing db_select() in
the files I missed the first time.

I also made some functions static in dbsearchmysql.c because they were
only used in that one file.

-- 
Jonas Jensen <[EMAIL PROTECTED]>
diff -druN dbmail-20030420-mysql-cleanup/dbinternal.h dbmail-patched/dbinternal.h
--- dbmail-20030420-mysql-cleanup/dbinternal.h	1970-01-01 01:00:00.000000000 +0100
+++ dbmail-patched/dbinternal.h	2003-04-20 22:57:06.000000000 +0200
@@ -0,0 +1,12 @@
+#ifndef _DBINTERNAL_H
+#define _DBINTERNAL_H
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "mysql.h"
+
+MYSQL_RES *db_select (const char *format, ...);
+
+#endif
diff -druN dbmail-20030420-mysql-cleanup/mysql/dbmsgbufmysql.c dbmail-patched/mysql/dbmsgbufmysql.c
--- dbmail-20030420-mysql-cleanup/mysql/dbmsgbufmysql.c	2003-03-17 17:04:09.000000000 +0100
+++ dbmail-patched/mysql/dbmsgbufmysql.c	2003-04-20 23:32:02.000000000 +0200
@@ -11,6 +11,7 @@
 
 #include "dbmsgbuf.h"
 #include "db.h"
+#include "dbinternal.h"
 #include "mysql.h"
 #include <stdlib.h>
 #include <string.h>
@@ -68,18 +69,9 @@
   snprintf(query, DEF_QUERYSIZE, "SELECT messageblk FROM messageblks WHERE "
 	   "message_idnr = %llu ORDER BY messageblk_idnr", uid);
 
-  if (db_query(query) == -1)
-    {
-      trace(TRACE_ERROR, "db_init_msgfetch(): could not get message\n");
-      return (-1);
-    }
-
-  if ((_msg_result = mysql_store_result(&conn)) == NULL)
-    {
-      trace(TRACE_ERROR,"db_init_msgfetch(): mysql_store_result failed: %s\n",
-	    mysql_error(&conn));
-      return (-1);
-    }
+  _msg_result = db_select("%s", query);
+  if (_msg_result == NULL) 
+    return -1;
 
   /* first determine block lengths */
   nblocks = mysql_num_rows(_msg_result);
@@ -105,21 +97,13 @@
 
   /* re-execute query */
   mysql_free_result(_msg_result);
-  if (db_query(query) == -1)
-    {
-      trace(TRACE_ERROR, "db_init_msgfetch(): could not get message\n");
-      my_free(blklengths);
-      blklengths = NULL;
-      return (-1);
-    }
+  _msg_result = db_select("%s", query);
 
-  if ((_msg_result = mysql_store_result(&conn)) == NULL)
+  if (_msg_result == NULL) 
     {
-      trace(TRACE_ERROR,"db_init_msgfetch(): mysql_store_result failed: %s\n",
-	    mysql_error(&conn));
       my_free(blklengths);
       blklengths = NULL;
-      return (-1);
+      return -1;
     }
 
   _msg_fetch_inited = 1;
@@ -374,21 +358,10 @@
       return -1;
     }
 
-  snprintf(query, DEF_QUERYSIZE, "SELECT messageblk FROM messageblks WHERE message_idnr = %llu"
-	   " ORDER BY messageblk_idnr", 
-	   msguid);
-
-  if (db_query(query) == -1)
-    {
-      trace(TRACE_ERROR, "db_dump_range(): could not get message\n");
-      return (-1);
-    }
-
-  if ((res = mysql_store_result(&conn)) == NULL)
-    {
-      trace(TRACE_ERROR,"db_dump_range(): mysql_store_result failed: %s\n",mysql_error(&conn));
-      return (-1);
-    }
+  res = db_select("SELECT messageblk FROM messageblks WHERE message_idnr = %llu"
+	   " ORDER BY messageblk_idnr", msguid);
+  if (res == NULL) 
+    return -1;
 
   for (row = mysql_fetch_row(res), i=0; row && i < start.block; i++, row = mysql_fetch_row(res)) ;
       
diff -druN dbmail-20030420-mysql-cleanup/mysql/dbmysql.c dbmail-patched/mysql/dbmysql.c
--- dbmail-20030420-mysql-cleanup/mysql/dbmysql.c	2003-04-20 23:16:32.000000000 +0200
+++ dbmail-patched/mysql/dbmysql.c	2003-04-20 23:00:30.000000000 +0200
@@ -9,6 +9,7 @@
 #endif
 
 #include "db.h"
+#include "dbinternal.h"
 #include "mysql.h"
 #include "dbmail.h"
 #include "pop3.h"
@@ -23,7 +24,6 @@
 #include "rfcmsg.h"
 #include "auth.h"
 
-static MYSQL_RES *db_select (const char *format, ...);
 
 MYSQL conn;  
 MYSQL_RES *res;
@@ -46,7 +46,7 @@
   };
 
 
-static MYSQL_RES *db_select (const char *format, ...)
+MYSQL_RES *db_select (const char *format, ...)
 {
   va_list ap;
   int count;
@@ -156,7 +156,6 @@
   res = db_select("SELECT SUM(m.messagesize) FROM messages m, mailboxes mb "
 	     "WHERE m.mailbox_idnr = mb.mailbox_idnr "
 	     "AND mb.owner_idnr = %llu AND m.status < 2", userid);
-  
   if (res == NULL) 
     return -1;
 
@@ -3277,95 +3276,6 @@
 }
 
 
-
-
-/*
- * searches the given range within a msg for key
- */
-int db_search_range(db_pos_t start, db_pos_t end, const char *key, u64_t msguid)
-{
-  int i,startpos,endpos,j;
-  int distance;
-
-  if (start.block > end.block)
-    {
-      trace(TRACE_ERROR,"db_search_range(): bad range specified\n");
-      return 0;
-    }
-
-  if (start.block == end.block && start.pos > end.pos)
-    {
-      trace(TRACE_ERROR,"db_search_range(): bad range specified\n");
-      return 0;
-    }
-
-  res = db_select("SELECT messageblk FROM messageblks WHERE message_idnr = %llu"
-	   " ORDER BY messageblk_idnr", msguid);
-  if (res == NULL) 
-    return 0;
-
-  for (row = mysql_fetch_row(res), i=0; row && i < start.block; i++, row = mysql_fetch_row(res)) ;
-      
-  if (!row)
-    {
-      trace(TRACE_ERROR,"db_search_range(): bad range specified\n");
-      mysql_free_result(res);
-      return 0;
-    }
-
-  /* just one block? */
-  if (start.block == end.block)
-    {
-      for (i=start.pos; i<=end.pos-strlen(key); i++)
-	{
-	  if (strncasecmp(&row[0][i], key, strlen(key)) == 0)
-	    {
-	      mysql_free_result(res);
-	      return 1;
-	    }
-	}
-
-      mysql_free_result(res);
-      return 0;
-    }
-
-
-  /* 
-   * multiple block range specified
-   */
-  
-  for (i=start.block; i<=end.block; i++)
-    {
-      if (!row)
-	{
-	  trace(TRACE_ERROR,"db_search_range(): bad range specified\n");
-	  mysql_free_result(res);
-	  return 0;
-	}
-
-      startpos = (i == start.block) ? start.pos : 0;
-      endpos   = (i == end.block) ? end.pos+1 : (mysql_fetch_lengths(res))[0];
-
-      distance = endpos - startpos;
-
-      for (j=0; j<distance-strlen(key); j++)
-	{
-	  if (strncasecmp(&row[0][i], key, strlen(key)) == 0)
-	    {
-	      mysql_free_result(res);
-	      return 1;
-	    }
-	}
-	
-      row = mysql_fetch_row(res); /* fetch next row */
-    }
-
-  mysql_free_result(res);
-
-  return 0;
-}
-
-
 /*
  * db_mailbox_msg_match()
  *
diff -druN dbmail-20030420-mysql-cleanup/mysql/dbsearchmysql.c dbmail-patched/mysql/dbsearchmysql.c
--- dbmail-20030420-mysql-cleanup/mysql/dbsearchmysql.c	2003-03-17 17:04:09.000000000 +0100
+++ dbmail-patched/mysql/dbsearchmysql.c	2003-04-20 23:03:38.000000000 +0200
@@ -10,6 +10,7 @@
 
 #include "dbsearch.h"
 #include "db.h"
+#include "dbinternal.h"
 #include "mysql.h"
 #include "rfcmsg.h"
 #include <stdlib.h>
@@ -31,10 +32,10 @@
 
 
 /* used only locally */
-int db_binary_search(const u64_t *array, int arraysize, u64_t key);
-int db_exec_search(mime_message_t *msg, search_key_t *sk, u64_t msguid);
-int db_search_range(db_pos_t start, db_pos_t end, const char *key, u64_t msguid);
-int num_from_imapdate(const char *date);
+static int db_binary_search(const u64_t *array, int arraysize, u64_t key);
+static int db_exec_search(mime_message_t *msg, search_key_t *sk, u64_t msguid);
+static int db_search_range(db_pos_t start, db_pos_t end, const char *key, u64_t msguid);
+static int num_from_imapdate(const char *date);
 
 
 /*
@@ -57,20 +58,10 @@
 
   memset(rset, 0, setlen * sizeof(int));
 
-  snprintf(query, DEF_QUERYSIZE, "SELECT message_idnr FROM messages WHERE mailbox_idnr = %llu "
+  res = db_select("SELECT message_idnr FROM messages WHERE mailbox_idnr = %llu "
 	   "AND status<2 AND unique_id!=\"\" AND %s", mb->uid, key);
-
-  if (db_query(query) == -1)
-    {
-      trace(TRACE_ERROR, "db_search(): could not execute query\n");
-      return (-1);
-    }
-
-  if ((res = mysql_store_result(&conn)) == NULL)
-    {
-      trace(TRACE_ERROR,"db_search(): mysql_store_result failed: %s\n",mysql_error(&conn));
-      return (-1);
-    }
+  if (res == NULL) 
+    return -1;
 
   while ((row = mysql_fetch_row(res)))
     {
@@ -141,7 +132,7 @@
  *
  * returns 1 if the msg matches, 0 if not
  */
-int db_exec_search(mime_message_t *msg, search_key_t *sk, u64_t msguid)
+static int db_exec_search(mime_message_t *msg, search_key_t *sk, u64_t msguid)
 {
   struct element *el;
   struct mime_record *mr;
@@ -349,17 +340,9 @@
       i++;
     }
       
-  if (db_query(query) == -1)
-    {
-      trace(TRACE_ERROR, "db_search_messages(): could not execute query\n");
-      return (-1);
-    }
-
-  if ((res = mysql_store_result(&conn)) == NULL)
-    {
-      trace(TRACE_ERROR,"db_search_messages(): mysql_store_result failed: %s\n",mysql_error(&conn));
-      return (-1);
-    }
+  res = db_select("%s", query);
+  if (res == NULL) 
+    return -1;
 
   *nsresults = mysql_num_rows(res);
   if (*nsresults == 0)
@@ -398,7 +381,7 @@
  *
  * returns index of key in array or -1 if not found
  */
-int db_binary_search(const u64_t *array, int arraysize, u64_t key)
+static int db_binary_search(const u64_t *array, int arraysize, u64_t key)
 {
   int low,high,mid;
 
@@ -427,7 +410,7 @@
  * d-mon-yyyy or dd-mon-yyyy; '-' may be a space
  *               01234567890
  */
-int num_from_imapdate(const char *date)
+static int num_from_imapdate(const char *date)
 {
   int j=0,i;
   char datenum[] = "YYYYMMDD";
@@ -466,4 +449,90 @@
 
   return atoi(datenum);
 }
+ 
+/*
+ * searches the given range within a msg for key
+ */
+static int db_search_range(db_pos_t start, db_pos_t end, const char *key, u64_t msguid)
+{
+  int i,startpos,endpos,j;
+  int distance;
+
+  if (start.block > end.block)
+    {
+      trace(TRACE_ERROR,"db_search_range(): bad range specified\n");
+      return 0;
+    }
+
+  if (start.block == end.block && start.pos > end.pos)
+    {
+      trace(TRACE_ERROR,"db_search_range(): bad range specified\n");
+      return 0;
+    }
+
+  res = db_select("SELECT messageblk FROM messageblks WHERE message_idnr = %llu"
+	   " ORDER BY messageblk_idnr", msguid);
+  if (res == NULL) 
+    return 0;
+
+  for (row = mysql_fetch_row(res), i=0; row && i < start.block; i++, row = mysql_fetch_row(res)) ;
+      
+  if (!row)
+    {
+      trace(TRACE_ERROR,"db_search_range(): bad range specified\n");
+      mysql_free_result(res);
+      return 0;
+    }
+
+  /* just one block? */
+  if (start.block == end.block)
+    {
+      for (i=start.pos; i<=end.pos-strlen(key); i++)
+	{
+	  if (strncasecmp(&row[0][i], key, strlen(key)) == 0)
+	    {
+	      mysql_free_result(res);
+	      return 1;
+	    }
+	}
+
+      mysql_free_result(res);
+      return 0;
+    }
+
+
+  /* 
+   * multiple block range specified
+   */
   
+  for (i=start.block; i<=end.block; i++)
+    {
+      if (!row)
+	{
+	  trace(TRACE_ERROR,"db_search_range(): bad range specified\n");
+	  mysql_free_result(res);
+	  return 0;
+	}
+
+      startpos = (i == start.block) ? start.pos : 0;
+      endpos   = (i == end.block) ? end.pos+1 : (mysql_fetch_lengths(res))[0];
+
+      distance = endpos - startpos;
+
+      for (j=0; j<distance-strlen(key); j++)
+	{
+	  if (strncasecmp(&row[0][i], key, strlen(key)) == 0)
+	    {
+	      mysql_free_result(res);
+	      return 1;
+	    }
+	}
+	
+      row = mysql_fetch_row(res); /* fetch next row */
+    }
+
+  mysql_free_result(res);
+
+  return 0;
+}
+

Reply via email to