コード例 #1
0
ファイル: AstroCatalog.C プロジェクト: olebole/skycat
/*
 * Pass a query to the catalog and return the number of objects found.
 * Only the given columns are retrieved.
 *
 * Args:
 *     q -        (in)   object describing the query
 *
 *     filename - (in)   filename to hold results, or null
 *
 *     result -   (out)  reference to object used to access the results
 *
 * The return value is the number of rows found, or 0 if none were found.
 * A return value of -1 indicates an error.
 */
int AstroCatalog::query(const AstroQuery& q, const char* filename, QueryResult& result)
{
    if (! isCatalog(entry_))
        return wrongServType(entry_);

    // generate the URL for a standard query in buf (using ostringstream)
    char* result_buf =  NULL;
    int nlines = 0;

    // if the first URL doesn't work, try the others, if specified
    const char* urls[3];
    urls[0] = entry_->url();
    urls[1] = entry_->backup1();
    urls[2] = entry_->backup2();
    char url[10000];

    char* ctype = (char *)"";
    for (int i = 0; i < 3 && urls[i]; i++) {
        if (genHttpQuery(url, sizeof(url), q, urls[i]) != 0)
            return -1;

        // send the query
        result_buf =  http_.get(url, nlines);

        ctype = http_.content_type();
        if (!ctype)
            ctype = (char *)"";
        if (result_buf != NULL && strcmp(ctype, "text/html") != 0)
            break;
        // don't go to backup URL if it was a request for authorization
        if (http_.authorizationRequired())
            break;
    }
    if (result_buf == NULL)
        return -1;		// error in http get

    // check the Content-type of the return data
    if (strcmp(ctype, "text/html") == 0) {
        // most likely an error message
        http_.html_error(result_buf);
        return -1;
    }

    // note the catalog config entry in the results
    // (This contains important info, such as the location of the id, a and dec cols)
    result.entry(entry_, result_buf);

    if (result.init(result_buf) != 0)
        return -1;		// error

    // sort result ?
    // note: do this before truncating to maxRows to get correct results
    if (q.numSortCols())
        result.sort(q.numSortCols(), q.sortCols(), q.sortOrder());

    if (q.maxRows() && result.numRows() > q.maxRows()) {
        more_ = 1;
        result.numRows(q.maxRows());
    } else {
        more_ = 0;
    }

    // if we didn't already, note the catalog's column heading info
    if (info_.numCols() <= 0
            && info_.init(result.numCols(), result.colNames(), "", 1) != 0)
        return -1;

    if (filename && result.save(filename) != 0)
        return -1;

    return result.numRows();
}