d96cce85869936e100269b1b5859acef8e1fec80 hiram Fri Feb 15 11:22:27 2019 -0800 now dumping contents of any database table refs #18869 diff --git src/hg/hubApi/getData.c src/hg/hubApi/getData.c index 8ad6160..835e65f 100644 --- src/hg/hubApi/getData.c +++ src/hg/hubApi/getData.c @@ -1,19 +1,37 @@ /* manage endpoint /getData/ functions */ #include "dataApi.h" +static void tableDataOutput(struct sqlConnection *conn, struct jsonWrite *jw, char *query, char *table) +/* output the table data from the specified query string */ +{ +int columnCount = tableColumns(conn, jw, table); +jsonWriteListStart(jw, "trackData"); +struct sqlResult *sr = sqlGetResult(conn, query); +char **row = NULL; +while ((row = sqlNextRow(sr)) != NULL) + { + jsonWriteListStart(jw, NULL); + int i = 0; + for (i = 0; i < columnCount; ++i) + jsonWriteString(jw, NULL, row[i]); + jsonWriteListEnd(jw); + } +jsonWriteListEnd(jw); +} + static void getTrackData() /* return data from a track, optionally just one chrom data, * optionally just one section of that chrom data */ { char *db = cgiOptionalString("db"); char *chrom = cgiOptionalString("chrom"); char *start = cgiOptionalString("start"); char *end = cgiOptionalString("end"); char *table = cgiOptionalString("track"); /* 'track' name in trackDb refers to a SQL 'table' */ if (isEmpty(db)) apiErrAbort("missing URL db=<ucscDb> name for endpoint '/getData/track"); if (isEmpty(table)) @@ -22,50 +40,56 @@ if (! sqlTableExists(conn, table)) apiErrAbort("can not find specified 'track=%s' for endpoint: /getData/track?db=%s&track=%s", table, db, table); struct jsonWrite *jw = apiStartOutput(); jsonWriteString(jw, "db", db); jsonWriteString(jw, "track", table); char *dataTime = sqlTableUpdate(conn, table); time_t dataTimeStamp = sqlDateToUnixTime(dataTime); replaceChar(dataTime, ' ', 'T'); jsonWriteString(jw, "dataTime", dataTime); jsonWriteNumber(jw, "dataTimeStamp", (long long)dataTimeStamp); /* no chrom specified, return entire table */ if (isEmpty(chrom)) { - tableColumns(conn, jw, table); + char query[4096]; + sqlSafef(query, sizeof(query), "select * from %s", table); + tableDataOutput(conn, jw, query, table); } else if (isEmpty(start) || isEmpty(end)) { if (! sqlColumnExists(conn, table, "chrom")) apiErrAbort("track '%s' is not a position track, request table without chrom specification, genome: '%s'", table, db); jsonWriteString(jw, "chrom", chrom); struct chromInfo *ci = hGetChromInfo(db, chrom); jsonWriteNumber(jw, "start", (long long)0); jsonWriteNumber(jw, "end", (long long)ci->size); - tableColumns(conn, jw, table); + char query[4096]; + sqlSafef(query, sizeof(query), "select * from %s where chrom='%s'", table, chrom); + tableDataOutput(conn, jw, query, table); } else { if (! sqlColumnExists(conn, table, "chrom")) apiErrAbort("track '%s' is not a position track, request table without chrom specification, genome: '%s'", table, db); jsonWriteString(jw, "chrom", chrom); jsonWriteNumber(jw, "start", (long long)sqlSigned(start)); jsonWriteNumber(jw, "end", (long long)sqlSigned(end)); - tableColumns(conn, jw, table); + char query[4096]; + sqlSafef(query, sizeof(query), "select * from %s where chrom='%s' AND chromEnd > %d AND chromStart < %d", table, chrom, sqlSigned(start), sqlSigned(end)); + tableDataOutput(conn, jw, query, table); } jsonWriteObjectEnd(jw); fputs(jw->dy->string,stdout); hFreeConn(&conn); } static void getSequenceData() /* return DNA sequence, given at least a db=name and chrom=chr, optionally start and end */ { char *db = cgiOptionalString("db"); char *chrom = cgiOptionalString("chrom"); char *start = cgiOptionalString("start"); char *end = cgiOptionalString("end");