Index: trunk/ippTools/src/dettool.c
===================================================================
--- trunk/ippTools/src/dettool.c	(revision 8407)
+++ trunk/ippTools/src/dettool.c	(revision 8420)
@@ -2663,8 +2663,36 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    // no options are required... use the default where statement
-    psArray *residImfiles = detResidImfileSelectRowObjects(config->dbh,
-            config->where, 0);
-    if (!residImfiles) {
+    // select detResidImfile.*
+    // by:
+    // where det_id, iteration, exp_id is not in detResidExp;
+
+    psString query = psStringCopy(
+        "SELECT" 
+        "   detResidImfile.*"
+        " FROM detResidImfile"
+        " LEFT JOIN detResidExp"
+        "   USING(det_id, iteration, exp_id)"
+        " WHERE"
+        "   detResidExp.det_id IS NULL"  
+        "   AND detResidExp.iteration IS NULL"  
+        "   AnD detResidExp.exp_id IS NULL"  
+        ); 
+
+    if (config->where) {
+        psString whereClaus = psDBGenerateWhereConditionSQL(config->where, "detResidImfile");
+        psStringAppend(&query, " AND %s", whereClaus);
+        psFree(whereClaus);
+    }
+
+    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 detResidImfile rows found");
         return false;
@@ -2677,4 +2705,5 @@
         if (!status) {
             psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
+            psFree(output);
             return false;
         }
@@ -2682,13 +2711,11 @@
 
     // negative simple so the default is true
-    if (residImfiles->n) {
-        if (!detResidImfilePrintObjects(stdout, residImfiles, !simple)) {
-            psError(PS_ERR_UNKNOWN, false, "failed to print array");
-            psFree(residImfiles);
-            return false;
-        }
-    }
-
-    psFree(residImfiles);
+    if (!ippdbPrintMetadatas(stdout, output, "rawResidImfile", !simple)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to print array");
+        psFree(output);
+        return false;
+    }
+
+    psFree(output);
 
     return true;
