Index: trunk/ippTools/src/dettool.c
===================================================================
--- trunk/ippTools/src/dettool.c	(revision 8531)
+++ trunk/ippTools/src/dettool.c	(revision 8532)
@@ -1587,41 +1587,39 @@
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
-    
-    // find detStackedImfiles
-    psArray *stackedImfiles =
-        detStackedImfileSelectRowObjects(config->dbh, config->where, 0);
-    if (!stackedImfiles) {
-        psError(PS_ERR_UNKNOWN, false, "no detStackedImfile rows found");
-        return NULL;
-    }
-
-    // remove detNormalizedImfiles
-    psArray *normalizedImfiles = detNormalizedImfileSelectRowObjects(
-        config->dbh, config->where, 0);
-    // XXX trace the not found case
-    if (normalizedImfiles) {
-        for (long i = 0; i < psArrayLength(stackedImfiles); i++) {
-            for (long j = 0; j < psArrayLength(normalizedImfiles); j++) {
-                if ((
-                ((detStackedImfileRow *)stackedImfiles->data[i])->det_id
-                    ==
-                ((detNormalizedImfileRow *)normalizedImfiles->data[j])->det_id)
-                &&
-                (((detStackedImfileRow *)stackedImfiles->data[i])->iteration
-                    ==
-              ((detNormalizedImfileRow *)normalizedImfiles->data[j])->iteration)
-                &&
-                (strcmp(
-                ((detStackedImfileRow *)stackedImfiles->data[i])->class_id,
-               ((detNormalizedImfileRow *)normalizedImfiles->data[j])->class_id)
-                == 0)) {
-                    // remove the detStackedImfiles from the list
-                    psArrayRemove(stackedImfiles, stackedImfiles->data[i]);
-                    i--;
-                    break;
-                }
-            }
-        }
-        psFree(normalizedImfiles);
+
+    // select detStackedImfile.*
+    // by:
+    // where det_id, iteration, class_id is not in detNormalizedImfile
+
+    psString query = psStringCopy(
+        "SELECT" 
+        "   detStackedImfile.*"
+        " FROM detStackedImfile"
+        " LEFT JOIN detNormalizedImfile"
+        "   USING(det_id, iteration, class_id)"
+        " WHERE"
+        "   detNormalizedImfile.det_id IS NULL"  
+        "   AND detNormalizedImfile.iteration IS NULL"  
+        "   AND detNormalizedImfile.class_id IS NULL"  
+        ); 
+
+    if (config->where) {
+        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "detStackedImfile");
+        psStringAppend(&query, " AND %s", whereClause);
+        psFree(whereClause);
+    }
+
+    if (!p_psDBRunQuery(config->dbh, query)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        psFree(query);
+        return false;
+    } 
+    psFree(query);
+
+    psArray *output = p_psDBFetchResult(config->dbh);
+    if (!output) {
+        // XXX check psError here
+        psError(PS_ERR_UNKNOWN, false, "no pending rawDetrendExp rows found");
+        return false;
     }
 
@@ -1632,19 +1630,17 @@
         if (!status) {
             psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
-            return false;
-        }
-    }
-
-    // print imfile list
-    if (stackedImfiles->n) {
-        // negative simple so the default is true
-        if (!detStackedImfilePrintObjects(stdout, stackedImfiles, !simple)) {
-            psError(PS_ERR_UNKNOWN, false, "failed to print array");
-            psFree(stackedImfiles);
-            return false;
-        }
-    }
-
-    psFree(stackedImfiles);
+            psFree(output);
+            return false;
+        }
+    }
+
+    // negative simple so the default is true
+    if (!ippdbPrintMetadatas(stdout, output, "rawDetrendImfile", !simple)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to print array");
+        psFree(output);
+        return false;
+    }
+
+    psFree(output);
 
     return true;
@@ -1946,5 +1942,5 @@
         "   detNormalizedImfile.det_id IS NULL"  
         "   AND detNormalizedImfile.iteration IS NULL"  
-        "   AnD detNormalizedImfile.class_id IS NULL"  
+        "   AND detNormalizedImfile.class_id IS NULL"  
         ); 
 
