Index: /branches/eam_branches/ipp-20101103/ippTools/share/difftool_definestackstack_part0.sql
===================================================================
--- /branches/eam_branches/ipp-20101103/ippTools/share/difftool_definestackstack_part0.sql	(revision 29663)
+++ /branches/eam_branches/ipp-20101103/ippTools/share/difftool_definestackstack_part0.sql	(revision 29664)
@@ -22,5 +22,5 @@
      	   stackRun.state = 'full' AND
 	   stackSumSkyfile.fault = 0 AND stackSumSkyfile.quality = 0
-	   -- template where hook %s
+	   @STACK2_QUERY@ -- template condition
      GROUP BY
      	   skycell_id,
@@ -38,9 +38,9 @@
 	  JOIN stackSumSkyfile USING(stack_id)
 	  WHERE 1
-	  -- input where hook %s
+	  @STACK1_QUERY@ -- input condition
 ) AS diffExp ON diffExp.stack1 = stackRun.stack_id AND diffExp.stack2 = template.max_stack_id
 WHERE
 	stackSumSkyfile.fault = 0 AND stackSumSkyfile.quality = 0
-        -- diff where hook %s
-	-- input where hook again %s
+	@DIFF0_QUERY@  -- diff constraint
+	@STACK1_QUERY@ -- input constraint
 ORDER BY stackRun.data_group,stackRun.filter
Index: /branches/eam_branches/ipp-20101103/ippTools/share/difftool_definestackstack_part1.sql
===================================================================
--- /branches/eam_branches/ipp-20101103/ippTools/share/difftool_definestackstack_part1.sql	(revision 29663)
+++ /branches/eam_branches/ipp-20101103/ippTools/share/difftool_definestackstack_part1.sql	(revision 29664)
@@ -23,5 +23,5 @@
      	   stackRun.state = 'full' AND
 	   stackSumSkyfile.fault = 0 AND stackSumSkyfile.quality = 0
-	   -- template where hook %s
+	   @STACK2_QUERY@ -- template constraint
      GROUP BY
      	   skycell_id,
@@ -39,10 +39,9 @@
 	  JOIN stackSumSkyfile USING(stack_id)
 	  WHERE 1
-	  -- input where hook %s
+	  @STACK1_QUERY@ -- input constraint
 ) AS diffExp ON diffExp.stack1 = stackRun.stack_id AND diffExp.stack2 = template.max_stack_id
 WHERE
 	stackSumSkyfile.fault = 0 AND stackSumSkyfile.quality = 0
-	-- diff where hook %s
-	-- input where hook again %s
+	@STACK1_QUERY@ -- input constraint
 group by stackRun.stack_id,
        stackRun.data_group,
@@ -54,3 +53,4 @@
        template.max_stack_id,
        template.template_label
+       @DIFF1_QUERY@  -- diff constraint
 ORDER BY stackRun.data_group,stackRun.filter,stackRun.skycell_id
Index: /branches/eam_branches/ipp-20101103/ippTools/src/difftool.c
===================================================================
--- /branches/eam_branches/ipp-20101103/ippTools/src/difftool.c	(revision 29663)
+++ /branches/eam_branches/ipp-20101103/ippTools/src/difftool.c	(revision 29664)
@@ -2009,20 +2009,15 @@
     if (psListLength(stack1Where->list)) {
 	psString whereClause = psDBGenerateWhereConditionSQL(stack1Where, NULL);
-	psStringAppend(&stack1Query, "\n AND %s", whereClause);
+	psStringAppend(&stack1Query, "AND %s", whereClause);
 	psFree(whereClause);
-    } else {
-	stack1Query = psStringCopy("\n");
-    }
+    } 
     psFree(stack1Where);
 
     if (psListLength(stack2Where->list)) {
 	psString whereClause = psDBGenerateWhereConditionSQL(stack2Where, NULL);
-	psStringAppend(&stack2Query, "\n AND %s", whereClause);
+	psStringAppend(&stack2Query, "AND %s", whereClause);
 	psFree(whereClause);
-    } else {
-	stack2Query = psStringCopy("\n");
-    }
+    } 
     psFree(stack2Where);
-
   
     // don't queue for stacks that have already been diffed unless requested
@@ -2030,10 +2025,7 @@
     psString diffQuery1 = NULL;
     if (! (reRun || newTemplates) ) {
-	psStringAppend(&diffQuery0, "\n AND diffExp.diff_id IS NULL");
-	psStringAppend(&diffQuery1, "\n AND n_diff = 0");
-    } else {
-	diffQuery0 = psStringCopy("\n");
-	diffQuery1 = psStringCopy("\n");
-    }
+	psStringAppend(&diffQuery0, "AND diffExp.diff_id IS NULL");
+	psStringAppend(&diffQuery1, "HAVING n_diff = 0");
+    } 
 
     // find the distinct set of data_groups and filters
@@ -2050,5 +2042,9 @@
     }
 
-    if (!p_psDBRunQueryF(config->dbh, query, stack2Query, stack1Query, diffQuery0, stack1Query)) {
+    psStringSubstitute(&query, stack1Query, "@STACK1_QUERY@");
+    psStringSubstitute(&query, stack2Query, "@STACK2_QUERY@");
+    psStringSubstitute(&query, diffQuery0,  "@DIFF0_QUERY@");
+
+    if (!p_psDBRunQuery(config->dbh, query)) {
 	psError(PS_ERR_UNKNOWN, false, "database error");
 	psFree(query);
@@ -2118,8 +2114,12 @@
 	psString thisWhere = psDBGenerateWhereConditionSQL(row,NULL);
 	psStringSubstitute(&thisWhere,"stackRun.","INPUT_");
-	psStringAppend(&this_stack1Query,"\n AND %s", thisWhere);
+	psStringAppend(&this_stack1Query,"AND %s", thisWhere);
 	psFree(thisWhere);
 
-	psTrace("difftool",1, query,stack2Query,this_stack1Query,diffQuery1,this_stack1Query);
+	psStringSubstitute(&query, this_stack1Query, "@STACK1_QUERY@");
+	psStringSubstitute(&query, stack2Query,      "@STACK2_QUERY@");
+	psStringSubstitute(&query, diffQuery1,       "@DIFF1_QUERY@");
+
+	psTrace("difftool", 1, "%s", query);
 	if (!psDBTransaction(config->dbh)) {
 	    psError(PS_ERR_UNKNOWN, false, "database error");
@@ -2127,5 +2127,5 @@
 	}
 
-	if (!p_psDBRunQueryF(config->dbh, query, stack2Query, this_stack1Query, diffQuery1, this_stack1Query)) {
+	if (!p_psDBRunQuery(config->dbh, query)) {
 	    psError(PS_ERR_UNKNOWN, false, "database error");
 	    psFree(query);
