Index: trunk/ippTools/share/difftool_inputskyfile.sql
===================================================================
--- trunk/ippTools/share/difftool_inputskyfile.sql	(revision 19574)
+++ trunk/ippTools/share/difftool_inputskyfile.sql	(revision 19582)
@@ -35,4 +35,5 @@
         AND camRun.state = 'full'
         AND chipRun.state = 'full'
+        -- where hook %s
     UNION
     SELECT 
@@ -71,3 +72,4 @@
         AND camRun.state = 'full'
         AND chipRun.state = 'full'
+        -- where hook %s
     ) as Foo
Index: trunk/ippTools/src/difftool.c
===================================================================
--- trunk/ippTools/src/difftool.c	(revision 19574)
+++ trunk/ippTools/src/difftool.c	(revision 19582)
@@ -318,25 +318,25 @@
     }
 
+    psString whereClause = NULL;
     if (psListLength(where->list)) {
-        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
-        psStringAppend(&query, " WHERE %s", whereClause);
-        psFree(whereClause);
-    }
+        whereClause = psDBGenerateWhereConditionSQL(where, NULL);
+        psStringPrepend(&whereClause, "\n AND ");
+    }
+    psFree(where);
 
     // Add condition to get only templates or only inputs
     {
-        psString whereClause = NULL;
+        psString templateClause = NULL;
         if (template) {
-            psStringAppend(&whereClause, " %s", " template != 0");
+            psStringAppend(&templateClause, " %s", " template != 0");
         } else if (input) {
-            psStringAppend(&whereClause, " %s", " template = 0");
-        }
-        if (whereClause) {
-            psStringAppend(&query, " %s %s", psListLength(where->list) ? "AND" : "WHERE", whereClause);
-        }
-        psFree(whereClause);
-    }
-
-    psFree(where);
+            psStringAppend(&templateClause, " %s", " template = 0");
+        }
+        if (templateClause) {
+            psStringAppend(&whereClause, "\n AND %s", templateClause);
+        }
+        psFree(templateClause);
+    }
+
 
     // treat limit == 0 as "no limit"
@@ -347,9 +347,11 @@
     }
 
-    if (!p_psDBRunQuery(config->dbh, query)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
+    if (!p_psDBRunQuery(config->dbh, query, whereClause, whereClause)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        psFree(whereClause);
         psFree(query);
         return false;
     }
+    psFree(whereClause);
     psFree(query);
 
