IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 30, 2008, 2:22:46 PM (18 years ago)
Author:
Paul Price
Message:

Changing code to read concepts from database so that it looks up a SQL query that simply gets executed.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psModules/src/concepts/pmConceptsRead.c

    r12890 r17868  
    66#include <assert.h>
    77#include <string.h>
    8 #include <strings.h>            /* for strn?casecmp */
     8#include <strings.h>            /* for strn?casecmp */
    99#include <pslib.h>
    1010
     
    4545        return psMetadataItemAllocF64(pattern->name, pattern->comment, psMetadataItemParseF64(concept));
    4646    default:
    47         psLogMsg(__func__, PS_LOG_WARN, "Concept %s (%s) is not of a standard type (%x)\n",
     47        psWarning("Concept %s (%s) is not of a standard type (%x)\n",
    4848                 pattern->name, pattern->comment, pattern->type);
    4949        return NULL;
     
    373373}
    374374
    375 
    376375// XXX --- the below code has NOT been tested!
    377376bool p_pmConceptsReadFromDatabase(psMetadata *target, const psMetadata *specs,
     
    381380    PS_ASSERT_PTR_NON_NULL(target, false);
    382381
    383     #ifndef HAVE_PSDB
    384 
     382#ifndef HAVE_PSDB
    385383    return true;
    386     #else
    387 
     384#else
    388385    pmHDU *hdu = pmHDUGetLowest(fpa, chip, cell); // The HDU at the lowest level
    389386    if (!hdu) {
     
    404401        pmConceptSpec *spec = specItem->data.V; // The specification
    405402        psString name = specItem->name; // The concept name
    406 
    407         psMetadata *dbLookup = psMetadataLookupMetadata(&mdok, dbSpec, name);
     403        const char *dbLookup = psMetadataLookupStr(&mdok, dbSpec, name);
    408404        if (mdok && dbLookup) {
    409             const char *tableName = psMetadataLookupStr(&mdok, dbLookup, "TABLE"); // Table name
    410             // Names of the "where" columns
    411             const char *givenCols = psMetadataLookupStr(&mdok, dbLookup, "GIVENDBCOL");
    412             // Values of the "where" columns
    413             const char *givenPS = psMetadataLookupStr(&mdok, dbLookup, "GIVENPS");
    414 
    415             // Now, need to get the "given"s
    416             if (strlen(givenCols) > 0 || strlen(givenPS) > 0) {
    417                 psList *cols = psStringSplit(givenCols, ",;", true); // List of column names
    418                 psList *values = psStringSplit(givenPS, ",;", true); // List of value names for the columns
    419                 psMetadata *selection = psMetadataAlloc(); // The stuff to select in the DB
    420                 if (cols->n != values->n) {
    421                     psLogMsg(__func__, PS_LOG_WARN,
    422                              "The GIVENDBCOL and GIVENPS entries for %s do not have "
    423                              "the same number of entries --- ignored.\n", name);
    424                 } else {
    425                     // Iterators for the lists
    426                     psListIterator *colsIter = psListIteratorAlloc(cols, PS_LIST_HEAD, false);
    427                     psListIterator *valuesIter = psListIteratorAlloc(values, PS_LIST_HEAD, false);
    428                     char *column = NULL;    // Name of the column
    429                     while ((column = psListGetAndIncrement(colsIter))) {
    430                         char *dependName = psListGetAndIncrement(valuesIter); // Name for the value
    431                         if (!strlen(column) || !strlen(name)) {
    432                             psLogMsg(__func__, PS_LOG_WARN, "One of the columns or value names for %s is "
    433                                      " empty --- ignored.\n", name);
    434                         } else {
    435                             // Search for the value name
    436                             psMetadataItem *item = NULL; // The value
    437                             if (!item && cell) {
    438                                 item = psMetadataLookup(cell->concepts, dependName);
    439                             }
    440                             if (!item && chip) {
    441                                 item = psMetadataLookup(chip->concepts, dependName);
    442                             }
    443                             if (!item && fpa) {
    444                                 item = psMetadataLookup(fpa->concepts, dependName);
    445                             }
    446                             if (! item) {
    447                                 psLogMsg(__func__, PS_LOG_ERROR, "Unable to find the value name %s for DB"
    448                                          " lookup on %s --- ignored.\n", dependName, name);
    449                             } else {
    450                                 // We need to create a new psMetadataItem.  I don't think we can't
    451                                 // simply hack the existing one, since that could conceivably cause
    452                                 // memory leaks
    453                                 psMetadataItem *newItem = psMetadataItemAlloc(name, item->type,
    454                                                           item->comment,
    455                                                           item->data.V);
    456                                 psMetadataAddItem(selection, newItem, PS_LIST_TAIL, PS_META_REPLACE);
    457                                 psFree(newItem);
    458                             }
    459                         }
    460                         psFree(dependName);
    461                         psFree(column);
    462                     } // Iterating through the columns
    463                     psFree(colsIter);
    464                     psFree(valuesIter);
    465 
    466                     psArray *dbResult = psDBSelectRows(db, tableName, selection, 2); // Lookup result
    467                     // Note that we use limit=2 in order to test if there are multiple rows returned
    468 
    469                     psMetadataItem *conceptItem = NULL; // The final result of the DB lookup
    470                     if (dbResult->n == 0) {
    471                         psLogMsg(__func__, PS_LOG_WARN,
    472                                  "Unable to find any rows in DB for %s --- ignored\n", name);
    473                     } else {
    474                         if (dbResult-> n > 1) {
    475                             psLogMsg(__func__, PS_LOG_WARN,
    476                                      "Multiple rows returned in DB lookup for %s --- "
    477                                      " using the first one only.\n", name);
    478                         }
    479                         conceptItem = (psMetadataItem*)dbResult->data[0];
    480                     }
    481 
    482                     // Now we have the result
    483                     if (!conceptParse(spec, conceptItem, PM_CONCEPT_SOURCE_DATABASE,
    484                                       cameraFormat, target, fpa, chip, cell)) {
    485                         psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to parse concept %s from database.\n",
    486                                 name);
    487                         status = false;
    488                     }
    489 
    490                 }
    491                 psFree(cols);
    492                 psFree(values);
    493             }
    494         } // Doing the "given"s.
    495 
     405            psString sql = pmConceptsInterpolate(dbLookup, fpa, chip, cell);
     406
     407            if (!p_psDBRunQuery(db, sql)) {
     408                psWarning("Unable to query database for concept %s --- ignored.", name);
     409                psFree(sql);
     410                continue;
     411            }
     412            psFree(sql);
     413
     414            psArray *rows = p_psDBFetchResult(db); // Rows returned from the query
     415            if (rows->n == 0) {
     416                psWarning("No rows returned from database query for concept %s --- ignored.", name);
     417                continue;
     418            }
     419            if (rows->n > 1) {
     420                psWarning("Multiple rows returned from database query for concept %s --- using the first",
     421                          name);
     422            }
     423            psMetadata *row = rows->data[0]; // First (and only) row
     424            if (row->list->n > 1) {
     425                psWarning("Multiple columns returned from database query for concept %s --- using the first",
     426                          name);
     427            }
     428            psMetadataItem *conceptItem = psMetadataGet(row, PS_LIST_HEAD); // Item of interest
     429
     430            // Now we have the result
     431            if (!conceptParse(spec, conceptItem, PM_CONCEPT_SOURCE_DATABASE,
     432                              cameraFormat, target, fpa, chip, cell)) {
     433                psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to parse concept %s from database.\n",
     434                        name);
     435                status = false;
     436            }
     437        }
    496438    } // Iterating through the concept specifications
    497439    psFree(specsIter);
    498440
    499441    return status;
    500     #endif
    501 }
    502 
    503 
     442#endif
     443}
     444
Note: See TracChangeset for help on using the changeset viewer.