e77dcca9b1bffc335f2843c8cfce67119b3d039d
braney
  Wed Nov 30 16:25:50 2011 -0800
allow specifying different names for the hubPublic and hubStatus tables (#6162)
diff --git src/hg/lib/hubConnect.c src/hg/lib/hubConnect.c
index aba29b4..2ffba5b 100644
--- src/hg/lib/hubConnect.c
+++ src/hg/lib/hubConnect.c
@@ -5,43 +5,57 @@
  * table by design.  We just want field-by-field access to this. */
 
 #include "common.h"
 #include "linefile.h"
 #include "hash.h"
 #include "dystring.h"
 #include "sqlNum.h"
 #include "jksql.h"
 #include "hdb.h"
 #include "net.h"
 #include "trackHub.h"
 #include "hubConnect.h"
 #include "hui.h"
 #include "errCatch.h"
 #include "obscure.h"
+#include "hgConfig.h"
 
 
 boolean isHubTrack(char *trackName)
 /* Return TRUE if it's a hub track. */
 {
 return startsWith(hubTrackPrefix, trackName);
 }
 
+static char *hubStatusTableName = NULL;
+
+static char *getHubStatusTableName()
+/* return the hubStatus table name from the environment, 
+ * or hg.conf, or use the default.  Cache the result */
+{
+if (hubStatusTableName == NULL)
+    hubStatusTableName = cfgOptionEnvDefault("HGDB_HUB_STATUS_TABLE",
+	    "hubStatusTableName", defaultHubStatusTableName);
+
+return hubStatusTableName;
+}
+
 boolean hubConnectTableExists()
 /* Return TRUE if the hubPublic table exists. */
 {
 struct sqlConnection *conn = hConnectCentral();
-boolean exists = sqlTableExists(conn, hubPublicTableName);
+boolean exists = sqlTableExists(conn, getHubStatusTableName());
 hDisconnectCentral(&conn);
 return exists;
 }
 
 void hubConnectStatusFree(struct hubConnectStatus **pHub)
 /* Free hubConnectStatus */
 {
 struct hubConnectStatus *hub = *pHub;
 if (hub != NULL)
     {
     freeMem(hub->hubUrl);
     freeMem(hub->errorMessage);
     trackHubClose(&hub->trackHub);
     freez(pHub);
     }
@@ -128,38 +142,38 @@
 if (errCatch->gotError)
     {
     gotWarning = TRUE;
     *errorMessage = cloneString(errCatch->message->string);
     }
 errCatchFree(&errCatch);
 
 if (gotWarning)
     {
     return NULL;
     }
 
 return tHub;
 }
 
-struct hubConnectStatus *hubConnectStatusForId(struct sqlConnection *conn, int id)
 /* Given a hub ID return associated status. Returns NULL if no such hub.  If hub
  * exists but has problems will return with errorMessage field filled in. */
+struct hubConnectStatus *hubConnectStatusForId(struct sqlConnection *conn, int id)
 {
 struct hubConnectStatus *hub = NULL;
 char query[1024];
 safef(query, sizeof(query), 
-    "select hubUrl,status, errorMessage from %s where id=%d", hubStatusTableName, id);
+    "select hubUrl,status, errorMessage from %s where id=%d", getHubStatusTableName(), id);
 struct sqlResult *sr = sqlGetResult(conn, query);
 char **row = sqlNextRow(sr);
 if (row != NULL)
     {
     AllocVar(hub);
     hub->id = id;
     hub->hubUrl = cloneString(row[0]);
     hub->status = sqlUnsigned(row[1]);
 
     char *errorMessage = cloneString(row[2]);
     if (isEmpty(errorMessage))
 	hub->trackHub = fetchHub( hub->hubUrl, isHubUnlisted(hub), &errorMessage);
     if (errorMessage != NULL)
 	hub->errorMessage = cloneString(errorMessage);
 
@@ -344,53 +358,54 @@
     dbCount++;
     dyStringPrintf(dy,"%s,", hel->name);
     }
 *pCount = dbCount;
 
 return dy->string;
 }
 
 static void insertHubUrlInStatus(char *url, boolean unlisted)
 /* add a url to the hubStatus table */
 {
 struct sqlConnection *conn = hConnectCentral();
 char query[512];
 
 safef(query, sizeof(query), "insert into %s (hubUrl,status) values (\"%s\",%d)",
-    hubStatusTableName, url, unlisted ? 1 : 0);
+    getHubStatusTableName(), url, unlisted ? 1 : 0);
 sqlUpdate(conn, query);
 hDisconnectCentral(&conn);
 }
 
 static unsigned getHubId(char *url, char **errorMessage)
 /* find id for url in hubStatus table */
 {
 struct sqlConnection *conn = hConnectCentral();
 char query[512];
 char **row;
 boolean foundOne = FALSE;
 int id = 0;
 
-safef(query, sizeof(query), "select id,errorMessage from %s where hubUrl = \"%s\"", hubStatusTableName, url);
+char *statusTableName = getHubStatusTableName();
+safef(query, sizeof(query), "select id,errorMessage from %s where hubUrl = \"%s\"", statusTableName, url);
 
 struct sqlResult *sr = sqlGetResult(conn, query);
 
 while ((row = sqlNextRow(sr)) != NULL)
     {
     if (foundOne)
 	errAbort("more than one line in %s with hubUrl %s\n", 
-	    hubStatusTableName, url);
+	    statusTableName, url);
 
     foundOne = TRUE;
 
     char *thisId = row[0], *thisError = row[1];
 
     if (!isEmpty(thisError))
 	*errorMessage = cloneString(thisError);
 
     id = sqlUnsigned(thisId);
     }
 sqlFreeResult(&sr);
 
 hDisconnectCentral(&conn);
 
 return id;
@@ -469,59 +484,59 @@
 
 if (url != NULL)
     {
     trimSpaces(url);
     getAndSetHubStatus(database, cart, url, TRUE, TRUE);
     cartRemove(cart, hgHubDataText);
     }
 }
 
 unsigned hubResetError(char *url)
 /* clear the error for this url in the hubStatus table,return the id */
 {
 struct sqlConnection *conn = hConnectCentral();
 char query[512];
 
-safef(query, sizeof(query), "select id from %s where hubUrl = \"%s\"", hubStatusTableName, url);
+safef(query, sizeof(query), "select id from %s where hubUrl = \"%s\"", getHubStatusTableName(), url);
 unsigned id = sqlQuickNum(conn, query);
 
 if (id == 0)
     errAbort("could not find url %s in status table (%s)\n", 
-	url, hubStatusTableName);
+	url, getHubStatusTableName());
 
-safef(query, sizeof(query), "update %s set errorMessage=\"\" where hubUrl = \"%s\"", hubStatusTableName, url);
+safef(query, sizeof(query), "update %s set errorMessage=\"\" where hubUrl = \"%s\"", getHubStatusTableName(), url);
 
 sqlUpdate(conn, query);
 hDisconnectCentral(&conn);
 
 return id;
 }
 
 unsigned hubClearStatus(char *url)
 /* drop the information about this url from the hubStatus table */
 {
 struct sqlConnection *conn = hConnectCentral();
 char query[512];
 
-safef(query, sizeof(query), "select id from %s where hubUrl = \"%s\"", hubStatusTableName, url);
+safef(query, sizeof(query), "select id from %s where hubUrl = \"%s\"", getHubStatusTableName(), url);
 unsigned id = sqlQuickNum(conn, query);
 
 if (id == 0)
     errAbort("could not find url %s in status table (%s)\n", 
-	url, hubStatusTableName);
+	url, getHubStatusTableName());
 
-safef(query, sizeof(query), "delete from %s where hubUrl = \"%s\"", hubStatusTableName, url);
+safef(query, sizeof(query), "delete from %s where hubUrl = \"%s\"", getHubStatusTableName(), url);
 
 sqlUpdate(conn, query);
 hDisconnectCentral(&conn);
 
 return id;
 }
 
 void hubDisconnect(struct cart *cart, char *url)
 /* drop the information about this url from the hubStatus table, and 
  * the cart variable the references this hub */
 {
 /* clear the hubStatus table */
 unsigned id = hubClearStatus(url);
 
 /* remove the cart variable */
@@ -536,41 +551,41 @@
 struct sqlConnection *conn = hConnectCentral();
 char query[4096];
 struct trackHub *tHub = hub->trackHub;
 
 if (errorMessage != NULL)
     {
     // make sure there is no newline at the end.  This should be unneccesary
     // but there are many, many places where newlines are added in calls
     // to warn and errAbort
     char buffer[4096];
     safecpy(buffer, sizeof buffer, errorMessage);
     while (lastChar(buffer) == '\n')
 	buffer[strlen(buffer) - 1] = '\0';
     safef(query, sizeof(query),
 	"update %s set errorMessage=\"%s\", lastNotOkTime=now() where id=%d",
-	hubStatusTableName, buffer, hub->id);
+	getHubStatusTableName(), buffer, hub->id);
     sqlUpdate(conn, query);
     }
 else if (tHub != NULL)
     {
     int dbCount = 0;
     char *dbList = getDbList(tHub, &dbCount);
 
     safef(query, sizeof(query),
 	"update %s set shortLabel=\"%s\",longLabel=\"%s\",dbCount=\"%d\",dbList=\"%s\",errorMessage=\"\",lastOkTime=now() where id=%d",
-	hubStatusTableName, tHub->shortLabel, tHub->shortLabel, 
+	getHubStatusTableName(), tHub->shortLabel, tHub->shortLabel, 
 	dbCount, dbList,
 	hub->id);
     sqlUpdate(conn, query);
     }
 hDisconnectCentral(&conn);
 }
 
 struct trackDb *hubAddTracks(struct hubConnectStatus *hub, char *database,
 	struct trackHub **pHubList)
 /* Load up stuff from data hub and append to list. The hubUrl points to
  * a trackDb.ra format file.  */
 {
 /* Load trackDb.ra file and make it into proper trackDb tree */
 struct trackDb *tdbList = NULL;
 struct trackHub *trackHub = hub->trackHub;