Index: trunk/ippTools/src/dettool_processedimfile.c
===================================================================
--- trunk/ippTools/src/dettool_processedimfile.c	(revision 19092)
+++ trunk/ippTools/src/dettool_processedimfile.c	(revision 19621)
@@ -165,5 +165,5 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    char *value = NULL;
+    bool hasWhere = false;
 
     PXOPT_LOOKUP_BOOL(included, config->args, "-included", false);
@@ -173,7 +173,9 @@
 
     psMetadata *where = psMetadataAlloc();
-    PXOPT_COPY_S64(config->args, where, "-det_id", "det_id", "==");
-    PXOPT_COPY_S64(config->args, where, "-exp_id", "exp_id", "==");
-    PXOPT_COPY_STR(config->args, where, "-class_id", "class_id", "==");
+    PXOPT_COPY_S64(config->args, where, "-det_id", "detProcessedImfile.det_id", "==");
+    PXOPT_COPY_S64(config->args, where, "-exp_id", "detProcessedImfile.exp_id", "==");
+    PXOPT_COPY_STR(config->args, where, "-class_id", "detProcessedImfile.class_id", "==");
+    PXOPT_COPY_STR(config->args, where, "-select_state", "detRun.state", "==");
+    PXOPT_COPY_STR(config->args, where, "-select_mode", "detRun.mode", "==");
 
     psString query = pxDataGet("dettool_processedimfile.sql");
@@ -184,24 +186,9 @@
     }
 
-    // add the two required restrictions: detRun.state and detRun.mode
-    // NOTE the above query requires one of the following two WHERE statements
-    {
-        bool status;
-        if ((value = psMetadataLookupStr(&status, config->args, "-select_state"))) {
-            psStringAppend(&query, " WHERE detRun.state = '%s'", value);
-        } else {
-            psStringAppend(&query, " WHERE detRun.state = 'run'");
-        }
-        if ((value = psMetadataLookupStr(&status, config->args, "-select_mode"))) {
-            psStringAppend(&query, " AND detRun.mode = '%s'", value);
-        } else {
-            psStringAppend(&query, " AND detRun.mode = 'master'");
-        }
-    }
-
     if (psListLength(where->list)) {
-        psString whereClause = psDBGenerateWhereConditionSQL(where, "detProcessedImfile");
-        psStringAppend(&query, " AND %s", whereClause);
+        psString whereClause = psDBGenerateWhereSQL(where, NULL);
+        psStringAppend(&query, " %s", whereClause);
         psFree(whereClause);
+	hasWhere = true;
     }
     psFree (where);
@@ -209,13 +196,23 @@
     // restrict search to included imfiles
     if (included) {
-	psStringAppend(&query, " AND detInputExp.include = 1");
+	if (hasWhere) {
+	    psStringAppend(&query, " AND detInputExp.include = 1");
+	} else {
+	    psStringAppend(&query, " WHERE detInputExp.include = 1");
+	}
+	hasWhere = true;
+    }
+
+    if (hasWhere) {
+	psStringAppend(&query, " AND");
+    } else {
+	psStringAppend(&query, " WHERE");
     }
 
     if (faulted) {
-        // list only faulted rows
-        psStringAppend(&query, " %s", "AND detProcessedImfile.fault != 0");
+	psStringAppend(&query, " %s", " detProcessedImfile.fault != 0");
     } else {
         // don't list faulted rows
-        psStringAppend(&query, " %s", "AND detProcessedImfile.fault = 0");
+        psStringAppend(&query, " %s", " detProcessedImfile.fault = 0");
     }
 
