Index: trunk/ippTools/src/pzgetexp.c
===================================================================
--- trunk/ippTools/src/pzgetexp.c	(revision 9968)
+++ trunk/ippTools/src/pzgetexp.c	(revision 9972)
@@ -40,16 +40,24 @@
     bool haveLastFileSet = false;
     psString lastFileSet = NULL;
-    // XXX this is a very ugly and brute force way of doing things need to add
-    // a metadatadb function to retrun the last entry without removing it from
-    // the database
-    psArray *summitExps = summitExpSelectRowObjects(config->dbh, NULL, 1);
-    if (!summitExps) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        goto FAIL;
-    }
-    if (psArrayLength(summitExps)) {
-        haveLastFileSet = true;
-        summitExpRow *lastExps = summitExps->data[psArrayLength(summitExps) - 1];
-        lastFileSet = psMemIncrRefCounter(lastExps->exp_id);
+    {
+        char *query = "SELECT * from summitExp ORDER BY time DESC LIMIT 1";
+        if (!p_psDBRunQuery(config->dbh, query)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+            return false;
+        }
+
+        psArray *output = p_psDBFetchResult(config->dbh);
+        if (!output) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+            return false;
+        }
+        if (!psArrayLength(output)) {
+            psError(PS_ERR_UNKNOWN, false, "no summitExp rows found");
+            haveLastFileSet = false;
+        } else {
+            haveLastFileSet = true;
+            bool status = false;
+            lastFileSet = psMetadataLookupStr(&status, output->data[0], "exp_id");
+        }
     }
 
@@ -96,5 +104,5 @@
     {
         char *query = "CREATE TEMPORARY TABLE incoming" 
-            " (exp_id VARCHAR(64), camera VARCHAR(64), telescope VARCHAR(64), exp_type VARCHAR(64), uri VARCHAR(255), PRIMARY KEY(exp_id, camera, telescope))"
+            " (exp_id VARCHAR(64), camera VARCHAR(64), telescope VARCHAR(64), time DATETIME, exp_type VARCHAR(64), uri VARCHAR(255), PRIMARY KEY(exp_id, camera, telescope))"
            " ENGINE=MEMORY";
         if (!p_psDBRunQuery(config->dbh, query)) {
@@ -106,5 +114,5 @@
 
     {
-        char *query = "INSERT INTO incoming (exp_id, camera, telescope, exp_type, uri) VALUES (?, ?, ?, ?, ?)";
+        char *query = "INSERT INTO incoming (exp_id, camera, telescope, time, exp_type, uri) VALUES (?, ?, ?, ?, ?, ?)";
 
         long inserted = p_psDBRunQueryPrepared(config->dbh, newSummitExps, query);
@@ -170,4 +178,5 @@
     }
 
+    // split the string into lines
     psList *doc = psStringSplit(str, "\n", false);
 
@@ -177,7 +186,4 @@
     psString line;
     while ((line = psListGetAndIncrement(lineCursor))) {
-        // XXX debugging
-        printf("-> %s\n", line);
-
         // split line into tokens
         psList *tokens = psStringSplit(line, " ", false);
@@ -201,8 +207,6 @@
         char *uri       = psListGetAndIncrement(tokenCursor);
         char *exp_id    = psListGetAndIncrement(tokenCursor); // fileset
-        char *time      = psListGetAndIncrement(tokenCursor); // datetime
+        char *timeStr   = psListGetAndIncrement(tokenCursor); // datetime
         char *exp_type  = psListGetAndIncrement(tokenCursor); // type
-
-        if (time) {} // silence unused warning
 
         // create a new metadata to represent this line and it's values
@@ -229,4 +233,14 @@
             return NULL;
         }
+        psTime *time = psTimeFromISO(timeStr, PS_TIME_UTC);
+        if (!psMetadataAddTime(md, PS_LIST_TAIL, "time", 0, NULL, time)) {
+            psError(PS_ERR_UNKNOWN, false, "failed to add item telescope");
+            psFree(time);
+            psFree(md);
+            psFree(tokenCursor);
+            psFree(tokens);
+            return NULL;
+        }
+        psFree(time);
         if (!psMetadataAddStr(md, PS_LIST_TAIL, "exp_type", 0, NULL, exp_type)) {
             psError(PS_ERR_UNKNOWN, false, "failed to add item exp_type");
@@ -244,4 +258,5 @@
         }
 
+        // add the new metadata to the result set
         psArrayAdd(summitExps, 0, md);
 
