Index: trunk/ippTools/src/stacktool.c
===================================================================
--- trunk/ippTools/src/stacktool.c	(revision 24661)
+++ trunk/ippTools/src/stacktool.c	(revision 24664)
@@ -153,4 +153,5 @@
 
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
+    PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
     PXOPT_LOOKUP_TIME(registered, config->args, "-registered", false, false);
 
@@ -236,5 +237,17 @@
     }
     if (!psArrayLength(output)) {
-        psTrace("stacktool", PS_LOG_INFO, "no rows found");
+        psWarning("stacktool: no rows found");
+        psFree(output);
+        psFree(where);
+        return true;
+    }
+    if (pretend) {
+        // negative simple so the default is true
+        if (!ippdbPrintMetadatas(stdout, output, "stackSkycells", !simple)) {
+            psError(PS_ERR_UNKNOWN, false, "failed to print array");
+            psFree(output);
+	    psFree(where);
+            return false;
+        }
         psFree(output);
         psFree(where);
@@ -362,10 +375,20 @@
         psFree(idString);
 
+        // replace @FILTER@, @SKYCELL_ID@, @RANDOM_LIMIT@
+        psStringSubstitute(&thisInsert, filter, "@FILTER@");
+        psStringSubstitute(&thisInsert, skycell_id, "@SKYCELL_ID@");
+
+	if (randomLimit > 0) {
+	  psString limString = NULL;
+	  psStringAppend(&limString, "%d", randomLimit);
+	  psStringSubstitute(&thisInsert, limString, "@RANDOM_LIMIT@");
+	  psFree(limString);
+	}
+
         // 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_psDBRunQueryF(config->dbh, thisInsert, skycell_id, filter, randomLimit)) ||
-            (randomLimit <= 0 && !p_psDBRunQueryF(config->dbh, thisInsert, skycell_id, filter))) {
+        if (!p_psDBRunQuery(config->dbh, thisInsert)) {
             psError(PS_ERR_UNKNOWN, false, "database error");
             psFree(thisInsert);
@@ -379,4 +402,40 @@
         }
         psFree(thisInsert);
+
+# if (0)
+	{
+	  psArray *output = p_psDBFetchResult(config->dbh);
+	  if (!output) {
+	    psErrorCode err = psErrorCodeLast();
+	    switch (err) {
+	    case PS_ERR_DB_CLIENT:
+	      psError(PXTOOLS_ERR_SYS, false, "database error");
+	    case PS_ERR_DB_SERVER:
+	      psError(PXTOOLS_ERR_PROG, false, "database error");
+	    default:
+	      psError(PXTOOLS_ERR_PROG, false, "unknown error");
+	    }
+	    psFree(where);
+	    return false;
+	  }
+	  if (!psArrayLength(output)) {
+	    psWarning("stacktool (definebyquery, insert): no rows found");
+	    psFree(output);
+	    psFree(where);
+	    return true;
+	  }
+	  // negative simple so the default is true
+	  if (!ippdbPrintMetadatas(stdout, output, "stackSkycells", !simple)) {
+	    psError(PS_ERR_UNKNOWN, false, "failed to print array");
+	    psFree(output);
+	    psFree(where);
+	    return false;
+	  }
+	  psFree(output);
+	  psFree(where);
+	  return true;
+	}
+# endif
+
     }
     psFree(output);
