8c8f63b91ea88283551e091dc95d5cebad182a84
galt
  Thu Aug 6 00:08:15 2015 -0700
adding setting HGDB_PROF which can over-ride the default profile value of "db" for any utility using jksql.

diff --git src/hg/lib/jksql.c src/hg/lib/jksql.c
index b120a1b..5008427 100644
--- src/hg/lib/jksql.c
+++ src/hg/lib/jksql.c
@@ -90,53 +90,62 @@
                                     /* store the database */
     };
 
 struct sqlResult
 /* This is an item on a list of sql open results. */
     {
     MYSQL_RES *result;			/* Result. */
     struct dlNode *node;		/* Pointer to list node we're on. */
     struct sqlConnection *conn;		/* Pointer to connection. */
     long fetchTime;                     /* cummulative time taken by row fetches for this result */
     };
 
 static struct dlList *sqlOpenConnections = NULL;
 static unsigned sqlNumOpenConnections = 0;
 
-char *defaultProfileName = "db";                  // name of default profile for main connection
+
 char *failoverProfPrefix = "slow-";               // prefix for failover profile of main profile (="slow-db")
 static struct hash *profiles = NULL;              // profiles parsed from hg.conf, by name
 static struct sqlProfile *defaultProfile = NULL;  // default profile, also in profiles list
 static struct hash* dbToProfile = NULL;           // db to sqlProfile
 
 // forward declarations to keep the git diffs clean
 static struct sqlResult *sqlUseOrStore(struct sqlConnection *sc,
 	char *query, ResGetter *getter, boolean abort);
 static void sqlConnectIfUnconnected(struct sqlConnection *sc, bool abort);
 bool sqlConnMustUseFailover(struct sqlConnection *sc);
 
 static char *envOverride(char *envName, char *defaultVal)
 /* look up envName in environment, if it exists and is non-empty, return its
  * value, otherwise return defaultVal */
 {
 char *val = getenv(envName);
 if (isEmpty(val))
     return defaultVal;
 else
     return val;
 }
 
+char *getDefaultProfileName()
+/* Return default profile name, handling initialization if needed */
+{
+static char *defaultProfileName = NULL;
+if (!defaultProfileName)
+    defaultProfileName = envOverride("HGDB_PROF", "db"); // name of default profile for main connection
+return defaultProfileName;
+}
+
 static struct sqlProfile *sqlProfileClone(struct sqlProfile *o)
 /* clone profile object (does not include ->dbs) */
 {
 struct sqlProfile *sp;
 AllocVar(sp);
 sp->name     = cloneString(o->name);
 sp->host     = cloneString(o->host);
 sp->port     = o->port;
 sp->socket   = cloneString(o->socket);
 sp->user     = cloneString(o->user);
 sp->password = cloneString(o->password);
 sp->db       = cloneString(o->db);
 sp->key      = cloneString(o->key);
 sp->cert     = cloneString(o->cert);
 sp->ca       = cloneString(o->ca);
@@ -199,60 +208,60 @@
 struct sqlProfile *sp2 = hashFindVal(dbToProfile, db);
 if ((sp2 != NULL) && (sp2 != sp))
     errAbort("databases %s already associated with profile %s, trying to associated it with %s",
              db, sp2->name, sp->name);
 if (sp2 == NULL)
     {
     hashAdd(dbToProfile, db, sp);
     slSafeAddHead(&sp->dbs, slNameNew(db));
     }
 }
 
 static void sqlProfileCreate(struct sqlProfile *sp)
 /* create a profile and add to global data structures */
 {
 hashAdd(profiles, sp->name, sp);
-if (sameString(sp->name, defaultProfileName))
+if (sameString(sp->name, getDefaultProfileName()))
     defaultProfile = sp;  // save default
 }
 
 static void sqlProfileAddProfIf(char *profileName)
 /* check if a config prefix is a profile, and if so, add a
  * sqlProfile object for it if doesn't already exist. */
 {
 char *host = cfgOption2(profileName, "host");
 char *portstr = cfgOption2(profileName, "port");
 char *socket = cfgOption2(profileName, "socket");
 char *user = cfgOption2(profileName, "user");
 char *password = cfgOption2(profileName, "password");
 char *db = cfgOption2(profileName, "db");
 // ssl
 char *key = cfgOption2(profileName, "key");
 char *cert = cfgOption2(profileName, "cert");
 char *ca = cfgOption2(profileName, "ca");
 char *caPath = cfgOption2(profileName, "caPath");
 char *cipher = cfgOption2(profileName, "cipher");
 char *crl = cfgOption2(profileName, "crl");
 char *crlPath = cfgOption2(profileName, "crlPath");
 char *verifyServerCert = cfgOption2(profileName, "verifyServerCert");
 
 unsigned int port = 0;
 
 if ((host != NULL) && (user != NULL) && (password != NULL) && (hashLookup(profiles, profileName) == NULL))
     {
     /* for the default profile, allow environment variable override */
-    if (sameString(profileName, defaultProfileName))
+    if (sameString(profileName, getDefaultProfileName()))
         {
         host     = envOverride("HGDB_HOST", host);
         portstr  = envOverride("HGDB_PORT", portstr);
         socket   = envOverride("HGDB_SOCKET", socket);
         user     = envOverride("HGDB_USER", user);
         password = envOverride("HGDB_PASSWORD", password);
         db       = envOverride("HGDB_DB", db);
 	// ssl
 	key      = envOverride("HGDB_KEY", key);
 	cert     = envOverride("HGDB_CERT", cert);
 	ca       = envOverride("HGDB_CA", ca);
 	caPath   = envOverride("HGDB_CAPATH", caPath);
 	cipher   = envOverride("HGDB_CIPHER", cipher);
 	crl      = envOverride("HGDB_CRL", crl);
 	crlPath  = envOverride("HGDB_CRLPATH", crlPath);
@@ -460,33 +469,33 @@
 replaceStr(&sp->ca,       spIn->ca);
 replaceStr(&sp->caPath,   spIn->caPath);
 replaceStr(&sp->cipher,   spIn->cipher);
 replaceStr(&sp->crl,      spIn->crl);
 replaceStr(&sp->crlPath,  spIn->crlPath);
 replaceStr(&sp->verifyServerCert, spIn->verifyServerCert);
 }
 
 void sqlProfileConfigDefault(struct slPair *pairs)
 /* Set configuration for the default profile.  This overrides an existing
  * profile in hg.conf or defines a new one.  Results are unpredictable if a
  * connect cache has been established for this profile. */
 {
 struct slPair *found = slPairFind(pairs, "name");
 if (found)
-    found->val = defaultProfileName;
+    found->val = cloneString(getDefaultProfileName());
 else
-    slPairAdd(&pairs, "name", defaultProfileName);
+    slPairAdd(&pairs, "name", cloneString(getDefaultProfileName()));
 sqlProfileConfig(pairs);
 }
 
 char *sqlProfileToMyCnf(char *profileName)
 /* Read in profile named, 
  * and create a multi-line setting string usable in my.cnf files.  
  * Return Null if profile not found. */
 {
 struct sqlProfile *sp = sqlProfileGet(profileName, NULL);
 if (!sp)
     return NULL;
 struct dyString *dy = dyStringNew(256);
 if (sp->host)
     dyStringPrintf(dy, "host=%s\n", sp->host);
 if (sp->user)
@@ -2826,34 +2835,34 @@
     struct slName *db, *dbs = sqlGetAllDatabase(sc);
     for (db = dbs; db != NULL; db = db->next)
         hashAdd(databases, db->name, NULL);
     sqlDisconnect(&sc);
     slFreeList(&dbs);
     }
 }
 
 struct hash *sqlHashOfDatabases(void)
 /* Get hash table with names of all databases that are online. */
 {
 if (profiles == NULL)
     sqlProfileLoad();
 struct hash *databases = newHash(8);
 // add databases found using default profile
-addProfileDatabases(defaultProfileName, databases);
+addProfileDatabases(getDefaultProfileName(), databases);
 
 // add databases found in failover profile
-char *failoverProfName = catTwoStrings(failoverProfPrefix, defaultProfileName);
+char *failoverProfName = catTwoStrings(failoverProfPrefix, getDefaultProfileName());
 addProfileDatabases(failoverProfName, databases);
 freez(&failoverProfName);
 
 // add other databases explicitly associated with other profiles
 struct hashCookie cookie = hashFirst(dbToProfile);
 struct hashEl *hel;
 while ((hel = hashNext(&cookie)) != NULL)
     {
     char *db = ((struct sqlProfile*)hel->val)->name;
     hashAdd(databases, db, NULL);
     }
 return databases;
 }
 
 struct slName *sqlListOfDatabases(void)