Index: trunk/psModules/src/concepts/pmConceptsRead.c
===================================================================
--- trunk/psModules/src/concepts/pmConceptsRead.c	(revision 17867)
+++ trunk/psModules/src/concepts/pmConceptsRead.c	(revision 17868)
@@ -6,5 +6,5 @@
 #include <assert.h>
 #include <string.h>
-#include <strings.h>		/* for strn?casecmp */
+#include <strings.h>            /* for strn?casecmp */
 #include <pslib.h>
 
@@ -45,5 +45,5 @@
         return psMetadataItemAllocF64(pattern->name, pattern->comment, psMetadataItemParseF64(concept));
     default:
-        psLogMsg(__func__, PS_LOG_WARN, "Concept %s (%s) is not of a standard type (%x)\n",
+        psWarning("Concept %s (%s) is not of a standard type (%x)\n",
                  pattern->name, pattern->comment, pattern->type);
         return NULL;
@@ -373,5 +373,4 @@
 }
 
-
 // XXX --- the below code has NOT been tested!
 bool p_pmConceptsReadFromDatabase(psMetadata *target, const psMetadata *specs,
@@ -381,9 +380,7 @@
     PS_ASSERT_PTR_NON_NULL(target, false);
 
-    #ifndef HAVE_PSDB
-
+#ifndef HAVE_PSDB
     return true;
-    #else
-
+#else
     pmHDU *hdu = pmHDUGetLowest(fpa, chip, cell); // The HDU at the lowest level
     if (!hdu) {
@@ -404,100 +401,44 @@
         pmConceptSpec *spec = specItem->data.V; // The specification
         psString name = specItem->name; // The concept name
-
-        psMetadata *dbLookup = psMetadataLookupMetadata(&mdok, dbSpec, name);
+        const char *dbLookup = psMetadataLookupStr(&mdok, dbSpec, name);
         if (mdok && dbLookup) {
-            const char *tableName = psMetadataLookupStr(&mdok, dbLookup, "TABLE"); // Table name
-            // Names of the "where" columns
-            const char *givenCols = psMetadataLookupStr(&mdok, dbLookup, "GIVENDBCOL");
-            // Values of the "where" columns
-            const char *givenPS = psMetadataLookupStr(&mdok, dbLookup, "GIVENPS");
-
-            // Now, need to get the "given"s
-            if (strlen(givenCols) > 0 || strlen(givenPS) > 0) {
-                psList *cols = psStringSplit(givenCols, ",;", true); // List of column names
-                psList *values = psStringSplit(givenPS, ",;", true); // List of value names for the columns
-                psMetadata *selection = psMetadataAlloc(); // The stuff to select in the DB
-                if (cols->n != values->n) {
-                    psLogMsg(__func__, PS_LOG_WARN,
-                             "The GIVENDBCOL and GIVENPS entries for %s do not have "
-                             "the same number of entries --- ignored.\n", name);
-                } else {
-                    // Iterators for the lists
-                    psListIterator *colsIter = psListIteratorAlloc(cols, PS_LIST_HEAD, false);
-                    psListIterator *valuesIter = psListIteratorAlloc(values, PS_LIST_HEAD, false);
-                    char *column = NULL;    // Name of the column
-                    while ((column = psListGetAndIncrement(colsIter))) {
-                        char *dependName = psListGetAndIncrement(valuesIter); // Name for the value
-                        if (!strlen(column) || !strlen(name)) {
-                            psLogMsg(__func__, PS_LOG_WARN, "One of the columns or value names for %s is "
-                                     " empty --- ignored.\n", name);
-                        } else {
-                            // Search for the value name
-                            psMetadataItem *item = NULL; // The value
-                            if (!item && cell) {
-                                item = psMetadataLookup(cell->concepts, dependName);
-                            }
-                            if (!item && chip) {
-                                item = psMetadataLookup(chip->concepts, dependName);
-                            }
-                            if (!item && fpa) {
-                                item = psMetadataLookup(fpa->concepts, dependName);
-                            }
-                            if (! item) {
-                                psLogMsg(__func__, PS_LOG_ERROR, "Unable to find the value name %s for DB"
-                                         " lookup on %s --- ignored.\n", dependName, name);
-                            } else {
-                                // We need to create a new psMetadataItem.  I don't think we can't
-                                // simply hack the existing one, since that could conceivably cause
-                                // memory leaks
-                                psMetadataItem *newItem = psMetadataItemAlloc(name, item->type,
-                                                          item->comment,
-                                                          item->data.V);
-                                psMetadataAddItem(selection, newItem, PS_LIST_TAIL, PS_META_REPLACE);
-                                psFree(newItem);
-                            }
-                        }
-                        psFree(dependName);
-                        psFree(column);
-                    } // Iterating through the columns
-                    psFree(colsIter);
-                    psFree(valuesIter);
-
-                    psArray *dbResult = psDBSelectRows(db, tableName, selection, 2); // Lookup result
-                    // Note that we use limit=2 in order to test if there are multiple rows returned
-
-                    psMetadataItem *conceptItem = NULL; // The final result of the DB lookup
-                    if (dbResult->n == 0) {
-                        psLogMsg(__func__, PS_LOG_WARN,
-                                 "Unable to find any rows in DB for %s --- ignored\n", name);
-                    } else {
-                        if (dbResult-> n > 1) {
-                            psLogMsg(__func__, PS_LOG_WARN,
-                                     "Multiple rows returned in DB lookup for %s --- "
-                                     " using the first one only.\n", name);
-                        }
-                        conceptItem = (psMetadataItem*)dbResult->data[0];
-                    }
-
-                    // Now we have the result
-                    if (!conceptParse(spec, conceptItem, PM_CONCEPT_SOURCE_DATABASE,
-                                      cameraFormat, target, fpa, chip, cell)) {
-                        psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to parse concept %s from database.\n",
-                                name);
-                        status = false;
-                    }
-
-                }
-                psFree(cols);
-                psFree(values);
-            }
-        } // Doing the "given"s.
-
+            psString sql = pmConceptsInterpolate(dbLookup, fpa, chip, cell);
+
+            if (!p_psDBRunQuery(db, sql)) {
+                psWarning("Unable to query database for concept %s --- ignored.", name);
+                psFree(sql);
+                continue;
+            }
+            psFree(sql);
+
+            psArray *rows = p_psDBFetchResult(db); // Rows returned from the query
+            if (rows->n == 0) {
+                psWarning("No rows returned from database query for concept %s --- ignored.", name);
+                continue;
+            }
+            if (rows->n > 1) {
+                psWarning("Multiple rows returned from database query for concept %s --- using the first",
+                          name);
+            }
+            psMetadata *row = rows->data[0]; // First (and only) row
+            if (row->list->n > 1) {
+                psWarning("Multiple columns returned from database query for concept %s --- using the first",
+                          name);
+            }
+            psMetadataItem *conceptItem = psMetadataGet(row, PS_LIST_HEAD); // Item of interest
+
+            // Now we have the result
+            if (!conceptParse(spec, conceptItem, PM_CONCEPT_SOURCE_DATABASE,
+                              cameraFormat, target, fpa, chip, cell)) {
+                psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to parse concept %s from database.\n",
+                        name);
+                status = false;
+            }
+        }
     } // Iterating through the concept specifications
     psFree(specsIter);
 
     return status;
-    #endif
-}
-
-
+#endif
+}
+
