Index: trunk/psModules/src/config/pmConfig.c
===================================================================
--- trunk/psModules/src/config/pmConfig.c	(revision 18921)
+++ trunk/psModules/src/config/pmConfig.c	(revision 18926)
@@ -1264,17 +1264,27 @@
 }
 
-// Look up a value from the user or site configuration
-#define CONFIG_USER_SITE(TYPE, TARGET, TYPENAME, NAME, DEFAULT) \
-    TYPE TARGET; \
-    { \
-        bool status = false;                /* Status of MD lookup */ \
-        TARGET = psMetadataLookup##TYPENAME(&status, config->user, NAME); \
-        if (!status) { \
-            TARGET = psMetadataLookup##TYPENAME(&status, config->site, NAME); \
-            if (!status) { \
-                TARGET = DEFAULT; \
-            } \
-        } \
-    }
+psMetadataItem *pmConfigUserSite(const pmConfig *config, const char *name, psDataType type)
+{
+    PS_ASSERT_PTR_NON_NULL(config, NULL);
+    PS_ASSERT_STRING_NON_EMPTY(name, NULL);
+
+    psMetadataItem *item = psMetadataLookup(config->user, name);
+    if (!item) {
+        item = psMetadataLookup(config->site, name);
+        if (!item) {
+            psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                    "Unable to find %s in user or site configuration", name);
+            return NULL;
+        }
+    }
+    if (item->type != type) {
+        psError(PS_ERR_BAD_PARAMETER_TYPE, true,
+                "Type of %s (%x) in user/site configuration does not match expected (%x)",
+                name, item->type, type);
+        return NULL;
+    }
+
+    return item;
+}
 
 
@@ -1296,22 +1306,29 @@
     }
 
-    CONFIG_USER_SITE(const char*, dbServer, Str, "DBSERVER",   NULL);
-    CONFIG_USER_SITE(const char*, dbUser,   Str, "DBUSER",     NULL);
-    CONFIG_USER_SITE(const char*, dbPass,   Str, "DBPASSWORD", NULL);
-    CONFIG_USER_SITE(const char*, dbName,   Str, "DBNAME",     NULL);
-    CONFIG_USER_SITE(int,         dbPort,   S32, "DBPORT",     0);
-
-    if (!dbServer || !dbUser || !dbPass || !dbName) {
+    // Connection details
+    psMetadataItem *server = pmConfigUserSite(config, "DBSERVER",   PS_DATA_STRING);
+    psMetadataItem *user   = pmConfigUserSite(config, "DBUSER",     PS_DATA_STRING);
+    psMetadataItem *pass   = pmConfigUserSite(config, "DBPASSWORD", PS_DATA_STRING);
+    psMetadataItem *name   = pmConfigUserSite(config, "DBNAME",     PS_DATA_STRING);
+    psMetadataItem *port   = pmConfigUserSite(config, "DBPORT",     PS_TYPE_S32);
+
+    if (!server || !user || !pass || !name) {
         psWarning("Cannot find DBSERVER/DBUSER/DBPASSWORD/DBNAME in user or site configuration: "
                   "unable to connect to database.");
+        psErrorClear();
         return NULL;
     }
-
-    if (strcasecmp(dbName, "XXX") == 0 || strcasecmp(dbName, "NONE") == 0) {
-        psTrace("psModules.config", 1, "Database initialisation skipped: database is %s.", dbName);
+    if (!port) {
+        psTrace("psModules.config", 1, "Database port defaulting to 0");
+        psErrorClear();
+    }
+
+    if (strcasecmp(name->data.str, "XXX") == 0 || strcasecmp(name->data.str, "NONE") == 0) {
+        psTrace("psModules.config", 1, "Database initialisation skipped: database is %s.", name->data.str);
         return NULL;
     }
 
-    config->database = psDBInit(dbServer, dbUser, dbPass, dbName, dbPort);
+    config->database = psDBInit(server->data.str, user->data.str, pass->data.str, name->data.str,
+                                port ? port->data.S32 : 0);
     return config->database;
 
Index: trunk/psModules/src/config/pmConfig.h
===================================================================
--- trunk/psModules/src/config/pmConfig.h	(revision 18921)
+++ trunk/psModules/src/config/pmConfig.h	(revision 18926)
@@ -5,6 +5,6 @@
  *  @author Eugene Magnier, IfA
  *
- *  @version $Revision: 1.40 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2008-08-05 03:45:56 $
+ *  @version $Revision: 1.41 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2008-08-06 01:13:14 $
  *  Copyright 2005-2006 Institute for Astronomy, University of Hawaii
  */
@@ -129,4 +129,14 @@
                                 );
 
+/// Derive a value from the user or site configuration
+///
+/// The value in the user configuration takes precedence.  Returns NULL if the value isn't present in either,
+/// or has the wrong type.
+psMetadataItem *pmConfigUserSite(const pmConfig *config, // Configuration
+                                 const char *name, // Name of value
+                                 psDataType type // Expected type
+    );
+
+
 /// Setup the database
 ///
