Index: /trunk/ippTools/share/chiptool_completely_processed_exp.sql
===================================================================
--- /trunk/ippTools/share/chiptool_completely_processed_exp.sql	(revision 12096)
+++ /trunk/ippTools/share/chiptool_completely_processed_exp.sql	(revision 12096)
@@ -0,0 +1,25 @@
+-- select * from chipPendingExp
+-- where exp_tag is not in chipProcessedExp
+-- where exp_tag is not in chipPendingImfile
+-- where the number of entries in chipProccessedImfile matches the .imfiles
+-- entry in rawExp
+SELECT DISTINCT
+    chipPendingExp.*,
+    rawExp.imfiles,
+    chipProcessedImfile.class_id
+FROM chipPendingExp
+JOIN rawExp
+    ON chipPendingExp.exp_tag = rawExp.exp_tag
+LEFT JOIN chipProcessedExp
+    ON chipPendingExp.exp_tag = chipProcessedExp.exp_tag
+LEFT JOIN chipPendingImfile
+    ON chipPendingExp.exp_tag = chipPendingImfile.exp_tag
+LEFT JOIN chipProcessedImfile
+    ON chipPendingExp.exp_tag = chipProcessedImfile.exp_tag
+WHERE
+    chipProcessedExp.exp_tag IS NULL
+    AND chipPendingImfile.exp_tag IS NULL
+    AND chipProcessedImfile.exp_tag IS NOT NULL
+GROUP BY
+    chipPendingExp.exp_tag
+HAVING rawExp.imfiles = COUNT(chipProcessedImfile.class_id)
Index: /trunk/ippTools/share/chiptool_find_unprocessed_imfile.pl
===================================================================
--- /trunk/ippTools/share/chiptool_find_unprocessed_imfile.pl	(revision 12096)
+++ /trunk/ippTools/share/chiptool_find_unprocessed_imfile.pl	(revision 12096)
@@ -0,0 +1,8 @@
+SELECT DISTINCT
+   chipPendingImfile.*
+FROM chipPendingImfile
+LEFT JOIN chipProcessedImfile
+    USING(exp_tag, class_id)
+WHERE
+    chipProcessedImfile.exp_tag IS NULL
+    AND chipProcessedImfile.class_id IS NULL
Index: /trunk/ippTools/share/chiptool_pendingimfile.sql
===================================================================
--- /trunk/ippTools/share/chiptool_pendingimfile.sql	(revision 12096)
+++ /trunk/ippTools/share/chiptool_pendingimfile.sql	(revision 12096)
@@ -0,0 +1,13 @@
+SELECT
+    chipPendingImfile.*,
+    rawExp.camera,
+    rawExp.workdir
+FROM chipPendingImfile
+JOIN chipPendingExp
+    USING(exp_tag)
+JOIN rawExp
+    USING(exp_tag)
+LEFT JOIN chipMask
+    ON chipPendingExp.label = chipMask.label
+WHERE
+    chipMask.label IS NULL
Index: /trunk/ippTools/share/chiptool_processedimfile.sql
===================================================================
--- /trunk/ippTools/share/chiptool_processedimfile.sql	(revision 12096)
+++ /trunk/ippTools/share/chiptool_processedimfile.sql	(revision 12096)
@@ -0,0 +1,11 @@
+SELECT
+    chipProcessedImfile.*,
+    rawExp.camera,
+    rawExp.workdir
+FROM chipProcessedImfile
+JOIN rawExp
+    USING(exp_tag)
+WHERE
+-- bogus test; just here so there there is a 'WHERE' stmt to append conditionals too
+    chipProcessedImfile.exp_tag is NOT NULL
+
Index: /trunk/ippTools/src/chiptool.c
===================================================================
--- /trunk/ippTools/src/chiptool.c	(revision 12095)
+++ /trunk/ippTools/src/chiptool.c	(revision 12096)
@@ -27,4 +27,5 @@
 
 #include "pxtools.h"
+#include "pxdata.h"
 #include "chiptool.h"
 
@@ -221,19 +222,9 @@
     // XXX does this need to be constrained so that it won't return any results
     // if a match chipPendingExp hasn't been registered?
-    psString query = psStringCopy(
-            "SELECT"
-            "   chipPendingImfile.*,"
-            "   rawExp.camera,"
-            "   rawExp.workdir"
-            " FROM chipPendingImfile"
-            " JOIN chipPendingExp"
-            "   USING(exp_tag)"
-            " JOIN rawExp"
-            "   USING(exp_tag)"
-            " LEFT JOIN chipMask"
-            "   ON chipPendingExp.label = chipMask.label"
-            " WHERE"
-            "   chipMask.label IS NULL"
-        );
+    psString query = pxDataGet("chiptool_pendingimfile.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
+    }
 
     if (config->where) {
@@ -307,14 +298,10 @@
     // if so move the chipPendingExp(s) to chipProcessedExp
 
-    psString query = psStringCopy(
-        "SELECT DISTINCT"
-        "   chipPendingImfile.*"
-        " FROM chipPendingImfile "
-        " LEFT JOIN chipProcessedImfile"
-        "   USING(exp_tag, class_id)"
-        " WHERE"
-        "  chipProcessedImfile.exp_tag IS NULL"
-        "  AND chipProcessedImfile.class_id IS NULL"
-        );
+    psString query = pxDataGet("chiptool_find_unprocessed_imfile.pl");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
+    }
+
     {
         psMetadata *where = psMetadataAlloc();
@@ -475,15 +462,9 @@
     // XXX does this need to be constrained so that it won't return any results
     // if a match chipPendingExp hasn't been registered?
-    psString query = psStringCopy(
-            "SELECT"
-            "   chipProcessedImfile.*,"
-            "   rawExp.camera,"
-            "   rawExp.workdir"
-            " FROM chipProcessedImfile"
-            " JOIN rawExp"
-            "   USING(exp_tag)"
-            " WHERE "
-            "   chipProcessedImfile.exp_tag is NOT NULL" // bogus test -- just here so there there is a 'WHERE' stmt to append conditionals too
-        );
+    psString query = pxDataGet("chiptool_processedimfile.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
+    }
 
     if (config->where) {
@@ -673,32 +654,9 @@
     // look for completed chipPendingExp
     // migrate them to chipProccessedExp & camPendingExp
-
-    // select * from chipPendingExp
-    // where exp_tag is not in chipProcessedExp
-    // where exp_tag is not in chipPendingImfile
-    // where the number of entries in chipProccessedImfile matches the .imfiles
-    // entry in rawExp
-    psString query = psStringCopy(
-        "SELECT DISTINCT"
-        "   chipPendingExp.*,"
-        "   rawExp.imfiles,"
-        "   chipProcessedImfile.class_id"
-        " FROM chipPendingExp"
-        " JOIN rawExp"
-        "   ON chipPendingExp.exp_tag = rawExp.exp_tag"
-        " LEFT JOIN chipProcessedExp"
-        "   ON chipPendingExp.exp_tag = chipProcessedExp.exp_tag"
-        " LEFT JOIN chipPendingImfile"
-        "   ON chipPendingExp.exp_tag = chipPendingImfile.exp_tag"
-        " LEFT JOIN chipProcessedImfile"
-        "   ON chipPendingExp.exp_tag = chipProcessedImfile.exp_tag"
-        " WHERE"
-        "  chipProcessedExp.exp_tag IS NULL"
-        "  AND chipPendingImfile.exp_tag IS NULL"
-        "  AND chipProcessedImfile.exp_tag IS NOT NULL"
-        " GROUP BY"
-        "   chipPendingExp.exp_tag"
-        " HAVING rawExp.imfiles = COUNT(chipProcessedImfile.class_id)"
-        );
+    psString query = pxDataGet("chiptool_completely_processed_exp.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
+    }
 
     if (!p_psDBRunQuery(config->dbh, query)) {
Index: /trunk/ippTools/src/regtool.c
===================================================================
--- /trunk/ippTools/src/regtool.c	(revision 12095)
+++ /trunk/ippTools/src/regtool.c	(revision 12096)
@@ -115,5 +115,5 @@
     psString query = pxDataGet("regtool_pendingimfile.sql");
     if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statment");
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
         return false;
     }
@@ -180,5 +180,5 @@
     psString query = pxDataGet("regtool_find_unprocessed_imfile.sql");
     if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statment");
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
         return false;
     }
@@ -352,5 +352,5 @@
     psString query = pxDataGet("regtool_processedimfile.sql");
     if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statment");
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
         return false;
     }
@@ -466,5 +466,5 @@
     psString query = pxDataGet("regtool_pendingexp.sql");
     if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statment");
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
         return false;
     }
@@ -553,5 +553,5 @@
     psString query = pxDataGet("regtool_find_unprocessed_exp.sql");
     if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statment");
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
         return false;
     }
@@ -559,6 +559,8 @@
     if (!p_psDBRunQuery(config->dbh, query, exp_tag)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
-        return false;
-    }
+        psFree(query);
+        return false;
+    }
+    psFree(query);
 
     psArray *output = p_psDBFetchResult(config->dbh);
@@ -758,5 +760,5 @@
     psString query = pxDataGet("regtool_processedexp.sql");
     if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statment");
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
         return false;
     }
