Index: branches/eam_branches/ipp-20101103/ippTools/src/difftool.c
===================================================================
--- branches/eam_branches/ipp-20101103/ippTools/src/difftool.c	(revision 29657)
+++ 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);
