IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Sep 27, 2009, 11:34:07 AM (17 years ago)
Author:
eugene
Message:

merge changes from trunk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/20090715/ippTools/src/disttool.c

    r25400 r25624  
    22 * disttool.c
    33 *
    4  * Copyright (C) 2008
     4 * Copyright (C) 2008-2009
    55 *
    66 * This program is free software; you can redistribute it and/or modify it
     
    3737static bool pendingcomponentMode(pxConfig *config);
    3838static bool addprocessedcomponentMode(pxConfig *config);
     39static bool revertcomponentMode(pxConfig *config);
    3940static bool processedcomponentMode(pxConfig *config);
    4041static bool toadvanceMode(pxConfig *config);
     
    5152static bool listtargetMode(pxConfig *config);
    5253
    53 static bool definedsproductMode(pxConfig *config);
    54 static bool updatedsproductMode(pxConfig *config);
    55 
    5654static bool definedestinationMode(pxConfig *config);
    5755static bool updatedestinationMode(pxConfig *config);
     
    5957static bool defineinterestMode(pxConfig *config);
    6058static bool updateinterestMode(pxConfig *config);
     59static bool listinterestsMode(pxConfig *config);
    6160
    6261# define MODECASE(caseName, func) \
     
    8685        MODECASE(DISTTOOL_MODE_ADDPROCESSEDCOMPONENT, addprocessedcomponentMode);
    8786        MODECASE(DISTTOOL_MODE_PROCESSEDCOMPONENT, processedcomponentMode);
     87        MODECASE(DISTTOOL_MODE_REVERTCOMPONENT, revertcomponentMode);
    8888        MODECASE(DISTTOOL_MODE_TOADVANCE, toadvanceMode);
    8989        MODECASE(DISTTOOL_MODE_PENDINGFILESET, pendingfilesetMode);
     
    9797        MODECASE(DISTTOOL_MODE_UPDATETARGET, updatetargetMode);
    9898        MODECASE(DISTTOOL_MODE_LISTTARGET, listtargetMode);
    99         MODECASE(DISTTOOL_MODE_DEFINEDSPRODUCT, definedsproductMode);
    100         MODECASE(DISTTOOL_MODE_UPDATEDSPRODUCT, updatedsproductMode);
    10199        MODECASE(DISTTOOL_MODE_DEFINEDESTINATION, definedestinationMode);
    102100        MODECASE(DISTTOOL_MODE_UPDATEDESTINATION, updatedestinationMode);
    103101        MODECASE(DISTTOOL_MODE_DEFINEINTEREST, defineinterestMode);
    104102        MODECASE(DISTTOOL_MODE_UPDATEINTEREST, updateinterestMode);
     103        MODECASE(DISTTOOL_MODE_LISTINTERESTS, listinterestsMode);
    105104        default:
    106105            psAbort("invalid option (this should not happen)");
     
    151150            stage,
    152151            stage_id,
     152            0,
    153153            set_label,
    154154            outroot,
     
    223223        }
    224224    } else if (!strcmp(stage, "camera")) {
    225         magicRunType = "chipRun";    // This is used below to set the magicked business
     225        magicRunType = "camRun";    // This is used below to set the magicked business
     226        runJoinStr = "camRun.cam_id";
    226227        query = pxDataGet("disttool_definebyquery_camera.sql");
    227228        if (!query) {
     
    288289            psStringAppend(&query, " AND (stackRun.label = '%s')", label);
    289290        }
    290         // stack stage doesn't require magic (perhaps let the script do this?
     291        // stack stage doesn't require magic
    291292        no_magic = true;
    292293    } else {
     
    306307
    307308    if (!no_magic) {
    308         psStringAppend(&query, " AND (distTarget.clean OR %s.magicked)", magicRunType);
     309        psStringAppend(&query, " AND (%s.magicked)", magicRunType);
    309310
    310311        // is selecting by magic_ds_id really interesting?
    311312        if (magic_ds_id) {
    312             if (strcmp(stage, "camera")) {
    313                 // stage other than camera
    314                 if (!runJoinStr) {
    315                     psError(PS_ERR_PROGRAMMING, true, "cannot select by magic_ds_id for stage: %s", stage);
    316                     psFree(query);
    317                     return false;
    318                 }
    319                 psStringAppend(&joinHook, "\nJOIN magicDSRun ON magicDSRun.stage = distTarget.stage"
     313            if (!runJoinStr) {
     314                psError(PS_ERR_PROGRAMMING, true, "cannot select by magic_ds_id for stage: %s", stage);
     315                psFree(query);
     316                return false;
     317            }
     318            psStringAppend(&joinHook, "\nJOIN magicDSRun ON magicDSRun.stage = distTarget.stage"
    320319                                              " AND magicDSRun.stage_id = %s", runJoinStr);
    321             } else {
    322                 // camera masks are magicked when the chipRun is magicked
    323                 // XXX: This is confusing. Is it dangerous?
    324                 // Maybe I should add a magicked bit to camRun. Note this isn't
    325                 psStringAppend(&joinHook, "\nJOIN magicDSRun ON magicDSRun.stage = 'chip'"
    326                                               " AND magicDSRun.stage_id = chipRun.chip_id");
    327             }
    328320            psStringAppend(&query, " AND (magicDSRun.state = 'full' AND magicDSRun.re_place AND (magic_ds_id = %" PRId64 "))", magic_ds_id);
    329321        }
     
    377369        psString run_tag = psMetadataLookupStr(NULL, md, "run_tag");
    378370        psS64 stage_id = psMetadataLookupS64(NULL, md, "stage_id");
     371        psS64 magic_ds_id = psMetadataLookupS64(NULL, md, "magicked");
    379372        psS64 target_id = psMetadataLookupS64(NULL, md, "target_id");
    380373        psString target_label = psMetadataLookupStr(NULL, md, "label");
     
    395388                stage,
    396389                stage_id,
     390                magic_ds_id,
    397391                new_label,
    398392                outroot,
     
    500494    PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
    501495
    502     // we need to disambiguate fault so make a copy of the where list before adding fault
    503     psMetadata *whereComponent = psMetadataCopy(NULL, where);
    504496    PXOPT_COPY_S16(config->args, where,  "-fault", "distRun.fault", "==");
    505     PXOPT_COPY_S16(config->args, whereComponent, "-fault", "distComponent.fault", "==");
    506497
    507498    // It might be useful to be able to query by the parameters of the underlying runs
     
    513504    }
    514505
    515     if (!psDBTransaction(config->dbh)) {
    516         psError(PS_ERR_UNKNOWN, false, "database error");
     506    psString query = pxDataGet("disttool_revertrun.sql");
     507    if (!query) {
     508        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     509        if (!psDBRollback(config->dbh)) {
     510            psError(PS_ERR_UNKNOWN, false, "database error");
     511        }
     512        return false;
     513    }
     514
     515    if (psListLength(where->list)) {
     516        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     517        psStringAppend(&query, " AND %s", whereClause);
     518        psFree(whereClause);
     519    }
     520    psFree(where);
     521
     522    if (!p_psDBRunQuery(config->dbh, query)) {
     523        psError(PS_ERR_UNKNOWN, false, "database error");
     524        psFree(query);
     525        return false;
     526    }
     527
     528    int numUpdated = psDBAffectedRows(config->dbh);
     529
     530    psLogMsg("disttool", PS_LOG_INFO, "Updated %d dist runs", numUpdated);
     531
     532    return true;
     533}
     534
     535static bool revertcomponentMode(pxConfig *config)
     536{
     537    psMetadata *where = psMetadataAlloc();
     538    PXOPT_COPY_S64(config->args, where, "-dist_id", "distRun.dist_id", "==");
     539    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");;
     540    PXOPT_COPY_STR(config->args, where, "-component", "component", "==");;
     541    PXOPT_COPY_S64(config->args, where, "-stage_id", "stage_id", "==");
     542    PXOPT_COPY_STR(config->args, where, "-state", "state", "==");
     543    PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
     544
     545    PXOPT_COPY_S16(config->args, where,  "-fault", "distComponent.fault", "==");
     546
     547    // It might be useful to be able to query by the parameters of the underlying runs
     548
     549    if (!psListLength(where->list) && !psMetadataLookupBool(NULL, config->args, "-all")) {
    517550        psFree(where);
    518         return false;
    519     }
    520 
    521     // Update state to 'new'
    522     int numUpdated;                     // Number updated
    523     {
    524         psString query = pxDataGet("disttool_revertrun_update.sql");
    525         if (!query) {
    526             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    527             if (!psDBRollback(config->dbh)) {
    528                 psError(PS_ERR_UNKNOWN, false, "database error");
    529             }
    530             return false;
    531         }
    532 
    533         if (psListLength(where->list)) {
    534             psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    535             psStringAppend(&query, " AND %s", whereClause);
    536             psFree(whereClause);
    537         }
    538 
    539         if (!p_psDBRunQuery(config->dbh, query)) {
    540             psError(PS_ERR_UNKNOWN, false, "database error");
    541             psFree(query);
    542             if (!psDBRollback(config->dbh)) {
    543                 psError(PS_ERR_UNKNOWN, false, "database error");
    544             }
    545             return false;
    546         }
    547         psFree(query);
    548 
    549         numUpdated = psDBAffectedRows(config->dbh);
    550     }
    551 
    552     psLogMsg("disttool", PS_LOG_INFO, "Updated %d dist runs", numUpdated);
    553 
    554     // Delete product
    555     int numDeleted;                     // Number deleted
    556     {
    557         psString query = pxDataGet("disttool_revertrun_delete.sql");
    558         if (!query) {
    559             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    560             if (!psDBRollback(config->dbh)) {
    561                 psError(PS_ERR_UNKNOWN, false, "database error");
    562             }
    563             return false;
    564         }
    565 
    566         if (psListLength(whereComponent->list)) {
    567             psString whereClause = psDBGenerateWhereConditionSQL(whereComponent, NULL);
    568             psStringAppend(&query, " AND %s", whereClause);
    569             psFree(whereClause);
    570         }
    571 
    572         if (!p_psDBRunQuery(config->dbh, query)) {
    573             psError(PS_ERR_UNKNOWN, false, "database error");
    574             psFree(query);
    575             if (!psDBRollback(config->dbh)) {
    576                 psError(PS_ERR_UNKNOWN, false, "database error");
    577             }
    578             return false;
    579         }
    580         psFree(query);
    581 
    582         numDeleted = psDBAffectedRows(config->dbh);
    583     }
     551        psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
     552        return false;
     553    }
     554
     555    psString query = pxDataGet("disttool_revertcomponent.sql");
     556    if (!query) {
     557        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     558        psFree(where);
     559        return false;
     560    }
     561
     562    if (psListLength(where->list)) {
     563        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     564        psStringAppend(&query, " AND %s", whereClause);
     565        psFree(whereClause);
     566    }
     567    psFree(where);
     568
     569    if (!p_psDBRunQuery(config->dbh, query)) {
     570        psError(PS_ERR_UNKNOWN, false, "database error");
     571        psFree(query);
     572        return false;
     573    }
     574
     575    int numDeleted = psDBAffectedRows(config->dbh);
    584576
    585577    psLogMsg("disttool", PS_LOG_INFO, "Deleted %d distComponents", numDeleted);
    586578
    587     psFree(where);
    588     psFree(whereComponent);
    589 
    590     if (!psDBCommit(config->dbh)) {
    591         psError(PS_ERR_UNKNOWN, false, "database error");
    592         return false;
    593     }
    594 
    595579    return true;
    596580}
     
    600584    PS_ASSERT_PTR_NON_NULL(config, false);
    601585
     586    PXOPT_LOOKUP_STR(stage, config->args, "-stage", true, false);
     587
    602588    psMetadata *where = psMetadataAlloc();
    603589    PXOPT_COPY_S64(config->args, where, "-dist_id", "dist_id", "==");
    604     PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
    605     pxAddLabelSearchArgs (config, where, "-label", "label", "==");
     590    pxAddLabelSearchArgs (config, where, "-label", "distRun.label", "==");
    606591
    607592    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
    608593    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    609594
    610     // look for "inputs" that need to processed
    611     psString query = pxDataGet("disttool_pendingcomponent.sql");
     595    psString queryFile = NULL;
     596    psStringAppend(&queryFile, "disttool_pending_%s.sql", stage);
     597    psString query = pxDataGet(queryFile);
    612598    if (!query) {
    613         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     599        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement from %s", queryFile);
    614600        return false;
    615601    }
     
    617603    if (psListLength(where->list)) {
    618604        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    619         psStringAppend(&query, " WHERE %s", whereClause);
     605        psStringAppend(&query, " AND %s", whereClause);
    620606        psFree(whereClause);
    621607    }
     
    629615    }
    630616
    631     // the query has where hooks for each stage.
    632     // right now we aren't using them.
    633     // XXX: I think that I want to change the query from a union of selects on the various
    634     // stages to separate queries. As it is pending data at the later stages of the pipline
    635     // will get blocked by pending earlier stages
    636     psString    raw_where = "";
    637     psString    raw_clean_where = "";
    638     psString    chip_where = "";
    639     psString    camera_where = "";
    640     psString    fake_where = "";
    641     psString    warp_where = "";
    642     psString    diff_where = "";
    643     psString    stack_where = "";
    644 
    645     if (!p_psDBRunQueryF(config->dbh,
    646             query,
    647             raw_where,
    648             raw_clean_where,
    649             chip_where,
    650             camera_where,
    651             fake_where,
    652             warp_where,
    653             diff_where,
    654             stack_where)) {
     617    if (!p_psDBRunQuery(config->dbh, query)) {
    655618        psError(PS_ERR_UNKNOWN, false, "database error");
    656619        psFree(query);
     
    868831    psMetadata *where = psMetadataAlloc();
    869832    PXOPT_COPY_S64(config->args, where, "-dist_id", "dist_id", "==");
     833    PXOPT_COPY_STR(config->args, where, "-stage", "distRun.stage", "==");
    870834    pxAddLabelSearchArgs (config, where, "-label", "distRun.label", "==");
    871835
     
    939903    // required values
    940904    PXOPT_LOOKUP_S64(dist_id, config->args, "-dist_id", true, false);
    941     PXOPT_LOOKUP_S64(prod_id, config->args, "-prod_id", true, false);
     905    PXOPT_LOOKUP_S64(dest_id, config->args, "-dest_id", true, false);
    942906
    943907    PXOPT_LOOKUP_S16(fault, config->args, "-fault", false, false);
     
    949913            0,          // fs_id
    950914            dist_id,
    951             prod_id,
     915            dest_id,
    952916            name,
    953917            "full",
     
    964928    PXOPT_COPY_S64(config->args, where, "-fs_id", "fs_id", "==");
    965929    PXOPT_COPY_S64(config->args, where, "-dist_id", "rcDSFileset.dist_id", "==");
    966     PXOPT_COPY_S64(config->args, where, "-prod_id", "prod_id", "==");
     930    PXOPT_COPY_S64(config->args, where, "-dest_id", "dest_id", "==");
    967931    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");;
    968932    PXOPT_COPY_S64(config->args, where, "-stage_id", "stage_id", "==");
     
    10921056    PXOPT_COPY_S64(config->args, where, "-dist_id",  "dist_id", "==");
    10931057    PXOPT_COPY_S64(config->args, where, "-dest_id",  "dest_id", "==");
    1094     PXOPT_COPY_S64(config->args, where, "-prod_id",  "prod_id", "==");
    10951058    PXOPT_COPY_S64(config->args, where, "-target_id","target_id", "==");
    10961059    PXOPT_COPY_S64(config->args, where, "-fs_id",    "fs_id", "==");
     
    14331396}
    14341397
    1435 static bool definedsproductMode(pxConfig *config)
     1398static bool definedestinationMode(pxConfig *config)
    14361399{
    14371400    PS_ASSERT_PTR_NON_NULL(config, false);
    14381401
    14391402    // required
    1440     PXOPT_LOOKUP_STR(name, config->args,   "-name", true, false);
    1441     PXOPT_LOOKUP_STR(dbname, config->args, "-ds_dbname", true, false);
    1442     PXOPT_LOOKUP_STR(dbhost, config->args, "-ds_dbhost", true, false);
    1443 
    1444     // XXX: should we insure that these names do not contatin any whitespace?
    1445 
    1446     rcDSProductRow *row = rcDSProductRowAlloc(
    1447             0,          // prod_id
    1448             name,
    1449             dbname,
    1450             dbhost
    1451             );
    1452            
    1453     if (!row) {
    1454         psError(PS_ERR_UNKNOWN, false, "failed to allocate rcDSProduct object");
    1455         return false;
    1456     }
    1457    if (!rcDSProductInsertObject(config->dbh, row)) {
    1458         psError(PS_ERR_UNKNOWN, false, "database error");
    1459         psFree(row);
    1460         return false;
    1461     }
    1462 
    1463     // get the assigned target_id
    1464     row->prod_id = psDBLastInsertID(config->dbh);
    1465 
    1466     if (!rcDSProductPrintObject(stdout, row, true)) {
    1467         psError(PS_ERR_UNKNOWN, false, "failed to print object");
    1468         psFree(row);
    1469         return false;
    1470     }
    1471 
    1472     psFree(row);
    1473 
    1474     return true;
    1475 }
    1476 static bool updatedsproductMode(pxConfig *config)
    1477 {
    1478     PS_ASSERT_PTR_NON_NULL(config, false);
    1479 
    1480     psMetadata *where = psMetadataAlloc();
    1481     PXOPT_COPY_S64(config->args, where, "-prod_id", "prod_id", "==");
    1482 
    1483     PXOPT_LOOKUP_STR(dbname, config->args, "-ds_dbname", false, false);
    1484     PXOPT_LOOKUP_STR(dbhost, config->args, "-ds_dbhost", false, false);
    1485 
    1486     if (!(dbname || dbhost)) {
    1487         psError(PS_ERR_UNKNOWN, true, "one or more of dbname or dbhost is required");
    1488         psFree(where);
    1489         return false;
    1490     }
    1491     psString query = psStringCopy("UPDATE rcDSProduct SET");
    1492     psString sep = "";
    1493     if (dbname) {
    1494         psStringAppend(&query, " dbname = '%s'", dbname);
    1495         sep = ",";
    1496     }
    1497     if (dbhost) {
    1498         psStringAppend(&query, " %s dbhost = '%s'", sep, dbhost);
    1499     }
    1500 
    1501     if (psListLength(where->list)) {
    1502         psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    1503         psStringAppend(&query, " WHERE %s", whereClause);
    1504         psFree(whereClause);
    1505     } else {
    1506         psError(PS_ERR_UNKNOWN, true, "search parameters are required");
    1507         psFree(where);
    1508         psFree(query);
    1509         return false;
    1510     }
    1511     psFree(where);
    1512 
    1513     if (!p_psDBRunQuery(config->dbh, query)) {
    1514         psError(PS_ERR_UNKNOWN, false, "database error");
    1515         psFree(query);
    1516         return false;
    1517     }
    1518     psFree(query);
    1519 
    1520     return true;
    1521 }
    1522 
    1523 static bool definedestinationMode(pxConfig *config)
    1524 {
    1525     PS_ASSERT_PTR_NON_NULL(config, false);
    1526 
    1527     // required
    1528     PXOPT_LOOKUP_S64(prod_id, config->args,      "-prod_id", true, false);
    15291403    PXOPT_LOOKUP_STR(name, config->args,         "-name", true, false);
     1404    PXOPT_LOOKUP_STR(dbname, config->args,       "-ds_dbname", true, false);
     1405    PXOPT_LOOKUP_STR(dbhost, config->args,       "-ds_dbhost", true, false);
    15301406
    15311407    // optional
     
    15391415    rcDestinationRow *row = rcDestinationRowAlloc(
    15401416            0,          // dest_id
    1541             prod_id,
    15421417            name,
    15431418            status_uri,
    15441419            comment,
    15451420            last_fileset,
     1421            dbname,
     1422            dbhost,
    15461423            state ? state : "enabled"
    15471424            );
     
    15771454    psMetadata *where = psMetadataAlloc();
    15781455    PXOPT_COPY_S64(config->args, where, "-dest_id", "dest_id", "==");
    1579     PXOPT_COPY_S64(config->args, where, "-prod_id", "prod_id", "==");
    15801456
    15811457    PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, false);
     
    15991475    // last_fileset normally gets set by updatercrunMode
    16001476    // Allowing it to be set here might cause problems
    1601     // especially since we are allowing selection by prod_id
     1477    // especially since we are allowing selection by dest_id
    16021478    if (last_fileset) {
    16031479        psStringAppend(&query, " %s last_fileset = '%s'", sep, last_fileset);
     
    16311507    PS_ASSERT_PTR_NON_NULL(config, false);
    16321508
    1633     // required
    1634     PXOPT_LOOKUP_S64(dest_id, config->args,      "-dest_id", true, false);
    1635     PXOPT_LOOKUP_S64(target_id, config->args,    "-target_id", true, false);
     1509    // one of these is required
     1510    PXOPT_LOOKUP_S64(dest_id, config->args,      "-dest_id", false, false);
     1511    PXOPT_LOOKUP_STR(dest_name, config->args,    "-dest_name", false, false);
     1512    if (!dest_id && !dest_name) {
     1513        psError(PS_ERR_UNKNOWN, true, "either dest_id or dest_name is required");
     1514        return false;
     1515    }
     1516
     1517    // either target_id or stage and label are required
     1518    PXOPT_LOOKUP_S64(target_id, config->args,    "-target_id", false, false);
     1519    PXOPT_LOOKUP_STR(stage, config->args,        "-stage", false, false);
     1520    PXOPT_LOOKUP_STR(label, config->args,        "-label", false, false);
     1521    PXOPT_LOOKUP_STR(filter, config->args,       "-filter", false, false);
     1522    PXOPT_LOOKUP_BOOL(clean, config->args,       "-clean", false);
     1523
     1524    if (!target_id) {
     1525        bool error = false;
     1526        if (!stage) {
     1527            psError(PS_ERR_UNKNOWN, true, "stage is required if target_id is not supplied");
     1528            error = true;
     1529        }
     1530        if (!label) {
     1531            psError(PS_ERR_UNKNOWN, !error, "label is required if target_id is not supplied");
     1532            error = true;
     1533        }
     1534        if (error) {
     1535            return false;
     1536        }
     1537    }
    16361538
    16371539    // optional
     1540    PXOPT_LOOKUP_S64(limit, config->args, "-limit", false, false);
    16381541    PXOPT_LOOKUP_STR(state, config->args,        "-set_state", false, false);
    1639 
    1640     // XXX: should we insure that these names do not contatin any whitespace?
    1641 
    1642     rcInterestRow *row = rcInterestRowAlloc(
    1643             0,          // int_id
    1644             dest_id,
    1645             target_id,
    1646             state ? state : "enabled"
    1647             );
    1648            
    1649     if (!row) {
    1650         psError(PS_ERR_UNKNOWN, false, "failed to allocate rcInterest object");
    1651         return false;
    1652     }
    1653    if (!rcInterestInsertObject(config->dbh, row)) {
    1654         psError(PS_ERR_UNKNOWN, false, "database error");
    1655         psFree(row);
    1656         return false;
    1657     }
    1658 
    1659     // get the assigned target_id
    1660     row->int_id = psDBLastInsertID(config->dbh);
    1661 
    1662     if (!rcInterestPrintObject(stdout, row, true)) {
    1663         psError(PS_ERR_UNKNOWN, false, "failed to print object");
    1664         psFree(row);
    1665         return false;
    1666     }
    1667 
    1668     psFree(row);
     1542    if (state) {
     1543        if (strcmp(state, "enabled") && strcmp(state, "disabled")) {
     1544            psError(PS_ERR_PROGRAMMING, true, "state must be enabled or disabled");
     1545            return false;
     1546        }
     1547    } else {
     1548        // default state
     1549        state = "enabled";
     1550    }
     1551
     1552    // now that we've done all of our argument checking, copy the values to where
     1553    psMetadata *where = psMetadataAlloc();
     1554    PXOPT_COPY_S64(config->args, where, "-dest_id", "dest_id", "==");
     1555    PXOPT_COPY_STR(config->args, where, "-dest_name", "rcDestination.name", "==");
     1556    PXOPT_COPY_S64(config->args, where, "-target_id", "target_id", "==");
     1557    PXOPT_COPY_STR(config->args, where, "-label", "label", "LIKE");
     1558    PXOPT_COPY_STR(config->args, where, "-filter", "filter", "LIKE");
     1559    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
     1560
     1561    psString query = pxDataGet("disttool_defineinterest.sql");
     1562
     1563    if (!psListLength(where->list)) {
     1564        // can't get here
     1565        psError(PS_ERR_PROGRAMMING, true, "search parameters are required");
     1566        psFree(where);
     1567        psFree(query);
     1568        return false;
     1569    }
     1570    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1571    psStringAppend(&query, " AND %s", whereClause);
     1572    psFree(whereClause);
     1573    psFree(where);
     1574    if (clean) {
     1575        psStringAppend(&query, " AND (distTarget.clean)");
     1576    } else {
     1577        psStringAppend(&query, " AND (!distTarget.clean)");
     1578    }
     1579    if (limit) {
     1580        psString limitString = psDBGenerateLimitSQL(limit);
     1581        psStringAppend(&query, " %s", limitString);
     1582        psFree(limitString);
     1583    }
     1584    {
     1585        // psStringSubstitute fails unless the input is a psString which it determines by
     1586        // comparing the memory blocks free function to an expected value.
     1587        // pxDataGet uses psSlurp which leaves a different free function on the memory block.
     1588        // To work around this make a copy of the query before doing the substitution.
     1589        psString queryCopy = psStringCopy(query);
     1590        psFree(query);
     1591        query = queryCopy;
     1592    }
     1593    // change the @STATE@ in the sql file to our state
     1594    if (!psStringSubstitute(&query, state, "@STATE@")) {
     1595        psError(PS_ERR_UNKNOWN, false, "failed to substitute state string");
     1596        return false;
     1597    }
     1598
     1599    if (!p_psDBRunQuery(config->dbh, query)) {
     1600        psError(PS_ERR_UNKNOWN, false, "database error");
     1601        psFree(query);
     1602        return false;
     1603    }
     1604    psFree(query);
     1605    int numInserted = psDBAffectedRows(config->dbh);
     1606    printf("inserted %d rows into rcInterest\n", numInserted);
    16691607
    16701608    return true;
     
    17101648    return true;
    17111649}
    1712 
     1650static bool listinterestsMode(pxConfig *config)
     1651{
     1652    PS_ASSERT_PTR_NON_NULL(config, false);
     1653
     1654    psMetadata *where = psMetadataAlloc();
     1655    PXOPT_COPY_S64(config->args, where, "-int_id", "int_id", "==");
     1656    PXOPT_COPY_S64(config->args, where, "-dest_id", "dest_id", "==");
     1657    PXOPT_COPY_STR(config->args, where, "-dest_name", "name", "==");
     1658    PXOPT_COPY_S64(config->args, where, "-target_id", "target_id", "==");
     1659    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
     1660    PXOPT_COPY_STR(config->args, where, "-label", "label", "LIKE");
     1661    PXOPT_COPY_STR(config->args, where, "-filter", "filter", "LIKE");
     1662    PXOPT_COPY_STR(config->args, where, "-state", "state", "==");
     1663
     1664    PXOPT_LOOKUP_BOOL(clean, config->args, "-clean", false);
     1665    PXOPT_LOOKUP_BOOL(full, config->args, "-full", false);
     1666
     1667    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     1668    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     1669
     1670    if (clean && full) {
     1671        psError(PS_ERR_UNKNOWN, false, "can't select both -clean and -full");
     1672        return false;
     1673    }
     1674
     1675    psString query = pxDataGet("disttool_listinterests.sql");
     1676
     1677    if (psListLength(where->list)) {
     1678        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1679        psStringAppend(&query, " WHERE %s", whereClause);
     1680        psFree(whereClause);
     1681        if (clean) {
     1682            psStringAppend(&query, " AND (clean)");
     1683        } else if (full) {
     1684            psStringAppend(&query, " AND (!clean)");
     1685        }
     1686    } else if (clean) {
     1687        psStringAppend(&query, " WHERE clean");
     1688    } else if (full) {
     1689        psStringAppend(&query, " WHERE !clean");
     1690    }
     1691    psFree(where);
     1692
     1693    // treat limit == 0 as "no limit"
     1694    if (limit) {
     1695        psString limitString = psDBGenerateLimitSQL(limit);
     1696        psStringAppend(&query, " %s", limitString);
     1697        psFree(limitString);
     1698    }
     1699
     1700    if (!p_psDBRunQuery(config->dbh, query)) {
     1701        psError(PS_ERR_UNKNOWN, false, "database error");
     1702        psFree(query);
     1703        if (!psDBRollback(config->dbh)) {
     1704            psError(PS_ERR_UNKNOWN, false, "database error");
     1705        }
     1706        return false;
     1707    }
     1708    psFree(query);
     1709
     1710    psArray *output = p_psDBFetchResult(config->dbh);
     1711    if (!output) {
     1712        psError(PS_ERR_UNKNOWN, false, "database error");
     1713        return false;
     1714    }
     1715    if (!psArrayLength(output)) {
     1716        psTrace("disttool", PS_LOG_INFO, "no rows found");
     1717        psFree(output);
     1718        return true;
     1719    }
     1720
     1721    if (!ippdbPrintMetadatas(stdout, output, "rcInterest", !simple)) {
     1722        psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1723        psFree(output);
     1724        return false;
     1725    }
     1726
     1727    psFree(output);
     1728
     1729    return true;
     1730}
Note: See TracChangeset for help on using the changeset viewer.