2f5148ec4aeeb62f11e3196beaea96f9022ae25d
tdreszer
  Tue Mar 8 16:30:25 2011 -0800
Subtle change to the mdbValLabelSearch() API to put the required val in the pair->name and the optional label in the pair->val.  This helps standardize other code that deals with val/label pairs, and enables using slPairFind().
diff --git src/hg/lib/mdb.c src/hg/lib/mdb.c
index 9833eb2..61c1ef7 100644
--- src/hg/lib/mdb.c
+++ src/hg/lib/mdb.c
@@ -2952,133 +2952,123 @@
     dyStringPrintf(dyQuery,"select distinct val");
 
 dyStringPrintf(dyQuery," from %s l1 where l1.var='%s' ",tableName,var);
 
 if (!tables || !files)
     dyStringPrintf(dyQuery,"and exists (select l2.obj from %s l2 where l2.obj = l1.obj and l2.var='objType' and l2.val='%s')",
                    tableName,tables?"table":"file");
 dyStringAppend(dyQuery," order by val");
 
 retVal = sqlQuickList(conn, dyStringCannibalize(&dyQuery));
 slNameSortCase(&retVal);
 return retVal;
 }
 
 // TODO: decide to make this public or hide it away inside the one function so far that uses it.
-static struct hash *cvHash = NULL;
 static char *cv_file()
 // return default location of cv.ra
 {
 static char filePath[PATH_LEN];
 char *root = hCgiRoot();
 if (root == NULL || *root == 0)
     root = "/usr/local/apache/cgi-bin/"; // Make this check out sandboxes?
 //    root = "/cluster/home/tdreszer/kent/src/hg/makeDb/trackDb/cv/alpha/"; // Make this check out sandboxes?
 safef(filePath, sizeof(filePath), "%s/encode/cv.ra", root);
 if(!fileExists(filePath))
     errAbort("Error: can't locate cv.ra; %s doesn't exist\n", filePath);
 return filePath;
 }
 
+struct hash *mdbCvTermHash(char *term)
+// returns a hash of hashes of a term which should be defined in cv.ra
+{
+static struct hash *cvHashOfHashOfHashes = NULL;
+if (sameString(term,"cell"))
+    term = "Cell Line";
+else if (sameString(term,"antibody"))
+    term = "Antibody";
+
+if (cvHashOfHashOfHashes == NULL)
+    cvHashOfHashOfHashes = hashNew(0);
+
+struct hash *cvTermHash = hashFindVal(cvHashOfHashOfHashes,term);
+// Establish cv hash of Term Types if it doesn't already exist
+if (cvTermHash == NULL)
+    {
+    cvTermHash = raReadWithFilter(cv_file(), "term","type",term);
+    if (cvTermHash != NULL)
+        hashAdd(cvHashOfHashOfHashes,term,cvTermHash);
+    }
+
+return cvTermHash;
+}
+
 struct slPair *mdbValLabelSearch(struct sqlConnection *conn, char *var, int limit, boolean tables, boolean files)
-// Search the metaDb table for vals by var and returns controlled vocabulary (cv) label
-// (if it exists) and val as a pair.  Can impose (non-zero) limit on returned string size of name.
-// Return is case insensitive sorted on name (label or else val).
+// Search the metaDb table for vals by var and returns val (as pair->name) and controlled vocabulary (cv) label
+// (if it exists) (as pair->val).  Can impose (non-zero) limit on returned string size of name.
+// Return is case insensitive sorted on label (cv label or else val).
 {  // TODO: Change this to use normal mdb struct routines?
 if (!tables && !files)
     errAbort("mdbValSearch requests values for neither table nor file objects.\n");
 
 char *tableName = mdbTableName(conn,TRUE); // Look for sandBox name first
 
 struct dyString *dyQuery = dyStringNew(512);
 if (limit > 0)
     dyStringPrintf(dyQuery,"select distinct LEFT(val,%d)",limit);
 else
     dyStringPrintf(dyQuery,"select distinct val");
 
 dyStringPrintf(dyQuery," from %s l1 where l1.var='%s' ",tableName,var);
 
 if (!tables || !files)
     dyStringPrintf(dyQuery,"and exists (select l2.obj from %s l2 where l2.obj = l1.obj and l2.var='objType' and l2.val='%s')",
                    tableName,tables?"table":"file");
 dyStringAppend(dyQuery," order by val");
 
-// Establish cv hash
-if (cvHash == NULL)
-    cvHash = raReadAll(cgiUsualString("ra", cv_file()), "term");
+struct hash *varHash = mdbCvTermHash(var);
 
 struct slPair *pairs = NULL, *pair;
 struct sqlResult *sr = sqlGetResult(conn, dyStringContents(dyQuery));
 dyStringFree(&dyQuery);
 char **row;
-struct hash *ra = NULL;
+struct hash *valHash = NULL;
 while ((row = sqlNextRow(sr)) != NULL)
     {
     AllocVar(pair);
     char *name = cloneString(row[0]);
     pair = slPairNew(name,name);  // defaults the label to the metaDb.val
-    ra = hashFindVal(cvHash,name);
-    if (ra == NULL && sameString(var,"lab"))  // FIXME: ugly special case to be removed when metaDb is cleaned up!
-        {
-        char *val = cloneString(name);
-        ra = hashFindVal(cvHash,strUpper(val));
-        if (ra == NULL)
-            ra = hashFindVal(cvHash,strLower(val));
-        freeMem(val);
-        }
-    if (ra != NULL)
+    valHash = hashFindVal(varHash,name);
+    if (valHash != NULL)
         {
-        char *label = hashFindVal(ra,"label");
+        char *label = hashFindVal(valHash,"label");
         if (label != NULL)
             {
-            freeMem(pair->name); // Allocated when pair was created
-            pair->name = strSwapChar(cloneString(label),'_',' ');  // vestigial _ meaning space
-            if (limit > 0 && strlen(pair->name) > limit)
-                pair->name[limit] = '\0';
+            label = strSwapChar(cloneString(label),'_',' ');  // vestigial _ meaning space
+            if (limit > 0 && strlen(label) > limit)
+                label[limit] = '\0';
+            freeMem(pair->val); // Allocated when pair was created
+            pair->val = label;
             }
         }
     slAddHead(&pairs, pair);
     }
 sqlFreeResult(&sr);
-slPairSortCase(&pairs);
+slPairValSortCase(&pairs);
 return pairs;
 }
 
-struct hash *mdbCvTermHash(char *term)
-// returns a hash of hashes of a term which should be defined in cv.ra
-{
-static struct hash *cvHashOfHashOfHashes = NULL;
-if (sameString(term,"cell"))
-    term = "Cell Line";
-else if (sameString(term,"antibody"))
-    term = "Antibody";
-
-if (cvHashOfHashOfHashes == NULL)
-    cvHashOfHashOfHashes = hashNew(0);
-
-struct hash *cvTermHash = hashFindVal(cvHashOfHashOfHashes,term);
-// Establish cv hash of Term Types if it doesn't already exist
-if (cvTermHash == NULL)
-    {
-    cvTermHash = raReadWithFilter(cv_file(), "term","type",term);
-    if (cvTermHash != NULL)
-        hashAdd(cvHashOfHashOfHashes,term,cvTermHash);
-    }
-
-return cvTermHash;
-}
-
 struct hash *mdbCvTermTypeHash()
 // returns a hash of hashes of mdb and controlled vocabulary (cv) term types
 // Those terms should contain label,description,searchable,cvDefined,hidden
 { // NOTE: "typeOfTerm" is specialized, so don't use mdbCvTermHash
 static struct hash *cvHashOfTermTypes = NULL;
 
 // Establish cv hash of Term Types if it doesn't already exist
 if (cvHashOfTermTypes == NULL)
     {
     cvHashOfTermTypes = raReadWithFilter(cv_file(), "term","type","typeOfTerm");
     // Patch up an ugly inconsistency with 'cell'
     struct hash *cellHash = hashRemove(cvHashOfTermTypes,"cellType");
     if (cellHash)
         {
         hashAdd(cvHashOfTermTypes,"cell",cellHash);