Index: /trunk/ippTools/src/dettool.c
===================================================================
--- /trunk/ippTools/src/dettool.c	(revision 14078)
+++ /trunk/ippTools/src/dettool.c	(revision 14079)
@@ -82,6 +82,7 @@
 static detInputExpRow *rawDetrenTodetInputExpRow(rawExpRow *rawExp, psS64 det_id, psS32 iteration);
 static psArray *searchRawImfiles(pxConfig *config, psMetadata *where);
-static psS32 incrementIteration(pxConfig *config, const char *det_id);
-static bool setDetRunState(pxConfig *config, const char *det_id, const char *state);
+static bool startNewIteration(pxConfig *config, psS64 det_id);
+static psS32 incrementIteration(pxConfig *config, psS64 det_id);
+static bool setDetRunState(pxConfig *config, psS64 det_id, const char *state);
 static bool isValidMode(pxConfig *config, const char *mode);
 
@@ -5804,55 +5805,31 @@
     if (state) {
         // set detRun.state to state
-        return setDetRunState(config, det_id, state);
+        return setDetRunState(config, (psS64)atoll(det_id), state);
     }
 
     // else
     // -again
-
-    // select detRun.det_id
-    // select detRun.iteration
-    // select detInputExp.exp_id
-    // select detResidExp.accept
-    // by:
-    // find the current iteration bassed on det_id
-    // find all exp_ids in the current det_id/iteration from detInputExp
-    // find all exp_ids in the current det_id/iteration from detResidExp
-    // compare the counts of exp_ids
-
-    psString query = psStringCopy(
-        "SELECT DISTINCT"
-        "   detRun.det_id AS det_id,"
-        "   detRun.iteration,"
-        "   detInputExp.exp_id,"
-        "   detResidExp.accept"
-        " FROM detRun"
-        " JOIN detInputExp"
-        "   ON detRun.det_id = detInputExp.det_id"
-        "   AND detRun.iteration = detInputExp.iteration"
-        " JOIN detResidExp"
-        "   ON detRun.det_id = detResidExp.det_id"
-        "   AND detRun.iteration = detResidExp.iteration"
-        "   AND detInputExp.exp_id = detResidExp.exp_id"
-        " WHERE"
-        "   detRun.state = 'run'"
-        "   AND detRun.mode = 'master'"
-    );
+    if (!startNewIteration(config, (psS64)atoll(det_id))) {
+        psError(PS_ERR_UNKNOWN, false, "failed to start new iteration");
+        return false;
+    }
+
+    return true;
+}
+
+static bool startNewIteration(pxConfig *config, psS64 det_id)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    psString query = pxDataGet("dettool_start_new_iteration.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
+    }
 
     // XXX this query was not restricted by det_id, resulting
     // in an inconsistent UPDATE below.  I added this AND clause
     // though there may be a cleaner method (EAM 2006.10.08)
-    psStringAppend (
-        &query,
-        "  AND detRun.det_id = '%s'", det_id);
-
-    psStringAppend (
-        &query,
-        " GROUP BY"
-        "   detRun.det_id,"
-        "   detRun.iteration,"
-        "   detInputExp.exp_id"
-        " HAVING"
-        "   COUNT(detResidExp.exp_id) = COUNT(detInputExp.exp_id)"
-        );
+    psStringAppend(&query, " WHERE detRun.det_id = %" PRId64 , det_id);
 
     if (!p_psDBRunQuery(config->dbh, query)) {
@@ -5869,7 +5846,7 @@
     }
     if (!psArrayLength(output)) {
-        psTrace("dettool", PS_LOG_INFO, "no rows found");
+        psError(PS_ERR_UNKNOWN, false, "det_id %" PRId64 " not found", det_id);
         psFree(output);
-        return true;
+        return false;
     }
 
@@ -5898,4 +5875,5 @@
     for (long i = 0; i < psArrayLength(output); i++) {
         psMetadata *row = output->data[i];
+        bool status = false;
         psS64 exp_id = psMetadataLookupS64(&status, row, "exp_id");
         if (!status) {
@@ -5922,5 +5900,5 @@
         if (!detInputExpInsert(
                     config->dbh,
-                    (psS64)atoll(det_id),
+                    det_id,
                     newIteration,
                     exp_id,
@@ -6046,5 +6024,5 @@
 
     // up the detRuns iteration count
-    psS32 newIteration = incrementIteration(config, det_id);
+    psS32 newIteration = incrementIteration(config, (psS64)atoll(det_id));
     if (!newIteration) {
         // rollback
@@ -6575,20 +6553,18 @@
 }
 
-static psS32 incrementIteration(pxConfig *config, const char *det_id)
+static psS32 incrementIteration(pxConfig *config, psS64 det_id)
 {
     // this function returns zero on error
     PS_ASSERT_PTR_NON_NULL(config, 0);
-    PS_ASSERT_PTR_NON_NULL(det_id, 0);
-
-    char *query = "UPDATE detRun SET iteration = iteration + 1 WHERE det_id = '%s'";
+
+    char *query = "UPDATE detRun SET iteration = iteration + 1 WHERE det_id = %" PRId64;
     if (!p_psDBRunQuery(config->dbh, query, det_id)) {
         psError(PS_ERR_UNKNOWN, false,
-                "failed to increment iteration for det_id %s", det_id);
+                "failed to increment iteration for det_id %" PRId64, det_id);
         return 0;
     }
 
     psMetadata *where = psMetadataAlloc();
-    if (!psMetadataAddS32(where, PS_LIST_TAIL, "det_id", 0, "==",
-                (psS32)atoll(det_id))) {
+    if (!psMetadataAddS64(where, PS_LIST_TAIL, "det_id", 0, "==", det_id)) {
         psError(PS_ERR_UNKNOWN, false, "failed to add item det_id");
         psFree(where);
@@ -6615,7 +6591,7 @@
 }
 
-static bool setDetRunState(pxConfig *config, const char *det_id, const char *state)
+static bool setDetRunState(pxConfig *config, psS64 det_id, const char *state)
 {
-    PS_ASSERT_PTR_NON_NULL(det_id, false);
+    PS_ASSERT_PTR_NON_NULL(config, false);
     PS_ASSERT_PTR_NON_NULL(state, false);
 
@@ -6633,8 +6609,8 @@
     }
 
-    char *query = "UPDATE detRun SET state = '%s' WHERE det_id = '%s'";
+    char *query = "UPDATE detRun SET state = '%s' WHERE det_id = %" PRId64;
     if (!p_psDBRunQuery(config->dbh, query, state, det_id)) {
         psError(PS_ERR_UNKNOWN, false,
-                "failed to change state for det_id %s", det_id);
+                "failed to change state for det_id %" PRId64, det_id);
         return false;
     }
