src/utils/raSqlQuery/raSqlQuery.c 1.23

1.23 2009/11/25 07:16:03 kent
Hacking a lot of very trackDb-specific settings parsing into raSqlQuery with db= option on. This enables inherit-by-view.
Index: src/utils/raSqlQuery/raSqlQuery.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/utils/raSqlQuery/raSqlQuery.c,v
retrieving revision 1.22
retrieving revision 1.23
diff -b -B -U 4 -r1.22 -r1.23
--- src/utils/raSqlQuery/raSqlQuery.c	22 Nov 2009 05:58:13 -0000	1.22
+++ src/utils/raSqlQuery/raSqlQuery.c	25 Nov 2009 07:16:03 -0000	1.23
@@ -363,14 +363,11 @@
 }
 
 
 static struct raRecord *findParent(struct raRecord *rec, 
-	char *parentFieldName, char *noInheritFieldName, struct hash *hash)
+	char *parentFieldName, struct hash *hash)
 /* Find parent field if possible. */
 {
-struct raField *noInheritField = raRecordField(rec, noInheritFieldName);
-if (noInheritField != NULL)
-    return NULL;
 struct raField *parentField = raRecordField(rec, parentFieldName);
 if (parentField == NULL)
     return NULL;
 char *parentLine = parentField->val;
@@ -398,14 +395,73 @@
     if (rec->key != NULL)
 	hashAdd(hash, rec->key, rec);
     }
 
+/* Scan through linking up parents. */
+for (rec = list; rec != NULL; rec = rec->next)
+    {
+    struct raRecord *parent = findParent(rec, parentField, hash);
+    if (parent != NULL)
+	{
+	rec->parent = parent;
+	rec->olderSibling = parent->children;
+	parent->children = rec;
+	}
+    }
+
 /* Scan through doing inheritance. */
 for (rec = list; rec != NULL; rec = rec->next)
     {
+    /* First inherit from view. */
+    char *viewName = rec->view;
+    if (viewName != NULL)
+        {
+	struct slPair *view;
+	if (rec->parent == NULL)
+	     {
+	     verbose(2, "%s has view %s but no parent\n", rec->key, viewName);
+	     continue;
+	     }
+	for (view = rec->parent->settingsByView; view != NULL; view = view->next)
+	    {
+	    if (sameString(view->name, viewName))
+		break;
+	    else 
+	        {
+		if (rec->parent->viewHash != NULL)
+		    {
+		    char *alias = hashFindVal(rec->parent->viewHash, viewName);
+		    if (alias != NULL)
+			if (sameString(view->name, alias))
+			    break;
+		    }
+		}
+	    }
+	if (view != NULL)
+	    {
+	    struct slPair *setting;
+	    for (setting = view->val; setting != NULL; setting = setting->next)
+	        {
+		struct raField *oldField = raRecordField(rec, setting->name);
+		if (oldField == NULL)
+		    {
+		    struct raField *newField;
+		    lmAllocVar(lm, newField);
+		    newField->name = lmCloneString(lm, setting->name);
+		    newField->val = lmCloneString(lm, setting->val);
+		    slAddTail(&rec->fieldList, newField);
+		    }
+		}
+	    }
+	else 
+	    {
+	    verbose(2, "view %s not in parent settingsByView of %s\n", viewName, rec->key);
+	    }
+	}
+
+    /* Then inherit from parents. */
     struct raRecord *parent;
-    for (parent = findParent(rec, parentField, noInheritField, hash); parent != NULL;
-    	parent = findParent(parent, parentField, noInheritField, hash) )
+    for (parent = rec->parent; parent != NULL; parent = parent->parent)
 	{
 	mergeParentRecord(rec, parent, lm);
 	}
     }