Index: trunk/ippTools/src/dettool.c
===================================================================
--- trunk/ippTools/src/dettool.c	(revision 9214)
+++ trunk/ippTools/src/dettool.c	(revision 9226)
@@ -1,3 +1,3 @@
-#ifdef HAVE_CONFIG_H
+#ifdef HAVB_CONFIG_H
 #include <config.h>
 #endif
@@ -160,7 +160,12 @@
     psArray *output = p_psDBFetchResult(config->dbh);
     if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
         // XXX check psError here
         psError(PS_ERR_UNKNOWN, false, "no pending rawDetrendExp rows found");
-        return false;
+        psFree(output);
+        return true;
     }
 
@@ -768,7 +773,12 @@
     psArray *output = p_psDBFetchResult(config->dbh);
     if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
         // XXX check psError here
         psError(PS_ERR_UNKNOWN, false, "no pending rawDetrendExp rows found");
-        return false;
+        psFree(output);
+        return true;
     }
 
@@ -823,5 +833,5 @@
     if (!output) {
         psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(query);
+        return false;
     }
     if (!psArrayLength(output)) {
@@ -901,5 +911,5 @@
     if (!output) {
         psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(query);
+        return false;
     }
     if (!psArrayLength(output)) {
@@ -1186,7 +1196,12 @@
     psArray *output = p_psDBFetchResult(config->dbh);
     if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
         // XXX check psError here
         psError(PS_ERR_UNKNOWN, false, "no pending rawDetrendExp rows found");
-        return false;
+        psFree(output);
+        return true;
     }
 
@@ -1330,9 +1345,13 @@
     psArray *output = p_psDBFetchResult(config->dbh);
     if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    psFree(output);
+    if (!psArrayLength(output)) {
         // XXX check psError here
         psError(PS_ERR_UNKNOWN, false, "no pending rawDetrendExp rows found");
-        return false;
-    }
-    psFree(output);
+        return true;
+    }
 
     // create a new detProcessedImfile object
@@ -1484,7 +1503,12 @@
     psArray *output = p_psDBFetchResult(config->dbh);
     if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
         // XXX check psError here
-        psError(PS_ERR_UNKNOWN, false, "no pending rawDetrendExp rows found");
-        return false;
+        psError(PS_ERR_UNKNOWN, false, "no stackable detProcessedImfiles found");
+        psFree(output);
+        return true;
     }
 
@@ -1541,7 +1565,12 @@
     psArray *output = p_psDBFetchResult(config->dbh);
     if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
         // XXX check psError here
-        psError(PS_ERR_UNKNOWN, false, "no pending rawDetrendExp rows found");
-        return false;
+        psError(PS_ERR_UNKNOWN, false, "no detProcessedImfile rows found");
+        psFree(output);
+        return true;
     }
 
@@ -1992,7 +2021,12 @@
     psArray *output = p_psDBFetchResult(config->dbh);
     if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
         // XXX check psError here
-        psError(PS_ERR_UNKNOWN, false, "no pending rawDetrendExp rows found");
-        return false;
+        psError(PS_ERR_UNKNOWN, false, "no detStackedImfile rows found");
+        psFree(output);
+        return true;
     }
 
@@ -2167,7 +2201,12 @@
     psArray *output = p_psDBFetchResult(config->dbh);
     if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
         // XXX check psError here
-        psError(PS_ERR_UNKNOWN, false, "no pending rawDetrendExp rows found");
-        return false;
+        psError(PS_ERR_UNKNOWN, false, "no detNormalizedStatImfile rows found");
+        psFree(output);
+        return true;
     }
 
@@ -2228,7 +2267,12 @@
     psArray *output = p_psDBFetchResult(config->dbh);
     if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
         // XXX check psError here
-        psError(PS_ERR_UNKNOWN, false, "no pending rawDetrendExp rows found");
-        return false;
+        psError(PS_ERR_UNKNOWN, false, "no detStackedImfile rows found");
+        psFree(output);
+        return true;
     }
 
@@ -2353,7 +2397,12 @@
     psArray *output = p_psDBFetchResult(config->dbh);
     if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
         // XXX check psError here
-        psError(PS_ERR_UNKNOWN, false, "no pending rawDetrendExp rows found");
-        return false;
+        psError(PS_ERR_UNKNOWN, false, "no normalizable detNormalizedStatImfiles found");
+        psFree(output);
+        return true;
     }
 
@@ -2555,7 +2604,12 @@
     psArray *output = p_psDBFetchResult(config->dbh);
     if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
         // XXX check psError here
         psError(PS_ERR_UNKNOWN, false, "no pending detNormalizedStatImfile rows found");
-        return false;
+        psFree(output);
+        return true;
     }
 
@@ -2663,5 +2717,5 @@
     if (!output) {
         psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(query);
+        return false;
     }
     if (!psArrayLength(output)) {
@@ -2811,5 +2865,5 @@
     if (!output) {
         psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(query);
+        return false;
     }
     if (!psArrayLength(output)) {
@@ -2947,9 +3001,9 @@
 
     psString query = psStringCopy(
-        "SELECT"
+        "SELECT DISTINCT"
         "   detRun.iteration,"
         "   detRun.det_type,"
         "   detProcessedImfile.*,"
-        "   detNormalizedImfile.uri,"
+        "   detNormalizedImfile.uri AS det_uri,"
         "   rawDetrendExp.camera"
         " FROM detRun"
@@ -2997,7 +3051,12 @@
     psArray *output = p_psDBFetchResult(config->dbh);
     if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
         // XXX check psError here
-        psError(PS_ERR_UNKNOWN, false, "no pending rows found");
-        return false;
+        psError(PS_ERR_UNKNOWN, false, "no residable detNormalizedImfileis found");
+        psFree(output);
+        return true;
     }
 
@@ -3300,7 +3359,12 @@
     psArray *output = p_psDBFetchResult(config->dbh);
     if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
         // XXX check psError here
         psError(PS_ERR_UNKNOWN, false, "no pending detNormalizedImfile rows found");
-        return false;
+        psFree(output);
+        return true;
     }
 
@@ -3472,7 +3536,12 @@
     psArray *output = p_psDBFetchResult(config->dbh);
     if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
         // XXX check psError here
         psError(PS_ERR_UNKNOWN, false, "no detResidImfile rows found");
-        return false;
+        psFree(output);
+        return true;
     }
 
@@ -3583,7 +3652,12 @@
     psArray *output = p_psDBFetchResult(config->dbh);
     if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
         // XXX check psError here
-        psError(PS_ERR_UNKNOWN, false, "no pending rawDetrendExp rows found");
-        return false;
+        psError(PS_ERR_UNKNOWN, false, "no residExpable detResidImfiles found");
+        psFree(output);
+        return true;
     }
 
@@ -3741,7 +3815,12 @@
     psArray *output = p_psDBFetchResult(config->dbh);
     if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
         // XXX check psError here
-        psError(PS_ERR_UNKNOWN, false, "no pending rawDetrendExp rows found");
-        return false;
+        psError(PS_ERR_UNKNOWN, false, "no detResidImfile rows found");
+        psFree(output);
+        return true;
     }
 
@@ -3996,7 +4075,12 @@
     psArray *output = p_psDBFetchResult(config->dbh);
     if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
         // XXX check psError here
-        psError(PS_ERR_UNKNOWN, false, "no pending rawDetrendExp rows found");
-        return false;
+        psError(PS_ERR_UNKNOWN, false, "no completed det runs found");
+        psFree(output);
+        return true;
     }
 
@@ -4288,7 +4372,12 @@
     psArray *output = p_psDBFetchResult(config->dbh);
     if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
         // XXX check psError here
         psError(PS_ERR_UNKNOWN, false, "no pending completed detrun iterations found");
-        return false;
+        psFree(output);
+        return true;
     }
 
@@ -4486,7 +4575,12 @@
     psArray *output = p_psDBFetchResult(config->dbh);
     if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
         // XXX check psError here
         psError(PS_ERR_UNKNOWN, false, "no completed detrun iterations found");
-        return false;
+        psFree(output);
+        return true;
     }
 
