Index: /trunk/ippTools/src/regtool.c
===================================================================
--- /trunk/ippTools/src/regtool.c	(revision 8340)
+++ /trunk/ippTools/src/regtool.c	(revision 8341)
@@ -69,14 +69,17 @@
 
     char *query =
-        "SELECT newExp.* FROM newExp"
+        "SELECT newExp.*"
+        "   FROM newExp"
+        " LEFT JOIN newImfile USING(exp_id)"
         " LEFT JOIN rawScienceExp USING(exp_id)"
-        " LEFT JOIN rawDetrendExp USING (exp_id)"
-        " LEFT JOIN newImfile USING (exp_id)"
-        " WHERE newExp.exp_id is NOT NULL"
-        " AND rawScienceExp.exp_id IS NULL"
-        " AND rawDetrendExp.exp_id IS NULL"
-        " AND newImfile.exp_id IS NULL"
-        " AND newExp.imfiles = (SELECT COUNT(exp_id)"
-                                " FROM rawImfile GROUP BY exp_id)";
+        " LEFT JOIN rawDetrendExp USING(exp_id)"
+        " WHERE"
+        "   newExp.exp_id IS NOT NULL"
+        "   AND newImfile.exp_id IS NULL"
+        "   AND rawScienceExp.exp_id IS NULL"
+        "   AND newExp.imfiles ="
+        "   (SELECT COUNT(exp_id) FROM rawImfile"
+        "       WHERE rawImfile.exp_id = newExp.exp_id)"
+        ;
 
     if (!p_psDBRunQuery(config->dbh, query)) {
@@ -176,5 +179,19 @@
     // exp_id is not in newImfile
     // that the correct count of imfiles is in rawImfile
-    psString query = psStringCopy("SELECT newExp.* FROM newExp LEFT JOIN newImfile USING(exp_id) LEFT JOIN rawScienceExp USING(exp_id) LEFT JOIN rawDetrendExp USING(exp_id) WHERE newExp.exp_id IS NOT NULL AND newImfile.exp_id IS NULL AND rawScienceExp.exp_id IS NULL AND newExp.imfiles = (SELECT COUNT(exp_id) FROM rawImfile GROUP BY exp_id)");
+
+    psString query = psStringCopy(
+        "SELECT newExp.*"
+        "   FROM newExp"
+        " LEFT JOIN newImfile USING(exp_id)"
+        " LEFT JOIN rawScienceExp USING(exp_id)"
+        " LEFT JOIN rawDetrendExp USING(exp_id)"
+        " WHERE"
+        "   newExp.exp_id IS NOT NULL"
+        "   AND newImfile.exp_id IS NULL"
+        "   AND rawScienceExp.exp_id IS NULL"
+        "   AND newExp.imfiles ="
+        "   (SELECT COUNT(exp_id) FROM rawImfile"
+        "       WHERE rawImfile.exp_id = newExp.exp_id)"
+        );
 
     {        
@@ -418,5 +435,5 @@
     // XXX search by the whole frame some imfiles without a newExp don't get
     // processed -- this may not be the correct thing to do
-    char *query = 
+    psString query = psStringCopy(
         "SELECT * FROM"
             " (SELECT newImfile.* FROM newImfile"
@@ -427,5 +444,5 @@
                 " AND rawScienceExp.exp_id IS NULL"
                 " AND rawDetrendExp.exp_id IS NULL) AS foo"
-        " %s"; // WHERE class is generated from exp_id, class, & class_id
+        ); // WHERE class is generated from exp_id, class, & class_id
 
     char *whereClaus = NULL;
@@ -437,4 +454,5 @@
         if (!status) {
             psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -exp_id");
+            psFree(query);
             return false;
         }
@@ -443,4 +461,5 @@
                 psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id");
                 psFree(where);
+                psFree(query);
                 return false;
             }
@@ -449,4 +468,5 @@
         if (!status) {
             psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -class");
+            psFree(query);
             return false;
         }
@@ -455,4 +475,5 @@
                 psError(PS_ERR_UNKNOWN, false, "failed to add item class");
                 psFree(where);
+                psFree(query);
                 return false;
             }
@@ -461,4 +482,5 @@
         if (!status) {
             psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -class_id");
+            psFree(query);
             return false;
         }
@@ -467,4 +489,5 @@
                 psError(PS_ERR_UNKNOWN, false, "failed to add item class_id");
                 psFree(where);
+                psFree(query);
                 return false;
             }
@@ -472,14 +495,18 @@
 
         // there's not
-        whereClaus = psDBGenerateWhereSQL(where);
+        whereClaus = psDBGenerateWhereSQL(where, NULL);
         psFree(where);
-    }
-
-    if (!p_psDBRunQuery(config->dbh, query, whereClaus)) {
+        if (whereClaus) {
+            psStringAppend(&query, " %s", whereClaus);
+            psFree(whereClaus);
+        }
+    }
+
+    if (!p_psDBRunQuery(config->dbh, query)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(whereClaus);
-        return false;
-    }
-    psFree(whereClaus);
+        psFree(query);
+        return false;
+    }
+    psFree(query);
 
     psArray *newImfiles = p_psDBFetchResult(config->dbh);
