IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Nov 17, 2006, 2:19:43 PM (20 years ago)
Author:
jhoblitt
Message:

VERSION 0.0.56

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippdb/src/ippdb.c

    r9970 r10060  
    3232
    3333#define SUMMITEXP_TABLE_NAME "summitExp"
     34#define SUMMITIMFILE_TABLE_NAME "summitImfile"
    3435#define PZPENDINGEXP_TABLE_NAME "pzPendingExp"
    3536#define PZPENDINGIMFILE_TABLE_NAME "pzPendingImfile"
     37#define PZDONEEXP_TABLE_NAME "pzDoneExp"
     38#define PZDONEIMFILE_TABLE_NAME "pzDoneImfile"
    3639#define NEWEXP_TABLE_NAME "newExp"
    3740#define NEWIMFILE_TABLE_NAME "newImfile"
     
    234237static void summitExpRowFree(summitExpRow *object);
    235238
    236 summitExpRow *summitExpRowAlloc(const char *exp_id, const char *camera, const char *telescope, psTime* time, const char *exp_type, const char *uri)
     239summitExpRow *summitExpRowAlloc(const char *exp_id, const char *camera, const char *telescope, psTime* dateobs, const char *exp_type, const char *uri, psS32 imfiles)
    237240{
    238241    summitExpRow    *_object;
     
    244247    _object->camera = psStringCopy(camera);
    245248    _object->telescope = psStringCopy(telescope);
    246     _object->time = psTimeCopy(time);
     249    _object->dateobs = psTimeCopy(dateobs);
    247250    _object->exp_type = psStringCopy(exp_type);
    248251    _object->uri = psStringCopy(uri);
     252    _object->imfiles = imfiles;
    249253
    250254    return _object;
     
    256260    psFree(object->camera);
    257261    psFree(object->telescope);
    258     psFree(object->time);
     262    psFree(object->dateobs);
    259263    psFree(object->exp_type);
    260264    psFree(object->uri);
     
    279283        return false;
    280284    }
    281     psTime* time = psTimeFromISO("", PS_TIME_UTC);
    282     if (!psMetadataAdd(md, PS_LIST_TAIL, "time", PS_DATA_TIME, NULL, time)) {
    283         psFree(time);
    284         psError(PS_ERR_UNKNOWN, false, "failed to add item time");
    285         psFree(md);
    286         return false;
    287     }
    288     psFree(time);
     285    psTime* dateobs = psTimeFromISO("", PS_TIME_UTC);
     286    if (!psMetadataAdd(md, PS_LIST_TAIL, "dateobs", PS_DATA_TIME, NULL, dateobs)) {
     287        psFree(dateobs);
     288        psError(PS_ERR_UNKNOWN, false, "failed to add item dateobs");
     289        psFree(md);
     290        return false;
     291    }
     292    psFree(dateobs);
    289293    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_type", PS_DATA_STRING, NULL, "64")) {
    290294        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_type");
     
    297301        return false;
    298302    }
     303    if (!psMetadataAdd(md, PS_LIST_TAIL, "imfiles", PS_DATA_S32, NULL, 0)) {
     304        psError(PS_ERR_UNKNOWN, false, "failed to add item imfiles");
     305        psFree(md);
     306        return false;
     307    }
    299308
    300309    bool status = psDBCreateTable(dbh, SUMMITEXP_TABLE_NAME, md);
     
    310319}
    311320
    312 bool summitExpInsert(psDB * dbh, const char *exp_id, const char *camera, const char *telescope, psTime* time, const char *exp_type, const char *uri)
     321bool summitExpInsert(psDB * dbh, const char *exp_id, const char *camera, const char *telescope, psTime* dateobs, const char *exp_type, const char *uri, psS32 imfiles)
    313322{
    314323    psMetadata *md = psMetadataAlloc();
     
    328337        return false;
    329338    }
    330     if (!psMetadataAdd(md, PS_LIST_TAIL, "time", PS_DATA_TIME, NULL, time)) {
    331         psError(PS_ERR_UNKNOWN, false, "failed to add item time");
     339    if (!psMetadataAdd(md, PS_LIST_TAIL, "dateobs", PS_DATA_TIME, NULL, dateobs)) {
     340        psError(PS_ERR_UNKNOWN, false, "failed to add item dateobs");
    332341        psFree(md);
    333342        return false;
     
    340349    if (!psMetadataAdd(md, PS_LIST_TAIL, "uri", PS_DATA_STRING, NULL, uri)) {
    341350        psError(PS_ERR_UNKNOWN, false, "failed to add item uri");
     351        psFree(md);
     352        return false;
     353    }
     354    if (!psMetadataAdd(md, PS_LIST_TAIL, "imfiles", PS_DATA_S32, NULL, imfiles)) {
     355        psError(PS_ERR_UNKNOWN, false, "failed to add item imfiles");
    342356        psFree(md);
    343357        return false;
     
    366380bool summitExpInsertObject(psDB *dbh, summitExpRow *object)
    367381{
    368     return summitExpInsert(dbh, object->exp_id, object->camera, object->telescope, object->time, object->exp_type, object->uri);
     382    return summitExpInsert(dbh, object->exp_id, object->camera, object->telescope, object->dateobs, object->exp_type, object->uri, object->imfiles);
    369383}
    370384
     
    454468        return false;
    455469    }
    456     if (!psMetadataAdd(md, PS_LIST_TAIL, "time", PS_DATA_TIME, NULL, object->time)) {
    457         psError(PS_ERR_UNKNOWN, false, "failed to add item time");
     470    if (!psMetadataAdd(md, PS_LIST_TAIL, "dateobs", PS_DATA_TIME, NULL, object->dateobs)) {
     471        psError(PS_ERR_UNKNOWN, false, "failed to add item dateobs");
    458472        psFree(md);
    459473        return false;
     
    469483        return false;
    470484    }
     485    if (!psMetadataAdd(md, PS_LIST_TAIL, "imfiles", PS_DATA_S32, NULL, object->imfiles)) {
     486        psError(PS_ERR_UNKNOWN, false, "failed to add item imfiles");
     487        psFree(md);
     488        return false;
     489    }
    471490
    472491
     
    493512        return false;
    494513    }
    495     psTime* time = psMetadataLookupPtr(&status, md, "time");
    496     if (!status) {
    497         psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item time");
     514    psTime* dateobs = psMetadataLookupPtr(&status, md, "dateobs");
     515    if (!status) {
     516        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item dateobs");
    498517        return false;
    499518    }
     
    508527        return false;
    509528    }
    510 
    511     return summitExpRowAlloc(exp_id, camera, telescope, time, exp_type, uri);
     529    psS32 imfiles = psMetadataLookupS32(&status, md, "imfiles");
     530    if (!status) {
     531        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item imfiles");
     532        return false;
     533    }
     534
     535    return summitExpRowAlloc(exp_id, camera, telescope, dateobs, exp_type, uri, imfiles);
    512536}
    513537psArray *summitExpSelectRowObjects(psDB *dbh, const psMetadata *where, unsigned long long limit)
     
    604628    return true;
    605629}
    606 static void pzPendingExpRowFree(pzPendingExpRow *object);
    607 
    608 pzPendingExpRow *pzPendingExpRowAlloc(const char *exp_id, const char *camera, const char *telescope, const char *exp_type, psS32 imfiles)
    609 {
    610     pzPendingExpRow *_object;
    611 
    612     _object = psAlloc(sizeof(pzPendingExpRow));
    613     psMemSetDeallocator(_object, (psFreeFunc)pzPendingExpRowFree);
     630static void summitImfileRowFree(summitImfileRow *object);
     631
     632summitImfileRow *summitImfileRowAlloc(const char *exp_id, const char *camera, const char *telescope, psS32 bytes, const char *md5sum, const char *class, const char *class_id, const char *uri)
     633{
     634    summitImfileRow *_object;
     635
     636    _object = psAlloc(sizeof(summitImfileRow));
     637    psMemSetDeallocator(_object, (psFreeFunc)summitImfileRowFree);
    614638
    615639    _object->exp_id = psStringCopy(exp_id);
    616640    _object->camera = psStringCopy(camera);
    617641    _object->telescope = psStringCopy(telescope);
    618     _object->exp_type = psStringCopy(exp_type);
    619     _object->imfiles = imfiles;
     642    _object->bytes = bytes;
     643    _object->md5sum = psStringCopy(md5sum);
     644    _object->class = psStringCopy(class);
     645    _object->class_id = psStringCopy(class_id);
     646    _object->uri = psStringCopy(uri);
    620647
    621648    return _object;
    622649}
    623650
    624 static void pzPendingExpRowFree(pzPendingExpRow *object)
     651static void summitImfileRowFree(summitImfileRow *object)
    625652{
    626653    psFree(object->exp_id);
    627654    psFree(object->camera);
    628655    psFree(object->telescope);
    629     psFree(object->exp_type);
    630 }
    631 
    632 bool pzPendingExpCreateTable(psDB *dbh)
     656    psFree(object->md5sum);
     657    psFree(object->class);
     658    psFree(object->class_id);
     659    psFree(object->uri);
     660}
     661
     662bool summitImfileCreateTable(psDB *dbh)
    633663{
    634664    psMetadata *md = psMetadataAlloc();
     
    648678        return false;
    649679    }
    650     if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_type", PS_DATA_STRING, NULL, "64")) {
    651         psError(PS_ERR_UNKNOWN, false, "failed to add item exp_type");
    652         psFree(md);
    653         return false;
    654     }
    655     if (!psMetadataAdd(md, PS_LIST_TAIL, "imfiles", PS_DATA_S32, NULL, 0)) {
    656         psError(PS_ERR_UNKNOWN, false, "failed to add item imfiles");
    657         psFree(md);
    658         return false;
    659     }
    660 
    661     bool status = psDBCreateTable(dbh, PZPENDINGEXP_TABLE_NAME, md);
     680    if (!psMetadataAdd(md, PS_LIST_TAIL, "bytes", PS_DATA_S32, NULL, 0)) {
     681        psError(PS_ERR_UNKNOWN, false, "failed to add item bytes");
     682        psFree(md);
     683        return false;
     684    }
     685    if (!psMetadataAdd(md, PS_LIST_TAIL, "md5sum", PS_DATA_STRING, NULL, "32")) {
     686        psError(PS_ERR_UNKNOWN, false, "failed to add item md5sum");
     687        psFree(md);
     688        return false;
     689    }
     690    if (!psMetadataAdd(md, PS_LIST_TAIL, "class", PS_DATA_STRING, "Primary Key", "64")) {
     691        psError(PS_ERR_UNKNOWN, false, "failed to add item class");
     692        psFree(md);
     693        return false;
     694    }
     695    if (!psMetadataAdd(md, PS_LIST_TAIL, "class_id", PS_DATA_STRING, "Primary Key", "64")) {
     696        psError(PS_ERR_UNKNOWN, false, "failed to add item class_id");
     697        psFree(md);
     698        return false;
     699    }
     700    if (!psMetadataAdd(md, PS_LIST_TAIL, "uri", PS_DATA_STRING, NULL, "255")) {
     701        psError(PS_ERR_UNKNOWN, false, "failed to add item uri");
     702        psFree(md);
     703        return false;
     704    }
     705
     706    bool status = psDBCreateTable(dbh, SUMMITIMFILE_TABLE_NAME, md);
    662707
    663708    psFree(md);
     
    666711}
    667712
    668 bool pzPendingExpDropTable(psDB *dbh)
    669 {
    670     return psDBDropTable(dbh, PZPENDINGEXP_TABLE_NAME);
    671 }
    672 
    673 bool pzPendingExpInsert(psDB * dbh, const char *exp_id, const char *camera, const char *telescope, const char *exp_type, psS32 imfiles)
     713bool summitImfileDropTable(psDB *dbh)
     714{
     715    return psDBDropTable(dbh, SUMMITIMFILE_TABLE_NAME);
     716}
     717
     718bool summitImfileInsert(psDB * dbh, const char *exp_id, const char *camera, const char *telescope, psS32 bytes, const char *md5sum, const char *class, const char *class_id, const char *uri)
    674719{
    675720    psMetadata *md = psMetadataAlloc();
     
    689734        return false;
    690735    }
    691     if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_type", PS_DATA_STRING, NULL, exp_type)) {
    692         psError(PS_ERR_UNKNOWN, false, "failed to add item exp_type");
    693         psFree(md);
    694         return false;
    695     }
    696     if (!psMetadataAdd(md, PS_LIST_TAIL, "imfiles", PS_DATA_S32, NULL, imfiles)) {
    697         psError(PS_ERR_UNKNOWN, false, "failed to add item imfiles");
    698         psFree(md);
    699         return false;
    700     }
    701 
    702     bool status = psDBInsertOneRow(dbh, PZPENDINGEXP_TABLE_NAME, md);
     736    if (!psMetadataAdd(md, PS_LIST_TAIL, "bytes", PS_DATA_S32, NULL, bytes)) {
     737        psError(PS_ERR_UNKNOWN, false, "failed to add item bytes");
     738        psFree(md);
     739        return false;
     740    }
     741    if (!psMetadataAdd(md, PS_LIST_TAIL, "md5sum", PS_DATA_STRING, NULL, md5sum)) {
     742        psError(PS_ERR_UNKNOWN, false, "failed to add item md5sum");
     743        psFree(md);
     744        return false;
     745    }
     746    if (!psMetadataAdd(md, PS_LIST_TAIL, "class", PS_DATA_STRING, NULL, class)) {
     747        psError(PS_ERR_UNKNOWN, false, "failed to add item class");
     748        psFree(md);
     749        return false;
     750    }
     751    if (!psMetadataAdd(md, PS_LIST_TAIL, "class_id", PS_DATA_STRING, NULL, class_id)) {
     752        psError(PS_ERR_UNKNOWN, false, "failed to add item class_id");
     753        psFree(md);
     754        return false;
     755    }
     756    if (!psMetadataAdd(md, PS_LIST_TAIL, "uri", PS_DATA_STRING, NULL, uri)) {
     757        psError(PS_ERR_UNKNOWN, false, "failed to add item uri");
     758        psFree(md);
     759        return false;
     760    }
     761
     762    bool status = psDBInsertOneRow(dbh, SUMMITIMFILE_TABLE_NAME, md);
    703763    psFree(md);
    704764
     
    706766}
    707767
    708 long long pzPendingExpDelete(psDB *dbh, const psMetadata *where, unsigned long long limit)
     768long long summitImfileDelete(psDB *dbh, const psMetadata *where, unsigned long long limit)
    709769{
    710770    long long       deleted = 0;
    711771
    712     long long count = psDBDeleteRows(dbh, PZPENDINGEXP_TABLE_NAME, where, limit);
     772    long long count = psDBDeleteRows(dbh, SUMMITIMFILE_TABLE_NAME, where, limit);
    713773    if (count < 0) {
    714         psError(PS_ERR_UNKNOWN, true, "failed to delete row from pzPendingExp");
     774        psError(PS_ERR_UNKNOWN, true, "failed to delete row from summitImfile");
    715775        return count;
    716776
     
    720780    return deleted;
    721781}
    722 bool pzPendingExpInsertObject(psDB *dbh, pzPendingExpRow *object)
    723 {
    724     return pzPendingExpInsert(dbh, object->exp_id, object->camera, object->telescope, object->exp_type, object->imfiles);
    725 }
    726 
    727 bool pzPendingExpInsertObjects(psDB *dbh, psArray *objects)
     782bool summitImfileInsertObject(psDB *dbh, summitImfileRow *object)
     783{
     784    return summitImfileInsert(dbh, object->exp_id, object->camera, object->telescope, object->bytes, object->md5sum, object->class, object->class_id, object->uri);
     785}
     786
     787bool summitImfileInsertObjects(psDB *dbh, psArray *objects)
    728788{
    729789    for (long i = 0; i < psArrayLength(objects); i++) {
    730         if (!pzPendingExpInsertObject(dbh, objects->data[i])) {
     790        if (!summitImfileInsertObject(dbh, objects->data[i])) {
    731791            return false;
    732792        }
     
    736796}
    737797
    738 bool pzPendingExpInsertFits(psDB *dbh, const psFits *fits)
     798bool summitImfileInsertFits(psDB *dbh, const psFits *fits)
    739799{
    740800    psArray         *rowSet;
    741801
    742     // move to (the first?) extension named  PZPENDINGEXP_TABLE_NAME
    743     if (!psFitsMoveExtName(fits, PZPENDINGEXP_TABLE_NAME)) {
    744         psError(PS_ERR_UNKNOWN, true, "failed to find FITS extension %s", PZPENDINGEXP_TABLE_NAME);
     802    // move to (the first?) extension named  SUMMITIMFILE_TABLE_NAME
     803    if (!psFitsMoveExtName(fits, SUMMITIMFILE_TABLE_NAME)) {
     804        psError(PS_ERR_UNKNOWN, true, "failed to find FITS extension %s", SUMMITIMFILE_TABLE_NAME);
    745805        return false;
    746806    }
     
    760820    }
    761821
     822    if (!psDBInsertRows(dbh, SUMMITIMFILE_TABLE_NAME, rowSet)) {
     823        psError(PS_ERR_UNKNOWN, false, "databse insert failed");
     824        psFree(rowSet);
     825        return false;
     826    }
     827
     828    psFree(rowSet);
     829
     830    return true;
     831}
     832
     833bool summitImfileSelectRowsFits(psDB *dbh, psFits *fits, const psMetadata *where, unsigned long long limit)
     834{
     835    psArray         *rowSet;
     836
     837    rowSet = psDBSelectRows(dbh, SUMMITIMFILE_TABLE_NAME, where, limit);
     838    if (!rowSet) {
     839        return false;
     840    }
     841
     842    // output to fits
     843    if (!psFitsWriteTable(fits, NULL, rowSet, SUMMITIMFILE_TABLE_NAME)) {
     844        psError(PS_ERR_UNKNOWN, false, "FITS table write failed");
     845        psFree(rowSet);
     846        return false;
     847    }
     848
     849    psFree(rowSet);
     850
     851    return true;
     852}
     853
     854psMetadata *summitImfileMetadataFromObject(const summitImfileRow *object)
     855{
     856    psMetadata *md = psMetadataAlloc();
     857    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_id", PS_DATA_STRING, NULL, object->exp_id)) {
     858        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id");
     859        psFree(md);
     860        return false;
     861    }
     862    if (!psMetadataAdd(md, PS_LIST_TAIL, "camera", PS_DATA_STRING, NULL, object->camera)) {
     863        psError(PS_ERR_UNKNOWN, false, "failed to add item camera");
     864        psFree(md);
     865        return false;
     866    }
     867    if (!psMetadataAdd(md, PS_LIST_TAIL, "telescope", PS_DATA_STRING, NULL, object->telescope)) {
     868        psError(PS_ERR_UNKNOWN, false, "failed to add item telescope");
     869        psFree(md);
     870        return false;
     871    }
     872    if (!psMetadataAdd(md, PS_LIST_TAIL, "bytes", PS_DATA_S32, NULL, object->bytes)) {
     873        psError(PS_ERR_UNKNOWN, false, "failed to add item bytes");
     874        psFree(md);
     875        return false;
     876    }
     877    if (!psMetadataAdd(md, PS_LIST_TAIL, "md5sum", PS_DATA_STRING, NULL, object->md5sum)) {
     878        psError(PS_ERR_UNKNOWN, false, "failed to add item md5sum");
     879        psFree(md);
     880        return false;
     881    }
     882    if (!psMetadataAdd(md, PS_LIST_TAIL, "class", PS_DATA_STRING, NULL, object->class)) {
     883        psError(PS_ERR_UNKNOWN, false, "failed to add item class");
     884        psFree(md);
     885        return false;
     886    }
     887    if (!psMetadataAdd(md, PS_LIST_TAIL, "class_id", PS_DATA_STRING, NULL, object->class_id)) {
     888        psError(PS_ERR_UNKNOWN, false, "failed to add item class_id");
     889        psFree(md);
     890        return false;
     891    }
     892    if (!psMetadataAdd(md, PS_LIST_TAIL, "uri", PS_DATA_STRING, NULL, object->uri)) {
     893        psError(PS_ERR_UNKNOWN, false, "failed to add item uri");
     894        psFree(md);
     895        return false;
     896    }
     897
     898
     899    return md;
     900}
     901
     902summitImfileRow *summitImfileObjectFromMetadata(psMetadata *md)
     903{
     904
     905bool status = false;
     906    char* exp_id = psMetadataLookupPtr(&status, md, "exp_id");
     907    if (!status) {
     908        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item exp_id");
     909        return false;
     910    }
     911    char* camera = psMetadataLookupPtr(&status, md, "camera");
     912    if (!status) {
     913        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item camera");
     914        return false;
     915    }
     916    char* telescope = psMetadataLookupPtr(&status, md, "telescope");
     917    if (!status) {
     918        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item telescope");
     919        return false;
     920    }
     921    psS32 bytes = psMetadataLookupS32(&status, md, "bytes");
     922    if (!status) {
     923        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item bytes");
     924        return false;
     925    }
     926    char* md5sum = psMetadataLookupPtr(&status, md, "md5sum");
     927    if (!status) {
     928        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item md5sum");
     929        return false;
     930    }
     931    char* class = psMetadataLookupPtr(&status, md, "class");
     932    if (!status) {
     933        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item class");
     934        return false;
     935    }
     936    char* class_id = psMetadataLookupPtr(&status, md, "class_id");
     937    if (!status) {
     938        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item class_id");
     939        return false;
     940    }
     941    char* uri = psMetadataLookupPtr(&status, md, "uri");
     942    if (!status) {
     943        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item uri");
     944        return false;
     945    }
     946
     947    return summitImfileRowAlloc(exp_id, camera, telescope, bytes, md5sum, class, class_id, uri);
     948}
     949psArray *summitImfileSelectRowObjects(psDB *dbh, const psMetadata *where, unsigned long long limit)
     950{
     951    psArray         *rowSet;
     952    psArray         *returnSet;
     953    psU64           i;
     954
     955    rowSet = psDBSelectRows(dbh, SUMMITIMFILE_TABLE_NAME, where, limit);
     956    if (!rowSet) {
     957        return NULL;
     958    }
     959
     960    // convert psMetadata rows to row objects
     961
     962    returnSet = psArrayAllocEmpty(rowSet->n);
     963
     964    for (i = 0; i < rowSet->n; i++) {
     965        summitImfileRow *object = summitImfileObjectFromMetadata(rowSet->data[i]);
     966        psArrayAdd(returnSet, 0, object);
     967        psFree(object);
     968    }
     969
     970    psFree(rowSet);
     971
     972    return returnSet;
     973}
     974bool summitImfileDeleteObject(psDB *dbh, const summitImfileRow *object)
     975{
     976    psMetadata *where = summitImfileMetadataFromObject(object);
     977    long long count = psDBDeleteRows(dbh, SUMMITIMFILE_TABLE_NAME, where, 0);
     978    psFree(where)
     979    if (count < 0) {
     980        psError(PS_ERR_UNKNOWN, true, "failed to delete row from summitImfile");
     981        return false;
     982    }
     983    if (count > 1) {
     984        // XXX should this be a psAbort() instead?  It is possible that
     985        // having an object match multiple rows was by design.
     986        psError(PS_ERR_UNKNOWN, true, "summitImfileRow object matched more then one row.  Check your database schema");
     987        return false;
     988    }
     989
     990    return true;
     991}
     992long long summitImfileDeleteRowObjects(psDB *dbh, const psArray *objects, unsigned long long limit)
     993{
     994    long long       deleted = 0;
     995
     996    for (long long i = 0; i < objects->n; i++) {
     997        summitImfileRow *object = objects->data[i];
     998        psMetadata *where = summitImfileMetadataFromObject(object);
     999        long long count = psDBDeleteRows(dbh, SUMMITIMFILE_TABLE_NAME, where, limit);
     1000        psFree(where)
     1001        if (count < 0) {
     1002            psError(PS_ERR_UNKNOWN, true, "failed to delete row from summitImfile");
     1003            return count;
     1004        }
     1005
     1006        deleted += count;
     1007    }
     1008
     1009    return deleted;
     1010}
     1011bool summitImfilePrintObjects(FILE *stream, psArray *objects, bool mdcf)
     1012{
     1013    PS_ASSERT_PTR_NON_NULL(objects, false);
     1014
     1015    psMetadata *output = psMetadataAlloc();
     1016    for (long i = 0; i < psArrayLength(objects); i++) {
     1017        psMetadata *md = summitImfileMetadataFromObject(objects->data[i]);
     1018        if (!psMetadataAddMetadata(
     1019            output,
     1020            PS_LIST_TAIL,
     1021            SUMMITIMFILE_TABLE_NAME,
     1022            PS_META_DUPLICATE_OK,
     1023            NULL,
     1024            md
     1025        )) {
     1026            psError(PS_ERR_UNKNOWN, false, "failed to add metadata");
     1027            psFree(md);
     1028            psFree(output);
     1029            return false;
     1030        }
     1031        psFree(md);
     1032    }
     1033
     1034    if (!ippdbPrintMetadataRaw(stream, output, mdcf)) {
     1035        psError(PS_ERR_UNKNOWN, false, "failed to print metadata");
     1036        psFree(output);
     1037    }
     1038    psFree(output);
     1039
     1040    return true;
     1041}
     1042static void pzPendingExpRowFree(pzPendingExpRow *object);
     1043
     1044pzPendingExpRow *pzPendingExpRowAlloc(const char *exp_id, const char *camera, const char *telescope)
     1045{
     1046    pzPendingExpRow *_object;
     1047
     1048    _object = psAlloc(sizeof(pzPendingExpRow));
     1049    psMemSetDeallocator(_object, (psFreeFunc)pzPendingExpRowFree);
     1050
     1051    _object->exp_id = psStringCopy(exp_id);
     1052    _object->camera = psStringCopy(camera);
     1053    _object->telescope = psStringCopy(telescope);
     1054
     1055    return _object;
     1056}
     1057
     1058static void pzPendingExpRowFree(pzPendingExpRow *object)
     1059{
     1060    psFree(object->exp_id);
     1061    psFree(object->camera);
     1062    psFree(object->telescope);
     1063}
     1064
     1065bool pzPendingExpCreateTable(psDB *dbh)
     1066{
     1067    psMetadata *md = psMetadataAlloc();
     1068    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_id", PS_DATA_STRING, "Primary Key", "64")) {
     1069        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id");
     1070        psFree(md);
     1071        return false;
     1072    }
     1073    if (!psMetadataAdd(md, PS_LIST_TAIL, "camera", PS_DATA_STRING, "Primary Key", "64")) {
     1074        psError(PS_ERR_UNKNOWN, false, "failed to add item camera");
     1075        psFree(md);
     1076        return false;
     1077    }
     1078    if (!psMetadataAdd(md, PS_LIST_TAIL, "telescope", PS_DATA_STRING, "Primary Key", "64")) {
     1079        psError(PS_ERR_UNKNOWN, false, "failed to add item telescope");
     1080        psFree(md);
     1081        return false;
     1082    }
     1083
     1084    bool status = psDBCreateTable(dbh, PZPENDINGEXP_TABLE_NAME, md);
     1085
     1086    psFree(md);
     1087
     1088    return status;
     1089}
     1090
     1091bool pzPendingExpDropTable(psDB *dbh)
     1092{
     1093    return psDBDropTable(dbh, PZPENDINGEXP_TABLE_NAME);
     1094}
     1095
     1096bool pzPendingExpInsert(psDB * dbh, const char *exp_id, const char *camera, const char *telescope)
     1097{
     1098    psMetadata *md = psMetadataAlloc();
     1099    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_id", PS_DATA_STRING, NULL, exp_id)) {
     1100        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id");
     1101        psFree(md);
     1102        return false;
     1103    }
     1104    if (!psMetadataAdd(md, PS_LIST_TAIL, "camera", PS_DATA_STRING, NULL, camera)) {
     1105        psError(PS_ERR_UNKNOWN, false, "failed to add item camera");
     1106        psFree(md);
     1107        return false;
     1108    }
     1109    if (!psMetadataAdd(md, PS_LIST_TAIL, "telescope", PS_DATA_STRING, NULL, telescope)) {
     1110        psError(PS_ERR_UNKNOWN, false, "failed to add item telescope");
     1111        psFree(md);
     1112        return false;
     1113    }
     1114
     1115    bool status = psDBInsertOneRow(dbh, PZPENDINGEXP_TABLE_NAME, md);
     1116    psFree(md);
     1117
     1118    return status;
     1119}
     1120
     1121long long pzPendingExpDelete(psDB *dbh, const psMetadata *where, unsigned long long limit)
     1122{
     1123    long long       deleted = 0;
     1124
     1125    long long count = psDBDeleteRows(dbh, PZPENDINGEXP_TABLE_NAME, where, limit);
     1126    if (count < 0) {
     1127        psError(PS_ERR_UNKNOWN, true, "failed to delete row from pzPendingExp");
     1128        return count;
     1129
     1130        deleted += count;
     1131    }
     1132
     1133    return deleted;
     1134}
     1135bool pzPendingExpInsertObject(psDB *dbh, pzPendingExpRow *object)
     1136{
     1137    return pzPendingExpInsert(dbh, object->exp_id, object->camera, object->telescope);
     1138}
     1139
     1140bool pzPendingExpInsertObjects(psDB *dbh, psArray *objects)
     1141{
     1142    for (long i = 0; i < psArrayLength(objects); i++) {
     1143        if (!pzPendingExpInsertObject(dbh, objects->data[i])) {
     1144            return false;
     1145        }
     1146    }
     1147
     1148    return true;
     1149}
     1150
     1151bool pzPendingExpInsertFits(psDB *dbh, const psFits *fits)
     1152{
     1153    psArray         *rowSet;
     1154
     1155    // move to (the first?) extension named  PZPENDINGEXP_TABLE_NAME
     1156    if (!psFitsMoveExtName(fits, PZPENDINGEXP_TABLE_NAME)) {
     1157        psError(PS_ERR_UNKNOWN, true, "failed to find FITS extension %s", PZPENDINGEXP_TABLE_NAME);
     1158        return false;
     1159    }
     1160
     1161    // check HDU type
     1162    if (psFitsGetExtType(fits) != PS_FITS_TYPE_BINARY_TABLE)  {
     1163        psError(PS_ERR_UNKNOWN, true, "FITS HDU type is not PS_FITS_TYPE_BINARY_TABLE");
     1164        return false;
     1165    }
     1166
     1167    // read fits table
     1168    rowSet = psFitsReadTable(fits);
     1169    if (!rowSet) {
     1170        psError(PS_ERR_UNKNOWN, true, "FITS read error or FITS table is empty");
     1171        psFree(rowSet);
     1172        return false;
     1173    }
     1174
    7621175    if (!psDBInsertRows(dbh, PZPENDINGEXP_TABLE_NAME, rowSet)) {
    7631176        psError(PS_ERR_UNKNOWN, false, "databse insert failed");
     
    8101223        return false;
    8111224    }
    812     if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_type", PS_DATA_STRING, NULL, object->exp_type)) {
    813         psError(PS_ERR_UNKNOWN, false, "failed to add item exp_type");
    814         psFree(md);
    815         return false;
    816     }
    817     if (!psMetadataAdd(md, PS_LIST_TAIL, "imfiles", PS_DATA_S32, NULL, object->imfiles)) {
    818         psError(PS_ERR_UNKNOWN, false, "failed to add item imfiles");
    819         psFree(md);
    820         return false;
    821     }
    8221225
    8231226
     
    8441247        return false;
    8451248    }
    846     char* exp_type = psMetadataLookupPtr(&status, md, "exp_type");
    847     if (!status) {
    848         psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item exp_type");
    849         return false;
    850     }
    851     psS32 imfiles = psMetadataLookupS32(&status, md, "imfiles");
    852     if (!status) {
    853         psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item imfiles");
    854         return false;
    855     }
    856 
    857     return pzPendingExpRowAlloc(exp_id, camera, telescope, exp_type, imfiles);
     1249
     1250    return pzPendingExpRowAlloc(exp_id, camera, telescope);
    8581251}
    8591252psArray *pzPendingExpSelectRowObjects(psDB *dbh, const psMetadata *where, unsigned long long limit)
     
    9521345static void pzPendingImfileRowFree(pzPendingImfileRow *object);
    9531346
    954 pzPendingImfileRow *pzPendingImfileRowAlloc(const char *exp_id, const char *camera, const char *telescope, psS32 bytes, const char *md5sum, const char *class, const char *class_id, const char *uri)
     1347pzPendingImfileRow *pzPendingImfileRowAlloc(const char *exp_id, const char *camera, const char *telescope, const char *class, const char *class_id, const char *exp_tag)
    9551348{
    9561349    pzPendingImfileRow *_object;
     
    9621355    _object->camera = psStringCopy(camera);
    9631356    _object->telescope = psStringCopy(telescope);
    964     _object->bytes = bytes;
    965     _object->md5sum = psStringCopy(md5sum);
    9661357    _object->class = psStringCopy(class);
    9671358    _object->class_id = psStringCopy(class_id);
    968     _object->uri = psStringCopy(uri);
     1359    _object->exp_tag = psStringCopy(exp_tag);
    9691360
    9701361    return _object;
     
    9761367    psFree(object->camera);
    9771368    psFree(object->telescope);
    978     psFree(object->md5sum);
    9791369    psFree(object->class);
    9801370    psFree(object->class_id);
    981     psFree(object->uri);
     1371    psFree(object->exp_tag);
    9821372}
    9831373
     
    10001390        return false;
    10011391    }
    1002     if (!psMetadataAdd(md, PS_LIST_TAIL, "bytes", PS_DATA_S32, NULL, 0)) {
    1003         psError(PS_ERR_UNKNOWN, false, "failed to add item bytes");
    1004         psFree(md);
    1005         return false;
    1006     }
    1007     if (!psMetadataAdd(md, PS_LIST_TAIL, "md5sum", PS_DATA_STRING, NULL, "32")) {
    1008         psError(PS_ERR_UNKNOWN, false, "failed to add item md5sum");
    1009         psFree(md);
    1010         return false;
    1011     }
    1012     if (!psMetadataAdd(md, PS_LIST_TAIL, "class", PS_DATA_STRING, NULL, "64")) {
     1392    if (!psMetadataAdd(md, PS_LIST_TAIL, "class", PS_DATA_STRING, "Primary Key", "64")) {
    10131393        psError(PS_ERR_UNKNOWN, false, "failed to add item class");
    10141394        psFree(md);
     
    10201400        return false;
    10211401    }
    1022     if (!psMetadataAdd(md, PS_LIST_TAIL, "uri", PS_DATA_STRING, NULL, "255")) {
    1023         psError(PS_ERR_UNKNOWN, false, "failed to add item uri");
     1402    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_STRING, "Unique Key", "64")) {
     1403        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag");
    10241404        psFree(md);
    10251405        return false;
     
    10381418}
    10391419
    1040 bool pzPendingImfileInsert(psDB * dbh, const char *exp_id, const char *camera, const char *telescope, psS32 bytes, const char *md5sum, const char *class, const char *class_id, const char *uri)
     1420bool pzPendingImfileInsert(psDB * dbh, const char *exp_id, const char *camera, const char *telescope, const char *class, const char *class_id, const char *exp_tag)
    10411421{
    10421422    psMetadata *md = psMetadataAlloc();
     
    10561436        return false;
    10571437    }
    1058     if (!psMetadataAdd(md, PS_LIST_TAIL, "bytes", PS_DATA_S32, NULL, bytes)) {
    1059         psError(PS_ERR_UNKNOWN, false, "failed to add item bytes");
    1060         psFree(md);
    1061         return false;
    1062     }
    1063     if (!psMetadataAdd(md, PS_LIST_TAIL, "md5sum", PS_DATA_STRING, NULL, md5sum)) {
    1064         psError(PS_ERR_UNKNOWN, false, "failed to add item md5sum");
    1065         psFree(md);
    1066         return false;
    1067     }
    10681438    if (!psMetadataAdd(md, PS_LIST_TAIL, "class", PS_DATA_STRING, NULL, class)) {
    10691439        psError(PS_ERR_UNKNOWN, false, "failed to add item class");
     
    10761446        return false;
    10771447    }
    1078     if (!psMetadataAdd(md, PS_LIST_TAIL, "uri", PS_DATA_STRING, NULL, uri)) {
    1079         psError(PS_ERR_UNKNOWN, false, "failed to add item uri");
     1448    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_STRING, NULL, exp_tag)) {
     1449        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag");
    10801450        psFree(md);
    10811451        return false;
     
    11041474bool pzPendingImfileInsertObject(psDB *dbh, pzPendingImfileRow *object)
    11051475{
    1106     return pzPendingImfileInsert(dbh, object->exp_id, object->camera, object->telescope, object->bytes, object->md5sum, object->class, object->class_id, object->uri);
     1476    return pzPendingImfileInsert(dbh, object->exp_id, object->camera, object->telescope, object->class, object->class_id, object->exp_tag);
    11071477}
    11081478
     
    11921562        return false;
    11931563    }
    1194     if (!psMetadataAdd(md, PS_LIST_TAIL, "bytes", PS_DATA_S32, NULL, object->bytes)) {
    1195         psError(PS_ERR_UNKNOWN, false, "failed to add item bytes");
    1196         psFree(md);
    1197         return false;
    1198     }
    1199     if (!psMetadataAdd(md, PS_LIST_TAIL, "md5sum", PS_DATA_STRING, NULL, object->md5sum)) {
    1200         psError(PS_ERR_UNKNOWN, false, "failed to add item md5sum");
    1201         psFree(md);
    1202         return false;
    1203     }
    12041564    if (!psMetadataAdd(md, PS_LIST_TAIL, "class", PS_DATA_STRING, NULL, object->class)) {
    12051565        psError(PS_ERR_UNKNOWN, false, "failed to add item class");
     
    12121572        return false;
    12131573    }
    1214     if (!psMetadataAdd(md, PS_LIST_TAIL, "uri", PS_DATA_STRING, NULL, object->uri)) {
    1215         psError(PS_ERR_UNKNOWN, false, "failed to add item uri");
     1574    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_STRING, NULL, object->exp_tag)) {
     1575        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag");
    12161576        psFree(md);
    12171577        return false;
     
    12411601        return false;
    12421602    }
    1243     psS32 bytes = psMetadataLookupS32(&status, md, "bytes");
    1244     if (!status) {
    1245         psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item bytes");
    1246         return false;
    1247     }
    1248     char* md5sum = psMetadataLookupPtr(&status, md, "md5sum");
    1249     if (!status) {
    1250         psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item md5sum");
    1251         return false;
    1252     }
    12531603    char* class = psMetadataLookupPtr(&status, md, "class");
    12541604    if (!status) {
     
    12611611        return false;
    12621612    }
    1263     char* uri = psMetadataLookupPtr(&status, md, "uri");
    1264     if (!status) {
    1265         psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item uri");
    1266         return false;
    1267     }
    1268 
    1269     return pzPendingImfileRowAlloc(exp_id, camera, telescope, bytes, md5sum, class, class_id, uri);
     1613    char* exp_tag = psMetadataLookupPtr(&status, md, "exp_tag");
     1614    if (!status) {
     1615        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item exp_tag");
     1616        return false;
     1617    }
     1618
     1619    return pzPendingImfileRowAlloc(exp_id, camera, telescope, class, class_id, exp_tag);
    12701620}
    12711621psArray *pzPendingImfileSelectRowObjects(psDB *dbh, const psMetadata *where, unsigned long long limit)
     
    13621712    return true;
    13631713}
    1364 static void newExpRowFree(newExpRow *object);
    1365 
    1366 newExpRow *newExpRowAlloc(const char *exp_tag, const char *exp_id, const char *camera, const char *telescope, const char *exp_type, psS32 imfiles)
    1367 {
    1368     newExpRow       *_object;
    1369 
    1370     _object = psAlloc(sizeof(newExpRow));
    1371     psMemSetDeallocator(_object, (psFreeFunc)newExpRowFree);
    1372 
    1373     _object->exp_tag = psStringCopy(exp_tag);
     1714static void pzDoneExpRowFree(pzDoneExpRow *object);
     1715
     1716pzDoneExpRow *pzDoneExpRowAlloc(const char *exp_id, const char *camera, const char *telescope)
     1717{
     1718    pzDoneExpRow    *_object;
     1719
     1720    _object = psAlloc(sizeof(pzDoneExpRow));
     1721    psMemSetDeallocator(_object, (psFreeFunc)pzDoneExpRowFree);
     1722
    13741723    _object->exp_id = psStringCopy(exp_id);
    13751724    _object->camera = psStringCopy(camera);
    13761725    _object->telescope = psStringCopy(telescope);
    1377     _object->exp_type = psStringCopy(exp_type);
    1378     _object->imfiles = imfiles;
    13791726
    13801727    return _object;
    13811728}
    13821729
    1383 static void newExpRowFree(newExpRow *object)
    1384 {
    1385     psFree(object->exp_tag);
     1730static void pzDoneExpRowFree(pzDoneExpRow *object)
     1731{
    13861732    psFree(object->exp_id);
    13871733    psFree(object->camera);
    13881734    psFree(object->telescope);
    1389     psFree(object->exp_type);
    1390 }
    1391 
    1392 bool newExpCreateTable(psDB *dbh)
     1735}
     1736
     1737bool pzDoneExpCreateTable(psDB *dbh)
    13931738{
    13941739    psMetadata *md = psMetadataAlloc();
    1395     if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_STRING, "Primary Key", "64")) {
    1396         psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag");
    1397         psFree(md);
    1398         return false;
    1399     }
    1400     if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_id", PS_DATA_STRING, "Key", "64")) {
     1740    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_id", PS_DATA_STRING, "Primary Key", "64")) {
    14011741        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id");
    14021742        psFree(md);
    14031743        return false;
    14041744    }
    1405     if (!psMetadataAdd(md, PS_LIST_TAIL, "camera", PS_DATA_STRING, NULL, "64")) {
     1745    if (!psMetadataAdd(md, PS_LIST_TAIL, "camera", PS_DATA_STRING, "Primary Key", "64")) {
    14061746        psError(PS_ERR_UNKNOWN, false, "failed to add item camera");
    14071747        psFree(md);
    14081748        return false;
    14091749    }
    1410     if (!psMetadataAdd(md, PS_LIST_TAIL, "telescope", PS_DATA_STRING, NULL, "64")) {
     1750    if (!psMetadataAdd(md, PS_LIST_TAIL, "telescope", PS_DATA_STRING, "Primary Key", "64")) {
    14111751        psError(PS_ERR_UNKNOWN, false, "failed to add item telescope");
    14121752        psFree(md);
    14131753        return false;
    14141754    }
    1415     if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_type", PS_DATA_STRING, NULL, "64")) {
    1416         psError(PS_ERR_UNKNOWN, false, "failed to add item exp_type");
    1417         psFree(md);
    1418         return false;
    1419     }
    1420     if (!psMetadataAdd(md, PS_LIST_TAIL, "imfiles", PS_DATA_S32, NULL, 0)) {
    1421         psError(PS_ERR_UNKNOWN, false, "failed to add item imfiles");
    1422         psFree(md);
    1423         return false;
    1424     }
    1425 
    1426     bool status = psDBCreateTable(dbh, NEWEXP_TABLE_NAME, md);
     1755
     1756    bool status = psDBCreateTable(dbh, PZDONEEXP_TABLE_NAME, md);
    14271757
    14281758    psFree(md);
     
    14311761}
    14321762
    1433 bool newExpDropTable(psDB *dbh)
    1434 {
    1435     return psDBDropTable(dbh, NEWEXP_TABLE_NAME);
    1436 }
    1437 
    1438 bool newExpInsert(psDB * dbh, const char *exp_tag, const char *exp_id, const char *camera, const char *telescope, const char *exp_type, psS32 imfiles)
     1763bool pzDoneExpDropTable(psDB *dbh)
     1764{
     1765    return psDBDropTable(dbh, PZDONEEXP_TABLE_NAME);
     1766}
     1767
     1768bool pzDoneExpInsert(psDB * dbh, const char *exp_id, const char *camera, const char *telescope)
    14391769{
    14401770    psMetadata *md = psMetadataAlloc();
    1441     if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_STRING, NULL, exp_tag)) {
    1442         psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag");
    1443         psFree(md);
    1444         return false;
    1445     }
    14461771    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_id", PS_DATA_STRING, NULL, exp_id)) {
    14471772        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id");
     
    14591784        return false;
    14601785    }
    1461     if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_type", PS_DATA_STRING, NULL, exp_type)) {
    1462         psError(PS_ERR_UNKNOWN, false, "failed to add item exp_type");
    1463         psFree(md);
    1464         return false;
    1465     }
    1466     if (!psMetadataAdd(md, PS_LIST_TAIL, "imfiles", PS_DATA_S32, NULL, imfiles)) {
    1467         psError(PS_ERR_UNKNOWN, false, "failed to add item imfiles");
    1468         psFree(md);
    1469         return false;
    1470     }
    1471 
    1472     bool status = psDBInsertOneRow(dbh, NEWEXP_TABLE_NAME, md);
     1786
     1787    bool status = psDBInsertOneRow(dbh, PZDONEEXP_TABLE_NAME, md);
    14731788    psFree(md);
    14741789
     
    14761791}
    14771792
    1478 long long newExpDelete(psDB *dbh, const psMetadata *where, unsigned long long limit)
     1793long long pzDoneExpDelete(psDB *dbh, const psMetadata *where, unsigned long long limit)
    14791794{
    14801795    long long       deleted = 0;
    14811796
    1482     long long count = psDBDeleteRows(dbh, NEWEXP_TABLE_NAME, where, limit);
     1797    long long count = psDBDeleteRows(dbh, PZDONEEXP_TABLE_NAME, where, limit);
    14831798    if (count < 0) {
    1484         psError(PS_ERR_UNKNOWN, true, "failed to delete row from newExp");
     1799        psError(PS_ERR_UNKNOWN, true, "failed to delete row from pzDoneExp");
    14851800        return count;
    14861801
     
    14901805    return deleted;
    14911806}
    1492 bool newExpInsertObject(psDB *dbh, newExpRow *object)
    1493 {
    1494     return newExpInsert(dbh, object->exp_tag, object->exp_id, object->camera, object->telescope, object->exp_type, object->imfiles);
    1495 }
    1496 
    1497 bool newExpInsertObjects(psDB *dbh, psArray *objects)
     1807bool pzDoneExpInsertObject(psDB *dbh, pzDoneExpRow *object)
     1808{
     1809    return pzDoneExpInsert(dbh, object->exp_id, object->camera, object->telescope);
     1810}
     1811
     1812bool pzDoneExpInsertObjects(psDB *dbh, psArray *objects)
    14981813{
    14991814    for (long i = 0; i < psArrayLength(objects); i++) {
    1500         if (!newExpInsertObject(dbh, objects->data[i])) {
     1815        if (!pzDoneExpInsertObject(dbh, objects->data[i])) {
    15011816            return false;
    15021817        }
     
    15061821}
    15071822
    1508 bool newExpInsertFits(psDB *dbh, const psFits *fits)
     1823bool pzDoneExpInsertFits(psDB *dbh, const psFits *fits)
    15091824{
    15101825    psArray         *rowSet;
    15111826
    1512     // move to (the first?) extension named  NEWEXP_TABLE_NAME
    1513     if (!psFitsMoveExtName(fits, NEWEXP_TABLE_NAME)) {
    1514         psError(PS_ERR_UNKNOWN, true, "failed to find FITS extension %s", NEWEXP_TABLE_NAME);
     1827    // move to (the first?) extension named  PZDONEEXP_TABLE_NAME
     1828    if (!psFitsMoveExtName(fits, PZDONEEXP_TABLE_NAME)) {
     1829        psError(PS_ERR_UNKNOWN, true, "failed to find FITS extension %s", PZDONEEXP_TABLE_NAME);
    15151830        return false;
    15161831    }
     
    15301845    }
    15311846
     1847    if (!psDBInsertRows(dbh, PZDONEEXP_TABLE_NAME, rowSet)) {
     1848        psError(PS_ERR_UNKNOWN, false, "databse insert failed");
     1849        psFree(rowSet);
     1850        return false;
     1851    }
     1852
     1853    psFree(rowSet);
     1854
     1855    return true;
     1856}
     1857
     1858bool pzDoneExpSelectRowsFits(psDB *dbh, psFits *fits, const psMetadata *where, unsigned long long limit)
     1859{
     1860    psArray         *rowSet;
     1861
     1862    rowSet = psDBSelectRows(dbh, PZDONEEXP_TABLE_NAME, where, limit);
     1863    if (!rowSet) {
     1864        return false;
     1865    }
     1866
     1867    // output to fits
     1868    if (!psFitsWriteTable(fits, NULL, rowSet, PZDONEEXP_TABLE_NAME)) {
     1869        psError(PS_ERR_UNKNOWN, false, "FITS table write failed");
     1870        psFree(rowSet);
     1871        return false;
     1872    }
     1873
     1874    psFree(rowSet);
     1875
     1876    return true;
     1877}
     1878
     1879psMetadata *pzDoneExpMetadataFromObject(const pzDoneExpRow *object)
     1880{
     1881    psMetadata *md = psMetadataAlloc();
     1882    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_id", PS_DATA_STRING, NULL, object->exp_id)) {
     1883        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id");
     1884        psFree(md);
     1885        return false;
     1886    }
     1887    if (!psMetadataAdd(md, PS_LIST_TAIL, "camera", PS_DATA_STRING, NULL, object->camera)) {
     1888        psError(PS_ERR_UNKNOWN, false, "failed to add item camera");
     1889        psFree(md);
     1890        return false;
     1891    }
     1892    if (!psMetadataAdd(md, PS_LIST_TAIL, "telescope", PS_DATA_STRING, NULL, object->telescope)) {
     1893        psError(PS_ERR_UNKNOWN, false, "failed to add item telescope");
     1894        psFree(md);
     1895        return false;
     1896    }
     1897
     1898
     1899    return md;
     1900}
     1901
     1902pzDoneExpRow *pzDoneExpObjectFromMetadata(psMetadata *md)
     1903{
     1904
     1905bool status = false;
     1906    char* exp_id = psMetadataLookupPtr(&status, md, "exp_id");
     1907    if (!status) {
     1908        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item exp_id");
     1909        return false;
     1910    }
     1911    char* camera = psMetadataLookupPtr(&status, md, "camera");
     1912    if (!status) {
     1913        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item camera");
     1914        return false;
     1915    }
     1916    char* telescope = psMetadataLookupPtr(&status, md, "telescope");
     1917    if (!status) {
     1918        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item telescope");
     1919        return false;
     1920    }
     1921
     1922    return pzDoneExpRowAlloc(exp_id, camera, telescope);
     1923}
     1924psArray *pzDoneExpSelectRowObjects(psDB *dbh, const psMetadata *where, unsigned long long limit)
     1925{
     1926    psArray         *rowSet;
     1927    psArray         *returnSet;
     1928    psU64           i;
     1929
     1930    rowSet = psDBSelectRows(dbh, PZDONEEXP_TABLE_NAME, where, limit);
     1931    if (!rowSet) {
     1932        return NULL;
     1933    }
     1934
     1935    // convert psMetadata rows to row objects
     1936
     1937    returnSet = psArrayAllocEmpty(rowSet->n);
     1938
     1939    for (i = 0; i < rowSet->n; i++) {
     1940        pzDoneExpRow *object = pzDoneExpObjectFromMetadata(rowSet->data[i]);
     1941        psArrayAdd(returnSet, 0, object);
     1942        psFree(object);
     1943    }
     1944
     1945    psFree(rowSet);
     1946
     1947    return returnSet;
     1948}
     1949bool pzDoneExpDeleteObject(psDB *dbh, const pzDoneExpRow *object)
     1950{
     1951    psMetadata *where = pzDoneExpMetadataFromObject(object);
     1952    long long count = psDBDeleteRows(dbh, PZDONEEXP_TABLE_NAME, where, 0);
     1953    psFree(where)
     1954    if (count < 0) {
     1955        psError(PS_ERR_UNKNOWN, true, "failed to delete row from pzDoneExp");
     1956        return false;
     1957    }
     1958    if (count > 1) {
     1959        // XXX should this be a psAbort() instead?  It is possible that
     1960        // having an object match multiple rows was by design.
     1961        psError(PS_ERR_UNKNOWN, true, "pzDoneExpRow object matched more then one row.  Check your database schema");
     1962        return false;
     1963    }
     1964
     1965    return true;
     1966}
     1967long long pzDoneExpDeleteRowObjects(psDB *dbh, const psArray *objects, unsigned long long limit)
     1968{
     1969    long long       deleted = 0;
     1970
     1971    for (long long i = 0; i < objects->n; i++) {
     1972        pzDoneExpRow *object = objects->data[i];
     1973        psMetadata *where = pzDoneExpMetadataFromObject(object);
     1974        long long count = psDBDeleteRows(dbh, PZDONEEXP_TABLE_NAME, where, limit);
     1975        psFree(where)
     1976        if (count < 0) {
     1977            psError(PS_ERR_UNKNOWN, true, "failed to delete row from pzDoneExp");
     1978            return count;
     1979        }
     1980
     1981        deleted += count;
     1982    }
     1983
     1984    return deleted;
     1985}
     1986bool pzDoneExpPrintObjects(FILE *stream, psArray *objects, bool mdcf)
     1987{
     1988    PS_ASSERT_PTR_NON_NULL(objects, false);
     1989
     1990    psMetadata *output = psMetadataAlloc();
     1991    for (long i = 0; i < psArrayLength(objects); i++) {
     1992        psMetadata *md = pzDoneExpMetadataFromObject(objects->data[i]);
     1993        if (!psMetadataAddMetadata(
     1994            output,
     1995            PS_LIST_TAIL,
     1996            PZDONEEXP_TABLE_NAME,
     1997            PS_META_DUPLICATE_OK,
     1998            NULL,
     1999            md
     2000        )) {
     2001            psError(PS_ERR_UNKNOWN, false, "failed to add metadata");
     2002            psFree(md);
     2003            psFree(output);
     2004            return false;
     2005        }
     2006        psFree(md);
     2007    }
     2008
     2009    if (!ippdbPrintMetadataRaw(stream, output, mdcf)) {
     2010        psError(PS_ERR_UNKNOWN, false, "failed to print metadata");
     2011        psFree(output);
     2012    }
     2013    psFree(output);
     2014
     2015    return true;
     2016}
     2017static void pzDoneImfileRowFree(pzDoneImfileRow *object);
     2018
     2019pzDoneImfileRow *pzDoneImfileRowAlloc(const char *exp_id, const char *camera, const char *telescope, const char *class, const char *class_id, const char *exp_tag, const char *uri)
     2020{
     2021    pzDoneImfileRow *_object;
     2022
     2023    _object = psAlloc(sizeof(pzDoneImfileRow));
     2024    psMemSetDeallocator(_object, (psFreeFunc)pzDoneImfileRowFree);
     2025
     2026    _object->exp_id = psStringCopy(exp_id);
     2027    _object->camera = psStringCopy(camera);
     2028    _object->telescope = psStringCopy(telescope);
     2029    _object->class = psStringCopy(class);
     2030    _object->class_id = psStringCopy(class_id);
     2031    _object->exp_tag = psStringCopy(exp_tag);
     2032    _object->uri = psStringCopy(uri);
     2033
     2034    return _object;
     2035}
     2036
     2037static void pzDoneImfileRowFree(pzDoneImfileRow *object)
     2038{
     2039    psFree(object->exp_id);
     2040    psFree(object->camera);
     2041    psFree(object->telescope);
     2042    psFree(object->class);
     2043    psFree(object->class_id);
     2044    psFree(object->exp_tag);
     2045    psFree(object->uri);
     2046}
     2047
     2048bool pzDoneImfileCreateTable(psDB *dbh)
     2049{
     2050    psMetadata *md = psMetadataAlloc();
     2051    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_id", PS_DATA_STRING, "Primary Key", "64")) {
     2052        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id");
     2053        psFree(md);
     2054        return false;
     2055    }
     2056    if (!psMetadataAdd(md, PS_LIST_TAIL, "camera", PS_DATA_STRING, "Primary Key", "64")) {
     2057        psError(PS_ERR_UNKNOWN, false, "failed to add item camera");
     2058        psFree(md);
     2059        return false;
     2060    }
     2061    if (!psMetadataAdd(md, PS_LIST_TAIL, "telescope", PS_DATA_STRING, "Primary Key", "64")) {
     2062        psError(PS_ERR_UNKNOWN, false, "failed to add item telescope");
     2063        psFree(md);
     2064        return false;
     2065    }
     2066    if (!psMetadataAdd(md, PS_LIST_TAIL, "class", PS_DATA_STRING, "Primary Key", "64")) {
     2067        psError(PS_ERR_UNKNOWN, false, "failed to add item class");
     2068        psFree(md);
     2069        return false;
     2070    }
     2071    if (!psMetadataAdd(md, PS_LIST_TAIL, "class_id", PS_DATA_STRING, "Primary Key", "64")) {
     2072        psError(PS_ERR_UNKNOWN, false, "failed to add item class_id");
     2073        psFree(md);
     2074        return false;
     2075    }
     2076    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_STRING, "Unique Key", "64")) {
     2077        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag");
     2078        psFree(md);
     2079        return false;
     2080    }
     2081    if (!psMetadataAdd(md, PS_LIST_TAIL, "uri", PS_DATA_STRING, NULL, "255")) {
     2082        psError(PS_ERR_UNKNOWN, false, "failed to add item uri");
     2083        psFree(md);
     2084        return false;
     2085    }
     2086
     2087    bool status = psDBCreateTable(dbh, PZDONEIMFILE_TABLE_NAME, md);
     2088
     2089    psFree(md);
     2090
     2091    return status;
     2092}
     2093
     2094bool pzDoneImfileDropTable(psDB *dbh)
     2095{
     2096    return psDBDropTable(dbh, PZDONEIMFILE_TABLE_NAME);
     2097}
     2098
     2099bool pzDoneImfileInsert(psDB * dbh, const char *exp_id, const char *camera, const char *telescope, const char *class, const char *class_id, const char *exp_tag, const char *uri)
     2100{
     2101    psMetadata *md = psMetadataAlloc();
     2102    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_id", PS_DATA_STRING, NULL, exp_id)) {
     2103        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id");
     2104        psFree(md);
     2105        return false;
     2106    }
     2107    if (!psMetadataAdd(md, PS_LIST_TAIL, "camera", PS_DATA_STRING, NULL, camera)) {
     2108        psError(PS_ERR_UNKNOWN, false, "failed to add item camera");
     2109        psFree(md);
     2110        return false;
     2111    }
     2112    if (!psMetadataAdd(md, PS_LIST_TAIL, "telescope", PS_DATA_STRING, NULL, telescope)) {
     2113        psError(PS_ERR_UNKNOWN, false, "failed to add item telescope");
     2114        psFree(md);
     2115        return false;
     2116    }
     2117    if (!psMetadataAdd(md, PS_LIST_TAIL, "class", PS_DATA_STRING, NULL, class)) {
     2118        psError(PS_ERR_UNKNOWN, false, "failed to add item class");
     2119        psFree(md);
     2120        return false;
     2121    }
     2122    if (!psMetadataAdd(md, PS_LIST_TAIL, "class_id", PS_DATA_STRING, NULL, class_id)) {
     2123        psError(PS_ERR_UNKNOWN, false, "failed to add item class_id");
     2124        psFree(md);
     2125        return false;
     2126    }
     2127    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_STRING, NULL, exp_tag)) {
     2128        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag");
     2129        psFree(md);
     2130        return false;
     2131    }
     2132    if (!psMetadataAdd(md, PS_LIST_TAIL, "uri", PS_DATA_STRING, NULL, uri)) {
     2133        psError(PS_ERR_UNKNOWN, false, "failed to add item uri");
     2134        psFree(md);
     2135        return false;
     2136    }
     2137
     2138    bool status = psDBInsertOneRow(dbh, PZDONEIMFILE_TABLE_NAME, md);
     2139    psFree(md);
     2140
     2141    return status;
     2142}
     2143
     2144long long pzDoneImfileDelete(psDB *dbh, const psMetadata *where, unsigned long long limit)
     2145{
     2146    long long       deleted = 0;
     2147
     2148    long long count = psDBDeleteRows(dbh, PZDONEIMFILE_TABLE_NAME, where, limit);
     2149    if (count < 0) {
     2150        psError(PS_ERR_UNKNOWN, true, "failed to delete row from pzDoneImfile");
     2151        return count;
     2152
     2153        deleted += count;
     2154    }
     2155
     2156    return deleted;
     2157}
     2158bool pzDoneImfileInsertObject(psDB *dbh, pzDoneImfileRow *object)
     2159{
     2160    return pzDoneImfileInsert(dbh, object->exp_id, object->camera, object->telescope, object->class, object->class_id, object->exp_tag, object->uri);
     2161}
     2162
     2163bool pzDoneImfileInsertObjects(psDB *dbh, psArray *objects)
     2164{
     2165    for (long i = 0; i < psArrayLength(objects); i++) {
     2166        if (!pzDoneImfileInsertObject(dbh, objects->data[i])) {
     2167            return false;
     2168        }
     2169    }
     2170
     2171    return true;
     2172}
     2173
     2174bool pzDoneImfileInsertFits(psDB *dbh, const psFits *fits)
     2175{
     2176    psArray         *rowSet;
     2177
     2178    // move to (the first?) extension named  PZDONEIMFILE_TABLE_NAME
     2179    if (!psFitsMoveExtName(fits, PZDONEIMFILE_TABLE_NAME)) {
     2180        psError(PS_ERR_UNKNOWN, true, "failed to find FITS extension %s", PZDONEIMFILE_TABLE_NAME);
     2181        return false;
     2182    }
     2183
     2184    // check HDU type
     2185    if (psFitsGetExtType(fits) != PS_FITS_TYPE_BINARY_TABLE)  {
     2186        psError(PS_ERR_UNKNOWN, true, "FITS HDU type is not PS_FITS_TYPE_BINARY_TABLE");
     2187        return false;
     2188    }
     2189
     2190    // read fits table
     2191    rowSet = psFitsReadTable(fits);
     2192    if (!rowSet) {
     2193        psError(PS_ERR_UNKNOWN, true, "FITS read error or FITS table is empty");
     2194        psFree(rowSet);
     2195        return false;
     2196    }
     2197
     2198    if (!psDBInsertRows(dbh, PZDONEIMFILE_TABLE_NAME, rowSet)) {
     2199        psError(PS_ERR_UNKNOWN, false, "databse insert failed");
     2200        psFree(rowSet);
     2201        return false;
     2202    }
     2203
     2204    psFree(rowSet);
     2205
     2206    return true;
     2207}
     2208
     2209bool pzDoneImfileSelectRowsFits(psDB *dbh, psFits *fits, const psMetadata *where, unsigned long long limit)
     2210{
     2211    psArray         *rowSet;
     2212
     2213    rowSet = psDBSelectRows(dbh, PZDONEIMFILE_TABLE_NAME, where, limit);
     2214    if (!rowSet) {
     2215        return false;
     2216    }
     2217
     2218    // output to fits
     2219    if (!psFitsWriteTable(fits, NULL, rowSet, PZDONEIMFILE_TABLE_NAME)) {
     2220        psError(PS_ERR_UNKNOWN, false, "FITS table write failed");
     2221        psFree(rowSet);
     2222        return false;
     2223    }
     2224
     2225    psFree(rowSet);
     2226
     2227    return true;
     2228}
     2229
     2230psMetadata *pzDoneImfileMetadataFromObject(const pzDoneImfileRow *object)
     2231{
     2232    psMetadata *md = psMetadataAlloc();
     2233    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_id", PS_DATA_STRING, NULL, object->exp_id)) {
     2234        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id");
     2235        psFree(md);
     2236        return false;
     2237    }
     2238    if (!psMetadataAdd(md, PS_LIST_TAIL, "camera", PS_DATA_STRING, NULL, object->camera)) {
     2239        psError(PS_ERR_UNKNOWN, false, "failed to add item camera");
     2240        psFree(md);
     2241        return false;
     2242    }
     2243    if (!psMetadataAdd(md, PS_LIST_TAIL, "telescope", PS_DATA_STRING, NULL, object->telescope)) {
     2244        psError(PS_ERR_UNKNOWN, false, "failed to add item telescope");
     2245        psFree(md);
     2246        return false;
     2247    }
     2248    if (!psMetadataAdd(md, PS_LIST_TAIL, "class", PS_DATA_STRING, NULL, object->class)) {
     2249        psError(PS_ERR_UNKNOWN, false, "failed to add item class");
     2250        psFree(md);
     2251        return false;
     2252    }
     2253    if (!psMetadataAdd(md, PS_LIST_TAIL, "class_id", PS_DATA_STRING, NULL, object->class_id)) {
     2254        psError(PS_ERR_UNKNOWN, false, "failed to add item class_id");
     2255        psFree(md);
     2256        return false;
     2257    }
     2258    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_STRING, NULL, object->exp_tag)) {
     2259        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag");
     2260        psFree(md);
     2261        return false;
     2262    }
     2263    if (!psMetadataAdd(md, PS_LIST_TAIL, "uri", PS_DATA_STRING, NULL, object->uri)) {
     2264        psError(PS_ERR_UNKNOWN, false, "failed to add item uri");
     2265        psFree(md);
     2266        return false;
     2267    }
     2268
     2269
     2270    return md;
     2271}
     2272
     2273pzDoneImfileRow *pzDoneImfileObjectFromMetadata(psMetadata *md)
     2274{
     2275
     2276bool status = false;
     2277    char* exp_id = psMetadataLookupPtr(&status, md, "exp_id");
     2278    if (!status) {
     2279        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item exp_id");
     2280        return false;
     2281    }
     2282    char* camera = psMetadataLookupPtr(&status, md, "camera");
     2283    if (!status) {
     2284        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item camera");
     2285        return false;
     2286    }
     2287    char* telescope = psMetadataLookupPtr(&status, md, "telescope");
     2288    if (!status) {
     2289        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item telescope");
     2290        return false;
     2291    }
     2292    char* class = psMetadataLookupPtr(&status, md, "class");
     2293    if (!status) {
     2294        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item class");
     2295        return false;
     2296    }
     2297    char* class_id = psMetadataLookupPtr(&status, md, "class_id");
     2298    if (!status) {
     2299        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item class_id");
     2300        return false;
     2301    }
     2302    char* exp_tag = psMetadataLookupPtr(&status, md, "exp_tag");
     2303    if (!status) {
     2304        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item exp_tag");
     2305        return false;
     2306    }
     2307    char* uri = psMetadataLookupPtr(&status, md, "uri");
     2308    if (!status) {
     2309        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item uri");
     2310        return false;
     2311    }
     2312
     2313    return pzDoneImfileRowAlloc(exp_id, camera, telescope, class, class_id, exp_tag, uri);
     2314}
     2315psArray *pzDoneImfileSelectRowObjects(psDB *dbh, const psMetadata *where, unsigned long long limit)
     2316{
     2317    psArray         *rowSet;
     2318    psArray         *returnSet;
     2319    psU64           i;
     2320
     2321    rowSet = psDBSelectRows(dbh, PZDONEIMFILE_TABLE_NAME, where, limit);
     2322    if (!rowSet) {
     2323        return NULL;
     2324    }
     2325
     2326    // convert psMetadata rows to row objects
     2327
     2328    returnSet = psArrayAllocEmpty(rowSet->n);
     2329
     2330    for (i = 0; i < rowSet->n; i++) {
     2331        pzDoneImfileRow *object = pzDoneImfileObjectFromMetadata(rowSet->data[i]);
     2332        psArrayAdd(returnSet, 0, object);
     2333        psFree(object);
     2334    }
     2335
     2336    psFree(rowSet);
     2337
     2338    return returnSet;
     2339}
     2340bool pzDoneImfileDeleteObject(psDB *dbh, const pzDoneImfileRow *object)
     2341{
     2342    psMetadata *where = pzDoneImfileMetadataFromObject(object);
     2343    long long count = psDBDeleteRows(dbh, PZDONEIMFILE_TABLE_NAME, where, 0);
     2344    psFree(where)
     2345    if (count < 0) {
     2346        psError(PS_ERR_UNKNOWN, true, "failed to delete row from pzDoneImfile");
     2347        return false;
     2348    }
     2349    if (count > 1) {
     2350        // XXX should this be a psAbort() instead?  It is possible that
     2351        // having an object match multiple rows was by design.
     2352        psError(PS_ERR_UNKNOWN, true, "pzDoneImfileRow object matched more then one row.  Check your database schema");
     2353        return false;
     2354    }
     2355
     2356    return true;
     2357}
     2358long long pzDoneImfileDeleteRowObjects(psDB *dbh, const psArray *objects, unsigned long long limit)
     2359{
     2360    long long       deleted = 0;
     2361
     2362    for (long long i = 0; i < objects->n; i++) {
     2363        pzDoneImfileRow *object = objects->data[i];
     2364        psMetadata *where = pzDoneImfileMetadataFromObject(object);
     2365        long long count = psDBDeleteRows(dbh, PZDONEIMFILE_TABLE_NAME, where, limit);
     2366        psFree(where)
     2367        if (count < 0) {
     2368            psError(PS_ERR_UNKNOWN, true, "failed to delete row from pzDoneImfile");
     2369            return count;
     2370        }
     2371
     2372        deleted += count;
     2373    }
     2374
     2375    return deleted;
     2376}
     2377bool pzDoneImfilePrintObjects(FILE *stream, psArray *objects, bool mdcf)
     2378{
     2379    PS_ASSERT_PTR_NON_NULL(objects, false);
     2380
     2381    psMetadata *output = psMetadataAlloc();
     2382    for (long i = 0; i < psArrayLength(objects); i++) {
     2383        psMetadata *md = pzDoneImfileMetadataFromObject(objects->data[i]);
     2384        if (!psMetadataAddMetadata(
     2385            output,
     2386            PS_LIST_TAIL,
     2387            PZDONEIMFILE_TABLE_NAME,
     2388            PS_META_DUPLICATE_OK,
     2389            NULL,
     2390            md
     2391        )) {
     2392            psError(PS_ERR_UNKNOWN, false, "failed to add metadata");
     2393            psFree(md);
     2394            psFree(output);
     2395            return false;
     2396        }
     2397        psFree(md);
     2398    }
     2399
     2400    if (!ippdbPrintMetadataRaw(stream, output, mdcf)) {
     2401        psError(PS_ERR_UNKNOWN, false, "failed to print metadata");
     2402        psFree(output);
     2403    }
     2404    psFree(output);
     2405
     2406    return true;
     2407}
     2408static void newExpRowFree(newExpRow *object);
     2409
     2410newExpRow *newExpRowAlloc(psU64 exp_tag, const char *exp_id, const char *camera, const char *telescope, psTime* dateobs, const char *exp_type, psS32 imfiles)
     2411{
     2412    newExpRow       *_object;
     2413
     2414    _object = psAlloc(sizeof(newExpRow));
     2415    psMemSetDeallocator(_object, (psFreeFunc)newExpRowFree);
     2416
     2417    _object->exp_tag = exp_tag;
     2418    _object->exp_id = psStringCopy(exp_id);
     2419    _object->camera = psStringCopy(camera);
     2420    _object->telescope = psStringCopy(telescope);
     2421    _object->dateobs = psTimeCopy(dateobs);
     2422    _object->exp_type = psStringCopy(exp_type);
     2423    _object->imfiles = imfiles;
     2424
     2425    return _object;
     2426}
     2427
     2428static void newExpRowFree(newExpRow *object)
     2429{
     2430    psFree(object->exp_id);
     2431    psFree(object->camera);
     2432    psFree(object->telescope);
     2433    psFree(object->dateobs);
     2434    psFree(object->exp_type);
     2435}
     2436
     2437bool newExpCreateTable(psDB *dbh)
     2438{
     2439    psMetadata *md = psMetadataAlloc();
     2440    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_U64, "Primary Key AUTO_INCREMENT", 0)) {
     2441        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag");
     2442        psFree(md);
     2443        return false;
     2444    }
     2445    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_id", PS_DATA_STRING, "Key", "64")) {
     2446        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id");
     2447        psFree(md);
     2448        return false;
     2449    }
     2450    if (!psMetadataAdd(md, PS_LIST_TAIL, "camera", PS_DATA_STRING, "Key", "64")) {
     2451        psError(PS_ERR_UNKNOWN, false, "failed to add item camera");
     2452        psFree(md);
     2453        return false;
     2454    }
     2455    if (!psMetadataAdd(md, PS_LIST_TAIL, "telescope", PS_DATA_STRING, "Key", "64")) {
     2456        psError(PS_ERR_UNKNOWN, false, "failed to add item telescope");
     2457        psFree(md);
     2458        return false;
     2459    }
     2460    psTime* dateobs = psTimeFromISO("", PS_TIME_UTC);
     2461    if (!psMetadataAdd(md, PS_LIST_TAIL, "dateobs", PS_DATA_TIME, NULL, dateobs)) {
     2462        psFree(dateobs);
     2463        psError(PS_ERR_UNKNOWN, false, "failed to add item dateobs");
     2464        psFree(md);
     2465        return false;
     2466    }
     2467    psFree(dateobs);
     2468    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_type", PS_DATA_STRING, NULL, "64")) {
     2469        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_type");
     2470        psFree(md);
     2471        return false;
     2472    }
     2473    if (!psMetadataAdd(md, PS_LIST_TAIL, "imfiles", PS_DATA_S32, NULL, 0)) {
     2474        psError(PS_ERR_UNKNOWN, false, "failed to add item imfiles");
     2475        psFree(md);
     2476        return false;
     2477    }
     2478
     2479    bool status = psDBCreateTable(dbh, NEWEXP_TABLE_NAME, md);
     2480
     2481    psFree(md);
     2482
     2483    return status;
     2484}
     2485
     2486bool newExpDropTable(psDB *dbh)
     2487{
     2488    return psDBDropTable(dbh, NEWEXP_TABLE_NAME);
     2489}
     2490
     2491bool newExpInsert(psDB * dbh, psU64 exp_tag, const char *exp_id, const char *camera, const char *telescope, psTime* dateobs, const char *exp_type, psS32 imfiles)
     2492{
     2493    psMetadata *md = psMetadataAlloc();
     2494    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_U64, NULL, exp_tag)) {
     2495        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag");
     2496        psFree(md);
     2497        return false;
     2498    }
     2499    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_id", PS_DATA_STRING, NULL, exp_id)) {
     2500        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id");
     2501        psFree(md);
     2502        return false;
     2503    }
     2504    if (!psMetadataAdd(md, PS_LIST_TAIL, "camera", PS_DATA_STRING, NULL, camera)) {
     2505        psError(PS_ERR_UNKNOWN, false, "failed to add item camera");
     2506        psFree(md);
     2507        return false;
     2508    }
     2509    if (!psMetadataAdd(md, PS_LIST_TAIL, "telescope", PS_DATA_STRING, NULL, telescope)) {
     2510        psError(PS_ERR_UNKNOWN, false, "failed to add item telescope");
     2511        psFree(md);
     2512        return false;
     2513    }
     2514    if (!psMetadataAdd(md, PS_LIST_TAIL, "dateobs", PS_DATA_TIME, NULL, dateobs)) {
     2515        psError(PS_ERR_UNKNOWN, false, "failed to add item dateobs");
     2516        psFree(md);
     2517        return false;
     2518    }
     2519    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_type", PS_DATA_STRING, NULL, exp_type)) {
     2520        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_type");
     2521        psFree(md);
     2522        return false;
     2523    }
     2524    if (!psMetadataAdd(md, PS_LIST_TAIL, "imfiles", PS_DATA_S32, NULL, imfiles)) {
     2525        psError(PS_ERR_UNKNOWN, false, "failed to add item imfiles");
     2526        psFree(md);
     2527        return false;
     2528    }
     2529
     2530    bool status = psDBInsertOneRow(dbh, NEWEXP_TABLE_NAME, md);
     2531    psFree(md);
     2532
     2533    return status;
     2534}
     2535
     2536long long newExpDelete(psDB *dbh, const psMetadata *where, unsigned long long limit)
     2537{
     2538    long long       deleted = 0;
     2539
     2540    long long count = psDBDeleteRows(dbh, NEWEXP_TABLE_NAME, where, limit);
     2541    if (count < 0) {
     2542        psError(PS_ERR_UNKNOWN, true, "failed to delete row from newExp");
     2543        return count;
     2544
     2545        deleted += count;
     2546    }
     2547
     2548    return deleted;
     2549}
     2550bool newExpInsertObject(psDB *dbh, newExpRow *object)
     2551{
     2552    return newExpInsert(dbh, object->exp_tag, object->exp_id, object->camera, object->telescope, object->dateobs, object->exp_type, object->imfiles);
     2553}
     2554
     2555bool newExpInsertObjects(psDB *dbh, psArray *objects)
     2556{
     2557    for (long i = 0; i < psArrayLength(objects); i++) {
     2558        if (!newExpInsertObject(dbh, objects->data[i])) {
     2559            return false;
     2560        }
     2561    }
     2562
     2563    return true;
     2564}
     2565
     2566bool newExpInsertFits(psDB *dbh, const psFits *fits)
     2567{
     2568    psArray         *rowSet;
     2569
     2570    // move to (the first?) extension named  NEWEXP_TABLE_NAME
     2571    if (!psFitsMoveExtName(fits, NEWEXP_TABLE_NAME)) {
     2572        psError(PS_ERR_UNKNOWN, true, "failed to find FITS extension %s", NEWEXP_TABLE_NAME);
     2573        return false;
     2574    }
     2575
     2576    // check HDU type
     2577    if (psFitsGetExtType(fits) != PS_FITS_TYPE_BINARY_TABLE)  {
     2578        psError(PS_ERR_UNKNOWN, true, "FITS HDU type is not PS_FITS_TYPE_BINARY_TABLE");
     2579        return false;
     2580    }
     2581
     2582    // read fits table
     2583    rowSet = psFitsReadTable(fits);
     2584    if (!rowSet) {
     2585        psError(PS_ERR_UNKNOWN, true, "FITS read error or FITS table is empty");
     2586        psFree(rowSet);
     2587        return false;
     2588    }
     2589
    15322590    if (!psDBInsertRows(dbh, NEWEXP_TABLE_NAME, rowSet)) {
    15332591        psError(PS_ERR_UNKNOWN, false, "databse insert failed");
     
    15652623{
    15662624    psMetadata *md = psMetadataAlloc();
    1567     if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_STRING, NULL, object->exp_tag)) {
     2625    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_U64, NULL, object->exp_tag)) {
    15682626        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag");
    15692627        psFree(md);
     
    15852643        return false;
    15862644    }
     2645    if (!psMetadataAdd(md, PS_LIST_TAIL, "dateobs", PS_DATA_TIME, NULL, object->dateobs)) {
     2646        psError(PS_ERR_UNKNOWN, false, "failed to add item dateobs");
     2647        psFree(md);
     2648        return false;
     2649    }
    15872650    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_type", PS_DATA_STRING, NULL, object->exp_type)) {
    15882651        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_type");
     
    16042667
    16052668bool status = false;
    1606     char* exp_tag = psMetadataLookupPtr(&status, md, "exp_tag");
     2669    psU64 exp_tag = psMetadataLookupU64(&status, md, "exp_tag");
    16072670    if (!status) {
    16082671        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item exp_tag");
     
    16242687        return false;
    16252688    }
     2689    psTime* dateobs = psMetadataLookupPtr(&status, md, "dateobs");
     2690    if (!status) {
     2691        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item dateobs");
     2692        return false;
     2693    }
    16262694    char* exp_type = psMetadataLookupPtr(&status, md, "exp_type");
    16272695    if (!status) {
     
    16352703    }
    16362704
    1637     return newExpRowAlloc(exp_tag, exp_id, camera, telescope, exp_type, imfiles);
     2705    return newExpRowAlloc(exp_tag, exp_id, camera, telescope, dateobs, exp_type, imfiles);
    16382706}
    16392707psArray *newExpSelectRowObjects(psDB *dbh, const psMetadata *where, unsigned long long limit)
     
    17632831        return false;
    17642832    }
    1765     if (!psMetadataAdd(md, PS_LIST_TAIL, "class", PS_DATA_STRING, NULL, "64")) {
     2833    if (!psMetadataAdd(md, PS_LIST_TAIL, "class", PS_DATA_STRING, "Primary Key", "64")) {
    17662834        psError(PS_ERR_UNKNOWN, false, "failed to add item class");
    17672835        psFree(md);
Note: See TracChangeset for help on using the changeset viewer.