15eb93c120512b8107f96f1d083663b37b13ef2e galt Wed Oct 25 14:55:58 2017 -0700 Fix problem with silently returning FALSE from sqlTableExists() when the error has nothing to do with whether the table exists or not. Reported by Hiram when using namedSession cleaner with custom trash db. diff --git src/hg/lib/jksql.c src/hg/lib/jksql.c index 592c773..134310b 100644 --- src/hg/lib/jksql.c +++ src/hg/lib/jksql.c @@ -1708,42 +1708,53 @@ return FALSE; } if (strchr(table,'-')) { return FALSE; // mysql does not allow tables with dash (-) so it will not be found. // hg/lib/hdb.c can generate an invalid table names with dashes while looking for split tables, // if the first chrom name has a dash in it. Examples found were: scaffold_0.1-193456 scaffold_0.1-13376 HERVE_a-int 1-1 // Assembly hubs also may have dashes in chrom names. } // use the table cache if we have one struct sqlConnection *cacheConn = sqlTableCacheFindConn(sc); if (cacheConn) return sqlTableCacheTableExists(cacheConn, table); +char *err; +unsigned int errNo; + sqlSafef(query, sizeof(query), "SELECT 1 FROM %-s LIMIT 0", sqlCkIl(table)); -if ((sr = sqlUseOrStore(sc, query, DEFAULTGETTER, FALSE)) == NULL) + +if ((sr = sqlGetResultExt(sc, query, &errNo, &err)) == NULL) { - if (!sc->failoverConn) + if (errNo == 1146) // table not found return FALSE; + if (sc->failoverConn) + { // if not found but we have a main connection, check the main connection, too - else if ((sr = sqlUseOrStore(sc->failoverConn, query, DEFAULTGETTER, FALSE)) == NULL) + if ((sr = sqlGetResultExt(sc->failoverConn, query, &errNo, &err)) == NULL) + { + if (errNo == 1146) // table not found return FALSE; } -// TODO consider using sqlGetResultExt or something that would -// allow you to abort on all errors except the actual table not found: -// ERROR 1146 (42S02): Table 'hg19.chr_est' doesn't exist + } + } + +if (!sr) + errAbort("Mysql error during sqlTableExists(%s) %d: %s", table, errNo, err); + sqlFreeResult(&sr); return TRUE; } bool sqlColumnExists(struct sqlConnection *conn, char *tableName, char *column) /* return TRUE if column exists in table. tableName can contain sql wildcards */ { char query[1024]; sqlSafef(query, 1024, "SHOW COLUMNS FROM `%s` LIKE '%s'", tableName, column); char buf[1024]; char *ret = sqlQuickQuery(conn, query, buf, 1024); return (ret!=NULL); } int sqlTableSizeIfExists(struct sqlConnection *sc, char *table)