Index: trunk/ippdb/src/ippdb.c
===================================================================
--- trunk/ippdb/src/ippdb.c	(revision 11809)
+++ trunk/ippdb/src/ippdb.c	(revision 11820)
@@ -69,4 +69,5 @@
 #define P5INPUTSCFILE_TABLE_NAME "p5InputScfile"
 #define P5DIFFSCFILE_TABLE_NAME "p5DiffScfile"
+#define P6RUN_TABLE_NAME "p6Run"
 #define MAX_STRING_LENGTH 1024
 
@@ -3523,5 +3524,5 @@
 static void rawExpRowFree(rawExpRow *object);
 
-rawExpRow *rawExpRowAlloc(const char *exp_tag, const char *exp_id, const char *camera, const char *telescope, psTime* dateobs, const char *exp_type, psS32 imfiles, const char *workdir, const char *filter, psF32 airmass, psF64 ra, psF64 decl, psF32 exp_time, psF64 bg, psF64 bg_stdev, psF64 bg_mean_stdev, psF64 alt, psF64 az, psF32 ccd_temp, psF64 posang, const char *object, psS16 fault)
+rawExpRow *rawExpRowAlloc(const char *exp_tag, const char *exp_id, const char *camera, const char *telescope, psTime* dateobs, const char *exp_type, psS32 imfiles, const char *workdir, const char *filter, psF32 airmass, psF64 ra, psF64 decl, psF32 exp_time, psF64 bg, psF64 bg_stdev, psF64 bg_mean_stdev, psF64 alt, psF64 az, psF32 ccd_temp, psF64 posang, const char *object, psF32 solang, psS16 fault)
 {
     rawExpRow       *_object;
@@ -3551,4 +3552,5 @@
     _object->posang = posang;
     _object->object = psStringCopy(object);
+    _object->solang = solang;
     _object->fault = fault;
 
@@ -3677,4 +3679,9 @@
         return false;
     }
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "solang", PS_DATA_F32, NULL, 0.0)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item solang");
+        psFree(md);
+        return false;
+    }
     if (!psMetadataAdd(md, PS_LIST_TAIL, "fault", PS_DATA_S16, "Key NOT NULL", 0)) {
         psError(PS_ERR_UNKNOWN, false, "failed to add item fault");
@@ -3695,5 +3702,5 @@
 }
 
-bool rawExpInsert(psDB * dbh, const char *exp_tag, const char *exp_id, const char *camera, const char *telescope, psTime* dateobs, const char *exp_type, psS32 imfiles, const char *workdir, const char *filter, psF32 airmass, psF64 ra, psF64 decl, psF32 exp_time, psF64 bg, psF64 bg_stdev, psF64 bg_mean_stdev, psF64 alt, psF64 az, psF32 ccd_temp, psF64 posang, const char *object, psS16 fault)
+bool rawExpInsert(psDB * dbh, const char *exp_tag, const char *exp_id, const char *camera, const char *telescope, psTime* dateobs, const char *exp_type, psS32 imfiles, const char *workdir, const char *filter, psF32 airmass, psF64 ra, psF64 decl, psF32 exp_time, psF64 bg, psF64 bg_stdev, psF64 bg_mean_stdev, psF64 alt, psF64 az, psF32 ccd_temp, psF64 posang, const char *object, psF32 solang, psS16 fault)
 {
     psMetadata *md = psMetadataAlloc();
@@ -3800,4 +3807,9 @@
     if (!psMetadataAdd(md, PS_LIST_TAIL, "object", PS_DATA_STRING, NULL, object)) {
         psError(PS_ERR_UNKNOWN, false, "failed to add item object");
+        psFree(md);
+        return false;
+    }
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "solang", PS_DATA_F32, NULL, solang)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item solang");
         psFree(md);
         return false;
@@ -3831,5 +3843,5 @@
 bool rawExpInsertObject(psDB *dbh, rawExpRow *object)
 {
-    return rawExpInsert(dbh, object->exp_tag, object->exp_id, object->camera, object->telescope, object->dateobs, object->exp_type, object->imfiles, object->workdir, object->filter, object->airmass, object->ra, object->decl, object->exp_time, object->bg, object->bg_stdev, object->bg_mean_stdev, object->alt, object->az, object->ccd_temp, object->posang, object->object, object->fault);
+    return rawExpInsert(dbh, object->exp_tag, object->exp_id, object->camera, object->telescope, object->dateobs, object->exp_type, object->imfiles, object->workdir, object->filter, object->airmass, object->ra, object->decl, object->exp_time, object->bg, object->bg_stdev, object->bg_mean_stdev, object->alt, object->az, object->ccd_temp, object->posang, object->object, object->solang, object->fault);
 }
 
@@ -4009,4 +4021,9 @@
         return false;
     }
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "solang", PS_DATA_F32, NULL, object->solang)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item solang");
+        psFree(md);
+        return false;
+    }
     if (!psMetadataAdd(md, PS_LIST_TAIL, "fault", PS_DATA_S16, NULL, object->fault)) {
         psError(PS_ERR_UNKNOWN, false, "failed to add item fault");
@@ -4128,4 +4145,9 @@
         return false;
     }
+    psF32 solang = psMetadataLookupF32(&status, md, "solang");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item solang");
+        return false;
+    }
     psS16 fault = psMetadataLookupS16(&status, md, "fault");
     if (!status) {
@@ -4134,5 +4156,5 @@
     }
 
-    return rawExpRowAlloc(exp_tag, exp_id, camera, telescope, dateobs, exp_type, imfiles, workdir, filter, airmass, ra, decl, exp_time, bg, bg_stdev, bg_mean_stdev, alt, az, ccd_temp, posang, object, fault);
+    return rawExpRowAlloc(exp_tag, exp_id, camera, telescope, dateobs, exp_type, imfiles, workdir, filter, airmass, ra, decl, exp_time, bg, bg_stdev, bg_mean_stdev, alt, az, ccd_temp, posang, object, solang, fault);
 }
 psArray *rawExpSelectRowObjects(psDB *dbh, const psMetadata *where, unsigned long long limit)
@@ -8334,5 +8356,5 @@
 static void detRunRowFree(detRunRow *object);
 
-detRunRow *detRunRowAlloc(psS32 det_id, psS32 iteration, const char *det_type, const char *mode, const char *state, const char *workdir, const char *camera, const char *telescope, const char *exp_type, const char *filter, psF32 airmass_min, psF32 airmass_max, psF32 exp_time_min, psF32 exp_time_max, psF32 ccd_temp_min, psF32 ccd_temp_max, psF64 posang_min, psF64 posang_max, psTime* registered, psTime* time_begin, psTime* time_end, psTime* use_begin, psTime* use_end)
+detRunRow *detRunRowAlloc(psS32 det_id, psS32 iteration, const char *det_type, const char *mode, const char *state, const char *workdir, const char *camera, const char *telescope, const char *exp_type, const char *filter, psF32 airmass_min, psF32 airmass_max, psF32 exp_time_min, psF32 exp_time_max, psF32 ccd_temp_min, psF32 ccd_temp_max, psF64 posang_min, psF64 posang_max, psTime* registered, psTime* time_begin, psTime* time_end, psTime* use_begin, psTime* use_end, psF32 solang_min, psF32 solang_max, const char *label, psS32 parent)
 {
     detRunRow       *_object;
@@ -8364,4 +8386,8 @@
     _object->use_begin = psTimeCopy(use_begin);
     _object->use_end = psTimeCopy(use_end);
+    _object->solang_min = solang_min;
+    _object->solang_max = solang_max;
+    _object->label = psStringCopy(label);
+    _object->parent = parent;
 
     return _object;
@@ -8383,4 +8409,5 @@
     psFree(object->use_begin);
     psFree(object->use_end);
+    psFree(object->label);
 }
 
@@ -8503,4 +8530,24 @@
         return false;
     }
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "solang_min", PS_DATA_F32, NULL, 0.0)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item solang_min");
+        psFree(md);
+        return false;
+    }
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "solang_max", PS_DATA_F32, NULL, 0.0)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item solang_max");
+        psFree(md);
+        return false;
+    }
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "label", PS_DATA_STRING, "key", "64")) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item label");
+        psFree(md);
+        return false;
+    }
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "parent", PS_DATA_S32, "Key", 0)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item parent");
+        psFree(md);
+        return false;
+    }
 
     bool status = psDBCreateTable(dbh, DETRUN_TABLE_NAME, md);
@@ -8516,5 +8563,5 @@
 }
 
-bool detRunInsert(psDB * dbh, psS32 det_id, psS32 iteration, const char *det_type, const char *mode, const char *state, const char *workdir, const char *camera, const char *telescope, const char *exp_type, const char *filter, psF32 airmass_min, psF32 airmass_max, psF32 exp_time_min, psF32 exp_time_max, psF32 ccd_temp_min, psF32 ccd_temp_max, psF64 posang_min, psF64 posang_max, psTime* registered, psTime* time_begin, psTime* time_end, psTime* use_begin, psTime* use_end)
+bool detRunInsert(psDB * dbh, psS32 det_id, psS32 iteration, const char *det_type, const char *mode, const char *state, const char *workdir, const char *camera, const char *telescope, const char *exp_type, const char *filter, psF32 airmass_min, psF32 airmass_max, psF32 exp_time_min, psF32 exp_time_max, psF32 ccd_temp_min, psF32 ccd_temp_max, psF64 posang_min, psF64 posang_max, psTime* registered, psTime* time_begin, psTime* time_end, psTime* use_begin, psTime* use_end, psF32 solang_min, psF32 solang_max, const char *label, psS32 parent)
 {
     psMetadata *md = psMetadataAlloc();
@@ -8631,4 +8678,24 @@
     if (!psMetadataAdd(md, PS_LIST_TAIL, "use_end", PS_DATA_TIME, NULL, use_end)) {
         psError(PS_ERR_UNKNOWN, false, "failed to add item use_end");
+        psFree(md);
+        return false;
+    }
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "solang_min", PS_DATA_F32, NULL, solang_min)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item solang_min");
+        psFree(md);
+        return false;
+    }
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "solang_max", PS_DATA_F32, NULL, solang_max)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item solang_max");
+        psFree(md);
+        return false;
+    }
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "label", PS_DATA_STRING, NULL, label)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item label");
+        psFree(md);
+        return false;
+    }
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "parent", PS_DATA_S32, NULL, parent)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item parent");
         psFree(md);
         return false;
@@ -8657,5 +8724,5 @@
 bool detRunInsertObject(psDB *dbh, detRunRow *object)
 {
-    return detRunInsert(dbh, object->det_id, object->iteration, object->det_type, object->mode, object->state, object->workdir, object->camera, object->telescope, object->exp_type, object->filter, object->airmass_min, object->airmass_max, object->exp_time_min, object->exp_time_max, object->ccd_temp_min, object->ccd_temp_max, object->posang_min, object->posang_max, object->registered, object->time_begin, object->time_end, object->use_begin, object->use_end);
+    return detRunInsert(dbh, object->det_id, object->iteration, object->det_type, object->mode, object->state, object->workdir, object->camera, object->telescope, object->exp_type, object->filter, object->airmass_min, object->airmass_max, object->exp_time_min, object->exp_time_max, object->ccd_temp_min, object->ccd_temp_max, object->posang_min, object->posang_max, object->registered, object->time_begin, object->time_end, object->use_begin, object->use_end, object->solang_min, object->solang_max, object->label, object->parent);
 }
 
@@ -8845,4 +8912,24 @@
         return false;
     }
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "solang_min", PS_DATA_F32, NULL, object->solang_min)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item solang_min");
+        psFree(md);
+        return false;
+    }
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "solang_max", PS_DATA_F32, NULL, object->solang_max)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item solang_max");
+        psFree(md);
+        return false;
+    }
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "label", PS_DATA_STRING, NULL, object->label)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item label");
+        psFree(md);
+        return false;
+    }
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "parent", PS_DATA_S32, NULL, object->parent)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item parent");
+        psFree(md);
+        return false;
+    }
 
 
@@ -8969,6 +9056,26 @@
         return false;
     }
-
-    return detRunRowAlloc(det_id, iteration, det_type, mode, state, workdir, camera, telescope, exp_type, filter, airmass_min, airmass_max, exp_time_min, exp_time_max, ccd_temp_min, ccd_temp_max, posang_min, posang_max, registered, time_begin, time_end, use_begin, use_end);
+    psF32 solang_min = psMetadataLookupF32(&status, md, "solang_min");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item solang_min");
+        return false;
+    }
+    psF32 solang_max = psMetadataLookupF32(&status, md, "solang_max");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item solang_max");
+        return false;
+    }
+    char* label = psMetadataLookupPtr(&status, md, "label");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item label");
+        return false;
+    }
+    psS32 parent = psMetadataLookupS32(&status, md, "parent");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item parent");
+        return false;
+    }
+
+    return detRunRowAlloc(det_id, iteration, det_type, mode, state, workdir, camera, telescope, exp_type, filter, airmass_min, airmass_max, exp_time_min, exp_time_max, ccd_temp_min, ccd_temp_max, posang_min, posang_max, registered, time_begin, time_end, use_begin, use_end, solang_min, solang_max, label, parent);
 }
 psArray *detRunSelectRowObjects(psDB *dbh, const psMetadata *where, unsigned long long limit)
@@ -13855,10 +13962,10 @@
         return false;
     }
-    if (!psMetadataAdd(md, PS_LIST_TAIL, "p3_version", PS_DATA_S32, "Primary Key", 0)) {
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "p3_version", PS_DATA_S32, "Key", 0)) {
         psError(PS_ERR_UNKNOWN, false, "failed to add item p3_version");
         psFree(md);
         return false;
     }
-    if (!psMetadataAdd(md, PS_LIST_TAIL, "magiced", PS_DATA_BOOL, "Primary Key", 0)) {
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "magiced", PS_DATA_BOOL, "Key", 0)) {
         psError(PS_ERR_UNKNOWN, false, "failed to add item magiced");
         psFree(md);
@@ -14586,10 +14693,10 @@
         return false;
     }
-    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_STRING, "Primary Key", "64")) {
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_STRING, "Key", "64")) {
         psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag");
         psFree(md);
         return false;
     }
-    if (!psMetadataAdd(md, PS_LIST_TAIL, "p3_version", PS_DATA_S32, "Primary Key", 0)) {
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "p3_version", PS_DATA_S32, "Key", 0)) {
         psError(PS_ERR_UNKNOWN, false, "failed to add item p3_version");
         psFree(md);
@@ -15304,5 +15411,5 @@
 static void p5InputScfileRowFree(p5InputScfileRow *object);
 
-p5InputScfileRow *p5InputScfileRowAlloc(psS32 p5_id, psS32 p4_id, const char *skycell_id, const char *tess_id, const char *exp_tag, psS32 p3_version, const char *kind, bool template)
+p5InputScfileRow *p5InputScfileRowAlloc(psS32 p5_id, psS32 p4_id, const char *skycell_id, const char *tess_id, const char *kind, bool template)
 {
     p5InputScfileRow *_object;
@@ -15315,6 +15422,4 @@
     _object->skycell_id = psStringCopy(skycell_id);
     _object->tess_id = psStringCopy(tess_id);
-    _object->exp_tag = psStringCopy(exp_tag);
-    _object->p3_version = p3_version;
     _object->kind = psStringCopy(kind);
     _object->template = template;
@@ -15327,5 +15432,4 @@
     psFree(object->skycell_id);
     psFree(object->tess_id);
-    psFree(object->exp_tag);
     psFree(object->kind);
 }
@@ -15354,14 +15458,4 @@
         return false;
     }
-    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_STRING, "Primary Key", "64")) {
-        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag");
-        psFree(md);
-        return false;
-    }
-    if (!psMetadataAdd(md, PS_LIST_TAIL, "p3_version", PS_DATA_S32, "Primary Key", 0)) {
-        psError(PS_ERR_UNKNOWN, false, "failed to add item p3_version");
-        psFree(md);
-        return false;
-    }
     if (!psMetadataAdd(md, PS_LIST_TAIL, "kind", PS_DATA_STRING, "Key", "64")) {
         psError(PS_ERR_UNKNOWN, false, "failed to add item kind");
@@ -15387,5 +15481,5 @@
 }
 
-bool p5InputScfileInsert(psDB * dbh, psS32 p5_id, psS32 p4_id, const char *skycell_id, const char *tess_id, const char *exp_tag, psS32 p3_version, const char *kind, bool template)
+bool p5InputScfileInsert(psDB * dbh, psS32 p5_id, psS32 p4_id, const char *skycell_id, const char *tess_id, const char *kind, bool template)
 {
     psMetadata *md = psMetadataAlloc();
@@ -15407,14 +15501,4 @@
     if (!psMetadataAdd(md, PS_LIST_TAIL, "tess_id", PS_DATA_STRING, NULL, tess_id)) {
         psError(PS_ERR_UNKNOWN, false, "failed to add item tess_id");
-        psFree(md);
-        return false;
-    }
-    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_STRING, NULL, exp_tag)) {
-        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag");
-        psFree(md);
-        return false;
-    }
-    if (!psMetadataAdd(md, PS_LIST_TAIL, "p3_version", PS_DATA_S32, NULL, p3_version)) {
-        psError(PS_ERR_UNKNOWN, false, "failed to add item p3_version");
         psFree(md);
         return false;
@@ -15453,5 +15537,5 @@
 bool p5InputScfileInsertObject(psDB *dbh, p5InputScfileRow *object)
 {
-    return p5InputScfileInsert(dbh, object->p5_id, object->p4_id, object->skycell_id, object->tess_id, object->exp_tag, object->p3_version, object->kind, object->template);
+    return p5InputScfileInsert(dbh, object->p5_id, object->p4_id, object->skycell_id, object->tess_id, object->kind, object->template);
 }
 
@@ -15546,14 +15630,4 @@
         return false;
     }
-    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_STRING, NULL, object->exp_tag)) {
-        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag");
-        psFree(md);
-        return false;
-    }
-    if (!psMetadataAdd(md, PS_LIST_TAIL, "p3_version", PS_DATA_S32, NULL, object->p3_version)) {
-        psError(PS_ERR_UNKNOWN, false, "failed to add item p3_version");
-        psFree(md);
-        return false;
-    }
     if (!psMetadataAdd(md, PS_LIST_TAIL, "kind", PS_DATA_STRING, NULL, object->kind)) {
         psError(PS_ERR_UNKNOWN, false, "failed to add item kind");
@@ -15595,14 +15669,4 @@
         return false;
     }
-    char* exp_tag = psMetadataLookupPtr(&status, md, "exp_tag");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item exp_tag");
-        return false;
-    }
-    psS32 p3_version = psMetadataLookupS32(&status, md, "p3_version");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item p3_version");
-        return false;
-    }
     char* kind = psMetadataLookupPtr(&status, md, "kind");
     if (!status) {
@@ -15616,5 +15680,5 @@
     }
 
-    return p5InputScfileRowAlloc(p5_id, p4_id, skycell_id, tess_id, exp_tag, p3_version, kind, template);
+    return p5InputScfileRowAlloc(p5_id, p4_id, skycell_id, tess_id, kind, template);
 }
 psArray *p5InputScfileSelectRowObjects(psDB *dbh, const psMetadata *where, unsigned long long limit)
@@ -16107,2 +16171,341 @@
     return true;
 }
+static void p6RunRowFree(p6RunRow *object);
+
+p6RunRow *p6RunRowAlloc(psS32 p6_id, const char *state, const char *workdir, psTime* registered)
+{
+    p6RunRow        *_object;
+
+    _object = psAlloc(sizeof(p6RunRow));
+    psMemSetDeallocator(_object, (psFreeFunc)p6RunRowFree);
+
+    _object->p6_id = p6_id;
+    _object->state = psStringCopy(state);
+    _object->workdir = psStringCopy(workdir);
+    _object->registered = psTimeCopy(registered);
+
+    return _object;
+}
+
+static void p6RunRowFree(p6RunRow *object)
+{
+    psFree(object->state);
+    psFree(object->workdir);
+    psFree(object->registered);
+}
+
+bool p6RunCreateTable(psDB *dbh)
+{
+    psMetadata *md = psMetadataAlloc();
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "p6_id", PS_DATA_S32, "Primary Key AUTO_INCREMENT", 0)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item p6_id");
+        psFree(md);
+        return false;
+    }
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "state", PS_DATA_STRING, "Key", "64")) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item state");
+        psFree(md);
+        return false;
+    }
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "workdir", PS_DATA_STRING, NULL, "255")) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item workdir");
+        psFree(md);
+        return false;
+    }
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "registered", PS_DATA_TIME, NULL, NULL)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item registered");
+        psFree(md);
+        return false;
+    }
+
+    bool status = psDBCreateTable(dbh, P6RUN_TABLE_NAME, md);
+
+    psFree(md);
+
+    return status;
+}
+
+bool p6RunDropTable(psDB *dbh)
+{
+    return psDBDropTable(dbh, P6RUN_TABLE_NAME);
+}
+
+bool p6RunInsert(psDB * dbh, psS32 p6_id, const char *state, const char *workdir, psTime* registered)
+{
+    psMetadata *md = psMetadataAlloc();
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "p6_id", PS_DATA_S32, NULL, p6_id)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item p6_id");
+        psFree(md);
+        return false;
+    }
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "state", PS_DATA_STRING, NULL, state)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item state");
+        psFree(md);
+        return false;
+    }
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "workdir", PS_DATA_STRING, NULL, workdir)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item workdir");
+        psFree(md);
+        return false;
+    }
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "registered", PS_DATA_TIME, NULL, registered)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item registered");
+        psFree(md);
+        return false;
+    }
+
+    bool status = psDBInsertOneRow(dbh, P6RUN_TABLE_NAME, md);
+    psFree(md);
+
+    return status;
+}
+
+long long p6RunDelete(psDB *dbh, const psMetadata *where, unsigned long long limit)
+{
+    long long       deleted = 0;
+
+    long long count = psDBDeleteRows(dbh, P6RUN_TABLE_NAME, where, limit);
+    if (count < 0) {
+        psError(PS_ERR_UNKNOWN, true, "failed to delete row from p6Run");
+        return count;
+
+        deleted += count;
+    }
+
+    return deleted;
+}
+bool p6RunInsertObject(psDB *dbh, p6RunRow *object)
+{
+    return p6RunInsert(dbh, object->p6_id, object->state, object->workdir, object->registered);
+}
+
+bool p6RunInsertObjects(psDB *dbh, psArray *objects)
+{
+    for (long i = 0; i < psArrayLength(objects); i++) {
+        if (!p6RunInsertObject(dbh, objects->data[i])) {
+            return false;
+        }
+    }
+
+    return true;
+}
+
+bool p6RunInsertFits(psDB *dbh, const psFits *fits)
+{
+    psArray         *rowSet;
+
+    // move to (the first?) extension named  P6RUN_TABLE_NAME
+    if (!psFitsMoveExtName(fits, P6RUN_TABLE_NAME)) {
+        psError(PS_ERR_UNKNOWN, true, "failed to find FITS extension %s", P6RUN_TABLE_NAME);
+        return false;
+    }
+
+    // check HDU type
+    if (psFitsGetExtType(fits) != PS_FITS_TYPE_BINARY_TABLE)  {
+        psError(PS_ERR_UNKNOWN, true, "FITS HDU type is not PS_FITS_TYPE_BINARY_TABLE");
+        return false;
+    }
+
+    // read fits table
+    rowSet = psFitsReadTable(fits);
+    if (!rowSet) {
+        psError(PS_ERR_UNKNOWN, true, "FITS read error or FITS table is empty");
+        psFree(rowSet);
+        return false;
+    }
+
+    if (!psDBInsertRows(dbh, P6RUN_TABLE_NAME, rowSet)) {
+        psError(PS_ERR_UNKNOWN, false, "databse insert failed");
+        psFree(rowSet);
+        return false;
+    }
+
+    psFree(rowSet);
+
+    return true;
+}
+
+bool p6RunSelectRowsFits(psDB *dbh, psFits *fits, const psMetadata *where, unsigned long long limit)
+{
+    psArray         *rowSet;
+
+    rowSet = psDBSelectRows(dbh, P6RUN_TABLE_NAME, where, limit);
+    if (!rowSet) {
+        return false;
+    }
+
+    // output to fits
+    if (!psFitsWriteTable(fits, NULL, rowSet, P6RUN_TABLE_NAME)) {
+        psError(PS_ERR_UNKNOWN, false, "FITS table write failed");
+        psFree(rowSet);
+        return false;
+    }
+
+    psFree(rowSet);
+
+    return true;
+}
+
+psMetadata *p6RunMetadataFromObject(const p6RunRow *object)
+{
+    psMetadata *md = psMetadataAlloc();
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "p6_id", PS_DATA_S32, NULL, object->p6_id)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item p6_id");
+        psFree(md);
+        return false;
+    }
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "state", PS_DATA_STRING, NULL, object->state)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item state");
+        psFree(md);
+        return false;
+    }
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "workdir", PS_DATA_STRING, NULL, object->workdir)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item workdir");
+        psFree(md);
+        return false;
+    }
+    if (!psMetadataAdd(md, PS_LIST_TAIL, "registered", PS_DATA_TIME, NULL, object->registered)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to add item registered");
+        psFree(md);
+        return false;
+    }
+
+
+    return md;
+}
+
+p6RunRow *p6RunObjectFromMetadata(psMetadata *md)
+{
+
+bool status = false;
+    psS32 p6_id = psMetadataLookupS32(&status, md, "p6_id");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item p6_id");
+        return false;
+    }
+    char* state = psMetadataLookupPtr(&status, md, "state");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item state");
+        return false;
+    }
+    char* workdir = psMetadataLookupPtr(&status, md, "workdir");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item workdir");
+        return false;
+    }
+    psTime* registered = psMetadataLookupPtr(&status, md, "registered");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item registered");
+        return false;
+    }
+
+    return p6RunRowAlloc(p6_id, state, workdir, registered);
+}
+psArray *p6RunSelectRowObjects(psDB *dbh, const psMetadata *where, unsigned long long limit)
+{
+    psArray         *rowSet;
+    psArray         *returnSet;
+    psU64           i;
+
+    rowSet = psDBSelectRows(dbh, P6RUN_TABLE_NAME, where, limit);
+    if (!rowSet) {
+        return NULL;
+    }
+
+    // convert psMetadata rows to row objects
+
+    returnSet = psArrayAllocEmpty(rowSet->n);
+
+    for (i = 0; i < rowSet->n; i++) {
+        p6RunRow *object = p6RunObjectFromMetadata(rowSet->data[i]);
+        psArrayAdd(returnSet, 0, object);
+        psFree(object);
+    }
+
+    psFree(rowSet);
+
+    return returnSet;
+}
+bool p6RunDeleteObject(psDB *dbh, const p6RunRow *object)
+{
+    psMetadata *where = p6RunMetadataFromObject(object);
+    long long count = psDBDeleteRows(dbh, P6RUN_TABLE_NAME, where, 0);
+    psFree(where);
+    if (count < 0) {
+        psError(PS_ERR_UNKNOWN, true, "failed to delete row from p6Run");
+        return false;
+    }
+    if (count > 1) {
+        // XXX should this be a psAbort() instead?  It is possible that
+        // having an object match multiple rows was by design.
+        psError(PS_ERR_UNKNOWN, true, "p6RunRow object matched more then one row.  Check your database schema");
+        return false;
+    }
+
+    return true;
+}
+long long p6RunDeleteRowObjects(psDB *dbh, const psArray *objects, unsigned long long limit)
+{
+    long long       deleted = 0;
+
+    for (long long i = 0; i < objects->n; i++) {
+        p6RunRow *object = objects->data[i];
+        psMetadata *where = p6RunMetadataFromObject(object);
+        long long count = psDBDeleteRows(dbh, P6RUN_TABLE_NAME, where, limit);
+        psFree(where);
+        if (count < 0) {
+            psError(PS_ERR_UNKNOWN, true, "failed to delete row from p6Run");
+            return count;
+        }
+
+        deleted += count;
+    }
+
+    return deleted;
+}
+bool p6RunPrintObjects(FILE *stream, psArray *objects, bool mdcf)
+{
+    PS_ASSERT_PTR_NON_NULL(objects, false);
+
+    psMetadata *output = psMetadataAlloc();
+    for (long i = 0; i < psArrayLength(objects); i++) {
+        psMetadata *md = p6RunMetadataFromObject(objects->data[i]);
+        if (!psMetadataAddMetadata(
+            output,
+            PS_LIST_TAIL,
+            P6RUN_TABLE_NAME,
+            PS_META_DUPLICATE_OK,
+            NULL,
+            md
+        )) {
+            psError(PS_ERR_UNKNOWN, false, "failed to add metadata");
+            psFree(md);
+            psFree(output);
+            return false;
+        }
+        psFree(md);
+    }
+
+    if (!ippdbPrintMetadataRaw(stream, output, mdcf)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to print metadata");
+        psFree(output);
+    }
+    psFree(output);
+
+    return true;
+}
+bool p6RunPrintObject(FILE *stream, p6RunRow *object, bool mdcf)
+{
+    PS_ASSERT_PTR_NON_NULL(object, false);
+
+    psMetadata *md = p6RunMetadataFromObject(object);
+
+    if (!ippdbPrintMetadataRaw(stream, md, mdcf)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to print metadata");
+        psFree(md);
+    }
+
+    psFree(md);
+
+    return true;
+}
