Index: trunk/ippTools/src/stacktool.c
===================================================================
--- trunk/ippTools/src/stacktool.c	(revision 19159)
+++ trunk/ippTools/src/stacktool.c	(revision 19249)
@@ -141,6 +141,6 @@
     PXOPT_LOOKUP_TIME(registered, config->args, "-registered", false, false);
 
-    if (!psListLength(where->list) && 
-	!psMetadataLookupBool(NULL, config->args, "-all")) {
+    if (!psListLength(where->list) &&
+        !psMetadataLookupBool(NULL, config->args, "-all")) {
         psFree(where);
         psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
@@ -159,9 +159,9 @@
         psFree(whereClause);
     }
-    psFree(where);
 
     psString groupby = pxDataGet("stacktool_definebyquery_part2.sql");
     if (!groupby) {
         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        psFree(where);
         return false;
     }
@@ -202,4 +202,5 @@
         psError(PS_ERR_UNKNOWN, false, "database error");
         psFree(select);
+        psFree(where);
         return false;
     }
@@ -217,5 +218,5 @@
                 psError(PXTOOLS_ERR_PROG, false, "unknown error");
         }
-
+        psFree(where);
         return false;
     }
@@ -223,4 +224,5 @@
         psTrace("stacktool", PS_LOG_INFO, "no rows found");
         psFree(output);
+        psFree(where);
         return true;
     }
@@ -228,5 +230,5 @@
     psString insert = NULL;             // Insertion query
     if (randomLimit > 0) {
-        insert = pxDataGet("stacktool_definebyquery_insert_random.sql");
+        insert = pxDataGet("stacktool_definebyquery_insert_random_part1.sql");
     } else {
         insert = pxDataGet("stacktool_definebyquery_insert.sql");
@@ -234,9 +236,29 @@
     if (!insert) {
         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
-        return false;
+        psFree(where);
+        return false;
+    }
+
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
+        psStringAppend(&select, " AND %s", whereClause);
+        psFree(whereClause);
+    }
+    psFree(where);
+
+    if (randomLimit > 0) {
+        psString part2 = pxDataGet("stacktool_definebyquery_insert_random_part2.sql");
+        if (!part2) {
+            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+            psFree(insert);
+            return false;
+        }
+        psStringAppend(&insert, "%s", part2);
+        psFree(part2);
     }
 
     if (!psDBTransaction(config->dbh)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
+        psFree(where);
         return false;
     }
@@ -288,9 +310,9 @@
             0,                          // ID
             "new",                      // state
-            workdir, 
+            workdir,
             NULL,                       // workdir_state
-            registered, 
-            skycell_id, 
-            tess_id, 
+            registered,
+            skycell_id,
+            tess_id,
             filter);
 
@@ -324,8 +346,8 @@
         psFree(idString);
 
-	// XXX this insert uses a select to generate the list of warp_ids for the stack,
-	// we have applied a set of criteria above (WHERE) to select the relevant warps
-	// this insert needs to use exactly the same restrictions (race condition is probably not critical)
-	// the insert below seems to only restrict matches to the skycell, tess, and filter
+        // XXX this insert uses a select to generate the list of warp_ids for the stack,
+        // we have applied a set of criteria above (WHERE) to select the relevant warps
+        // this insert needs to use exactly the same restrictions (race condition is probably not critical)
+        // the insert below seems to only restrict matches to the skycell, tess, and filter
         if ((randomLimit > 0 && !p_psDBRunQuery(config->dbh, thisInsert, skycell_id, tess_id, filter,
                                                 randomLimit)) ||
@@ -384,12 +406,12 @@
 
     stackRunRow *run = stackRunRowAlloc(
-	0, 				// ID
-	"new", 				// state
-	workdir, 
-	NULL, 				// workdir_state
-	registered, 
-	skycell_id, 
-	tess_id, 
-	filter);
+        0,                              // ID
+        "new",                          // state
+        workdir,
+        NULL,                           // workdir_state
+        registered,
+        skycell_id,
+        tess_id,
+        filter);
 
     if (!run) {
