IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 18, 2009, 10:23:28 AM (17 years ago)
Author:
eugene
Message:

updated vysos branch from trunk

Location:
branches/eam_branches/20090820
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/20090820

  • branches/eam_branches/20090820/ippTools/src/pxtools.c

    r25766 r25870  
    108108    return true;
    109109}
     110
     111// shared code for updating the various strings for a Run
     112bool pxUpdateRun(pxConfig *config, psMetadata *where, psString *pQuery, psString table, bool has_dist_group)
     113{
     114    PS_ASSERT_PTR_NON_NULL(config, false);
     115    PS_ASSERT_PTR_NON_NULL(where, false);
     116    PS_ASSERT_PTR_NON_NULL(pQuery, false);
     117    PS_ASSERT_PTR_NON_NULL(*pQuery, false);
     118
     119    // make sure that -state is not the only selection parameter
     120    PXOPT_LOOKUP_STR(where_state, config->args, "-state", false, false);
     121    if (where_state && (psListLength(where->list) < 2)) {
     122        psError(PXTOOLS_ERR_DATA, true, "selection by -state alone is not allowed");
     123        return false;
     124    }
     125
     126    PXOPT_LOOKUP_STR(state, config->args,       "-set_state", false, false);
     127    PXOPT_LOOKUP_STR(label, config->args,       "-set_label", false, false);
     128    PXOPT_LOOKUP_STR(data_group, config->args,  "-set_data_group", false, false);
     129    PXOPT_LOOKUP_STR(note, config->args,        "-set_note", false, false);
     130
     131    psString dist_group = NULL;
     132    if (has_dist_group) {
     133        PXOPT_LOOKUP_STR(tmp_dist_group, config->args,  "-set_dist_group", false, false);
     134        dist_group = tmp_dist_group;
     135    }
     136
     137    if ((!state) && (!label) && (!data_group) && (has_dist_group && !dist_group) && !(note)) {
     138        psError(PXTOOLS_ERR_DATA, false, "parameters are required");
     139        return false;
     140    }
     141
     142    if (state && ! pxIsValidState(state)) {
     143        psError(PXTOOLS_ERR_DATA, false, "pxIsValidState failed");
     144        return false;
     145    }
     146
     147    // first paramter is added with "SET param = 'value'"
     148    // subseqent ones with ", param = 'value'"
     149    char *separator = " SET ";
     150    char *comma = ",";
     151
     152#   define addColumn(_tab, _val) \
     153        do { \
     154            if (_val) { \
     155                psStringAppend(pQuery, "%s %s.%s = '%s'", separator, _tab, #_val, _val); \
     156                separator = comma; \
     157            } \
     158        } while (0)
     159
     160    addColumn(table, state);
     161    addColumn(table, data_group);
     162    if (has_dist_group) {
     163        addColumn(table, dist_group);
     164    }
     165    addColumn(table, note);
     166    addColumn(table, label);
     167
     168    psString whereClause =  psDBGenerateWhereSQL(where, NULL);
     169    psStringAppend(pQuery, " %s", whereClause);
     170    psFree(whereClause);
     171
     172    if (!p_psDBRunQuery(config->dbh, *pQuery)) {
     173        psError(PS_ERR_UNKNOWN, false, "database error");
     174        return false;
     175    }
     176
     177    return true;
     178}
     179
     180bool pxLookupVersion(pxConfig *config, psArray **pArray)
     181{
     182    const char *query = "SELECT * FROM dbversion";
     183
     184    if (!p_psDBRunQuery(config->dbh, query)) {
     185        psError(PS_ERR_UNKNOWN, false, "database error");
     186        return false;
     187    }
     188
     189    psArray *output = p_psDBFetchResult(config->dbh);
     190    if (!output) {
     191        psError(PS_ERR_UNKNOWN, false, "database error");
     192        return false;
     193    }
     194    if (!psArrayLength(output)) {
     195        psFree(output);
     196        psError(PS_ERR_UNKNOWN, true, "no rows in dbversion");
     197        return false;
     198    }
     199    if (psArrayLength(output) > 1) {
     200        psError(PS_ERR_UNKNOWN, true, "unexpected number of rows found in dbversion: %" PRId64,
     201                psArrayLength(output));
     202        return false;
     203    }
     204    *pArray = output;
     205
     206    return true;
     207}
     208
     209psString pxGetDBVersion(pxConfig *config)
     210{
     211    PS_ASSERT_PTR_NON_NULL(config, NULL);
     212
     213    psArray *array = NULL;
     214    if (!pxLookupVersion(config, &array)) {
     215        psError(PS_ERR_UNKNOWN, false, "pxLookupVersion failed");
     216        return NULL;
     217    }
     218    psMetadata *md = array->data[0];
     219    if (!md) {
     220        psError(PS_ERR_UNKNOWN, true, "output of pxLookupVersion is null");
     221        return NULL;
     222    }
     223   
     224    psString version = psMetadataLookupStr(NULL, md, "schema_version");
     225
     226    return version;
     227}
     228
     229bool pxExportVersion(pxConfig *config, FILE *file)
     230{
     231    PS_ASSERT_PTR_NON_NULL(config, NULL);
     232    PS_ASSERT_PTR_NON_NULL(file, NULL);
     233
     234    psArray *array = NULL;
     235    if (!pxLookupVersion(config, &array) || !array) {
     236        psError(PS_ERR_UNKNOWN, false, "pxLookupVersion failed");
     237        return false;
     238    }
     239    if (!ippdbPrintMetadatas(file, array, "dbversion", true)) {
     240        psError(PS_ERR_UNKNOWN, false, "failed to print array");
     241        psFree(array);
     242        return false;
     243    }
     244    return true;
     245}
     246
     247bool pxCheckImportVersion(pxConfig *config, psMetadata *input)
     248{
     249    PS_ASSERT_PTR_NON_NULL(config, NULL);
     250    PS_ASSERT_PTR_NON_NULL(input, NULL);
     251
     252    // This code was adapted from the way camtool parses the structures.
     253    // Is this really the way to do it?
     254    psMetadataItem *multi_item =  psMetadataLookup(input, "dbversion");
     255    if (!multi_item || (multi_item->type != PS_DATA_METADATA_MULTI)) {
     256        psError(PS_ERR_UNKNOWN, true, "dbversion multi not found in input");
     257        return false;
     258    }
     259   
     260    psMetadataItem *dbversion = psListGet(multi_item->data.list, 0);
     261    if (!dbversion) {
     262        psError(PS_ERR_UNKNOWN, true, "dbversion not found in input");
     263        return false;
     264    }
     265
     266    if (!strcmp(dbversion->name, "dbversion")) {
     267        // horray
     268        psMetadata *md = dbversion->data.md;
     269        psString schema_version = pxGetDBVersion(config);
     270        if (!schema_version) {
     271            psError(PS_ERR_UNKNOWN, false, "pxGetDBVersion failed");
     272            return false;
     273        }
     274       
     275        psString import_version = psMetadataLookupStr(NULL, md, "schema_version");
     276        if (import_version && strcmp(import_version, schema_version)) {
     277            psError(PS_ERR_UNKNOWN, true, "input file schema_version: %s does not match data base: %s",
     278                import_version, schema_version);
     279            return false;
     280        } else if (!import_version) {
     281            psError(PS_ERR_UNKNOWN, true, "input file schema_version is NULL");
     282            return false;
     283        } else {
     284            // YIPPEE this file is the same version
     285        }
     286    } else {
     287        psError(PS_ERR_UNKNOWN, true, "Unexpected config dump format");
     288        return false;
     289    }
     290
     291    return true;
     292}
Note: See TracChangeset for help on using the changeset viewer.