IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 8291


Ignore:
Timestamp:
Aug 11, 2006, 4:12:30 PM (20 years ago)
Author:
jhoblitt
Message:

implement -definebyquery

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippTools/src/dettool.c

    r8289 r8291  
    334334static bool definebyqueryMode(pxConfig *config)
    335335{
     336    bool status     = false;
     337
     338    PS_ASSERT_PTR_NON_NULL(config, false);
     339
     340    // what type of detRun is this?
     341    // det_type is required
     342    psString det_type = psMetadataLookupStr(&status, config->args, "-det_type");
     343    if (!status) {
     344        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -det_type");
     345        return false;
     346    }
     347    if (!det_type) {
     348        psError(PS_ERR_UNKNOWN, true, "-det_type is required");
     349        return false;
     350    }
     351
     352    psMetadata *where = psMetadataAlloc();
     353    {
     354        bool status = false;
     355        psString exp_type = psMetadataLookupStr(&status, config->args, "-exp_type");        if (!status) {
     356            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -exp_type");
     357            return false;
     358        }
     359        if (exp_type) {
     360            if (!psMetadataAddStr(where, PS_LIST_TAIL, "exp_type", 0, "==", exp_type)) {
     361                psError(PS_ERR_UNKNOWN, false, "failed to add item exp_type");
     362                psFree(where);
     363                return false;
     364            }
     365        }
     366        // map -inst -> camera
     367        psString camera = psMetadataLookupStr(&status, config->args, "-inst");
     368        if (!status) {
     369            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -inst");
     370            return false;
     371        }
     372        if (camera) {
     373            if (!psMetadataAddStr(where, PS_LIST_TAIL, "camera", 0, "==", camera)) {
     374                psError(PS_ERR_UNKNOWN, false, "failed to add item inst");
     375                psFree(where);
     376                return false;
     377            }
     378        }
     379        psString telescope = psMetadataLookupStr(&status, config->args, "-telescope");        if (!status) {
     380            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -telescope");
     381            return false;
     382        }
     383        if (telescope) {
     384            if (!psMetadataAddStr(where, PS_LIST_TAIL, "telescope", 0, "==", telescope)) {
     385                psError(PS_ERR_UNKNOWN, false, "failed to add item telescope");
     386                psFree(where);
     387                return false;
     388            }
     389        }
     390        psString filter = psMetadataLookupStr(&status, config->args, "-filter");
     391        if (!status) {
     392            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -filter");
     393            return false;
     394        }
     395        if (filter) {
     396            if (!psMetadataAddStr(where, PS_LIST_TAIL, "filter", 0, "==", filter)) {
     397                psError(PS_ERR_UNKNOWN, false, "failed to add item filter");
     398                psFree(where);
     399                return false;
     400            }
     401        }
     402        psString uri = psMetadataLookupStr(&status, config->args, "-uri");
     403        if (!status) {
     404            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -filter");
     405            return false;
     406        }
     407        if (uri) {
     408            if (!psMetadataAddStr(where, PS_LIST_TAIL, "uri", 0, "==", uri)) {
     409                psError(PS_ERR_UNKNOWN, false, "failed to add item uri");
     410                psFree(where);
     411                return false;
     412            }
     413        }
     414    }
     415    if (!psListLength(where->list)) {
     416        psFree(where);
     417        where = NULL;
     418    }
     419
     420    // search for rawDetrendExps with the specified options
     421    psArray *detrendExps = rawDetrendExpSelectRowObjects(config->dbh, where, 0);
     422    psFree(where);
     423    // make sure that we found at least one rawDetrendExp
     424    if (!detrendExps) {
     425        psError(PS_ERR_UNKNOWN, false, "no rawDetrendExp rows found");
     426        return false;
     427    }
     428
     429    // start a transaction so we don't end up with orphaned det_ids
     430    if (!psDBTransaction(config->dbh)) {
     431        psError(PS_ERR_UNKNOWN, false, "database error");
     432        psFree(detrendExps);
     433        return false;
     434    }
     435
     436    // grab the 'position' column's value as the new det_id
     437    // the first iteration is always 0
     438    // XXX the camera name is set from the first inputExp
     439    detRunInsert(config->dbh, 0, det_type);
     440    long det_id = psDBLastInsertID(config->dbh);
     441
     442    // create new detInputExp row(s) from the rawDetrendExp row(s)
     443    psArray *inputExps = psArrayAlloc(psArrayLength(detrendExps));
     444    for (long i = 0; i < psArrayLength(detrendExps); i++) {
     445        detInputExpRow *inputExp = rawDetrenTodetInputExpRow(
     446            detrendExps->data[i],
     447            det_id,
     448            0 // the first iteration is explicitly 0
     449        );
     450        psArrayAdd(inputExps, 0, inputExp);
     451        psFree(inputExp);
     452    }
     453
     454    psFree(detrendExps);
     455
     456    // insert detInputExp objects into the database
     457    if (!detInputExpInsertObjects(config->dbh, inputExps)) {
     458        psError(PS_ERR_UNKNOWN, false, "database error");
     459        // rollback
     460        if (!psDBRollback(config->dbh)) {
     461            psError(PS_ERR_UNKNOWN, false, "database error");
     462        }
     463        psFree(inputExps);
     464        return false;
     465    }
     466    psFree(inputExps);
     467
     468    // point of no return for det_id creation
     469    if (!psDBCommit(config->dbh)) {
     470        psError(PS_ERR_UNKNOWN, false, "database error");
     471        return false;
     472    }
     473
     474    bool simple = false;
     475    {
     476        bool status = false;
     477        simple = psMetadataLookupBool(&status, config->args, "-simple");
     478        if (!status) {
     479            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
     480            return false;
     481        }
     482    }
     483
     484    // print the new det_id
     485    psArray *detRuns = NULL;
     486    {
     487        psMetadata *where = psMetadataAlloc();
     488        // map det_id -> position
     489        psMetadataAddS32(where, PS_LIST_TAIL, "position", 0, "==", det_id);
     490        detRuns = psDBSelectRows(config->dbh, "detRun", where, 0);
     491        psFree(where);
     492    }
     493    if (!detRuns) {
     494        psError(PS_ERR_UNKNOWN, false, "can't find the detRun we just created");
     495        return false;
     496    }
     497    // sanity check results
     498    if (psArrayLength(detRuns) != 1) {
     499        psAbort(config->argv[0], "found more then one detRun matching det_id %ld(this should not happen)", det_id);
     500        return false;
     501    }
     502
     503    // map position -> det_id
     504    // but leave position in the metadata as ippdbPrintMetadatas() will strip it
     505    // for us
     506    for (long i = 0; i < psArrayLength(detRuns); i++) {
     507        bool status = false;
     508        psS32 position = psMetadataLookupS32(&status, detRuns->data[i], "position");
     509        if (!status) {
     510            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for position");
     511            psFree(detRuns);
     512            return false;
     513        }
     514        psMetadataAddS32(detRuns->data[i], PS_LIST_HEAD, "det_id", 0, NULL, position);
     515    }
     516
     517    // negative simple so the default is true
     518    if (!ippdbPrintMetadatas(stdout, detRuns, "detRun", !simple)) {
     519        psError(PS_ERR_UNKNOWN, false, "failed to print array");
     520        psFree(detRuns);
     521        return false;
     522    }
     523    psFree(detRuns);
     524
    336525    return true;
    337526}
Note: See TracChangeset for help on using the changeset viewer.