IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 3, 2010, 8:41:49 AM (16 years ago)
Author:
eugene
Message:

updates from trunk

Location:
branches/tap_branches
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/tap_branches

  • branches/tap_branches/ippTools/src

    • Property svn:ignore
      •  

        old new  
        1616difftool
        1717disttool
         18dqstatstool
        1819faketool
        1920flatcorr
  • branches/tap_branches/ippTools/src/magicdstool.c

    r25837 r27838  
    3333
    3434static bool definebyqueryMode(pxConfig *config);
     35static bool definecopyMode(pxConfig *config);
    3536static bool updaterunMode(pxConfig *config);
    3637static bool todestreakMode(pxConfig *config);
     
    3839static bool advancerunMode(pxConfig *config);
    3940static bool revertdestreakedfileMode(pxConfig *config);
     41static bool clearstatefaultsMode(pxConfig *config);
    4042static bool getskycellsMode(pxConfig *config);
    4143static bool toremoveMode(pxConfig *config);
     
    4446static bool tocleanupMode(pxConfig *config);
    4547
    46 static bool setmagicDSRunState(pxConfig *config, psS64 magic_id, psMetadata *where, const char *state);
     48static bool setmagicDSRunState(pxConfig *config, psS64 magic_id, psString extraSetString, psMetadata *where, const char *state);
    4749
    4850# define MODECASE(caseName, func) \
     
    6567    switch (config->mode) {
    6668        MODECASE(MAGICDSTOOL_MODE_DEFINEBYQUERY,       definebyqueryMode);
     69        MODECASE(MAGICDSTOOL_MODE_DEFINECOPY,          definecopyMode);
    6770        MODECASE(MAGICDSTOOL_MODE_UPDATERUN,           updaterunMode);
    6871        MODECASE(MAGICDSTOOL_MODE_TODESTREAK,          todestreakMode);
     
    7073        MODECASE(MAGICDSTOOL_MODE_ADVANCERUN,          advancerunMode);
    7174        MODECASE(MAGICDSTOOL_MODE_REVERTDESTREAKEDFILE,revertdestreakedfileMode);
     75        MODECASE(MAGICDSTOOL_MODE_CLEARSTATEFAULTS,    clearstatefaultsMode);
    7276        MODECASE(MAGICDSTOOL_MODE_GETSKYCELLS,         getskycellsMode);
    7377        MODECASE(MAGICDSTOOL_MODE_TOREMOVE,            toremoveMode);
     
    110114    PXOPT_LOOKUP_STR(note, config->args, "-set_note", false, false);
    111115    PXOPT_LOOKUP_BOOL(rerun, config->args, "-rerun", false);
    112     PXOPT_LOOKUP_BOOL(dry_run, config->args, "-dry_run", false);
     116    PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
    113117    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    114118    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     
    124128    PXOPT_COPY_S32(config->args, where, "-streaks_max","streaks", "<=");
    125129
    126     pxAddLabelSearchArgs (config, where, "-label", "magicRun.label", "==");
     130    pxAddLabelSearchArgs (config, where, "-label", "magicRun.label", "=="); // define using magicRun label
    127131
    128132    ippStage stageNum = ippStringToStage(stage);
    129    
     133
    130134    psString query = NULL;
    131135    switch (stageNum) {
     
    147151    case IPP_STAGE_FAKE:
    148152    case IPP_STAGE_STACK:
    149         psError(PXTOOLS_ERR_DATA, true, "%sRuns do not need to be destreaked", stage);
     153        psError(PXTOOLS_ERR_CONFIG, true, "%sRuns do not need to be destreaked", stage);
    150154        return false;
    151155    case IPP_STAGE_NONE:
    152         psError(PXTOOLS_ERR_DATA, true, "%s is not a valid stage", stage);
     156        psError(PXTOOLS_ERR_CONFIG, true, "%s is not a valid stage", stage);
    153157        return false;
    154158    default:
     
    159163
    160164    if (!query) {
    161         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     165        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    162166        return false;
    163167    }
     
    233237    // Parse the list of runs ready to be destreaked
    234238
    235     if (!dry_run && !psDBTransaction(config->dbh)) {
     239    if (!pretend && !psDBTransaction(config->dbh)) {
    236240        psError(PS_ERR_UNKNOWN, false, "database error");
    237241        return false;
     
    249253        psString magicRunDataGroup = psMetadataLookupStr(NULL, row, "data_group");
    250254        psString magicRunWorkdir = psMetadataLookupStr(NULL, row, "workdir");
    251        
     255
    252256        // if workdir is not supplied use the magicRun's
    253257        if (!workdir) {
     
    282286        }
    283287
    284         if (!dry_run) {
     288        if (!pretend) {
    285289            if (!magicDSRunInsertObject(config->dbh, run)) {
    286290                psError(PS_ERR_UNKNOWN, false, "database error");
     
    301305    }
    302306
    303     if (!dry_run && !psDBCommit(config->dbh)) {
     307    if (!pretend && !psDBCommit(config->dbh)) {
    304308        psError(PS_ERR_UNKNOWN, false, "database error");
    305309        return false;
     
    319323
    320324
     325// XXX This currently allows multiple destreak runs to be queued on the same exposure if there are multiple
     326// magicRuns selected!
     327static bool definecopyMode(pxConfig *config)
     328{
     329    // Required
     330    PXOPT_LOOKUP_STR(stage, config->args, "-stage", true, false);
     331    PXOPT_LOOKUP_STR(workdir, config->args, "-workdir", true, false);
     332
     333    // Optional
     334    PXOPT_LOOKUP_STR(recoveryroot, config->args, "-recoveryroot", false, false);
     335    PXOPT_LOOKUP_BOOL(noreplace, config->args, "-noreplace", false);
     336    PXOPT_LOOKUP_STR(set_label, config->args, "-set_label", false, false);
     337    PXOPT_LOOKUP_STR(set_data_group, config->args, "-set_data_group", false, false);
     338    PXOPT_LOOKUP_STR(note, config->args, "-set_note", false, false);
     339    PXOPT_LOOKUP_BOOL(rerun, config->args, "-rerun", false);
     340    PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
     341    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     342    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     343
     344    // search args
     345    psMetadata *where = psMetadataAlloc();
     346    PXOPT_COPY_S64(config->args, where, "-exp_id",  "chipRun.exp_id", "==");
     347    PXOPT_COPY_S64(config->args, where, "-chip_id", "chipRun.chip_id", "==");
     348    PXOPT_COPY_S64(config->args, where, "-cam_id",  "camRun.cam_id", "==");
     349    PXOPT_COPY_S64(config->args, where, "-warp_id", "warpRun.warp_id", "==");
     350    PXOPT_COPY_S64(config->args, where, "-diff_id", "diffRun.diff_id", "==");
     351    PXOPT_COPY_S64(config->args, where, "-magic_id", "magicRun.magic_id", "==");
     352    PXOPT_COPY_S32(config->args, where, "-streaks_max", "magicMask.streaks", "<=");
     353
     354    pxAddLabelSearchArgs (config, where, "-magic_label", "magicRun.label", "=="); // define magic label
     355    psString labelName = NULL;                                                    // Name of label
     356    psStringAppend(&labelName, "%sRun.label", stage);
     357    pxAddLabelSearchArgs (config, where, "-stage_label", labelName, "=="); // define stageRun label
     358    psFree(labelName);
     359
     360    ippStage stageNum = ippStringToStage(stage);
     361
     362    psString query = NULL;
     363    switch (stageNum) {
     364      case IPP_STAGE_RAW:
     365        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Raw stage is not appropriate for a copied destreak");
     366        return false;
     367      case IPP_STAGE_CHIP:
     368        query = pxDataGet("magicdstool_definecopy_chip.sql");
     369        break;
     370      case IPP_STAGE_WARP:
     371        query = pxDataGet("magicdstool_definecopy_warp.sql");
     372        break;
     373      case IPP_STAGE_CAMERA:
     374      case IPP_STAGE_DIFF:
     375      case IPP_STAGE_FAKE:
     376        psError(PS_ERR_BAD_PARAMETER_VALUE, true, "%s has not been coded.", stage);
     377        return false;
     378      case IPP_STAGE_STACK:
     379        psError(PXTOOLS_ERR_CONFIG, true, "Stacks do not need to be destreaked");
     380        return false;
     381      case IPP_STAGE_NONE:
     382        psError(PXTOOLS_ERR_CONFIG, true, "%s is not a valid stage", stage);
     383        return false;
     384      default:
     385        psError(PXTOOLS_ERR_PROG, true, "ippStageToString returned %d for invalid stage %s",
     386                stageNum, stage);
     387        return false;
     388    }
     389
     390    if (!query) {
     391        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     392        return false;
     393    }
     394    const char *rerun_flag =  rerun ? "" : "\n"; // String to give query to activate (or not) rerun
     395
     396    if (stageNum != IPP_STAGE_DIFF) {
     397        if (psListLength(where->list)) {
     398            psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     399            psStringAppend(&query, "\nAND %s", whereClause);
     400            psFree(whereClause);
     401        }
     402        psFree(where);
     403
     404        // treat limit == 0 as "no limit"
     405        if (limit) {
     406            psString limitString = psDBGenerateLimitSQL(limit);
     407            psStringAppend(&query, " %s", limitString);
     408            psFree(limitString);
     409        }
     410        if (!p_psDBRunQueryF(config->dbh, query, set_label, rerun_flag)) {
     411            psError(PS_ERR_UNKNOWN, false, "database error");
     412            psFree(query);
     413            return false;
     414        }
     415    } else {
     416        // diff stage query has two types bothways and !bothways
     417        // so we need to send the rerun flag and the where data twice
     418        psString whereString = psStringCopy("");
     419        if (psListLength(where->list)) {
     420            psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     421            psStringAppend(&whereString, "\nAND %s", whereClause);
     422            psFree(whereClause);
     423        }
     424        psFree(where);
     425
     426        // treat limit == 0 as "no limit"
     427        if (limit) {
     428            psString limitString = psDBGenerateLimitSQL(limit);
     429            psStringAppend(&query, " %s", limitString);
     430            psFree(limitString);
     431        }
     432
     433        if (!p_psDBRunQueryF(config->dbh, query, rerun_flag, whereString, rerun_flag, whereString)) {
     434            psError(PS_ERR_UNKNOWN, false, "database error");
     435            psFree(whereString);
     436            psFree(query);
     437            return false;
     438        }
     439        psFree(whereString);
     440    }
     441    psFree(query);
     442
     443    psArray *output = p_psDBFetchResult(config->dbh);
     444    if (!output) {
     445        psErrorCode err = psErrorCodeLast();
     446        switch (err) {
     447          case PS_ERR_DB_CLIENT:
     448            psError(PXTOOLS_ERR_SYS, false, "database error");
     449          case PS_ERR_DB_SERVER:
     450            psError(PXTOOLS_ERR_PROG, false, "database error");
     451          default:
     452            psError(PXTOOLS_ERR_PROG, false, "unknown error");
     453        }
     454
     455        return false;
     456    }
     457    if (!psArrayLength(output)) {
     458        psTrace("magictool", PS_LOG_INFO, "no rows found");
     459        psFree(output);
     460        return true;
     461    }
     462
     463    // Parse the list of runs ready to be destreaked
     464
     465    if (!pretend && !psDBTransaction(config->dbh)) {
     466        psError(PS_ERR_UNKNOWN, false, "database error");
     467        return false;
     468    }
     469
     470    psArray *list = psArrayAllocEmpty(16); // List of runs, to print
     471    for (long i = 0; i < psArrayLength(output); i++) {
     472        psMetadata *row = output->data[i]; // Row of interest
     473        psS64 stage_id = psMetadataLookupS64(NULL, row, "stage_id");
     474        psS64 exp_id = psMetadataLookupS64(NULL, row, "exp_id");
     475        psS64 magic_id = psMetadataLookupS64(NULL, row, "magic_id");
     476        psS64 inv_magic_id = psMetadataLookupS64(NULL, row, "inv_magic_id");
     477        psS64 cam_id = psMetadataLookupS64(NULL, row, "cam_id");
     478        psString magicRunLabel = psMetadataLookupStr(NULL, row, "label");
     479        psString magicRunDataGroup = psMetadataLookupStr(NULL, row, "data_group");
     480
     481        psString outroot = NULL;
     482        // set outroot to workdir/exp_id/stage for example /somewhere/424242/chip
     483        psStringAppend(&outroot, "%s/%" PRId64 "/%s", workdir, exp_id, stage);
     484
     485        // create a new magicRun for this group
     486        magicDSRunRow *run = magicDSRunRowAlloc(
     487                0, // magic_ds_id
     488                magic_id,
     489                inv_magic_id,
     490                "new",
     491                stage,
     492                stage_id,
     493                cam_id,
     494                set_label ? set_label : magicRunLabel,
     495                set_data_group ? set_data_group : magicRunDataGroup,
     496                outroot,
     497                recoveryroot,
     498                noreplace ? 0 :1,   // re_place
     499                0,      // remove
     500                0,      // fault
     501                note);  // remove
     502
     503        psFree(outroot);
     504        if (!run) {
     505            psAbort("failed to alloc magicDSRun object");
     506        }
     507
     508        if (!pretend) {
     509            if (!magicDSRunInsertObject(config->dbh, run)) {
     510                psError(PS_ERR_UNKNOWN, false, "database error");
     511                psFree(run);
     512                psFree(output);
     513                psFree(list);
     514                if (!psDBRollback(config->dbh)) {
     515                    psError(PS_ERR_UNKNOWN, false, "database error");
     516                }
     517                return false;
     518            }
     519            psS64 magic_ds_id = psDBLastInsertID(config->dbh); // Assigned identifier
     520            run->magic_ds_id = magic_ds_id;
     521        }
     522
     523        psArrayAdd(list, list->n, run);
     524        psFree(run);
     525    }
     526
     527    if (!pretend && !psDBCommit(config->dbh)) {
     528        psError(PS_ERR_UNKNOWN, false, "database error");
     529        return false;
     530    }
     531    psFree(output);
     532
     533    if (!magicDSRunPrintObjects(stdout, list, !simple)) {
     534        psError(PS_ERR_UNKNOWN, false, "failed to print object");
     535        psFree(list);
     536        return false;
     537    }
     538
     539    psFree(list);
     540
     541    return true;
     542}
     543
     544
    321545static bool updaterunMode(pxConfig *config)
    322546{
     
    326550    PXOPT_LOOKUP_STR(state, config->args, "-set_state", true, false);
    327551
     552    // optional
     553    PXOPT_LOOKUP_STR(set_label, config->args, "-set_label", false, false);
     554    PXOPT_LOOKUP_STR(set_data_group, config->args, "-set_data_group", false, false);
     555    psString setString = NULL;
     556    if (set_label) {
     557        psStringAppend(&setString, ", label = '%s'", set_label);
     558    }
     559    if (set_data_group) {
     560        psStringAppend(&setString, ", data_group = '%s'", set_data_group);
     561    }
     562
    328563    PXOPT_LOOKUP_S64(magic_ds_id, config->args, "-magic_ds_id", false, false);
    329564    if (magic_ds_id) {
    330565
    331         return setmagicDSRunState(config, magic_ds_id, NULL, state);
     566        return setmagicDSRunState(config, magic_ds_id, setString, NULL, state);
    332567
    333568    } else if (!strcmp(state, "full")) {
     
    341576    PXOPT_COPY_S64(config->args, where, "-stage_id", "stage_id", "==");
    342577    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
    343 
     578    PXOPT_COPY_STR(config->args, where, "-state", "state", "==");
     579    PXOPT_COPY_STR(config->args, where, "-data_group", "data_group", "LIKE");
    344580
    345581    if (psListLength(where->list) < 2) {
     
    353589        psMetadataAddS32(where, PS_LIST_TAIL, "re_place", 0, ">", 0);
    354590    }
    355     bool result = setmagicDSRunState(config, magic_ds_id, where, state);
     591    bool result = setmagicDSRunState(config, magic_ds_id, setString, where, state);
    356592    psFree(where);
    357593
     
    379615    psString query = pxDataGet(sql_file);
    380616    if (!query) {
    381         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement from %s", sql_file);
     617        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement from %s", sql_file);
    382618        psFree(sql_file);
    383619        return false;
     
    445681{
    446682    // first query the magicDSRun to find the stage and the stage_id
    447     psString query = "SELECT stage, stage_id from magicDSRun where magic_ds_id = %" PRId64;
     683    psString query = "SELECT stage, stage_id, magic_id from magicDSRun where magic_ds_id = %" PRId64;
    448684
    449685    if (!p_psDBRunQueryF(config->dbh, query, magic_ds_id)) {
     
    472708    psString stage= psMetadataLookupStr(NULL, row, "stage");
    473709    psS64 stage_id = psMetadataLookupS64(NULL, row, "stage_id");
     710    psS64 magic_id = psMetadataLookupS64(NULL, row, "magic_id");
    474711
    475712    ippStage stageNum = ippStringToStage(stage);
     
    499736    }
    500737
    501     if (!p_psDBRunQueryF(config->dbh, query, magic_ds_id, stage_id, component)) {
     738    if (!p_psDBRunQueryF(config->dbh, query, magic_id, stage_id, component)) {
    502739        psError(PS_ERR_UNKNOWN, false, "database error");
    503740        return false;
     
    518755{
    519756    // first query the magicDSRun to find the stage and the stage_id
    520     psString query = "SELECT stage, stage_id from magicDSRun where magic_ds_id = %" PRId64;
     757    psString query = "SELECT stage, stage_id, magic_id from magicDSRun where magic_ds_id = %" PRId64;
    521758
    522759    if (!p_psDBRunQueryF(config->dbh, query, magic_ds_id)) {
     
    545782    psString stage = psMetadataLookupStr(NULL, row, "stage");
    546783    psS64 stage_id = psMetadataLookupS64(NULL, row, "stage_id");
     784    psS64 magic_id = psMetadataLookupS64(NULL, row, "magic_id");
    547785
    548786    ippStage stageNum = ippStringToStage(stage);
     
    570808        return false;
    571809    }
    572     if (!p_psDBRunQueryF(config->dbh, query, magic_ds_id, stage_id)) {
     810    if (!p_psDBRunQueryF(config->dbh, query, magic_id, stage_id)) {
    573811        psError(PS_ERR_UNKNOWN, false, "database error");
    574812        return false;
     
    598836    PXOPT_LOOKUP_STR(recovery_path_base, config->args, "-recovery_path_base", false, false);
    599837    PXOPT_LOOKUP_BOOL(setmagicked, config->args, "-setmagicked", false);
     838    PXOPT_LOOKUP_F32(streak_frac, config->args, "-streak_frac", false, false);
     839    PXOPT_LOOKUP_F32(nondiff_frac, config->args, "-nondiff_frac", false, false);
     840    PXOPT_LOOKUP_F32(run_time, config->args, "-run_time", false, false);
    600841
    601842    if (setmagicked && (fault != 0)) {
     
    620861    }
    621862
    622     if (!magicDSFileInsert(config->dbh, magic_ds_id, component, backup_path_base, recovery_path_base, fault, "full")) {
     863    if (!magicDSFileInsert(config->dbh,
     864            magic_ds_id,
     865            component,
     866            backup_path_base,
     867            recovery_path_base,
     868            streak_frac,
     869            nondiff_frac,
     870            run_time,
     871            fault,
     872            "full"  // data_state
     873        )) {
    623874            // rollback
    624875        if (!psDBRollback(config->dbh)) {
     
    651902    psString query = pxDataGet("magicdstool_completed_runs.sql");
    652903    if (!query) {
    653         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     904        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    654905        return false;
    655906    }
     
    699950
    700951        // set magicDSRun.state to 'full'
    701         if (!setmagicDSRunState(config, magic_ds_id, NULL, "full")) {
     952        if (!setmagicDSRunState(config, magic_ds_id, NULL, NULL, "full")) {
    702953            psError(PS_ERR_UNKNOWN, false, "failed to change magicDSRun.state for magic_ds_id: %" PRId64,
    703954                magic_ds_id);
     
    747998
    748999    if (!p_psDBRunQuery(config->dbh, query)) {
     1000        psError(PS_ERR_UNKNOWN, false, "failed to revert");
     1001        psFree(query);
     1002        return false;
     1003    }
     1004    psFree(query);
     1005    return true;
     1006}
     1007static bool clearstatefaultsMode(pxConfig *config)
     1008{
     1009    PS_ASSERT_PTR_NON_NULL(config, false);
     1010
     1011    psMetadata *where = psMetadataAlloc();
     1012    // new state
     1013    PXOPT_LOOKUP_STR(new_state, config->args, "-set_state", false, false);
     1014    // old state (required)
     1015    PXOPT_LOOKUP_STR(state, config->args, "-state", true, false);
     1016
     1017    PXOPT_COPY_STR(config->args, where, "-state", "state", "==");
     1018    PXOPT_COPY_S64(config->args, where, "-magic_ds_id", "magicDSRun.magic_ds_id", "==");
     1019    PXOPT_COPY_S16(config->args, where, "-fault", "fault", "==");
     1020    pxAddLabelSearchArgs (config, where, "-label", "label", "==");
     1021
     1022    psString query = pxDataGet("magicdstool_clearstatefaults.sql");
     1023
     1024    if (psListLength(where->list)) {
     1025        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1026        psStringAppend(&query, " WHERE %s", whereClause);
     1027        psFree(whereClause);
     1028    } else {
     1029        psError(PS_ERR_UNKNOWN, true, "search arguments are required");
     1030        return false;
     1031    }
     1032    psFree(where);
     1033
     1034    if (!new_state) {
     1035        if (!strcmp(state, "failed_revert")) {
     1036            new_state = "new";
     1037        } else if (!strcmp(state, "failed_cleanup")) {
     1038            new_state = "goto_cleaned";
     1039        } else {
     1040            psError(PS_ERR_UNKNOWN, true, "unexpected value for state: %s", state);
     1041            return false;
     1042        }
     1043    }
     1044    if (!p_psDBRunQueryF(config->dbh, query, new_state)) {
    7491045        psError(PS_ERR_UNKNOWN, false, "failed to revert");
    7501046        psFree(query);
     
    8091105    for (int i=0; i<psArrayLength(output); i++) {
    8101106        psMetadata *row = output->data[i];
    811         psS64 magic_ds_id = psMetadataLookupS64(NULL, row, "magic_ds_id"); 
    812         psString old_state = psMetadataLookupStr(NULL, row, "state"); 
     1107        psS64 magic_ds_id = psMetadataLookupS64(NULL, row, "magic_ds_id");
     1108        psString old_state = psMetadataLookupStr(NULL, row, "state");
    8131109        psString new_state;
    8141110        if (!strcmp(old_state, "goto_censored")) {
     
    8381134
    8391135    psMetadata *where = psMetadataAlloc();
     1136    PXOPT_COPY_S64(config->args, where, "-magic_ds_id", "magicDSRun.magic_ds_id", "==");
    8401137    PXOPT_COPY_STR(config->args, where, "-class_id",    "warpSkyCellMap.class_id", "==");
    8411138    PXOPT_COPY_STR(config->args, where, "-skycell_id",  "warpSkyCellMap.skycell_id", "==");
     
    8451142    psString query = pxDataGet("magicdstool_getskycells.sql");
    8461143    if (!query) {
    847         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    848         return false;
    849     }
    850 
     1144        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     1145        return false;
     1146    }
     1147
     1148    psString whereClause = NULL;
    8511149    if (psListLength(where->list)) {
    852         psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    853         psStringAppend(&query, " AND %s", whereClause);
    854         psFree(whereClause);
     1150        whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1151        psStringPrepend(&whereClause, "\n AND ");
    8551152    }
    8561153    psFree(where);
    8571154
    858     if (!p_psDBRunQueryF(config->dbh, query, magic_ds_id)) {
     1155    if (!p_psDBRunQueryF(config->dbh, query, whereClause, whereClause)) {
    8591156        psError(PS_ERR_UNKNOWN, false, "database error");
    8601157        psFree(query);
     
    9011198    if (!((strcmp(state, "new") == 0) ||
    9021199          (strcmp(state, "full") == 0) ||
     1200          (strcmp(state, "drop") == 0) ||
     1201          (strcmp(state, "failed_revert") == 0) ||
     1202          (strcmp(state, "failed_cleanup") == 0) ||
    9031203          (strcmp(state, "restored") == 0) ||
    9041204          (strcmp(state, "censored") == 0) ||
     
    9141214}
    9151215
    916 static bool setmagicDSRunState(pxConfig *config, psS64 magic_ds_id, psMetadata *where, const char *state)
     1216static bool setmagicDSRunState(pxConfig *config, psS64 magic_ds_id, psString extraSetStr, psMetadata *where, const char *state)
    9171217{
    9181218    PS_ASSERT_PTR_NON_NULL(state, false);
     
    9241224    }
    9251225
    926     psString query = psStringCopy("UPDATE magicDSRun SET state = '%s'");
     1226    psString query = NULL;
     1227    psStringAppend(&query, "UPDATE magicDSRun SET state = '%s' %s\n", state, extraSetStr ? extraSetStr : "");
    9271228    if (magic_ds_id) {
    9281229        psStringAppend(&query, " WHERE magic_ds_id = %" PRId64, magic_ds_id);
     
    9321233        psFree(whereClause);
    9331234    } else {
    934         psError(PS_ERR_UNKNOWN, true, "search arugments are required");
    935         return false;
    936     }
    937 
    938     if (!p_psDBRunQueryF(config->dbh, query, state)) {
     1235        psError(PS_ERR_UNKNOWN, true, "search arguments are required");
     1236        return false;
     1237    }
     1238
     1239    if (!strcmp(state, "goto_cleaned")) {
     1240        // Don't set state back to goto_cleaned if it is already cleaned
     1241        psStringAppend(&query, " AND (magicDSRun.state != 'cleaned')");
     1242
     1243        // don't clean up magicDSRun's where stage is camera
     1244        psStringAppend(&query, " AND (magicDSRun.stage != 'camera')");
     1245    }
     1246
     1247    if (!p_psDBRunQuery(config->dbh, query)) {
    9391248        psError(PS_ERR_UNKNOWN, false,
    9401249                "failed to change state for magic_id %" PRId64, magic_ds_id);
     
    9601269    psString query = pxDataGet("magicdstool_toremove.sql");
    9611270    if (!query) {
    962         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1271        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    9631272        return false;
    9641273    }
     
    10381347    psString query = pxDataGet(sql_file);
    10391348    if (!query) {
    1040         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement from %s", sql_file);
     1349        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement from %s", sql_file);
    10411350        psFree(sql_file);
    10421351        return false;
     
    11061415    psMetadata *where = psMetadataAlloc();
    11071416
    1108     PXOPT_COPY_S64(config->args, where, "-stage", "stage", "==");
     1417    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
    11091418    PXOPT_COPY_S64(config->args, where, "-magic_ds_id", "magic_ds_id", "==");
    11101419    PXOPT_COPY_S64(config->args, where, "-magic_id", "magic_id", "==");
     
    11161425    psString query = pxDataGet("magicdstool_tocleanup.sql");
    11171426    if (!query) {
    1118         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1427        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    11191428        return false;
    11201429    }
Note: See TracChangeset for help on using the changeset viewer.