IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jul 15, 2008, 10:30:59 AM (18 years ago)
Author:
eugene
Message:

completely deprecate config->where; make all command-line handling consistent; move nearly all sql into share/*.sql; ensure consistency between args supplied and used

File:
1 edited

Legend:

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

    r18526 r18561  
    104104    PS_ASSERT_PTR_NON_NULL(config, false);
    105105
     106    PXOPT_LOOKUP_STR(workdir, config->args, "-workdir", false, false);
     107    PXOPT_LOOKUP_STR(label, config->args, "-label", false, false);
     108    PXOPT_LOOKUP_STR(dvodb, config->args, "-dvodb", false, false);
     109    PXOPT_LOOKUP_TIME(registered, config->args, "-registered", false, false);
     110
    106111    // create warped skycells temp table
    107 {
    108     char *query =
    109         "CREATE TEMPORARY TABLE warpComplete\n"
    110         " (warp_id BIGINT, skycell_id VARCHAR(64), tess_id VARCHAR(64),\n"
    111         " PRIMARY KEY(warp_id, skycell_id, tess_id)) ENGINE=MEMORY\n";
    112 
    113     if (!p_psDBRunQuery(config->dbh, query)) {
    114         psError(PS_ERR_UNKNOWN, false, "database error");
    115         return false;
    116     }
    117 }
     112    {
     113        psString query = pxDataGet("magictool_create_tmp_warpcomplete.sql");
     114        if (!query) {
     115            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     116            return false;
     117        }
     118
     119        if (!p_psDBRunQuery(config->dbh, query)) {
     120            psError(PS_ERR_UNKNOWN, false, "database error");
     121            return false;
     122        }
     123    }
    118124
    119125    // find warped skycells
    120 {
    121     psString query = pxDataGet("magictool_find_complete_warpruns.sql");
     126    {
     127        psString query = pxDataGet("magictool_find_complete_warpruns.sql");
     128        if (!query) {
     129            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     130            return false;
     131        }
     132
     133        if (!p_psDBRunQuery(config->dbh, query)) {
     134            psError(PS_ERR_UNKNOWN, false, "database error");
     135            psFree(query);
     136            return false;
     137        }
     138        psFree(query);
     139    }
     140
     141    // find the diff_id's of the warped skycells
     142    {
     143        psString query = pxDataGet("magictool_find_complete_diffed_exposures.sql");
     144        if (!query) {
     145            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     146            return false;
     147        }
     148
     149        if (!p_psDBRunQuery(config->dbh, query)) {
     150            psError(PS_ERR_UNKNOWN, false, "database error");
     151            psFree(query);
     152            return false;
     153        }
     154        psFree(query);
     155    }
     156    psArray *output = p_psDBFetchResult(config->dbh);
     157    if (!output) {
     158        psErrorCode err = psErrorCodeLast();
     159        switch (err) {
     160          case PS_ERR_DB_CLIENT:
     161            psError(PXTOOLS_ERR_SYS, false, "database error");
     162          case PS_ERR_DB_SERVER:
     163            psError(PXTOOLS_ERR_PROG, false, "database error");
     164          default:
     165            psError(PXTOOLS_ERR_PROG, false, "unknown error");
     166        }
     167
     168        return false;
     169    }
     170    if (!psArrayLength(output)) {
     171        psTrace("magictool", PS_LOG_INFO, "no rows found");
     172        psFree(output);
     173        return true;
     174    }
     175
     176    // pre-allocate enough hash buckets for 1 unique warp_id per row
     177    psHash *groups = psHashAlloc(psArrayLength(output));
     178    // iterate over result set, and group by warp_id
     179    for (long i = 0; i < psArrayLength(output); i++) {
     180        // lookup the key we're grouping by
     181        bool status = false;
     182        psS64 warp_id = psMetadataLookupS64(&status, output->data[i], "warp_id");
     183        if (!status) {
     184            psAbort("failed to lookup value for warp_id");
     185        }
     186
     187        psString key = psDBIntToString(warp_id);
     188        // look to see if there is a bin for this key
     189        psArray *groupBin = psHashLookup(groups, key);
     190        if (!groupBin) {
     191            // if not, create one
     192            groupBin = psArrayAlloc(0);
     193            psHashAdd(groups, key, groupBin);
     194        }
     195        psFree(key);
     196
     197        // add this row to the bin
     198        psArrayAdd(groupBin, 0, output->data[i]);
     199        psFree(groupBin);
     200    }
     201    psFree(output);
     202
     203    // create a magic run
     204    psArray *grouped =  psHashToArray(groups);
     205    psFree(groups);
     206
     207    // iterate over array of grouped warp_ids
     208    for (long i = 0; i < psArrayLength(grouped); i++) {
     209        psArray *group = grouped->data[i];
     210
     211        // create a new magicRun for this group
     212        magicRunRow *run = magicRunRowAlloc(
     213            0,                          // ID
     214            "reg",                      // state
     215            workdir,                    // workdir
     216            "dirty",                    // workdir_state
     217            label,                      // label
     218            dvodb,                      // dvodb
     219            registered,                 // registered
     220            0                           // fault
     221            );
     222        if (!run) {
     223            psAbort("failed to alloc magicRun object");
     224        }
     225        if (!magicRunInsertObject(config->dbh, run)) {
     226            psError(PS_ERR_UNKNOWN, false, "database error");
     227            psFree(run);
     228            psFree(grouped);
     229            return false;
     230        }
     231
     232        // get the assigned warp_id
     233        psS64 magic_id = psDBLastInsertID(config->dbh);
     234
     235        // insert all rows in this bin into the new magicRun
     236        for (long j = 0; j < psArrayLength(group); j++) {
     237            psMetadata *row = group->data[j];
     238
     239            bool status = false;
     240            psS64 diff_id = psMetadataLookupS64(&status, row, "diff_id");
     241            if (!status) {
     242                psAbort("failed to lookup value for diff_id");
     243            }
     244            psString node = psMetadataLookupStr(&status, row, "skycell_id");
     245            if (!status) {
     246                psAbort("failed to lookup value for diff_id");
     247            }
     248
     249            if (!magicInputSkyfileInsert(config->dbh,
     250                                         magic_id,
     251                                         diff_id,
     252                                         node)) {
     253                psError(PS_ERR_UNKNOWN, false, "database error");
     254                psFree(grouped);
     255                return false;
     256            }
     257        }
     258
     259        // set magicRun.state to 'run'
     260        return setmagicRunState(config, magic_id, "run");
     261    }
     262    psFree(grouped);
     263
     264    return true;
     265}
     266
     267static psS64 definerunMode(pxConfig *config)
     268{
     269    PS_ASSERT_PTR_NON_NULL(config, false);
     270
     271    // required
     272    PXOPT_LOOKUP_STR(workdir, config->args, "-workdir", true, false);
     273
     274    // optional
     275    PXOPT_LOOKUP_STR(label, config->args, "-label", false, false);
     276    PXOPT_LOOKUP_STR(dvodb, config->args, "-dvodb", false, false);
     277    PXOPT_LOOKUP_TIME(registered, config->args, "-registered", false, false);
     278    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     279
     280    magicRunRow *run = magicRunRowAlloc(
     281            0,          // ID
     282            "reg",      // state
     283            workdir,
     284            "dirty",    // workdir_state
     285            label,
     286            dvodb,
     287            registered,
     288            0
     289    );
     290
     291    if (!run) {
     292        psError(PS_ERR_UNKNOWN, false, "failed to alloc magicRun object");
     293        return false;
     294    }
     295    if (!magicRunInsertObject(config->dbh, run)) {
     296        psError(PS_ERR_UNKNOWN, false, "database error");
     297        psFree(run);
     298        return false;
     299    }
     300
     301    // get the assigned warp_id
     302    psS64 magic_id = psDBLastInsertID(config->dbh);
     303    run->magic_id = magic_id;
     304
     305    if (!magicRunPrintObject(stdout, run, !simple)) {
     306            psError(PS_ERR_UNKNOWN, false, "failed to print object");
     307            psFree(run);
     308            return false;
     309    }
     310
     311    psFree(run);
     312
     313    return magic_id;
     314}
     315
     316
     317static bool updaterunMode(pxConfig *config)
     318{
     319    PS_ASSERT_PTR_NON_NULL(config, false);
     320
     321    // required
     322    PXOPT_LOOKUP_STR(magic_id, config->args, "-magic_id", true, false);
     323    PXOPT_LOOKUP_STR(state, config->args, "-state", true, false);
     324
     325    if (state) {
     326        // set detRun.state to state
     327        return setmagicRunState(config, (psS64)atoll(magic_id), state);
     328    }
     329
     330    return true;
     331}
     332
     333
     334static bool addinputskyfileMode(pxConfig *config)
     335{
     336    PS_ASSERT_PTR_NON_NULL(config, false);
     337
     338    // required
     339    PXOPT_LOOKUP_STR(magic_id, config->args, "-magic_id", true, false);
     340    PXOPT_LOOKUP_STR(diff_id, config->args, "-diff_id", true, false);
     341    PXOPT_LOOKUP_STR(node, config->args, "-node", true, false);
     342
     343    magicInputSkyfileInsert(
     344            config->dbh,
     345            (psS64)atoll(magic_id),
     346            (psS64)atoll(diff_id),
     347            node
     348    );
     349
     350    return true;
     351}
     352
     353
     354static bool inputskyfileMode(pxConfig *config)
     355{
     356    PS_ASSERT_PTR_NON_NULL(config, false);
     357
     358    psMetadata *where = psMetadataAlloc();
     359    PXOPT_COPY_STR(config->args, where, "-magic_id", "magic_id", "==");
     360    PXOPT_COPY_STR(config->args, where, "-diff_id", "diff_id", "==");
     361    PXOPT_COPY_STR(config->args, where, "-node", "node", "==");
     362
     363    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     364    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     365
     366    // find all rawImfiles matching the default query
     367    psString query = pxDataGet("magictool_inputskyfile.sql");
    122368    if (!query) {
    123369        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     
    125371    }
    126372
     373    if (psListLength(where->list)) {
     374        psString whereClause = psDBGenerateWhereConditionSQL(where, "magicInputSkyfile");
     375        psStringAppend(&query, " AND %s", whereClause);
     376        psFree(whereClause);
     377    }
     378    psFree(where);
     379
     380    // treat limit == 0 as "no limit"
     381    if (limit) {
     382        psString limitString = psDBGenerateLimitSQL(limit);
     383        psStringAppend(&query, " %s", limitString);
     384        psFree(limitString);
     385    }
     386
    127387    if (!p_psDBRunQuery(config->dbh, query)) {
    128388        psError(PS_ERR_UNKNOWN, false, "database error");
     
    131391    }
    132392    psFree(query);
    133 }
    134 
    135     // find the diff_id's of the warped skycells
    136 {
    137     psString query = pxDataGet("magictool_find_complete_diffed_exposures.sql");
    138     if (!query) {
    139         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    140         return false;
    141     }
    142 
    143     if (!p_psDBRunQuery(config->dbh, query)) {
    144         psError(PS_ERR_UNKNOWN, false, "database error");
    145         psFree(query);
    146         return false;
    147     }
    148     psFree(query);
    149 }
     393
    150394    psArray *output = p_psDBFetchResult(config->dbh);
    151395    if (!output) {
     
    168412    }
    169413
    170     // pre-allocate enough hash buckets for 1 unique warp_id per row
    171     psHash *groups = psHashAlloc(psArrayLength(output));
    172     // iterate over result set, and group by warp_id
    173     for (long i = 0; i < psArrayLength(output); i++) {
    174         // lookup the key we're grouping by
    175         bool status = false;
    176         psS64 warp_id = psMetadataLookupS64(&status, output->data[i], "warp_id");
    177         if (!status) {
    178             psAbort("failed to lookup value for warp_id");
    179         }
    180 
    181         psString key = psDBIntToString(warp_id);
    182         // look to see if there is a bin for this key
    183         psArray *groupBin = psHashLookup(groups, key);
    184         if (!groupBin) {
    185             // if not, create one
    186             groupBin = psArrayAlloc(0);
    187             psHashAdd(groups, key, groupBin);
    188         }
    189         psFree(key);
    190 
    191         // add this row to the bin
    192         psArrayAdd(groupBin, 0, output->data[i]);
    193         psFree(groupBin);
    194     }
     414    if (psArrayLength(output)) {
     415        // negative simple so the default is true
     416        if (!ippdbPrintMetadatas(stdout, output, "magicInputSkyfile", !simple)) {
     417            psError(PS_ERR_UNKNOWN, false, "failed to print array");
     418            psFree(output);
     419            return false;
     420        }
     421    }
     422
    195423    psFree(output);
    196424
    197     // create a magic run
    198     psArray *grouped =  psHashToArray(groups);
    199     psFree(groups);
    200 
    201     // iterate over array of grouped warp_ids
    202     for (long i = 0; i < psArrayLength(grouped); i++) {
    203         psArray *group = grouped->data[i];
    204 
    205         // create a new magicRun for this group
    206         // XXX This need to have options passed through to it
    207 
    208         magicRunRow *run = magicRunRowAlloc(
    209                 0,          // ID
    210                 "reg",      // state
    211                 NULL,       // workdir
    212                 "dirty",    // workdir_state
    213                 NULL,       // label
    214                 NULL,       // dvodb
    215                 NULL,       // registered
    216                 0           // fault
    217         );
    218         if (!run) {
    219             psAbort("failed to alloc magicRun object");
    220         }
    221         if (!magicRunInsertObject(config->dbh, run)) {
    222             psError(PS_ERR_UNKNOWN, false, "database error");
    223             psFree(run);
    224             psFree(grouped);
    225             return false;
    226         }
    227 
    228         // get the assigned warp_id
    229         psS64 magic_id = psDBLastInsertID(config->dbh);
    230 
    231         // insert all rows in this bin into the new magicRun
    232         for (long j = 0; j < psArrayLength(group); j++) {
    233             psMetadata *row = group->data[j];
    234 
    235             bool status = false;
    236             psS64 diff_id = psMetadataLookupS64(&status, row, "diff_id");
    237             if (!status) {
    238                 psAbort("failed to lookup value for diff_id");
    239             }
    240             psString node = psMetadataLookupStr(&status, row, "skycell_id");
    241             if (!status) {
    242                 psAbort("failed to lookup value for diff_id");
    243             }
    244 
    245             if (!magicInputSkyfileInsert(config->dbh,
    246                 magic_id,
    247                 diff_id,
    248                 node)) {
    249                 psError(PS_ERR_UNKNOWN, false, "database error");
    250                 psFree(grouped);
    251                 return false;
    252             }
    253         }
    254 
    255         // set magicRun.state to 'run'
    256         return setmagicRunState(config, magic_id, "run");
    257     }
    258     psFree(grouped);
    259 
    260     return true;
    261 }
    262 
    263 static psS64 definerunMode(pxConfig *config)
    264 {
    265     PS_ASSERT_PTR_NON_NULL(config, false);
    266 
    267     // required
    268     PXOPT_LOOKUP_STR(workdir, config->args, "-workdir", true, false);
    269 
    270     // optional
    271     PXOPT_LOOKUP_STR(label, config->args, "-label", false, false);
    272     PXOPT_LOOKUP_STR(dvodb, config->args, "-dvodb", false, false);
    273     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    274     PXOPT_LOOKUP_TIME(registered, config->args, "-registered", false, false);
    275 
    276     magicRunRow *run = magicRunRowAlloc(
    277             0,          // ID
    278             "reg",      // state
    279             workdir,
    280             "dirty",    // workdir_state
    281             label,
    282             dvodb,
    283             registered,
    284             0
    285     );
    286 
    287     if (!run) {
    288         psError(PS_ERR_UNKNOWN, false, "failed to alloc magicRun object");
    289         return false;
    290     }
    291     if (!magicRunInsertObject(config->dbh, run)) {
    292         psError(PS_ERR_UNKNOWN, false, "database error");
    293         psFree(run);
    294         return false;
    295     }
    296 
    297     // get the assigned warp_id
    298     psS64 magic_id = psDBLastInsertID(config->dbh);
    299     run->magic_id = magic_id;
    300 
    301     if (!magicRunPrintObject(stdout, run, !simple)) {
    302             psError(PS_ERR_UNKNOWN, false, "failed to print object");
    303             psFree(run);
    304             return false;
    305     }
    306 
    307     psFree(run);
    308 
    309     return magic_id;
    310 }
    311 
    312 
    313 static bool updaterunMode(pxConfig *config)
    314 {
    315     PS_ASSERT_PTR_NON_NULL(config, false);
    316 
    317     // required
    318     PXOPT_LOOKUP_STR(magic_id, config->args, "-magic_id", true, false);
    319     PXOPT_LOOKUP_STR(state, config->args, "-state", true, false);
    320 
    321     if (state) {
    322         // set detRun.state to state
    323         return setmagicRunState(config, (psS64)atoll(magic_id), state);
    324     }
    325 
    326     return true;
    327 }
    328 
    329 
    330 static bool addinputskyfileMode(pxConfig *config)
    331 {
    332     PS_ASSERT_PTR_NON_NULL(config, false);
    333 
    334     // required
    335     PXOPT_LOOKUP_STR(magic_id, config->args, "-magic_id", true, false);
    336     PXOPT_LOOKUP_STR(diff_id, config->args, "-diff_id", true, false);
    337     PXOPT_LOOKUP_STR(node, config->args, "-node", true, false);
    338 
    339     magicInputSkyfileInsert(
    340             config->dbh,
    341             (psS64)atoll(magic_id),
    342             (psS64)atoll(diff_id),
    343             node
    344     );
    345 
    346     return true;
    347 }
    348 
    349 
    350 static bool inputskyfileMode(pxConfig *config)
    351 {
    352     PS_ASSERT_PTR_NON_NULL(config, false);
     425    return true;
     426}
     427
     428static bool totreeMode(pxConfig *config)
     429{
     430    PS_ASSERT_PTR_NON_NULL(config, false);
     431
     432    psMetadata *where = psMetadataAlloc();
     433    PXOPT_COPY_STR(config->args, where, "-magic_id", "magicRun.magic_id", "==");
    353434
    354435    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
    355436    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    356437
    357     // find all rawImfiles matching the default query
    358     psString query = pxDataGet("magictool_inputskyfile.sql");
     438    // look for "inputs" that need to processed
     439    psString query = pxDataGet("magictool_totree.sql");
    359440    if (!query) {
    360441        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     
    362443    }
    363444
    364     if (config->where) {
    365         psString whereClause = psDBGenerateWhereConditionSQL(config->where, "magicInputSkyfile");
     445    if (psListLength(where->list)) {
     446        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    366447        psStringAppend(&query, " AND %s", whereClause);
    367448        psFree(whereClause);
    368449    }
     450    psFree(where);
    369451
    370452    // treat limit == 0 as "no limit"
     
    404486    if (psArrayLength(output)) {
    405487        // negative simple so the default is true
    406         if (!ippdbPrintMetadatas(stdout, output, "magicInputSkyfile", !simple)) {
     488        if (!ippdbPrintMetadatas(stdout, output, "totree", !simple)) {
    407489            psError(PS_ERR_UNKNOWN, false, "failed to print array");
    408490            psFree(output);
     
    416498}
    417499
    418 
    419 static bool totreeMode(pxConfig *config)
    420 {
    421     PS_ASSERT_PTR_NON_NULL(config, false);
     500static bool inputtreeMode(pxConfig *config)
     501{
     502    PS_ASSERT_PTR_NON_NULL(config, false);
     503
     504    // required
     505    PXOPT_LOOKUP_STR(magic_id, config->args, "-magic_id", true, false);
     506    PXOPT_LOOKUP_STR(dep_file, config->args, "-dep_file", true, false);
     507
     508    if (!parseAndInsertNodeDeps(config, (psS64)atoll(magic_id), dep_file)) {
     509        psError(PS_ERR_UNKNOWN, false, "failed to parse file");
     510        return false;
     511    }
     512
     513    return true;
     514}
     515
     516static bool inputsMode(pxConfig *config)
     517{
     518    PS_ASSERT_PTR_NON_NULL(config, false);
     519
     520    psMetadata *where = psMetadataAlloc();
     521    PXOPT_COPY_STR(config->args, where, "-magic_id", "magic_id", "==");
     522    PXOPT_COPY_STR(config->args, where, "-node", "node", "==");
    422523
    423524    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
    424525    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    425526
    426     // look for "inputs" that need to processed
    427     psString query = pxDataGet("magictool_totree.sql");
     527    psString query = pxDataGet("magictool_inputs.sql");
    428528    if (!query) {
    429529        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    430530        return false;
    431531    }
     532
     533    if (psListLength(where->list)) {
     534        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     535        psStringAppend(&query, " AND %s", whereClause);
     536        psFree(whereClause);
     537    }
     538    psFree(where);
    432539
    433540    // treat limit == 0 as "no limit"
     
    467574    if (psArrayLength(output)) {
    468575        // negative simple so the default is true
    469         if (!ippdbPrintMetadatas(stdout, output, "totree", !simple)) {
     576        if (!ippdbPrintMetadatas(stdout, output, "magicNode", !simple)) {
    470577            psError(PS_ERR_UNKNOWN, false, "failed to print array");
    471578            psFree(output);
     
    478585    return true;
    479586}
    480 
    481 
    482 static bool inputtreeMode(pxConfig *config)
    483 {
    484     PS_ASSERT_PTR_NON_NULL(config, false);
    485 
    486     // required
    487     PXOPT_LOOKUP_STR(magic_id, config->args, "-magic_id", true, false);
    488     PXOPT_LOOKUP_STR(dep_file, config->args, "-dep_file", true, false);
    489 
    490     if (!parseAndInsertNodeDeps(config, (psS64)atoll(magic_id), dep_file)) {
    491         psError(PS_ERR_UNKNOWN, false, "failed to parse file");
    492         return false;
    493     }
    494 
    495     return true;
    496 }
    497 
    498587
    499588bool findBaseNodes(void *arg, pxNode *node)
     
    573662    PS_ASSERT_PTR_NON_NULL(config, false);
    574663
    575     PXOPT_LOOKUP_STR(magic_id, config->args, "-magic_id", false, false);
     664    psMetadata *where = psMetadataAlloc();
     665    PXOPT_COPY_STR(config->args, where, "-magic_id", "magic_id", "==");
     666
    576667    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    577668
     
    583674    }
    584675
    585     if (magic_id) {
    586         psMetadata *where = psMetadataAlloc();
    587         psMetadataAddS64(where, PS_LIST_TAIL, "magic_id", 0, "==", (psS64)atoll(magic_id));
    588         psString whereClause = psDBGenerateWhereSQL(where, NULL);
    589         psFree(where);
     676    psString whereClause = NULL;
     677    if (psListLength(where->list)) {
     678        whereClause = psDBGenerateWhereSQL(where, NULL);
    590679        psStringAppend(&query, " %s", whereClause);
    591         psFree(whereClause);
    592     }
     680        psFree(whereClause);
     681    }
     682    psFree(where);
    593683
    594684    if (!p_psDBRunQuery(config->dbh, query)) {
     
    615705    if (!psArrayLength(output)) {
    616706        psTrace("magictool", PS_LOG_INFO, "no rows found");
    617 //        psFree(output);
    618 //        return true;
     707        // psFree(output);
     708        // return true;
    619709    }
    620710
     
    626716    }
    627717
    628 
    629     if (magic_id) {
    630         psMetadata *where = psMetadataAlloc();
    631         psMetadataAddS64(where, PS_LIST_TAIL, "magic_id", 0, "==", (psS64)atoll(magic_id));
    632         psString whereClause = psDBGenerateWhereSQL(where, NULL);
    633         psFree(where);
     718    if (whereClause) {
    634719        psStringAppend(&query, " %s", whereClause);
    635         psFree(whereClause);
    636720    }
    637721
     
    686770    pxNode *root = psHashLookup(forest, "root");
    687771    psFree(forest);
    688 //    pxTreePrint(stdout, root);
     772    // pxTreePrint(stdout, root);
    689773
    690774    // crawl through the tree and looking for nodes with children that are all
     
    703787
    704788    psFree(output);
     789    psFree(whereClause);
    705790
    706791    return true;
     
    735820}
    736821
    737 static bool inputsMode(pxConfig *config)
     822static bool tomaskMode(pxConfig *config)
    738823{
    739824    PS_ASSERT_PTR_NON_NULL(config, false);
     
    742827    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    743828
    744     psString query = pxDataGet("magictool_inputs.sql");
     829    // look for "inputs" that need to processed
     830    psString query = pxDataGet("magictool_tomask.sql");
    745831    if (!query) {
    746832        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    747833        return false;
    748     }
    749 
    750     if (config->where) {
    751         psString whereClause = psDBGenerateWhereConditionSQL(config->where, NULL);
    752         psStringAppend(&query, " AND %s", whereClause);
    753         psFree(whereClause);
    754834    }
    755835
     
    790870    if (psArrayLength(output)) {
    791871        // negative simple so the default is true
    792         if (!ippdbPrintMetadatas(stdout, output, "magicNode", !simple)) {
     872        if (!ippdbPrintMetadatas(stdout, output, "toprocess", !simple)) {
    793873            psError(PS_ERR_UNKNOWN, false, "failed to print array");
    794874            psFree(output);
     
    803883
    804884
    805 static bool tomaskMode(pxConfig *config)
    806 {
    807     PS_ASSERT_PTR_NON_NULL(config, false);
     885static bool addmaskMode(pxConfig *config)
     886{
     887    PS_ASSERT_PTR_NON_NULL(config, false);
     888
     889    // required
     890    PXOPT_LOOKUP_STR(magic_id, config->args, "-magic_id", true, false);
     891
     892    // optional
     893    PXOPT_LOOKUP_STR(uri, config->args, "-uri", false, false);
     894    PXOPT_LOOKUP_S32(streaks, config->args, "-streaks", false, false);
     895
     896    // default values
     897    PXOPT_LOOKUP_S16(code, config->args, "-code", false, false);
     898
     899    if (!psDBTransaction(config->dbh)) {
     900        psError(PS_ERR_UNKNOWN, false, "database error");
     901        return false;
     902    }
     903
     904    if (!magicMaskInsert(config->dbh,
     905                         (psS64)atoll(magic_id),
     906                         uri,
     907                         streaks,
     908                         code
     909        )) {
     910        psError(PS_ERR_UNKNOWN, false, "database error");
     911        return false;
     912    }
     913
     914    // Set the magicRun state
     915    psString query = pxDataGet("magictool_addmask.sql");
     916    if (!query) {
     917        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     918        return false;
     919    }
     920
     921    // manually add constraint
     922    psStringAppend(&query, " AND magic_id = %s", magic_id);
     923
     924    if (!p_psDBRunQuery(config->dbh, query)) {
     925        psError(PS_ERR_UNKNOWN, false, "database error");
     926        psFree(query);
     927        return false;
     928    }
     929    psFree(query);
     930
     931    if (!psDBCommit(config->dbh)) {
     932        psError(PS_ERR_UNKNOWN, false, "database error");
     933        return false;
     934    }
     935
     936    return true;
     937}
     938
     939
     940static bool maskMode(pxConfig *config)
     941{
     942    PS_ASSERT_PTR_NON_NULL(config, false);
     943
     944    psMetadata *where = psMetadataAlloc();
     945    PXOPT_COPY_STR(config->args, where, "-magic_id", "magicRun.magic_id", "==");
    808946
    809947    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
    810948    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    811949
    812     // look for "inputs" that need to processed
    813     psString query = pxDataGet("magictool_tomask.sql");
     950    psString query = pxDataGet("magictool_mask.sql");
    814951    if (!query) {
    815952        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    816953        return false;
    817954    }
     955
     956    if (psListLength(where->list)) {
     957        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     958        psStringAppend(&query, " AND %s", whereClause);
     959        psFree(whereClause);
     960    }
     961    psFree(where);
    818962
    819963    // treat limit == 0 as "no limit"
     
    853997    if (psArrayLength(output)) {
    854998        // negative simple so the default is true
    855         if (!ippdbPrintMetadatas(stdout, output, "toprocess", !simple)) {
    856             psError(PS_ERR_UNKNOWN, false, "failed to print array");
    857             psFree(output);
    858             return false;
    859         }
    860     }
    861 
    862     psFree(output);
    863 
    864     return true;
    865 }
    866 
    867 
    868 static bool addmaskMode(pxConfig *config)
    869 {
    870     PS_ASSERT_PTR_NON_NULL(config, false);
    871 
    872     // required
    873     PXOPT_LOOKUP_STR(magic_id, config->args, "-magic_id", true, false);
    874 
    875     // optional
    876     PXOPT_LOOKUP_STR(uri, config->args, "-uri", false, false);
    877     PXOPT_LOOKUP_S32(streaks, config->args, "-streaks", false, false);
    878 
    879     // default values
    880     PXOPT_LOOKUP_S16(code, config->args, "-code", false, false);
    881 
    882     if (!psDBTransaction(config->dbh)) {
    883         psError(PS_ERR_UNKNOWN, false, "database error");
    884         return false;
    885     }
    886 
    887     if (!magicMaskInsert(config->dbh,
    888                          (psS64)atoll(magic_id),
    889                          uri,
    890                          streaks,
    891                          code
    892         )) {
    893         psError(PS_ERR_UNKNOWN, false, "database error");
    894         return false;
    895     }
    896 
    897     // Set the magicRun state
    898     psString query = pxDataGet("magictool_addmask.sql");
    899     if (!query) {
    900         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    901         return false;
    902     }
    903 
    904     // Add "magic_id = value"
    905     psString whereClause = psDBGenerateWhereConditionSQL(config->where, NULL);
    906     psStringAppend(&query, " AND %s", whereClause);
    907     psFree(whereClause);
    908 
    909     if (!p_psDBRunQuery(config->dbh, query)) {
    910         psError(PS_ERR_UNKNOWN, false, "database error");
    911         psFree(query);
    912         return false;
    913     }
    914     psFree(query);
    915 
    916     if (!psDBCommit(config->dbh)) {
    917         psError(PS_ERR_UNKNOWN, false, "database error");
    918         return false;
    919     }
    920 
    921     return true;
    922 }
    923 
    924 
    925 static bool maskMode(pxConfig *config)
    926 {
    927     PS_ASSERT_PTR_NON_NULL(config, false);
    928 
    929     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
    930     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    931 
    932     psString query = pxDataGet("magictool_mask.sql");
    933     if (!query) {
    934         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    935         return false;
    936     }
    937 
    938     if (config->where) {
    939         psString whereClause = psDBGenerateWhereConditionSQL(config->where, NULL);
    940         psStringAppend(&query, " AND %s", whereClause);
    941         psFree(whereClause);
    942     }
    943 
    944     // treat limit == 0 as "no limit"
    945     if (limit) {
    946         psString limitString = psDBGenerateLimitSQL(limit);
    947         psStringAppend(&query, " %s", limitString);
    948         psFree(limitString);
    949     }
    950 
    951     if (!p_psDBRunQuery(config->dbh, query)) {
    952         psError(PS_ERR_UNKNOWN, false, "database error");
    953         psFree(query);
    954         return false;
    955     }
    956     psFree(query);
    957 
    958     psArray *output = p_psDBFetchResult(config->dbh);
    959     if (!output) {
    960         psErrorCode err = psErrorCodeLast();
    961         switch (err) {
    962             case PS_ERR_DB_CLIENT:
    963                 psError(PXTOOLS_ERR_SYS, false, "database error");
    964             case PS_ERR_DB_SERVER:
    965                 psError(PXTOOLS_ERR_PROG, false, "database error");
    966             default:
    967                 psError(PXTOOLS_ERR_PROG, false, "unknown error");
    968         }
    969 
    970         return false;
    971     }
    972     if (!psArrayLength(output)) {
    973         psTrace("magictool", PS_LOG_INFO, "no rows found");
    974         psFree(output);
    975         return true;
    976     }
    977 
    978     if (psArrayLength(output)) {
    979         // negative simple so the default is true
    980999        if (!ippdbPrintMetadatas(stdout, output, "magicNode", !simple)) {
    9811000            psError(PS_ERR_UNKNOWN, false, "failed to print array");
Note: See TracChangeset for help on using the changeset viewer.