src/utils/raSqlQuery/raSqlQuery.c 1.22

1.22 2009/11/22 05:58:13 kent
Making count statement work better with db=all and with from clauses.
Index: src/utils/raSqlQuery/raSqlQuery.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/utils/raSqlQuery/raSqlQuery.c,v
retrieving revision 1.21
retrieving revision 1.22
diff -b -B -U 4 -r1.21 -r1.22
--- src/utils/raSqlQuery/raSqlQuery.c	22 Nov 2009 05:28:52 -0000	1.21
+++ src/utils/raSqlQuery/raSqlQuery.c	22 Nov 2009 05:58:13 -0000	1.22
@@ -276,25 +276,9 @@
     }
 }
 
 boolean rqlStatementMatch(struct rqlStatement *rql, struct raRecord *ra)
-/* Return TRUE if where clause in statement evaluates true for ra. */
-{
-struct rqlParse *whereClause = rql->whereClause;
-if (whereClause == NULL)
-    return TRUE;
-else
-    {
-    struct rqlEval res = rqlEvalOnRecord(whereClause, ra);
-    res = rqlEvalCoerceToBoolean(res);
-    return res.val.b;
-    }
-}
-
-void rqlStatementOutput(struct rqlStatement *rql, struct raRecord *ra, 
-	char *addFileField, char *addDbField, FILE *out)
-/* Output fields  from ra to file.  If addFileField is non-null add a new
- * field with this name at end of output. */
+/* Return TRUE if where clause and tableList in statement evaluates true for ra. */
 {
 if (rql->tableList != NULL)
     {
     boolean gotMatch = FALSE;
@@ -313,10 +297,26 @@
 	if (gotMatch)
 	    break;
 	}
     if (!gotMatch)
-        return;
+        return FALSE;
+    }
+struct rqlParse *whereClause = rql->whereClause;
+if (whereClause == NULL)
+    return TRUE;
+else
+    {
+    struct rqlEval res = rqlEvalOnRecord(whereClause, ra);
+    res = rqlEvalCoerceToBoolean(res);
+    return res.val.b;
     }
+}
+
+void rqlStatementOutput(struct rqlStatement *rql, struct raRecord *ra, 
+	char *addFileField, char *addDbField, FILE *out)
+/* Output fields  from ra to file.  If addFileField is non-null add a new
+ * field with this name at end of output. */
+{
 if (addDbField)
     fprintf(out, "%s %s\n", addDbField, ra->db);
 struct slName *fieldList = rql->fieldList, *field;
 for (field = fieldList; field != NULL; field = field->next)
@@ -410,17 +410,12 @@
 	}
     }
 }
 
-void raSqlQuery(int inCount, char *inNames[], 
+int raSqlQuery(struct rqlStatement *rql, int inCount, char *inNames[], 
 	char *db, char *parentField, struct lm *lm, FILE *out)
 /* raSqlQuery - Do a SQL-like query on a RA file.. */
 {
-struct lineFile *query;
-if (clQuery)
-    query = lineFileOnString("query", TRUE, cloneString(clQuery));
-else
-    query = lineFileOpen(clQueryFile, TRUE);
 struct raRecord *raList = readRaRecords(inCount, inNames, clKey, 
 	clMerge, db, clAddDb, clOverrideNeeded, lm);
 if (parentField != NULL)
     {
@@ -442,9 +437,8 @@
     slReverse(&newList);
     raList = newList;
     hashFree(&restrictHash);
     }
-struct rqlStatement *rql = rqlStatementParse(query);
 verbose(2, "Got %d records in raFiles\n", slCount(raList));
 if (verboseLevel() > 1)
     rqlStatementDump(rql, stderr);
 struct raRecord *ra;
@@ -464,10 +458,9 @@
 		}
 	    }
 	}
     }
-if (!doSelect)
-    printf("%d\n", matchCount);
+return matchCount;
 }
 
 int main(int argc, char *argv[])
 /* Process command line. */
@@ -504,11 +497,20 @@
     clKey = "track";
     if (sameString(clDb, "all"))
         clAddDb = TRUE;
     }
+/* Parse query */
+struct lineFile *query;
+if (clQuery)
+    query = lineFileOnString("query", TRUE, cloneString(clQuery));
+else
+    query = lineFileOpen(clQueryFile, TRUE);
+struct rqlStatement *rql = rqlStatementParse(query);
+
 char *parentField = (clParent ? clParentField : NULL);
 char **fileNames;
 int fileCount;
+int matchCount = 0;
 if (clDb)
     {
     if (argc != 1)
 	 errAbort("You can't specify any input files with the -db option.");
@@ -527,9 +529,9 @@
 	    for (i=0, path = pathList; path != NULL; path = path->next, ++i)
 		{
 		fileNames[i] = path->name;
 		}
-	    raSqlQuery(fileCount, fileNames, db->db, parentField, lm, stdout);
+	    matchCount += raSqlQuery(rql, fileCount, fileNames, db->db, parentField, lm, stdout);
 	    gotAny = TRUE;
 	    }
 	}
     if (!gotAny)
@@ -538,8 +540,10 @@
 else
     {
     fileNames = argv+1;
     fileCount = argc-1;
-    raSqlQuery(fileCount, fileNames, "n/a", parentField, lm, stdout);
+    matchCount += raSqlQuery(rql, fileCount, fileNames, "n/a", parentField, lm, stdout);
     }
+if (sameString(rql->command, "count"))
+    printf("%d\n", matchCount);
 return 0;
 }