Index: /trunk/ippTools/src/dettool.c
===================================================================
--- /trunk/ippTools/src/dettool.c	(revision 14980)
+++ /trunk/ippTools/src/dettool.c	(revision 14981)
@@ -95,5 +95,4 @@
 static detNormalizedStatImfileRow *detStackedToDetNormalizedStatImfile(pxConfig *config, detStackedImfileRow *stackedImfile);
 static  detNormalizedImfileRow *detNormalizedStatToDetNormalizedmfile(pxConfig *config, detNormalizedStatImfileRow *statImfile);
-static  detResidImfileRow *detNormalizedToDetResidImfile(pxConfig *config, detNormalizedImfileRow *normalizedImfile);
 static detResidExpRow *mdToDetResidExp(pxConfig *config, psMetadata *row);
 static detRunSummaryRow *mdToDetRunSummary(pxConfig *config, psMetadata *row);
@@ -4653,101 +4652,19 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    // make sure that there is a coresponding entry in detNormalizedImfile
-    // and the exp_id specified is valid
-    // select * from detNormalizedImfile
-    // by det_id, iteration, class_id
-    // where det_id, iteration, class_id is not in detResidImfile
-    psString query = psStringCopy(
-        " SELECT * FROM ("
-        "SELECT\n"
-        "   detNormalizedImfile.*\n"
-        " FROM detRun\n"
-        " JOIN detInputExp\n"
-        "   USING(det_id, iteration)\n"
-        " JOIN detNormalizedImfile\n"
-        "   USING(det_id, iteration)\n"
-        " LEFT JOIN detResidImfile\n"
-        "   ON detRun.det_id = detResidImfile.det_id\n"
-        "   AND detRun.iteration = detResidImfile.iteration\n"
-        "   AND detInputExp.exp_id = detResidImfile.exp_id\n"
-        "   AND detNormalizedImfile.class_id = detResidImfile.class_id\n"
-        " WHERE\n"
-        "  detRun.state = 'run'\n"
-        "  AND detRun.mode= 'master'\n"
-        "  AND detResidImfile.det_id IS NULL\n"
-        "  AND detResidImfile.iteration IS NULL\n"
-        "  AND detResidImfile.exp_id IS NULL\n"
-        "  AND detResidImfile.class_id IS NULL\n"
-        " UNION\n"
-        " SELECT\n"
-        "   detRun.det_id,\n"
-        "   detRun.iteration,\n"
-        "   rawImfile.class_id,\n"
-        "   rawImfile.uri,\n"
-        "   0.0 as bg,\n"
-        "   0.0 as bg_stdev,\n"
-        "   0.0 as bg_mean_stdev,\n"
-        "   0.0 as user_1,\n"
-        "   0.0 as user_2,\n"
-        "   0.0 as user_3,\n"
-        "   0.0 as user_4,\n"
-        "   0.0 as user_5,\n"
-        "   'NULL' as path_base,\n"
-        "    0 as fault\n"
-        " FROM detRun\n"
-        " JOIN detInputExp\n"
-        "    USING(det_id, iteration)\n"
-        " JOIN rawExp\n"
-        "    ON detInputExp.exp_id = rawExp.exp_id\n"
-        " JOIN rawImfile\n"
-        "    ON detInputExp.exp_id = rawImfile.exp_id\n"
-        " LEFT JOIN detResidImfile\n"
-        "   ON detRun.det_id = detResidImfile.det_id\n"
-        "   AND detRun.iteration = detResidImfile.iteration\n"
-        "   AND rawImfile.exp_id = detResidImfile.exp_id\n"
-        "   AND rawImfile.class_id = detResidImfile.class_id\n"
-        " WHERE\n"
-        "   detRun.state = 'run'\n"
-        "   AND detRun.mode = 'verify'\n"
-        "   AND detResidImfile.det_id IS NULL\n"
-        "   AND detResidImfile.iteration IS NULL\n"
-        "   AND detResidImfile.exp_id IS NULL\n"
-        "   AND detResidImfile.class_id IS NULL\n"
-        "   AND detInputExp.exp_id = '%1$s'\n"
-        ") AS foo\n"
-        "  %2$s"
-    );
-
-    // build a query to search by det_id, iteration, class_id
-    psMetadata *where = psMetadataAlloc();
     bool status = false;
     psString det_id = psMetadataLookupStr(&status, config->args, "-det_id");
     if (!status) {
         psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -det_id");
-        psFree(where);
-        psFree(query);
-        return false;
-    }
-    if (det_id) {
-        if (!psMetadataAddStr(where, PS_LIST_TAIL, "det_id", 0, "==", det_id)) {
-            psError(PS_ERR_UNKNOWN, false, "failed to add item det_id");
-            psFree(where);
-            psFree(query);
-            return false;
-        }
-    }
-
+        return false;
+    }
+    if (!det_id) {
+        psError(PS_ERR_UNKNOWN, true, "-det_id is required");
+        return false;
+    }
+
+    // defaults to 0
     psS32 iteration = psMetadataLookupS32(&status, config->args, "-iteration");
     if (!status) {
         psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -iteration");
-        psFree(where);
-        psFree(query);
-        return false;
-    }
-    // always set iteration
-    if (!psMetadataAddS32(where, PS_LIST_TAIL, "iteration", 0, "==", iteration)) {
-        psError(PS_ERR_UNKNOWN, false, "failed to add item iteration");
-        psFree(where);
-        psFree(query);
         return false;
     }
@@ -4756,107 +4673,11 @@
     if (!status) {
         psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -class_id");
-        psFree(where);
-        psFree(query);
-        return false;
-    }
-    if (class_id) {
-        if (!psMetadataAddStr(where, PS_LIST_TAIL, "class_id", 0, "==", class_id)) {
-            psError(PS_ERR_UNKNOWN, false, "failed to add item class_id");
-            psFree(where);
-            psFree(query);
-            return false;
-        }
-    }
-
-    psString whereClause = psDBGenerateWhereSQL(where, NULL);
-    psFree(where);
-
-    // exp_id is manadatory to cross check that this is a exp_id for this
-    // detRun
-    psString exp_id = psMetadataLookupStr(&status, config->args, "-exp_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -exp_id");
-        psFree(whereClause);
-        psFree(query);
-        return false;
-    }
-    if (!exp_id) {
-        psError(PS_ERR_UNKNOWN, true, "-exp_id is required");
-        psFree(whereClause);
-        psFree(query);
-        return false;
-    }
-
-    if (!p_psDBRunQuery(config->dbh, query, exp_id, whereClause)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(whereClause);
-        psFree(query);
-        return false;
-    }
-    psFree(whereClause);
-    psFree(query);
-
-    psArray *output = p_psDBFetchResult(config->dbh);
-    if (!output) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        return false;
-    }
-    if (!psArrayLength(output)) {
-        psError(PS_ERR_UNKNOWN, false, "no pending detNormalizedImfile rows found");
-        psFree(output);
-        return false;
-    }
-
-    // start a transaction so it's all rows or nothing
-    if (!psDBTransaction(config->dbh)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(output);
-        return false;
-    }
-
-    for (long i = 0; i < psArrayLength(output); i++) {
-        psMetadata *row = output->data[i];
-        // convert metadata into a detNormalizedImfile object
-        detNormalizedImfileRow *normalizedImfile = detNormalizedImfileObjectFromMetadata(row);
-        // convert detNormalizedImfile object into a detResidImfile
-        detResidImfileRow *residImfile  = detNormalizedToDetResidImfile(config, normalizedImfile);
-        psFree(normalizedImfile);
-        if (!residImfile) {
-            if (!psDBRollback(config->dbh)) {
-                psError(PS_ERR_UNKNOWN, false, "database error");
-            }
-            psError(PS_ERR_UNKNOWN, false, "failed to convert detNormalizedImfile to detResidImfile");
-            psFree(output);
-            return false;
-        }
-        // insert detResidImfile object into the database
-        if (!detResidImfileInsertObject(config->dbh, residImfile)) {
-            if (!psDBRollback(config->dbh)) {
-                psError(PS_ERR_UNKNOWN, false, "database error");
-            }
-            psError(PS_ERR_UNKNOWN, false, "database error");
-            psFree(residImfile);
-            psFree(output);
-            return false;
-        }
-        psFree(residImfile);
-    }
-
-    psFree(output);
-
-    if (!psDBCommit(config->dbh)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        return false;
-    }
-
-    return true;
-}
-
-static  detResidImfileRow *detNormalizedToDetResidImfile(pxConfig *config, detNormalizedImfileRow *normalizedImfile)
-{
-    PS_ASSERT_PTR_NON_NULL(config, NULL);
-    PS_ASSERT_PTR_NON_NULL(normalizedImfile, NULL);
-
-    bool status = false;
+        return false;
+    }
+    if (!class_id) {
+        psError(PS_ERR_UNKNOWN, false, "-class_id is required");
+        return false;
+    }
+
     psString exp_id = psMetadataLookupStr(&status, config->args, "-exp_id");
     if (!status) {
@@ -4999,9 +4820,10 @@
 
     // create a new detResidImfileRow and insert it
-    return detResidImfileRowAlloc(
-            normalizedImfile->det_id,
-            normalizedImfile->iteration,
+    if (!detResidImfileInsert(
+            config->dbh,
+            (psS64)atoll(det_id),
+            iteration,
             (psS64)atoll(exp_id),
-            normalizedImfile->class_id,
+            class_id,
             uri,
             recipe,
@@ -5025,5 +4847,10 @@
             path_base,
             code
-        );
+    )) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+
+    return true;
 }
 
