45990976617dde97993635e7a28dd34dfcce56d2
angie
  Mon Apr 20 15:46:01 2015 -0700
cleanup: variable renaming, getting rid of unnecessary second conn,
use bigDataUrl if it's present regardless of hub/db, use existing
lib function instead of ad-hoc copy-paste.

diff --git src/hg/lib/hAnno.c src/hg/lib/hAnno.c
index 423f0df..ed6ba07 100644
--- src/hg/lib/hAnno.c
+++ src/hg/lib/hAnno.c
@@ -92,60 +92,57 @@
         continue;
     char *sqlType = field->type;
     // hg19.wgEncodeOpenChromSynthGm12878Pk.pValue has sql type "float unsigned",
     // and I'd rather pretend it's just a float than work unsigned floats into autoSql.
     if (sameString(sqlType, "float unsigned"))
 	sqlType = "float";
     char *asType = asTypeNameFromSqlType(sqlType);
     if (asType == NULL)
 	errAbort("No asTypeInfo for sql type '%s'!", field->type);
     dyStringPrintf(dy, "    %s %s;\t\"\"\n", asType, field->field);
     }
 dyStringAppend(dy, "    )\n");
 return asParseText(dy->string);
 }
 
-static struct asObject *getAutoSqlForTable(char *dataDb, char *dbTable, struct trackDb *tdb,
+static struct asObject *getAutoSqlForTable(char *db, char *table, struct trackDb *tdb,
                                            boolean skipBin)
-/* Get autoSql for dataDb.dbTable from tdb and/or db.tableDescriptions;
- * if it doesn't match columns, make one up from dataDb.table sql fields.
+/* Get autoSql for db.dbTable from tdb and/or db.tableDescriptions;
+ * if it doesn't match columns, make one up from db.table sql fields.
  * Some subtleties are lost in translation from .as to .sql, that's why
  * we try tdb & db.tableDescriptions first.  But ultimately we need to return
  * an asObj whose columns match all fields of the table. */
 {
-struct sqlConnection *connDataDb = hAllocConn(dataDb);
-struct sqlFieldInfo *fieldList = sqlFieldInfoGet(connDataDb, dbTable);
-hFreeConn(&connDataDb);
+struct sqlConnection *conn = hAllocConn(db);
+struct sqlFieldInfo *fieldList = sqlFieldInfoGet(conn, table);
 struct asObject *asObj = NULL;
 if (tdb != NULL)
-    {
-    struct sqlConnection *connDb = hAllocConn(dataDb);
-    asObj = asForTdb(connDb, tdb);
-    hFreeConn(&connDb);
-    }
+    asObj = asForTdb(conn, tdb);
+hFreeConn(&conn);
 if (columnsMatch(asObj, fieldList))
     return asObj;
 else
-    return asObjectFromFields(dbTable, fieldList, skipBin);
+    return asObjectFromFields(table, fieldList, skipBin);
 }
 
 static char *getBigDataFileName(char *db, struct trackDb *tdb, char *selTable, char *chrom)
 /* Get fileName from bigBed/bigWig/BAM/VCF database table, or bigDataUrl from custom track. */
 {
-if (trackHubDatabase(db))
+char *bigDataUrl = trackDbSetting(tdb, "bigDataUrl");
+if (isNotEmpty(bigDataUrl))
     {
-    return trackDbSetting(tdb, "bigDataUrl");
+    return bigDataUrl;
     }
 else
     {
     struct sqlConnection *conn = hAllocConn(db);
     char *fileOrUrl = bbiNameFromSettingOrTableChrom(tdb, conn, selTable, chrom);
     hFreeConn(&conn);
     return fileOrUrl;
     }
 }
 
 struct annoStreamer *hAnnoStreamerFromTrackDb(struct annoAssembly *assembly, char *selTable,
                                               struct trackDb *tdb, char *chrom, int maxOutRows)
 /* Figure out the source and type of data and make an annoStreamer. */
 {
 struct annoStreamer *streamer = NULL;
@@ -189,38 +186,33 @@
     {
     char *sourceTable = trackDbSetting(tdb, "sourceTable");
     char *inputsTable = trackDbSetting(tdb, "inputTrackTable");
     streamer = annoStreamDbFactorSourceNew(dataDb, tdb->track, sourceTable, inputsTable, assembly,
 					   maxOutRows);
     }
 else if (sameString("knownGene", tdb->track))
     {
     struct sqlConnection *conn = hAllocConn(dataDb);
     if (sqlTableExists(conn, "knownGene") && sqlTableExists(conn, "kgXref"))
         streamer = annoStreamDbKnownGeneNew(dataDb, assembly, maxOutRows);
     hFreeConn(&conn);
     }
 if (streamer == NULL)
     {
-    struct sqlConnection *conn = hAllocConn(dataDb);
-    char maybeSplitTable[1024];
-    if (sqlTableExists(conn, dbTable))
-	safecpy(maybeSplitTable, sizeof(maybeSplitTable), dbTable);
-    else
-	safef(maybeSplitTable, sizeof(maybeSplitTable), "%s_%s", chrom, dbTable);
-    hFreeConn(&conn);
-    struct asObject *asObj = getAutoSqlForTable(dataDb, maybeSplitTable, tdb, TRUE);
+    char maybeSplitTable[HDB_MAX_TABLE_STRING];
+    (void)hFindSplitTable(db, chrom, selTable, maybeSplitTable, NULL);
+    struct asObject *asObj = getAutoSqlForTable(dataDb, selTable, tdb, TRUE);
     streamer = annoStreamDbNew(dataDb, maybeSplitTable, assembly, asObj, maxOutRows);
     }
 return streamer;
 }
 
 struct annoGrator *hAnnoGratorFromBigFileUrl(char *fileOrUrl, struct annoAssembly *assembly,
                                              int maxOutRows, enum annoGratorOverlap overlapRule)
 /* Determine what kind of big data file/url we have and make streamer & grator for it. */
 {
 struct annoStreamer *streamer = NULL;
 struct annoGrator *grator = NULL;
 char *type = customTrackTypeFromBigFile(fileOrUrl);
 if (sameString(type, "bigBed"))
     streamer = annoStreamBigBedNew(fileOrUrl, assembly, maxOutRows);
 else if (sameString(type, "vcfTabix"))
@@ -307,27 +299,22 @@
 {
 struct asObject *asObj = getAutoSqlForType(db, chrom, tdb);
 
 if (!asObj && !isHubTrack(tdb->track))
     {
     // If none of the above, it must be a database table; deduce autoSql from sql fields.
     char *dataDb = db, *dbTable = tdb->table;
     if (isCustomTrack(tdb->track))
         {
         dbTable = trackDbSetting(tdb, "dbTableName");
         if (dbTable)
             dataDb = CUSTOM_TRASH;
         else
             return NULL;
         }
-    struct sqlConnection *conn = hAllocConn(dataDb);
-    char maybeSplitTable[1024];
-    if (sqlTableExists(conn, dbTable))
-	safecpy(maybeSplitTable, sizeof(maybeSplitTable), dbTable);
-    else
-	safef(maybeSplitTable, sizeof(maybeSplitTable), "%s_%s", chrom, dbTable);
-    hFreeConn(&conn);
+    char maybeSplitTable[HDB_MAX_TABLE_STRING];
+    (void)hFindSplitTable(db, chrom, dbTable, maybeSplitTable, NULL);
     asObj = getAutoSqlForTable(dataDb, maybeSplitTable, tdb, TRUE);
     }
 return asObj;
 }