IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 24, 2010, 2:59:09 PM (16 years ago)
Author:
Paul Price
Message:

Merging trunk in advance of reintegrating into trunk.

Location:
branches/pap
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/pap

  • branches/pap/ippTools/src

    • Property svn:ignore
      •  

        old new  
        3939warptool
        4040staticskytool
        41 
        4241bgtool
         42diffphottool
  • branches/pap/ippTools/src/addtool.c

    r27773 r28484  
    4040static bool maskedMode(pxConfig *config);
    4141static bool unblockMode(pxConfig *config);
     42static bool addminidvodbrunMode(pxConfig *config);
     43static bool updateminidvodbrunMode(pxConfig *config);
     44static bool listminidvodbrunMode(pxConfig *config);
     45static bool flipminidvodbrunMode(pxConfig *config);
     46static bool checkminidvodbrunaddrunMode(pxConfig *config);
     47static bool addminidvodbprocessedMode(pxConfig *config);
     48static bool listminidvodbprocessedMode(pxConfig *config);
     49static bool revertminidvodbprocessedMode(pxConfig *config);
     50static bool updateminidvodbprocessedMode(pxConfig *config);
     51
     52
    4253
    4354# define MODECASE(caseName, func) \
     
    6980        MODECASE(ADDTOOL_MODE_MASKED,               maskedMode);
    7081        MODECASE(ADDTOOL_MODE_UNBLOCK,              unblockMode);
     82        MODECASE(ADDTOOL_MODE_ADDMINIDVODBRUN,      addminidvodbrunMode);
     83        MODECASE(ADDTOOL_MODE_UPDATEMINIDVODBRUN,   updateminidvodbrunMode);
     84        MODECASE(ADDTOOL_MODE_LISTMINIDVODBRUN,     listminidvodbrunMode);
     85        MODECASE(ADDTOOL_MODE_FLIPMINIDVODBRUN,     flipminidvodbrunMode);
     86        MODECASE(ADDTOOL_MODE_CHECKMINIDVODBRUNADDRUN, checkminidvodbrunaddrunMode);
     87        MODECASE(ADDTOOL_MODE_ADDMINIDVODBPROCESSED,addminidvodbprocessedMode);
     88        MODECASE(ADDTOOL_MODE_LISTMINIDVODBPROCESSED,listminidvodbprocessedMode);
     89        MODECASE(ADDTOOL_MODE_REVERTMINIDVODBPROCESSED,revertminidvodbprocessedMode);
     90        MODECASE(ADDTOOL_MODE_UPDATEMINIDVODBPROCESSED,updateminidvodbprocessedMode);
     91
    7192        default:
    7293            psAbort("invalid option (this should not happen)");
     
    102123    PXOPT_COPY_STR(config->args, where,  "-reduction", "camRun.reduction", "==");
    103124
     125
    104126    if (!psListLength(where->list)) {
    105127        psFree(where);
     
    114136    PXOPT_LOOKUP_STR(reduction,   config->args, "-set_reduction", false, false);
    115137    PXOPT_LOOKUP_STR(note,        config->args, "-set_note", false, false);
     138    PXOPT_LOOKUP_STR(minidvodb_name,  config->args, "-set_minidvodb_name", false, false);
     139    PXOPT_LOOKUP_STR(minidvodb_group, config->args, "-set_minidvodb_group", false, false);
    116140    PXOPT_LOOKUP_BOOL(image_only, config->args, "-image_only", false);
     141    PXOPT_LOOKUP_BOOL(minidvodb,  config->args, "-set_minidvodb", false);
    117142    PXOPT_LOOKUP_BOOL(destreaked, config->args, "-destreaked", false);
    118143    PXOPT_LOOKUP_BOOL(pretend,    config->args, "-pretend", false);
    119144    PXOPT_LOOKUP_BOOL(simple,     config->args, "-simple", false);
     145
     146
    120147
    121148    // find the cam_id of all the exposures that we want to queue up.
     
    245272                               dvodb       ? dvodb     : row->dvodb,
    246273                               note        ? note      : NULL,
    247                                image_only
     274                               image_only,
     275                               minidvodb,
     276                               minidvodb_group,
     277                               minidvodb_name
    248278        )) {
    249279            if (!psDBRollback(config->dbh)) {
    250                 psError(PS_ERR_UNKNOWN, false, "database error");
     280                psError(PS_ERR_UNKNOWN, false, "database error sfg");
    251281            }
    252282            psError(PS_ERR_UNKNOWN, false,
     
    291321
    292322    // pxUpdateRun gets parameters from config->args and runs the update query
    293     bool result = pxUpdateRun(config, where, &query, "addRun", "add_id", 
     323    bool result = pxUpdateRun(config, where, &query, "addRun", "add_id",
    294324        "addProcessedExp", false);
    295325
     
    374404    PXOPT_LOOKUP_STR(path_base,     config->args, "-path_base", false, false);
    375405    PXOPT_LOOKUP_F32(dtime_addstar, config->args, "-dtime_addstar", false, false);
     406    PXOPT_LOOKUP_STR(dvodb_path, config->args, "-dvodb_path", false, false);
     407    PXOPT_LOOKUP_STR(minidvodb_name, config->args, "-minidvodb_name", false, false);
    376408    PXOPT_LOOKUP_S16(fault,         config->args, "-fault", false, false);
    377409
     
    399431        return false;
    400432    }
     433
    401434    psFree(query);
    402435
     
    424457        dtime_addstar,
    425458        path_base,
     459        dvodb_path,
    426460        fault
    427461        );
     
    437471        return false;
    438472    }
     473
     474    //if there is a minidvodb_name, set it in addRun (it's not known until it is processed)
     475    if (minidvodb_name) {
     476      psString setName = NULL;
     477      psStringAppend (&setName, "UPDATE addRun set minidvodb_name = '%s' where add_id = %" PRId64, minidvodb_name, row->add_id);
     478      if (!p_psDBRunQuery(config->dbh, setName)) {
     479        if (!psDBRollback(config->dbh)) {
     480          psError(PS_ERR_UNKNOWN, false, "database error");
     481        }
     482        psError(PS_ERR_UNKNOWN, false, "database error");
     483
     484        return false;
     485      }
     486    }
     487
     488
     489
     490
    439491
    440492    // since there is only one exp per 'new' set addRun.state = 'full'
     
    522574
    523575    if (!p_psDBRunQuery(config->dbh, query)) {
    524         psError(PS_ERR_UNKNOWN, false, "database error");
     576      psError(PS_ERR_UNKNOWN, false, "database error");
    525577        psFree(query);
    526578        return false;
     
    576628
    577629    {
    578         psString query = pxDataGet("addtool_reset_faulted_runs.sql");
     630        psString query = pxDataGet("addtool_revertprocessedexp.sql");
    579631        if (!query) {
     632            // rollback
     633            if (!psDBRollback(config->dbh)) {
     634                psError(PS_ERR_UNKNOWN, false, "database error");
     635            }
    580636            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    581637            psFree(where);
     
    602658        psFree(query);
    603659    }
    604 
    605     {
    606         psString query = pxDataGet("addtool_revertprocessedexp.sql");
    607         if (!query) {
    608             // rollback
    609             if (!psDBRollback(config->dbh)) {
    610                 psError(PS_ERR_UNKNOWN, false, "database error");
    611             }
    612             psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    613             psFree(where);
    614             return false;
    615         }
    616 
    617         // use psDBGenerateWhereConditionalSQL with AND ... because the SQL ends in a WHERE
    618         if (where && psListLength(where->list)) {
    619             psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    620             psStringAppend(&query, " AND %s", whereClause);
    621             psFree(whereClause);
    622         }
    623 
    624         if (!p_psDBRunQuery(config->dbh, query)) {
    625             // rollback
    626             if (!psDBRollback(config->dbh)) {
    627                 psError(PS_ERR_UNKNOWN, false, "database error");
    628             }
    629             psError(PS_ERR_UNKNOWN, false, "database error");
    630             psFree(query);
    631             psFree(where);
    632             return false;
    633         }
    634         psFree(query);
    635     }
    636660    psFree(where);
    637661
     
    733757    return true;
    734758}
     759
     760static bool addminidvodbrunMode(pxConfig *config)
     761{
     762  PS_ASSERT_PTR_NON_NULL(config, NULL);
     763
     764  // required
     765
     766  PXOPT_LOOKUP_STR(minidvodb_group, config->args, "-set_minidvodb_group", true, false);
     767  PXOPT_LOOKUP_STR(mergedvodb_path, config->args, "-set_mergedvodb_path", true, false);
     768
     769  //optional
     770  PXOPT_LOOKUP_STR(minidvodb_name, config->args, "-set_minidvodb_name", false, false);
     771  PXOPT_LOOKUP_STR(minidvodb_path, config->args, "-set_minidvodb_path", false, false);
     772  PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, false);
     773
     774  if (!psDBTransaction(config->dbh)) {
     775    psError(PS_ERR_UNKNOWN, false, "database error");
     776    return false;
     777  }
     778
     779  psString minidvodbpath = "NULL";
     780
     781  // I don't know how to get around the complaints of minidvodb_path can't be null. this 'fixes' it, but someone smarter can fix it properly.
     782  if (minidvodb_path) {
     783    minidvodbpath = minidvodb_path;
     784  }
     785
     786  if (!minidvodbRunInsert(config->dbh,
     787            0, // job_id
     788            minidvodb_name,
     789            minidvodb_group,
     790            minidvodbpath,
     791            mergedvodb_path,
     792            "new",
     793            0
     794            )) {
     795    psError(PS_ERR_UNKNOWN, false, "database error");
     796    return false;
     797  }
     798
     799  psU64 affected = psDBAffectedRows(config->dbh);
     800  if (affected != 1) {
     801    psError(PS_ERR_UNKNOWN, false,
     802            "should have affected one row but %" PRIu64 " rows were modified",
     803            affected);
     804    return false;
     805  }
     806
     807  psS64 minidvodb_id = psDBLastInsertID(config->dbh);
     808  printf("%" PRId64 "\n", minidvodb_id);
     809
     810
     811  if (!minidvodb_name) {
     812    psStringAppend(&minidvodb_name, "%s.%" PRIu64,minidvodb_group,minidvodb_id);
     813  }
     814
     815  if (!minidvodb_path) {
     816    psStringAppend(&minidvodb_path,"%s/%s",mergedvodb_path,minidvodb_name);
     817  }
     818
     819  if (minidvodb_path) {
     820    psStringAppend(&minidvodb_path,"/%s",minidvodb_name);
     821  }
     822
     823
     824  psString query = NULL;
     825
     826  psStringAppend(&query, "UPDATE minidvodbRun SET minidvodb_path = '%s', minidvodb_name = '%s' where minidvodb_id = %" PRIu64";", minidvodb_path, minidvodb_name, minidvodb_id);
     827
     828  if (!p_psDBRunQuery(config->dbh, query)) {
     829    psError(PS_ERR_UNKNOWN, false, "database error");
     830    psFree(query);
     831    return false;
     832  }
     833
     834  if (!psDBCommit(config->dbh)) {
     835    psError(PS_ERR_UNKNOWN, false, "database error");
     836    return false;
     837  }
     838
     839  return true;
     840}
     841
     842static bool updateminidvodbrunMode(pxConfig *config) {
     843  PS_ASSERT_PTR_NON_NULL(config, false);
     844
     845    psMetadata *where = psMetadataAlloc();
     846    PXOPT_COPY_S64(config->args, where, "-minidvodb_id",     "minidvodb_id", "==");
     847    PXOPT_COPY_STR(config->args, where, "-minidvodb_name",     "minidvodb_name", "==");
     848    PXOPT_COPY_STR(config->args, where, "-state",     "state", "==");
     849    PXOPT_COPY_STR(config->args, where, "-minidvodb_path", "minidvodb_path", "==");
     850    PXOPT_COPY_STR(config->args, where, "-mergedvodb_path", "mergedvodb_path", "==");
     851    PXOPT_COPY_STR(config->args, where, "-minidvodb_group",     "minidvodb_group", "==");
     852
     853    PXOPT_LOOKUP_STR(minidvodb_name,  config->args, "-set_minidvodb_name", false, false);
     854    PXOPT_LOOKUP_STR(minidvodb_path,  config->args, "-set_minidvodb_path", false, false);
     855    PXOPT_LOOKUP_STR(state,  config->args, "-set_state", false, false);
     856    PXOPT_LOOKUP_STR(mergedvodb_path,  config->args, "-set_mergedvodb_path", false, false);
     857    PXOPT_LOOKUP_STR(minidvodb_group,  config->args, "-set_minidvodb_group", false, false);
     858
     859
     860    if (!psListLength(where->list)) {
     861      psFree(where);
     862      psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     863      return false;
     864    }
     865
     866    psString query = psStringCopy("UPDATE minidvodbRun SET ");
     867    int cnt = 0;
     868    psString comma = ",";
     869    if (minidvodb_name) {
     870      psStringAppend(&query, " minidvodb_name = '%s'", minidvodb_name);
     871      cnt++;
     872    }
     873
     874    if (minidvodb_path) {
     875      if (cnt) {
     876          psStringAppend(&query, "%s", comma);
     877      }
     878
     879      psStringAppend(&query, " minidvodb_path = '%s'", minidvodb_path);
     880      cnt++;
     881    }
     882
     883    if (state) {
     884      if (cnt) {
     885        psStringAppend(&query, "%s", comma);
     886      }
     887      psStringAppend(&query, " state = '%s'", state);
     888      cnt++;
     889    }
     890
     891    if (mergedvodb_path) {
     892      if (cnt) {
     893        psStringAppend(&query, "%s", comma);
     894      }
     895      psStringAppend(&query, " mergedvodb_path = '%s'", mergedvodb_path);
     896      cnt++;
     897    }
     898
     899    if (minidvodb_group) {
     900      if (cnt) {
     901        psStringAppend(&query, "%s", comma);
     902      }
     903      psStringAppend(&query, " minidvodb_group = '%s'", minidvodb_group);
     904      cnt++;
     905    }
     906
     907    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     908    psStringAppend(&query, " WHERE %s", whereClause);
     909
     910    if (!p_psDBRunQuery(config->dbh, query)) {
     911      psError(PS_ERR_UNKNOWN, false, "database error");
     912      psFree(query);
     913      return false;
     914    }
     915
     916    psFree(query);
     917    psFree(where);
     918
     919    return true;
     920
     921}
     922
     923static bool flipminidvodbrunMode(pxConfig *config) {
     924  PS_ASSERT_PTR_NON_NULL(config, false);
     925
     926  psMetadata *where = psMetadataAlloc();
     927  PXOPT_LOOKUP_STR(minidvodb_group,  config->args, "-minidvodb_group",true, false);
     928  PXOPT_COPY_STR(config->args, where, "-minidvodb_group",     "minidvodb_group", "==");
     929
     930//this flips the new - > active
     931// and the active - > waiting in one action
     932
     933
     934// the first query looks to find things that are new and where all the fields are filled (ie, ready to be flipped to active)
     935
     936// start a transaction eraly so it will contain any row level locks
     937  if (!psDBTransaction(config->dbh)) {
     938    psError(PS_ERR_UNKNOWN, false, "database error");
     939    return false;
     940  }
     941
     942  psString firstquery = psStringCopy("SELECT * from  minidvodbRun where state = 'new' and minidvodb_name is NOT NULL and minidvodb_group IS NOT NULL and minidvodb_path IS NOT NULL and mergedvodb_path IS NOT NULL");
     943
     944  psString firstwhereClause = psDBGenerateWhereConditionSQL(where, NULL);
     945  psStringAppend(&firstquery, " AND %s", firstwhereClause);
     946
     947
     948
     949  if (!p_psDBRunQuery(config->dbh, firstquery)) {
     950    psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     951    if (!psDBRollback(config->dbh)) {
     952      psError(PS_ERR_UNKNOWN, false, "database error");
     953    }
     954    return false;
     955  }
     956  psFree(firstquery);
     957
     958 //we don't care what the stuff is that is found, just that there is stuff. This is a check to see that there is something in the 'new' state, before flipping (so that if there is nothing in new, it won't flip the active to waiting.  the flipminidvo is just to make it easy to flip from new -> active.
     959
     960  psArray *output = p_psDBFetchResult(config->dbh);
     961  if (!output) {
     962    psError(PS_ERR_UNKNOWN, false, "database error");
     963    return false;
     964  }
     965  if (!psArrayLength(output)) {
     966    psTrace("addtool", PS_LOG_INFO, "no rows found");
     967    psFree(output);
     968    return true;
     969  }
     970
     971
     972  //ok, there's something, so flip active -> waiting
     973
     974  psString query = psStringCopy("UPDATE minidvodbRun SET state = 'waiting' WHERE state = 'active' ");
     975
     976  psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     977  psStringAppend(&query, " AND %s", whereClause);
     978
     979
     980  if (!p_psDBRunQuery(config->dbh, query)) {
     981    psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     982    if (!psDBRollback(config->dbh)) {
     983      psError(PS_ERR_UNKNOWN, false, "database error");
     984    }
     985    return false;
     986  }
     987
     988
     989  //now flip new -> active
     990  psString query2 = psStringCopy("UPDATE minidvodbRun SET state = 'active' WHERE state = 'new' AND minidvodb_name is NOT NULL and minidvodb_group IS NOT NULL and minidvodb_path IS NOT NULL and mergedvodb_path IS NOT NULL ");
     991  psStringAppend(&query2, " AND minidvodb_group = '%s' limit 1;", minidvodb_group);
     992  if (!p_psDBRunQuery(config->dbh, query2)) {
     993    psError(PS_ERR_UNKNOWN, false, "database error");
     994    if (!psDBRollback(config->dbh)) {
     995      psError(PS_ERR_UNKNOWN, false, "database error");
     996    }
     997    psFree(query);
     998    return false;
     999  }
     1000
     1001
     1002  if (!psDBCommit(config->dbh)) {
     1003    psError(PS_ERR_UNKNOWN, false, "database error");
     1004    if (!psDBRollback(config->dbh)) {
     1005      psError(PS_ERR_UNKNOWN, false, "database error");
     1006    }
     1007    psFree(query);
     1008  return false;
     1009  }
     1010 psFree(query2);
     1011 psFree(where);
     1012
     1013 return true;
     1014
     1015}
     1016
     1017
     1018static bool checkminidvodbrunaddrunMode(pxConfig *config) {
     1019  PS_ASSERT_PTR_NON_NULL(config, false);
     1020  psMetadata *where = psMetadataAlloc();
     1021
     1022  //this checks to see if a minidvod_group/name is has completed addRun processing
     1023  PXOPT_COPY_STR(config->args, where, "-minidvodb_group", "minidvodbRun.minidvodb_group", "==");
     1024  PXOPT_COPY_STR(config->args, where, "-minidvodb_name", "minidvodbRun.minidvodb_name", "==");
     1025  PXOPT_COPY_STR(config->args, where, "-state", "minidvodbRun.state", "==");
     1026
     1027
     1028  PXOPT_LOOKUP_U64(limit,      config->args, "-limit", false, false);
     1029  PXOPT_LOOKUP_BOOL(all_addrun_states, config->args, "-all_addrun_states", false);
     1030  //this doesn't care what state the addRun is in (useful for counting addRuns)
     1031  PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     1032
     1033  if (!psListLength(where->list)) {
     1034        psFree(where);
     1035        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     1036        return false;
     1037  }
     1038
     1039  psString query = pxDataGet("addtool_checkminidvodbaddrun.sql");
     1040
     1041  if (!query) {
     1042        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     1043        return false;
     1044  }
     1045
     1046
     1047  if (psListLength(where->list)) {
     1048    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1049        psStringAppend(&query, " WHERE %s", whereClause);
     1050        psFree(whereClause);
     1051  }
     1052
     1053  if (!all_addrun_states) {
     1054    psStringAppend(&query, " AND (cnt2 = cnt) ");
     1055  }
     1056
     1057  if (limit) {
     1058    psString limitString = psDBGenerateLimitSQL(limit);
     1059    psStringAppend(&query, " %s", limitString);
     1060    psFree(limitString);
     1061  }
     1062
     1063
     1064  if (!p_psDBRunQuery(config->dbh, query)) {
     1065    psError(PS_ERR_UNKNOWN, false, "database error");
     1066    psFree(query);
     1067    return false;
     1068  }
     1069  psFree(query);
     1070  psArray *output = p_psDBFetchResult(config->dbh);
     1071  if (!output) {
     1072    psError(PS_ERR_UNKNOWN, false, "database error");
     1073    return false;
     1074  }
     1075  if (!psArrayLength(output)) {
     1076    psTrace("addtool", PS_LOG_INFO, "no rows found");
     1077    psFree(output);
     1078    return true;
     1079  }
     1080
     1081  if (!ippdbPrintMetadatas(stdout, output, "minidvodbRun", !simple)) {
     1082    psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1083    psFree(output);
     1084    return false;
     1085  }
     1086
     1087  psFree(output);
     1088
     1089  return true;
     1090}
     1091
     1092
     1093
     1094static bool listminidvodbrunMode(pxConfig *config) {
     1095    PS_ASSERT_PTR_NON_NULL(config, false);
     1096    psMetadata *where = psMetadataAlloc();
     1097
     1098    PXOPT_COPY_STR(config->args, where, "-minidvodb_name", "minidvodbRun.minidvodb_name", "LIKE");
     1099    PXOPT_COPY_STR(config->args, where, "-minidvodb_group", "minidvodbRun.minidvodb_group", "==");
     1100    PXOPT_COPY_S64(config->args, where, "-minidvodb_id", "minidvodbRun.minidvodb_id", "==");
     1101
     1102    PXOPT_COPY_STR(config->args, where, "-state", "minidvodbRun.state", "==");
     1103    PXOPT_LOOKUP_U64(limit,      config->args, "-limit", false, false);
     1104    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     1105
     1106
     1107    if (!psListLength(where->list)) {
     1108      psFree(where);
     1109      psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     1110      return false;
     1111    }
     1112
     1113    psString query = pxDataGet("addtool_find_minidvodbrun.sql");
     1114
     1115    if (!query) {
     1116      psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     1117      return false;
     1118    }
     1119
     1120    if (psListLength(where->list)) {
     1121      psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1122      psStringAppend(&query, " WHERE %s", whereClause);
     1123      psFree(whereClause);
     1124    }
     1125
     1126    if (limit) {
     1127      psString limitString = psDBGenerateLimitSQL(limit);
     1128      psStringAppend(&query, " %s", limitString);
     1129      psFree(limitString);
     1130    }
     1131
     1132    if (!p_psDBRunQuery(config->dbh, query)) {
     1133      psError(PS_ERR_UNKNOWN, false, "database error");
     1134      psFree(query);
     1135      return false;
     1136    }
     1137
     1138    psFree(query);
     1139    psArray *output = p_psDBFetchResult(config->dbh);
     1140    if (!output) {
     1141      psError(PS_ERR_UNKNOWN, false, "database error");
     1142      return false;
     1143    }
     1144    if (!psArrayLength(output)) {
     1145      psTrace("addtool", PS_LOG_INFO, "no rows found");
     1146      psFree(output);
     1147      return true;
     1148    }
     1149
     1150    if (!ippdbPrintMetadatas(stdout, output, "minidvodbRun",  !simple)) {
     1151      psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1152      psFree(output);
     1153      return false;
     1154    }
     1155
     1156    psFree(output);
     1157
     1158return true;
     1159}
     1160
     1161static bool addminidvodbprocessedMode(pxConfig *config) {
     1162
     1163  PS_ASSERT_PTR_NON_NULL(config, false);
     1164
     1165    // required
     1166  PXOPT_LOOKUP_U64(minidvodb_id, config->args, "-minidvodb_id", true, false);
     1167  PXOPT_LOOKUP_STR(mergedvodb_path, config->args, "-mergedvodb_path", true, false);
     1168  PXOPT_LOOKUP_STR(minidvodb_group, config->args, "-minidvodb_group", true, false);
     1169
     1170    // optional
     1171  PXOPT_LOOKUP_U64(merge_order,     config->args, "-merge_order", false, false);
     1172  PXOPT_LOOKUP_F32(dtime_relphot, config->args, "-dtime_relphot", false, false);
     1173  PXOPT_LOOKUP_F32(dtime_resort, config->args, "-dtime_resort", false, false);
     1174  PXOPT_LOOKUP_F32(dtime_merge, config->args, "-dtime_merge", false, false);
     1175  PXOPT_LOOKUP_TIME(epoch, config->args, "-epoch", false, false);
     1176  PXOPT_LOOKUP_S16(fault,         config->args, "-fault", false, false);
     1177    //generate restrictions
     1178  psMetadata *where = psMetadataAlloc();
     1179  PXOPT_COPY_S64(config->args, where, "-minidvodb_id",   "minidvodbRun.minidvodb_id",   "==");
     1180
     1181
     1182  if (!psDBTransaction(config->dbh)) {
     1183    psError(PS_ERR_UNKNOWN, false, "database error");
     1184    return false;
     1185  }
     1186
     1187
     1188  psString query = pxDataGet("addtool_find_pendingmergeprocess.sql");
     1189  if (!query) {
     1190    psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     1191    return false;
     1192  }
     1193  // use psDBGenerateWhereSQL because the SQL yields an intermediate table
     1194  if (psListLength(where->list)) {
     1195    psString whereClaus = psDBGenerateWhereConditionSQL(where, NULL);
     1196    psStringAppend(&query, " AND %s", whereClaus);
     1197    psFree(whereClaus);
     1198  }
     1199  psFree(where);
     1200
     1201  if (!p_psDBRunQuery(config->dbh, query)) {
     1202    psError(PS_ERR_UNKNOWN, false, "database error");
     1203    psFree(query);
     1204    return false;
     1205  }
     1206  psFree(query);
     1207
     1208  psArray *output = p_psDBFetchResult(config->dbh);
     1209  if (!output) {
     1210    psError(PS_ERR_UNKNOWN, false, "database error");
     1211    return false;
     1212  }
     1213  if (!psArrayLength(output)) {
     1214    psTrace("addtool", PS_LOG_INFO, "no rows found");
     1215    psFree(output);
     1216    return true;
     1217  }
     1218
     1219  minidvodbRunRow *pendingRow = minidvodbRunObjectFromMetadata(output->data[0]);
     1220  psFree(output);
     1221  minidvodbProcessedRow *row = minidvodbProcessedRowAlloc(
     1222               pendingRow->minidvodb_id,
     1223               merge_order,
     1224               dtime_resort,
     1225               dtime_relphot,
     1226               dtime_merge,
     1227               epoch,
     1228               mergedvodb_path,
     1229               fault
     1230               );
     1231
     1232  if (!minidvodbProcessedInsertObject(config->dbh, row)) {
     1233    // rollback
     1234    if (!psDBRollback(config->dbh)) {
     1235      psError(PS_ERR_UNKNOWN, false, "database error");
     1236    }
     1237    psError(PS_ERR_UNKNOWN, false, "database error");
     1238    psFree(row);
     1239    psFree(pendingRow);
     1240    return false;
     1241  }
     1242
     1243
     1244
     1245
     1246  //this finds the # of merged things (for the merge order)
     1247  psString query3 = NULL;
     1248  psStringAppend(&query3, "select count(*) from minidvodbRun join minidvodbProcessed using (minidvodb_id) where state = 'merged' and minidvodb_group = '%s';", minidvodb_group);
     1249
     1250  if (!p_psDBRunQuery(config->dbh, query3)) {
     1251    // rollback
     1252    if (!psDBRollback(config->dbh)) {
     1253      psError(PS_ERR_UNKNOWN, false, "database error");
     1254    }
     1255    psError(PS_ERR_UNKNOWN, false, "database error");
     1256    psFree(query3);
     1257    return false;
     1258  }
     1259  psArray *output2 = p_psDBFetchResult(config->dbh);
     1260  if (!output2) {
     1261    psError(PS_ERR_UNKNOWN, false, "database error");
     1262    return false;
     1263  }
     1264  if (!psArrayLength(output2)) {
     1265    psTrace("addtool", PS_LOG_INFO, "no rows found");
     1266    psFree(output);
     1267    return true;
     1268  }
     1269  bool status;
     1270  psS64 m_order = psMetadataLookupS64(&status, output2->data[0], "count(*)");
     1271  if (!status) {
     1272
     1273    psAbort("failed to lookup value for count column");
     1274    return false;
     1275  }
     1276  psString final = NULL;
     1277  psStringAppend(&final, "%" PRIu64, m_order);
     1278    //return false;
     1279  psFree(query3);
     1280  psFree(output2);
     1281
     1282
     1283
     1284  //update the merge_order
     1285
     1286  psString query4 = NULL;
     1287  psStringAppend(&query4, "update minidvodbProcessed set merge_order = %"PRIu64,m_order);
     1288  psStringAppend(&query4," where minidvodb_id = %" PRIu64,  minidvodb_id);
     1289  printf("%s", query4);
     1290  if (!p_psDBRunQuery(config->dbh, query4)) {
     1291    // rollback
     1292    if (!psDBRollback(config->dbh)) {
     1293      psError(PS_ERR_UNKNOWN, false, "database error");
     1294    }
     1295    psError(PS_ERR_UNKNOWN, false, "database error");
     1296    psFree(query4);
     1297    return false;
     1298  }
     1299  //
     1300psFree(query4);
     1301
     1302
     1303
     1304// since there is only one exp per 'new' set mindvodbRun.state = 'merged'
     1305
     1306  psString query2 = NULL ;
     1307  psStringAppend(&query2, "UPDATE minidvodbRun SET state = 'merged' WHERE minidvodb_id = %'" PRIu64, row->minidvodb_id);
     1308
     1309  if (!p_psDBRunQuery(config->dbh, query2)) {
     1310    // rollback
     1311    if (!psDBRollback(config->dbh)) {
     1312      psError(PS_ERR_UNKNOWN, false, "database error");
     1313    }
     1314    psError(PS_ERR_UNKNOWN, false, "database error");
     1315    psFree(query2);
     1316    return false;
     1317  }
     1318
     1319
     1320
     1321
     1322
     1323
     1324
     1325
     1326  psFree(row);
     1327  psFree(pendingRow);
     1328
     1329
     1330
     1331
     1332
     1333  //commit the changes
     1334  if (!psDBCommit(config->dbh)) {
     1335    psError(PS_ERR_UNKNOWN, false, "database error");
     1336    return false;
     1337  }
     1338
     1339
     1340  //print the merge_order (why not!)
     1341  printf("%s", final);
     1342  psFree(final);
     1343
     1344  return true;
     1345}
     1346
     1347
     1348
     1349static bool listminidvodbprocessedMode(pxConfig *config) {
     1350  psMetadata *where = psMetadataAlloc();
     1351  PXOPT_COPY_S64(config->args, where, "-minidvodb_id", "minidvodbProcessed.minidvodb_id", "==");
     1352  PXOPT_COPY_STR(config->args, where, "-minidvodb_name", "minidvodbProcessed.minidvodb_name", "==");
     1353  PXOPT_COPY_STR(config->args, where, "-minidvodb_group", "minidvodbProcessed.minidvodb_group", "==");
     1354  PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     1355  PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     1356  PXOPT_LOOKUP_BOOL(faulted, config->args, "-faulted", false);
     1357  if (!psListLength(where->list)) {
     1358        psFree(where);
     1359        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     1360        return false;
     1361    }
     1362
     1363  psString query = pxDataGet("addtool_find_minidvodbprocessed.sql");
     1364    if (!query) {
     1365        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     1366        return false;
     1367    }
     1368
     1369if (psListLength(where->list)) {
     1370        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1371        psStringAppend(&query, " WHERE %s", whereClause);
     1372        psFree(whereClause);
     1373    }
     1374
     1375// we either add AND (condition) or WHERE (condition):
     1376    if (where->list && faulted) {
     1377        // list only faulted rows
     1378        psStringAppend(&query, " %s", " AND minidvodbProcessed.fault != 0");
     1379    }
     1380    if (where->list && !faulted) {
     1381        // don't list faulted rows
     1382        psStringAppend(&query, " %s", " AND minidvodbProcessed.fault = 0");
     1383    }
     1384    if (!where->list && faulted) {
     1385        // list only faulted rows
     1386        psStringAppend(&query, " %s", " WHERE minidvodbProcessed.fault != 0");
     1387    }
     1388    if (!where->list && !faulted) {
     1389        // don't list faulted rows
     1390        psStringAppend(&query, " %s", " WHERE minidvodbProcessed.fault = 0");
     1391    }
     1392    psFree(where);
     1393
     1394    // order by epoch
     1395    psStringAppend(&query, " ORDER BY minidvodb_id");
     1396
     1397    // treat limit == 0 as "no limit"
     1398    if (limit) {
     1399        psString limitString = psDBGenerateLimitSQL(limit);
     1400        psStringAppend(&query, " %s", limitString);
     1401        psFree(limitString);
     1402    }
     1403
     1404    if (!p_psDBRunQuery(config->dbh, query)) {
     1405      psError(PS_ERR_UNKNOWN, false, "database error ");
     1406        psFree(query);
     1407        return false;
     1408    }
     1409    psFree(query);
     1410
     1411    psArray *output = p_psDBFetchResult(config->dbh);
     1412    if (!output) {
     1413        psError(PS_ERR_UNKNOWN, false, "database error");
     1414        return false;
     1415    }
     1416    if (!psArrayLength(output)) {
     1417        psTrace("addtool", PS_LOG_INFO, "no rows found");
     1418        psFree(output);
     1419        return true;
     1420    }
     1421
     1422    // negate simple so the default is true
     1423    if (!ippdbPrintMetadatas(stdout, output, "minidvodbProcessed", !simple)) {
     1424        psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1425        psFree(output);
     1426        return false;
     1427    }
     1428
     1429    psFree(output);
     1430
     1431return true;
     1432}
     1433
     1434
     1435
     1436static bool revertminidvodbprocessedMode(pxConfig *config) {
     1437  psMetadata *where = psMetadataAlloc();
     1438  PS_ASSERT_PTR_NON_NULL(config, false);
     1439  PXOPT_COPY_S64(config->args, where, "-minidvodb_id", "minidvodbProcessed.minidvodb_id", "==");
     1440  PXOPT_COPY_STR(config->args, where, "-minidvodb_group", "addRun.minidvodb_group", "==");
     1441
     1442  if (!psListLength(where->list) && !psMetadataLookupBool(NULL, config->args, "-all")) {
     1443    psFree(where);
     1444    psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     1445    return false;
     1446  }
     1447
     1448  if (!psDBTransaction(config->dbh)) {
     1449      psError(PS_ERR_UNKNOWN, false, "database error");
     1450      psFree(where);
     1451      return false;
     1452  }
     1453
     1454  {
     1455    psString query = pxDataGet("addtool_revertminidvodbprocessed.sql");
     1456    if (!query) {
     1457      // rollback
     1458      if (!psDBRollback(config->dbh)) {
     1459        psError(PS_ERR_UNKNOWN, false, "database error");
     1460      }
     1461      psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     1462      psFree(where);
     1463      return false;
     1464    }
     1465
     1466    // use psDBGenerateWhereConditionalSQL with AND ... because the SQL ends in a WHERE
     1467    if (where && psListLength(where->list)) {
     1468      psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1469      psStringAppend(&query, " AND %s", whereClause);
     1470      psFree(whereClause);
     1471    }
     1472
     1473    if (!p_psDBRunQuery(config->dbh, query)) {
     1474      // rollback
     1475      if (!psDBRollback(config->dbh)) {
     1476        psError(PS_ERR_UNKNOWN, false, "database error");
     1477      }
     1478      psError(PS_ERR_UNKNOWN, false, "database error");
     1479      psFree(query);
     1480      psFree(where);
     1481            return false;
     1482    }
     1483    psFree(query);
     1484  }
     1485  psFree(where);
     1486
     1487  if (!psDBCommit(config->dbh)) {
     1488    psError(PS_ERR_UNKNOWN, false, "database error");
     1489    return false;
     1490  }
     1491
     1492  return true;
     1493}
     1494
     1495
     1496
     1497static bool updateminidvodbprocessedMode(pxConfig *config) {
     1498  PS_ASSERT_PTR_NON_NULL(config, false);
     1499  psMetadata *where = psMetadataAlloc();
     1500
     1501  PXOPT_LOOKUP_U64(minidvodb_id,  config->args, "-minidvodb_id", true, false);
     1502  PXOPT_LOOKUP_U64(merge_order,  config->args, "-set_merge_order", false, false);
     1503  PXOPT_LOOKUP_S16(fault,  config->args, "-set_fault", false, false);
     1504  PXOPT_LOOKUP_F32(dtime_relphot,  config->args, "-set_dtime_relphot", false, false);
     1505  PXOPT_LOOKUP_F32(dtime_resort,  config->args, "-set_dtime_resort", false, false);
     1506  PXOPT_LOOKUP_F32(dtime_merge,  config->args, "-set_dtime_merge", false, false);
     1507
     1508  PXOPT_COPY_S64(config->args, where, "-minidvodb_id",     "minidvodbProcessed.minidvodb_id", "==");
     1509  PXOPT_COPY_STR(config->args, where, "-minidvodb_name",     "minidvodbRun.minidvodb_name", "==");
     1510
     1511
     1512  if (!psListLength(where->list)) {
     1513    psFree(where);
     1514    psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     1515    return false;
     1516  }
     1517
     1518  psString query = psStringCopy("UPDATE minidvodbProcessed JOIN minidvodbRun USING (minidvodb_id) SET ");
     1519  int cnt = 0;
     1520  psString comma = ",";
     1521  if (fault) {
     1522    psStringAppend(&query, " fault = %d", fault);
     1523  cnt++;
     1524  }
     1525
     1526  if (merge_order) {
     1527    if (cnt) {
     1528      psStringAppend(&query, "%s", comma);
     1529    }
     1530
     1531    psStringAppend(&query, " merge_order = %" PRId64, merge_order);
     1532    cnt++;
     1533  }
     1534
     1535  if (dtime_relphot) {
     1536    if (cnt) {
     1537      psStringAppend(&query, "%s", comma);
     1538    }
     1539    psStringAppend(&query, " dtime_relphot = %f", dtime_relphot);
     1540    cnt++;
     1541  }
     1542
     1543  if (dtime_resort) {
     1544    if (cnt) {
     1545      psStringAppend(&query, "%s", comma);
     1546    }
     1547    psStringAppend(&query, " dtime_resort = %f", dtime_resort);
     1548    cnt++;
     1549  }
     1550
     1551  if (dtime_merge) {
     1552    if (cnt) {
     1553      psStringAppend(&query, "%s", comma);
     1554    }
     1555    psStringAppend(&query, " dtime_merge = %f", dtime_merge);
     1556    cnt++;
     1557 }
     1558
     1559  psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1560  psStringAppend(&query, " WHERE %s", whereClause);
     1561
     1562  if (!p_psDBRunQuery(config->dbh, query)) {
     1563   psError(PS_ERR_UNKNOWN, false, "database error");
     1564   psFree(query);
     1565   return false;
     1566  }
     1567
     1568  psFree(query);
     1569  psFree(where);
     1570
     1571  return true;
     1572}
     1573
     1574
Note: See TracChangeset for help on using the changeset viewer.