Changeset 25624 for branches/eam_branches/20090715/ippTools/src/disttool.c
- Timestamp:
- Sep 27, 2009, 11:34:07 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/20090715/ippTools/src/disttool.c
r25400 r25624 2 2 * disttool.c 3 3 * 4 * Copyright (C) 2008 4 * Copyright (C) 2008-2009 5 5 * 6 6 * This program is free software; you can redistribute it and/or modify it … … 37 37 static bool pendingcomponentMode(pxConfig *config); 38 38 static bool addprocessedcomponentMode(pxConfig *config); 39 static bool revertcomponentMode(pxConfig *config); 39 40 static bool processedcomponentMode(pxConfig *config); 40 41 static bool toadvanceMode(pxConfig *config); … … 51 52 static bool listtargetMode(pxConfig *config); 52 53 53 static bool definedsproductMode(pxConfig *config);54 static bool updatedsproductMode(pxConfig *config);55 56 54 static bool definedestinationMode(pxConfig *config); 57 55 static bool updatedestinationMode(pxConfig *config); … … 59 57 static bool defineinterestMode(pxConfig *config); 60 58 static bool updateinterestMode(pxConfig *config); 59 static bool listinterestsMode(pxConfig *config); 61 60 62 61 # define MODECASE(caseName, func) \ … … 86 85 MODECASE(DISTTOOL_MODE_ADDPROCESSEDCOMPONENT, addprocessedcomponentMode); 87 86 MODECASE(DISTTOOL_MODE_PROCESSEDCOMPONENT, processedcomponentMode); 87 MODECASE(DISTTOOL_MODE_REVERTCOMPONENT, revertcomponentMode); 88 88 MODECASE(DISTTOOL_MODE_TOADVANCE, toadvanceMode); 89 89 MODECASE(DISTTOOL_MODE_PENDINGFILESET, pendingfilesetMode); … … 97 97 MODECASE(DISTTOOL_MODE_UPDATETARGET, updatetargetMode); 98 98 MODECASE(DISTTOOL_MODE_LISTTARGET, listtargetMode); 99 MODECASE(DISTTOOL_MODE_DEFINEDSPRODUCT, definedsproductMode);100 MODECASE(DISTTOOL_MODE_UPDATEDSPRODUCT, updatedsproductMode);101 99 MODECASE(DISTTOOL_MODE_DEFINEDESTINATION, definedestinationMode); 102 100 MODECASE(DISTTOOL_MODE_UPDATEDESTINATION, updatedestinationMode); 103 101 MODECASE(DISTTOOL_MODE_DEFINEINTEREST, defineinterestMode); 104 102 MODECASE(DISTTOOL_MODE_UPDATEINTEREST, updateinterestMode); 103 MODECASE(DISTTOOL_MODE_LISTINTERESTS, listinterestsMode); 105 104 default: 106 105 psAbort("invalid option (this should not happen)"); … … 151 150 stage, 152 151 stage_id, 152 0, 153 153 set_label, 154 154 outroot, … … 223 223 } 224 224 } 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"; 226 227 query = pxDataGet("disttool_definebyquery_camera.sql"); 227 228 if (!query) { … … 288 289 psStringAppend(&query, " AND (stackRun.label = '%s')", label); 289 290 } 290 // stack stage doesn't require magic (perhaps let the script do this?291 // stack stage doesn't require magic 291 292 no_magic = true; 292 293 } else { … … 306 307 307 308 if (!no_magic) { 308 psStringAppend(&query, " AND ( distTarget.clean OR%s.magicked)", magicRunType);309 psStringAppend(&query, " AND (%s.magicked)", magicRunType); 309 310 310 311 // is selecting by magic_ds_id really interesting? 311 312 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" 320 319 " AND magicDSRun.stage_id = %s", runJoinStr); 321 } else {322 // camera masks are magicked when the chipRun is magicked323 // XXX: This is confusing. Is it dangerous?324 // Maybe I should add a magicked bit to camRun. Note this isn't325 psStringAppend(&joinHook, "\nJOIN magicDSRun ON magicDSRun.stage = 'chip'"326 " AND magicDSRun.stage_id = chipRun.chip_id");327 }328 320 psStringAppend(&query, " AND (magicDSRun.state = 'full' AND magicDSRun.re_place AND (magic_ds_id = %" PRId64 "))", magic_ds_id); 329 321 } … … 377 369 psString run_tag = psMetadataLookupStr(NULL, md, "run_tag"); 378 370 psS64 stage_id = psMetadataLookupS64(NULL, md, "stage_id"); 371 psS64 magic_ds_id = psMetadataLookupS64(NULL, md, "magicked"); 379 372 psS64 target_id = psMetadataLookupS64(NULL, md, "target_id"); 380 373 psString target_label = psMetadataLookupStr(NULL, md, "label"); … … 395 388 stage, 396 389 stage_id, 390 magic_ds_id, 397 391 new_label, 398 392 outroot, … … 500 494 PXOPT_COPY_STR(config->args, where, "-label", "label", "=="); 501 495 502 // we need to disambiguate fault so make a copy of the where list before adding fault503 psMetadata *whereComponent = psMetadataCopy(NULL, where);504 496 PXOPT_COPY_S16(config->args, where, "-fault", "distRun.fault", "=="); 505 PXOPT_COPY_S16(config->args, whereComponent, "-fault", "distComponent.fault", "==");506 497 507 498 // It might be useful to be able to query by the parameters of the underlying runs … … 513 504 } 514 505 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 535 static 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")) { 517 550 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); 584 576 585 577 psLogMsg("disttool", PS_LOG_INFO, "Deleted %d distComponents", numDeleted); 586 578 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 595 579 return true; 596 580 } … … 600 584 PS_ASSERT_PTR_NON_NULL(config, false); 601 585 586 PXOPT_LOOKUP_STR(stage, config->args, "-stage", true, false); 587 602 588 psMetadata *where = psMetadataAlloc(); 603 589 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", "=="); 606 591 607 592 PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false); 608 593 PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false); 609 594 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); 612 598 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); 614 600 return false; 615 601 } … … 617 603 if (psListLength(where->list)) { 618 604 psString whereClause = psDBGenerateWhereConditionSQL(where, NULL); 619 psStringAppend(&query, " WHERE%s", whereClause);605 psStringAppend(&query, " AND %s", whereClause); 620 606 psFree(whereClause); 621 607 } … … 629 615 } 630 616 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)) { 655 618 psError(PS_ERR_UNKNOWN, false, "database error"); 656 619 psFree(query); … … 868 831 psMetadata *where = psMetadataAlloc(); 869 832 PXOPT_COPY_S64(config->args, where, "-dist_id", "dist_id", "=="); 833 PXOPT_COPY_STR(config->args, where, "-stage", "distRun.stage", "=="); 870 834 pxAddLabelSearchArgs (config, where, "-label", "distRun.label", "=="); 871 835 … … 939 903 // required values 940 904 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); 942 906 943 907 PXOPT_LOOKUP_S16(fault, config->args, "-fault", false, false); … … 949 913 0, // fs_id 950 914 dist_id, 951 prod_id,915 dest_id, 952 916 name, 953 917 "full", … … 964 928 PXOPT_COPY_S64(config->args, where, "-fs_id", "fs_id", "=="); 965 929 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", "=="); 967 931 PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");; 968 932 PXOPT_COPY_S64(config->args, where, "-stage_id", "stage_id", "=="); … … 1092 1056 PXOPT_COPY_S64(config->args, where, "-dist_id", "dist_id", "=="); 1093 1057 PXOPT_COPY_S64(config->args, where, "-dest_id", "dest_id", "=="); 1094 PXOPT_COPY_S64(config->args, where, "-prod_id", "prod_id", "==");1095 1058 PXOPT_COPY_S64(config->args, where, "-target_id","target_id", "=="); 1096 1059 PXOPT_COPY_S64(config->args, where, "-fs_id", "fs_id", "=="); … … 1433 1396 } 1434 1397 1435 static bool defined sproductMode(pxConfig *config)1398 static bool definedestinationMode(pxConfig *config) 1436 1399 { 1437 1400 PS_ASSERT_PTR_NON_NULL(config, false); 1438 1401 1439 1402 // 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_id1448 name,1449 dbname,1450 dbhost1451 );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_id1464 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 // required1528 PXOPT_LOOKUP_S64(prod_id, config->args, "-prod_id", true, false);1529 1403 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); 1530 1406 1531 1407 // optional … … 1539 1415 rcDestinationRow *row = rcDestinationRowAlloc( 1540 1416 0, // dest_id 1541 prod_id,1542 1417 name, 1543 1418 status_uri, 1544 1419 comment, 1545 1420 last_fileset, 1421 dbname, 1422 dbhost, 1546 1423 state ? state : "enabled" 1547 1424 ); … … 1577 1454 psMetadata *where = psMetadataAlloc(); 1578 1455 PXOPT_COPY_S64(config->args, where, "-dest_id", "dest_id", "=="); 1579 PXOPT_COPY_S64(config->args, where, "-prod_id", "prod_id", "==");1580 1456 1581 1457 PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, false); … … 1599 1475 // last_fileset normally gets set by updatercrunMode 1600 1476 // Allowing it to be set here might cause problems 1601 // especially since we are allowing selection by prod_id1477 // especially since we are allowing selection by dest_id 1602 1478 if (last_fileset) { 1603 1479 psStringAppend(&query, " %s last_fileset = '%s'", sep, last_fileset); … … 1631 1507 PS_ASSERT_PTR_NON_NULL(config, false); 1632 1508 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 } 1636 1538 1637 1539 // optional 1540 PXOPT_LOOKUP_S64(limit, config->args, "-limit", false, false); 1638 1541 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); 1669 1607 1670 1608 return true; … … 1710 1648 return true; 1711 1649 } 1712 1650 static 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.
