IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 18, 2009, 10:23:28 AM (17 years ago)
Author:
eugene
Message:

updated vysos branch from trunk

Location:
branches/eam_branches/20090820
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/20090820

  • branches/eam_branches/20090820/ippTools/src/addtool.c

    r25766 r25870  
    4040static bool maskedMode(pxConfig *config);
    4141static bool unblockMode(pxConfig *config);
    42 static bool pendingcleanuprunMode(pxConfig *config);
    43 static bool pendingcleanupexpMode(pxConfig *config);
    44 static bool donecleanupMode(pxConfig *config);
    45 static bool exportrunMode(pxConfig *config);
    46 static bool importrunMode(pxConfig *config);
    4742
    4843# define MODECASE(caseName, func) \
     
    7469        MODECASE(ADDTOOL_MODE_MASKED,               maskedMode);
    7570        MODECASE(ADDTOOL_MODE_UNBLOCK,              unblockMode);
    76         MODECASE(ADDTOOL_MODE_PENDINGCLEANUPRUN,    pendingcleanuprunMode);
    77         MODECASE(ADDTOOL_MODE_PENDINGCLEANUPEXP,    pendingcleanupexpMode);
    78         MODECASE(ADDTOOL_MODE_DONECLEANUP,          donecleanupMode);
    79         MODECASE(ADDTOOL_MODE_EXPORTRUN,            exportrunMode);
    80         MODECASE(ADDTOOL_MODE_IMPORTRUN,            importrunMode);
    8171        default:
    8272            psAbort("invalid option (this should not happen)");
     
    10696
    10797    psMetadata *where = psMetadataAlloc();
    108     pxaddGetSearchArgs (config, where);
    109     pxAddLabelSearchArgs (config, where, "-label", "addRun.label", "==");
    110     PXOPT_COPY_STR(config->args, where, "-reduction", "addRun.reduction", "==");
     98    pxcamGetSearchArgs (config, where);
     99    PXOPT_COPY_S64(config->args, where,  "-cam_id",    "camRun.cam_id", "==");
     100    pxAddLabelSearchArgs (config, where, "-label",     "camRun.label", "==");
     101    PXOPT_COPY_STR(config->args, where,  "-reduction", "camRun.reduction", "==");
    111102
    112103    if (!psListLength(where->list) &&
     
    117108    }
    118109
    119     PXOPT_LOOKUP_STR(workdir, config->args, "-set_workdir", false, false);
    120     PXOPT_LOOKUP_STR(label, config->args, "-set_label", false, false);
    121     PXOPT_LOOKUP_STR(reduction, config->args, "-set_reduction", false, false);
    122     PXOPT_LOOKUP_STR(dvodb, config->args, "-set_dvodb", false, false);
     110    PXOPT_LOOKUP_STR(workdir,     config->args, "-set_workdir", false, false);
     111    PXOPT_LOOKUP_STR(dvodb,       config->args, "-set_dvodb", false, false);
     112    PXOPT_LOOKUP_STR(label,       config->args, "-set_label", false, false);
     113    PXOPT_LOOKUP_STR(data_group,  config->args, "-set_data_group", false, false);
     114    PXOPT_LOOKUP_STR(reduction,   config->args, "-set_reduction", false, false);
     115    PXOPT_LOOKUP_STR(note,        config->args, "-set_note", false, false);
     116    PXOPT_LOOKUP_BOOL(image_only, config->args, "-image_only", false);
     117    PXOPT_LOOKUP_BOOL(pretend,    config->args, "-pretend", false);
     118    PXOPT_LOOKUP_BOOL(simple,     config->args, "-simple", false);
    123119
    124120    // find the cam_id of all the exposures that we want to queue up.
     
    130126    }
    131127
    132     // use psDBGenerateWhereSQL because the SQL yields an intermediate table
     128    // use psDBGenerateWhereConditionSQL because the SQL ends in a WHERE
    133129    if (where && psListLength(where->list)) {
    134130        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     
    136132        psFree(whereClause);
    137133    }
    138 
    139134    psFree(where);
    140135
     
    155150        psFree(output);
    156151        return true;
     152    }
     153
     154    if (pretend) {
     155        // negative simple so the default is true
     156        if (!ippdbPrintMetadatas(stdout, output, "addRun", !simple)) {
     157            psError(PS_ERR_UNKNOWN, false, "failed to print array");
     158            psFree(output);
     159            return false;
     160        }
     161        psFree(output);
     162        return true;
     163    }
     164
     165    // loop over our list of camRun rows to check the supplied and selected dvodb and workdir values:
     166    for (long i = 0; i < psArrayLength(output); i++) {
     167        psMetadata *md = output->data[i];
     168
     169        camRunRow *row = camRunObjectFromMetadata(md);
     170        if (!row) {
     171            psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into camRun");
     172            psFree(output);
     173            return false;
     174        }
     175
     176        if (!dvodb && !row->dvodb) {
     177            psError(PS_ERR_UNKNOWN, false, "cannot queue addstar run without a defined dvodb: label: %s, cam_id %" PRId64, row->label, row->cam_id);
     178            psFree(output);
     179            return false;
     180        }
     181        if (!workdir && !row->workdir) {
     182            psError(PS_ERR_UNKNOWN, false, "cannot queue addstar run without a defined workdir: label: %s, cam_id %" PRId64, row->label, row->cam_id);
     183            psFree(output);
     184            return false;
     185        }
     186
     187        psFree(row);
    157188    }
    158189
     
    170201    // old values in place (i.e., passing the values through).
    171202
    172 
    173     // loop over our list of addRun rows
     203    // loop over our list of camRun rows
    174204    for (long i = 0; i < psArrayLength(output); i++) {
    175205        psMetadata *md = output->data[i];
    176206
    177         addRunRow *row = addRunObjectFromMetadata(md);
     207        camRunRow *row = camRunObjectFromMetadata(md);
    178208        if (!row) {
    179             psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into addRun");
     209            psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into camRun");
    180210            psFree(output);
    181211            return false;
     
    186216                               row->cam_id,
    187217                               workdir     ? workdir   : row->workdir,
     218                               reduction   ? reduction : row->reduction,
    188219                               label       ? label     : row->label,
    189                                "RECIPE",
    190                                dvodb       ? dvodb     : row->dvodb
    191                                
     220                               data_group  ? data_group : (label ? label : row->label),
     221                               dvodb       ? dvodb     : row->dvodb,
     222                               note        ? note      : NULL,
     223                               image_only
    192224        )) {
    193225            if (!psDBRollback(config->dbh)) {
     
    218250
    219251    psMetadata *where = psMetadataAlloc();
    220     pxaddGetSearchArgs (config, where);
    221252    PXOPT_COPY_S64(config->args, where, "-add_id",    "addRun.add_id", "==");
     253    PXOPT_COPY_S64(config->args, where, "-cam_id",    "camRun.cam_id", "==");
     254    pxcamGetSearchArgs (config, where); // most search arguments based on camera
    222255    PXOPT_COPY_STR(config->args, where, "-label",     "addRun.label", "==");
    223256    PXOPT_COPY_STR(config->args, where, "-state",     "addRun.state", "==");
    224257    PXOPT_COPY_STR(config->args, where, "-reduction", "addRun.reduction", "==");
    225258
    226     if (!psListLength(where->list)
    227         && !psMetadataLookupBool(NULL, config->args, "-all")) {
     259    if (!psListLength(where->list)) {
    228260        psFree(where);
    229261        psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
     
    231263    }
    232264
     265    psString query = psStringCopy("UPDATE addRun JOIN camRun USING(cam_id) JOIN chipRun USING(chip_id) JOIN rawExp USING(exp_id)");
     266
     267    // pxUpdateRun gets parameters from config->args and runs the update query
     268    bool result = pxUpdateRun(config, where, &query, "addRun", false);
     269
     270    psFree(query);
     271    psFree(where);
     272
     273    return result;
     274
     275
     276#ifdef notdef
    233277    PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, false);
    234278    PXOPT_LOOKUP_STR(label, config->args, "-set_label", false, false);
     
    259303
    260304    return true;
     305#endif
    261306}
    262307
     
    267312
    268313    psMetadata *where = psMetadataAlloc();
    269     pxaddGetSearchArgs (config, where);
    270314    PXOPT_COPY_S64(config->args, where, "-add_id",    "addRun.add_id", "==");
     315    PXOPT_COPY_S64(config->args, where, "-cam_id",    "camRun.cam_id", "==");
     316    pxcamGetSearchArgs (config, where);
    271317    pxAddLabelSearchArgs (config, where, "-label", "addRun.label", "==");
    272318    PXOPT_COPY_STR(config->args, where, "-reduction", "addRun.reduction", "==");
    273 
    274319    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
    275320    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     
    325370}
    326371
    327 /* static bool pendingimfileMode(pxConfig *config) */
    328 /* { */
    329 /*     PS_ASSERT_PTR_NON_NULL(config, false); */
    330 
    331 /*     psMetadata *where = psMetadataAlloc(); */
    332 /*     pxaddGetSearchArgs (config, where); */
    333 /*     PXOPT_COPY_S64(config->args, where, "-add_id",    "addRun.add_id",                "=="); */
    334 /*     pxAddLabelSearchArgs (config, where, "-label",    "addRun.label",                 "=="); */
    335 /*     PXOPT_COPY_STR(config->args, where, "-reduction", "addRun.reduction",             "=="); */
    336 /*     PXOPT_COPY_S64(config->args, where, "-chip_id",   "addRun.chip_id",              "=="); */
    337 /*     PXOPT_COPY_STR(config->args, where, "-class_id",  "addProcessedExp.class_id", "=="); */
    338 
    339 /*     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false); */
    340 
    341 /*     psString query = pxDataGet("addtool_find_pendingimfile.sql"); */
    342 /*     if (!query) { */
    343 /*         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement"); */
    344 /*         return false; */
    345 /*     } */
    346 
    347 /*     // use psDBGenerateWhereSQL because the SQL yields an intermediate table */
    348 /*     if (psListLength(where->list)) { */
    349 /*         psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); */
    350 /*         psStringAppend(&query, " AND %s", whereClause); */
    351 /*         psFree(whereClause); */
    352 /*     } */
    353 /*     psFree(where); */
    354 
    355 /*     if (!p_psDBRunQuery(config->dbh, query)) { */
    356 /*         psError(PS_ERR_UNKNOWN, false, "database error"); */
    357 /*         psFree(query); */
    358 /*         return false; */
    359 /*     } */
    360 /*     psFree(query); */
    361 
    362 /*     psArray *output = p_psDBFetchResult(config->dbh); */
    363 /*     if (!output) { */
    364 /*         psError(PS_ERR_UNKNOWN, false, "database error"); */
    365 /*         return false; */
    366 /*     } */
    367 /*     if (!psArrayLength(output)) { */
    368 /*         psTrace("addtool", PS_LOG_INFO, "no rows found"); */
    369 /*         psFree(output); */
    370 /*         return true; */
    371 /*     } */
    372 
    373 /*     // negate simple so the default is true */
    374 /*     if (!ippdbPrintMetadatas(stdout, output, "addProcessedExp", !simple)) { */
    375 /*         psError(PS_ERR_UNKNOWN, false, "failed to print array"); */
    376 /*         psFree(output); */
    377 /*         return false; */
    378 /*     } */
    379 
    380 /*     psFree(output); */
    381 
    382 /*     return true; */
    383 /* } */
    384 
    385372static bool addprocessedexpMode(pxConfig *config)
    386373{
     
    391378
    392379    // optional
    393     PXOPT_LOOKUP_F32(dtime_addstar, config->args,  "-dtime_addstar", false, false);
    394 
    395     PXOPT_LOOKUP_S32(n_stars, config->args,        "-n_stars", false, false);
    396 
    397     PXOPT_LOOKUP_STR(path_base, config->args, "-path_base", false, false);
    398 
    399     PXOPT_LOOKUP_S64(magicked, config->args, "-magicked", false, false);
     380    PXOPT_LOOKUP_STR(path_base,     config->args, "-path_base", false, false);
     381    PXOPT_LOOKUP_F32(dtime_addstar, config->args, "-dtime_addstar", false, false);
     382    PXOPT_LOOKUP_S16(fault,         config->args, "-fault", false, false);
    400383
    401384    // generate restrictions
     
    446429        pendingRow->add_id,
    447430        dtime_addstar,
    448         n_stars,
    449431        path_base,
    450         0
     432        fault
    451433        );
    452434
     
    463445
    464446    // since there is only one exp per 'new' set addRun.state = 'full'
    465     if (!pxaddRunSetState(config, row->add_id, "full", magicked)) {
     447    if (!pxaddRunSetState(config, row->add_id, "full")) {
    466448        psError(PS_ERR_UNKNOWN, false, "failed to change addRun.state for add_id: %" PRId64, row->add_id);
    467449        psFree(row);
     
    469451        return false;
    470452    }
    471 
    472     // NULL for end_stage means go as far as possible
    473     // EAM : skip here if fault != 0
    474     // Also, we can run fake even if tess_id is not defined
    475 /*     if (fault || (pendingRow->end_stage && psStrcasestr(pendingRow->end_stage, "add"))) { */
    476 /*         psFree(row); */
    477 /*         psFree(pendingRow); */
    478 /*         if (!psDBCommit(config->dbh)) { */
    479 /*             psError(PS_ERR_UNKNOWN, false, "database error"); */
    480 /*             return false; */
    481 /*         } */
    482 /*         return true; */
    483 /*     } */
    484453    psFree(row);
    485     // else continue on...
    486 
    487 /*     if (!pxfakeQueueByAddID(config, */
    488 /*             pendingRow->add_id, */
    489 /*             pendingRow->workdir, */
    490 /*             pendingRow->label, */
    491 /*             pendingRow->reduction, */
    492 /*             pendingRow->expgroup, */
    493 /*             pendingRow->dvodb, */
    494 /*             pendingRow->tess_id, */
    495 /*             pendingRow->end_stage */
    496 /*     )) { */
    497 /*         // rollback */
    498 /*         if (!psDBRollback(config->dbh)) { */
    499 /*             psError(PS_ERR_UNKNOWN, false, "database error"); */
    500 /*         } */
    501 /*         psError(PS_ERR_UNKNOWN, false, "failed to queue new fakeRun"); */
    502 /*         psFree(pendingRow); */
    503 /*         return false; */
    504 /*     } */
    505454    psFree(pendingRow);
    506455
     
    517466{
    518467    PS_ASSERT_PTR_NON_NULL(config, false);
     468
     469    // generate restrictions
     470    psMetadata *where = psMetadataAlloc();
     471    PXOPT_COPY_S64(config->args, where, "-add_id",    "addRun.add_id",    "==");
     472    PXOPT_COPY_S64(config->args, where, "-cam_id",    "camRun.cam_id",    "==");
     473    pxcamGetSearchArgs (config, where);
     474    pxAddLabelSearchArgs (config, where, "-label",    "addRun.label",     "==");
     475    PXOPT_COPY_STR(config->args, where, "-reduction", "addRun.reduction", "==");
    519476
    520477    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
    521478    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    522479    PXOPT_LOOKUP_BOOL(faulted, config->args, "-faulted", false);
    523 
    524     // generate restrictions
    525     psMetadata *where = psMetadataAlloc();
    526     pxaddGetSearchArgs (config, where);
    527     PXOPT_COPY_S64(config->args, where, "-add_id",    "addRun.add_id",    "==");
    528     pxAddLabelSearchArgs (config, where, "-label",    "addRun.label",     "==");
    529     PXOPT_COPY_STR(config->args, where, "-reduction", "addRun.reduction", "==");
    530480
    531481    if (!psListLength(where->list) &&
     
    613563
    614564    psMetadata *where = psMetadataAlloc();
    615     pxaddGetSearchArgs (config, where);
    616565    PXOPT_COPY_S64(config->args, where, "-add_id",    "addRun.add_id",         "==");
     566    PXOPT_COPY_S64(config->args, where, "-cam_id",    "camRun.cam_id",         "==");
     567    pxcamGetSearchArgs (config, where);
    617568    pxAddLabelSearchArgs (config, where, "-label",    "addRun.label",     "==");
    618569    PXOPT_COPY_STR(config->args, where, "-reduction", "addRun.reduction",      "==");
    619 /*     PXOPT_COPY_S16(config->args, where, "-fault", "addProcessedExp.fault", "=="); */
    620570
    621571    if (!psListLength(where->list) && !psMetadataLookupBool(NULL, config->args, "-all")) {
     
    705655    PS_ASSERT_PTR_NON_NULL(config, false);
    706656
    707     PXOPT_LOOKUP_S16(fault, config->args, "-fault", true, false);
    708 
    709657    psMetadata *where = psMetadataAlloc();
    710658    PXOPT_COPY_S64(config->args, where, "-add_id",   "add_id",   "==");
    711659    PXOPT_COPY_S64(config->args, where, "-cam_id",  "cam_id",  "==");
    712 /*     PXOPT_COPY_STR(config->args, where, "-class",    "class",    "=="); */
    713 /*     PXOPT_COPY_STR(config->args, where, "-class_id", "class_id", "=="); */
     660    PXOPT_LOOKUP_S16(fault, config->args, "-fault", true, false);
    714661
    715662    if (!pxSetFaultCode(config->dbh, "addProcessedExp", where, fault)) {
     
    777724}
    778725
    779 
    780726static bool unblockMode(pxConfig *config)
    781727{
     
    793739    return true;
    794740}
    795 
    796 static bool pendingcleanuprunMode(pxConfig *config)
    797 {
    798     PS_ASSERT_PTR_NON_NULL(config, NULL);
    799 
    800     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
    801     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    802 
    803     psMetadata *where = psMetadataAlloc();
    804     pxAddLabelSearchArgs (config, where, "-label", "addRun.label", "==");
    805 
    806     psString query = pxDataGet("addtool_pendingcleanuprun.sql");
    807     if (!query) {
    808         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    809         return false;
    810     }
    811 
    812     if (where && psListLength(where->list)) {
    813         psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    814         psStringAppend(&query, " AND %s", whereClause);
    815         psFree(whereClause);
    816     }
    817     psFree(where);
    818 
    819     // treat limit == 0 as "no limit"
    820     if (limit) {
    821         psString limitString = psDBGenerateLimitSQL(limit);
    822         psStringAppend(&query, " %s", limitString);
    823         psFree(limitString);
    824     }
    825 
    826     if (!p_psDBRunQuery(config->dbh, query)) {
    827         psError(PS_ERR_UNKNOWN, false, "database error");
    828         psFree(query);
    829         return false;
    830     }
    831     psFree(query);
    832 
    833     psArray *output = p_psDBFetchResult(config->dbh);
    834     if (!output) {
    835         psError(PS_ERR_UNKNOWN, false, "database error");
    836         return false;
    837     }
    838     if (!psArrayLength(output)) {
    839         psTrace("addtool", PS_LOG_INFO, "no rows found");
    840         psFree(output);
    841         return true;
    842     }
    843 
    844     // negative simple so the default is true
    845     if (!ippdbPrintMetadatas(stdout, output, "addPendingCleanupRun", !simple)) {
    846         psError(PS_ERR_UNKNOWN, false, "failed to print array");
    847         psFree(output);
    848         return false;
    849     }
    850 
    851     psFree(output);
    852 
    853     return true;
    854 }
    855 
    856 
    857 static bool pendingcleanupexpMode(pxConfig *config)
    858 {
    859     PS_ASSERT_PTR_NON_NULL(config, NULL);
    860 
    861     PXOPT_LOOKUP_S64(add_id, config->args, "-add_id", false, false);
    862     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
    863     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    864 
    865     psMetadata *where = psMetadataAlloc();
    866     if (add_id) {
    867         PXOPT_COPY_S64(config->args, where, "-add_id", "add_id", "==");
    868     }
    869     pxAddLabelSearchArgs (config, where, "-label", "label", "==");
    870 
    871     psString query = pxDataGet("addtool_pendingcleanupexp.sql");
    872     if (!query) {
    873         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    874         return false;
    875     }
    876 
    877     if (where && psListLength(where->list)) {
    878         psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    879         psStringAppend(&query, " AND %s", whereClause);
    880         psFree(whereClause);
    881     }
    882     psFree(where);
    883 
    884     // treat limit == 0 as "no limit"
    885     if (limit) {
    886         psString limitString = psDBGenerateLimitSQL(limit);
    887         psStringAppend(&query, " %s", limitString);
    888         psFree(limitString);
    889     }
    890 
    891     if (!p_psDBRunQuery(config->dbh, query)) {
    892         psError(PS_ERR_UNKNOWN, false, "database error");
    893         psFree(query);
    894         return false;
    895     }
    896     psFree(query);
    897 
    898     psArray *output = p_psDBFetchResult(config->dbh);
    899     if (!output) {
    900         psError(PS_ERR_UNKNOWN, false, "database error");
    901         return false;
    902     }
    903     if (!psArrayLength(output)) {
    904         psTrace("chiptool", PS_LOG_INFO, "no rows found");
    905         psFree(output);
    906         return true;
    907     }
    908 
    909     // negative simple so the default is true
    910     if (!ippdbPrintMetadatas(stdout, output, "addPendingCleanupExp", !simple)) {
    911         psError(PS_ERR_UNKNOWN, false, "failed to print array");
    912         psFree(output);
    913         return false;
    914     }
    915 
    916     psFree(output);
    917 
    918     return true;
    919 }
    920 
    921 
    922 static bool donecleanupMode(pxConfig *config)
    923 {
    924     PS_ASSERT_PTR_NON_NULL(config, NULL);
    925 
    926     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
    927     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    928 
    929     psMetadata *where = psMetadataAlloc();
    930     PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
    931 
    932     psString query = pxDataGet("addtool_donecleanup.sql");
    933     if (!query) {
    934         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    935         return false;
    936     }
    937 
    938     if (where && psListLength(where->list)) {
    939         psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    940         psStringAppend(&query, " AND %s", whereClause);
    941         psFree(whereClause);
    942     }
    943     psFree(where);
    944 
    945     // treat limit == 0 as "no limit"
    946     if (limit) {
    947         psString limitString = psDBGenerateLimitSQL(limit);
    948         psStringAppend(&query, " %s", limitString);
    949         psFree(limitString);
    950     }
    951 
    952     if (!p_psDBRunQuery(config->dbh, query)) {
    953         psError(PS_ERR_UNKNOWN, false, "database error");
    954         psFree(query);
    955         return false;
    956     }
    957     psFree(query);
    958 
    959     psArray *output = p_psDBFetchResult(config->dbh);
    960     if (!output) {
    961         psError(PS_ERR_UNKNOWN, false, "database error");
    962         return false;
    963     }
    964     if (!psArrayLength(output)) {
    965         psTrace("addtool", PS_LOG_INFO, "no rows found");
    966         psFree(output);
    967         return true;
    968     }
    969 
    970     // negative simple so the default is true
    971     if (!ippdbPrintMetadatas(stdout, output, "addDoneCleanup", !simple)) {
    972         psError(PS_ERR_UNKNOWN, false, "failed to print array");
    973         psFree(output);
    974         return false;
    975     }
    976 
    977     psFree(output);
    978 
    979     return true;
    980 }
    981 
    982 bool exportrunMode(pxConfig *config)
    983 {
    984   typedef struct ExportTable {
    985     char tableName[80];
    986     char sqlFilename[80];
    987   } ExportTable;
    988 
    989   int numExportTables = 2;
    990 
    991   PS_ASSERT_PTR_NON_NULL(config, NULL);
    992 
    993   PXOPT_LOOKUP_S64(det_id, config->args, "-add_id", true,  false);
    994   PXOPT_LOOKUP_STR(outfile, config->args, "-outfile", true,  false);
    995   PXOPT_LOOKUP_U64(limit,   config->args, "-limit",   false, false);
    996   PXOPT_LOOKUP_BOOL(clean, config->args, "-clean", false);
    997 
    998   FILE *f = fopen (outfile, "w");
    999   if (f == NULL) {
    1000     psError(PS_ERR_UNKNOWN, false, "failed to open output file");
    1001     return false;
    1002   }
    1003 
    1004   psMetadata *where = psMetadataAlloc();
    1005   PXOPT_COPY_S64(config->args, where, "-add_id", "add_id", "==");
    1006 
    1007   ExportTable tables [] = {
    1008     {"addRun", "addtool_export_run.sql"},
    1009     {"addProcessedExp", "addtool_export_processed_exp.sql"},
    1010   };
    1011 
    1012   for (int i=0; i < numExportTables; i++) {
    1013     psString query = pxDataGet(tables[i].sqlFilename);
    1014     if (!query) {
    1015       psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    1016       return false;
    1017     }
    1018 
    1019     if (where && psListLength(where->list)) {
    1020       psString whereClause = psDBGenerateWhereSQL(where, NULL);
    1021       psStringAppend(&query, " %s", whereClause);
    1022       psFree(whereClause);
    1023     }
    1024 
    1025     // treat limit == 0 as "no limit"
    1026     if (limit) {
    1027       psString limitString = psDBGenerateLimitSQL(limit);
    1028       psStringAppend(&query, " %s", limitString);
    1029       psFree(limitString);
    1030     }
    1031 
    1032     if (!p_psDBRunQuery(config->dbh, query)) {
    1033       psError(PS_ERR_UNKNOWN, false, "database error");
    1034       psFree(query);
    1035       return false;
    1036     }
    1037     psFree(query);
    1038 
    1039     psArray *output = p_psDBFetchResult(config->dbh);
    1040     if (!output) {
    1041       psError(PS_ERR_UNKNOWN, false, "database error");
    1042       return false;
    1043     }
    1044     if (!psArrayLength(output)) {
    1045       psError(PS_ERR_UNKNOWN, true, "no rows found");
    1046       psFree(output);
    1047       return false;
    1048     }
    1049 
    1050     if (clean) {
    1051         if (!strcmp(tables[i].tableName, "addRun")) {
    1052             if (!pxSetStateCleaned("addRun", "state", output)) {
    1053                 psFree(output);
    1054                 psError(PS_ERR_UNKNOWN, false, "pxSetStateClean failed for table %s",  tables[i].tableName);
    1055                 return false;
    1056             }
    1057         }
    1058     }
    1059 
    1060     // we must write the export table in non-simple (true) format
    1061     if (!ippdbPrintMetadatas(f, output, tables[i].tableName, true)) {
    1062       psError(PS_ERR_UNKNOWN, false, "failed to print array");
    1063       psFree(output);
    1064       return false;
    1065     }
    1066     psFree(output);
    1067   }
    1068 
    1069   fclose (f);
    1070 
    1071   return true;
    1072 }
    1073 
    1074 bool importrunMode(pxConfig *config)
    1075 {
    1076   unsigned int nFail;
    1077 
    1078   PS_ASSERT_PTR_NON_NULL(config, NULL);
    1079 
    1080   PXOPT_LOOKUP_STR(infile, config->args, "-infile", true,  false);
    1081 
    1082   psMetadata *input = psMetadataConfigRead (NULL, &nFail, infile, false);
    1083 
    1084   fprintf (stdout, "---- input ----\n");
    1085   psMetadataPrint (stderr, input, 1);
    1086 
    1087   psMetadataItem *item = psMetadataLookup (input, "addRun");
    1088   psAssert (item, "entry not in input?");
    1089   psAssert (item->type == PS_DATA_METADATA_MULTI, "entry not multi?");
    1090 
    1091   psMetadataItem *entry = psListGet (item->data.list, 0);
    1092   assert (entry);
    1093   assert (entry->type == PS_DATA_METADATA);
    1094   addRunRow *addRun = addRunObjectFromMetadata (entry->data.md);
    1095   addRunInsertObject (config->dbh, addRun);
    1096 
    1097   // fprintf (stdout, "---- add run ----\n");
    1098   // psMetadataPrint (stderr, entry->data.md, 1);
    1099 
    1100   item = psMetadataLookup (input, "addProcessedExp");
    1101   psAssert (item, "entry not in input?");
    1102   psAssert (item->type == PS_DATA_METADATA_MULTI, "entry not multi?");
    1103 
    1104   for (int i = 0; i < item->data.list->n; i++) {
    1105     psMetadataItem *entry = psListGet (item->data.list, i);
    1106     assert (entry);
    1107     assert (entry->type == PS_DATA_METADATA);
    1108     addProcessedExpRow *addProcessedExp = addProcessedExpObjectFromMetadata (entry->data.md);
    1109     addProcessedExpInsertObject (config->dbh, addProcessedExp);
    1110 
    1111     // fprintf (stdout, "---- row %d ----\n", i);
    1112     // psMetadataPrint (stderr, entry->data.md, 1);
    1113   }
    1114 
    1115   return true;
    1116 }
Note: See TracChangeset for help on using the changeset viewer.