IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 11867 for trunk/ippdb/src


Ignore:
Timestamp:
Feb 16, 2007, 4:42:59 PM (19 years ago)
Author:
jhoblitt
Message:

VERSION 1.1.8

Location:
trunk/ippdb/src
Files:
2 edited

Legend:

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

    r11820 r11867  
    7070#define P5DIFFSCFILE_TABLE_NAME "p5DiffScfile"
    7171#define P6RUN_TABLE_NAME "p6Run"
     72#define P6INPUTSCFILE_TABLE_NAME "p6InputScfile"
     73#define P6SUMSCFILE_TABLE_NAME "p6SumScfile"
    7274#define MAX_STRING_LENGTH 1024
    7375
     
    1651016512    return true;
    1651116513}
     16514static void p6InputScfileRowFree(p6InputScfileRow *object);
     16515
     16516p6InputScfileRow *p6InputScfileRowAlloc(psS32 p6_id, psS32 p4_id, const char *skycell_id, const char *tess_id)
     16517{
     16518    p6InputScfileRow *_object;
     16519
     16520    _object = psAlloc(sizeof(p6InputScfileRow));
     16521    psMemSetDeallocator(_object, (psFreeFunc)p6InputScfileRowFree);
     16522
     16523    _object->p6_id = p6_id;
     16524    _object->p4_id = p4_id;
     16525    _object->skycell_id = psStringCopy(skycell_id);
     16526    _object->tess_id = psStringCopy(tess_id);
     16527
     16528    return _object;
     16529}
     16530
     16531static void p6InputScfileRowFree(p6InputScfileRow *object)
     16532{
     16533    psFree(object->skycell_id);
     16534    psFree(object->tess_id);
     16535}
     16536
     16537bool p6InputScfileCreateTable(psDB *dbh)
     16538{
     16539    psMetadata *md = psMetadataAlloc();
     16540    if (!psMetadataAdd(md, PS_LIST_TAIL, "p6_id", PS_DATA_S32, "Primary Key", 0)) {
     16541        psError(PS_ERR_UNKNOWN, false, "failed to add item p6_id");
     16542        psFree(md);
     16543        return false;
     16544    }
     16545    if (!psMetadataAdd(md, PS_LIST_TAIL, "p4_id", PS_DATA_S32, "Primary Key", 0)) {
     16546        psError(PS_ERR_UNKNOWN, false, "failed to add item p4_id");
     16547        psFree(md);
     16548        return false;
     16549    }
     16550    if (!psMetadataAdd(md, PS_LIST_TAIL, "skycell_id", PS_DATA_STRING, "Primary Key", "64")) {
     16551        psError(PS_ERR_UNKNOWN, false, "failed to add item skycell_id");
     16552        psFree(md);
     16553        return false;
     16554    }
     16555    if (!psMetadataAdd(md, PS_LIST_TAIL, "tess_id", PS_DATA_STRING, "Primary Key", "64")) {
     16556        psError(PS_ERR_UNKNOWN, false, "failed to add item tess_id");
     16557        psFree(md);
     16558        return false;
     16559    }
     16560
     16561    bool status = psDBCreateTable(dbh, P6INPUTSCFILE_TABLE_NAME, md);
     16562
     16563    psFree(md);
     16564
     16565    return status;
     16566}
     16567
     16568bool p6InputScfileDropTable(psDB *dbh)
     16569{
     16570    return psDBDropTable(dbh, P6INPUTSCFILE_TABLE_NAME);
     16571}
     16572
     16573bool p6InputScfileInsert(psDB * dbh, psS32 p6_id, psS32 p4_id, const char *skycell_id, const char *tess_id)
     16574{
     16575    psMetadata *md = psMetadataAlloc();
     16576    if (!psMetadataAdd(md, PS_LIST_TAIL, "p6_id", PS_DATA_S32, NULL, p6_id)) {
     16577        psError(PS_ERR_UNKNOWN, false, "failed to add item p6_id");
     16578        psFree(md);
     16579        return false;
     16580    }
     16581    if (!psMetadataAdd(md, PS_LIST_TAIL, "p4_id", PS_DATA_S32, NULL, p4_id)) {
     16582        psError(PS_ERR_UNKNOWN, false, "failed to add item p4_id");
     16583        psFree(md);
     16584        return false;
     16585    }
     16586    if (!psMetadataAdd(md, PS_LIST_TAIL, "skycell_id", PS_DATA_STRING, NULL, skycell_id)) {
     16587        psError(PS_ERR_UNKNOWN, false, "failed to add item skycell_id");
     16588        psFree(md);
     16589        return false;
     16590    }
     16591    if (!psMetadataAdd(md, PS_LIST_TAIL, "tess_id", PS_DATA_STRING, NULL, tess_id)) {
     16592        psError(PS_ERR_UNKNOWN, false, "failed to add item tess_id");
     16593        psFree(md);
     16594        return false;
     16595    }
     16596
     16597    bool status = psDBInsertOneRow(dbh, P6INPUTSCFILE_TABLE_NAME, md);
     16598    psFree(md);
     16599
     16600    return status;
     16601}
     16602
     16603long long p6InputScfileDelete(psDB *dbh, const psMetadata *where, unsigned long long limit)
     16604{
     16605    long long       deleted = 0;
     16606
     16607    long long count = psDBDeleteRows(dbh, P6INPUTSCFILE_TABLE_NAME, where, limit);
     16608    if (count < 0) {
     16609        psError(PS_ERR_UNKNOWN, true, "failed to delete row from p6InputScfile");
     16610        return count;
     16611
     16612        deleted += count;
     16613    }
     16614
     16615    return deleted;
     16616}
     16617bool p6InputScfileInsertObject(psDB *dbh, p6InputScfileRow *object)
     16618{
     16619    return p6InputScfileInsert(dbh, object->p6_id, object->p4_id, object->skycell_id, object->tess_id);
     16620}
     16621
     16622bool p6InputScfileInsertObjects(psDB *dbh, psArray *objects)
     16623{
     16624    for (long i = 0; i < psArrayLength(objects); i++) {
     16625        if (!p6InputScfileInsertObject(dbh, objects->data[i])) {
     16626            return false;
     16627        }
     16628    }
     16629
     16630    return true;
     16631}
     16632
     16633bool p6InputScfileInsertFits(psDB *dbh, const psFits *fits)
     16634{
     16635    psArray         *rowSet;
     16636
     16637    // move to (the first?) extension named  P6INPUTSCFILE_TABLE_NAME
     16638    if (!psFitsMoveExtName(fits, P6INPUTSCFILE_TABLE_NAME)) {
     16639        psError(PS_ERR_UNKNOWN, true, "failed to find FITS extension %s", P6INPUTSCFILE_TABLE_NAME);
     16640        return false;
     16641    }
     16642
     16643    // check HDU type
     16644    if (psFitsGetExtType(fits) != PS_FITS_TYPE_BINARY_TABLE)  {
     16645        psError(PS_ERR_UNKNOWN, true, "FITS HDU type is not PS_FITS_TYPE_BINARY_TABLE");
     16646        return false;
     16647    }
     16648
     16649    // read fits table
     16650    rowSet = psFitsReadTable(fits);
     16651    if (!rowSet) {
     16652        psError(PS_ERR_UNKNOWN, true, "FITS read error or FITS table is empty");
     16653        psFree(rowSet);
     16654        return false;
     16655    }
     16656
     16657    if (!psDBInsertRows(dbh, P6INPUTSCFILE_TABLE_NAME, rowSet)) {
     16658        psError(PS_ERR_UNKNOWN, false, "databse insert failed");
     16659        psFree(rowSet);
     16660        return false;
     16661    }
     16662
     16663    psFree(rowSet);
     16664
     16665    return true;
     16666}
     16667
     16668bool p6InputScfileSelectRowsFits(psDB *dbh, psFits *fits, const psMetadata *where, unsigned long long limit)
     16669{
     16670    psArray         *rowSet;
     16671
     16672    rowSet = psDBSelectRows(dbh, P6INPUTSCFILE_TABLE_NAME, where, limit);
     16673    if (!rowSet) {
     16674        return false;
     16675    }
     16676
     16677    // output to fits
     16678    if (!psFitsWriteTable(fits, NULL, rowSet, P6INPUTSCFILE_TABLE_NAME)) {
     16679        psError(PS_ERR_UNKNOWN, false, "FITS table write failed");
     16680        psFree(rowSet);
     16681        return false;
     16682    }
     16683
     16684    psFree(rowSet);
     16685
     16686    return true;
     16687}
     16688
     16689psMetadata *p6InputScfileMetadataFromObject(const p6InputScfileRow *object)
     16690{
     16691    psMetadata *md = psMetadataAlloc();
     16692    if (!psMetadataAdd(md, PS_LIST_TAIL, "p6_id", PS_DATA_S32, NULL, object->p6_id)) {
     16693        psError(PS_ERR_UNKNOWN, false, "failed to add item p6_id");
     16694        psFree(md);
     16695        return false;
     16696    }
     16697    if (!psMetadataAdd(md, PS_LIST_TAIL, "p4_id", PS_DATA_S32, NULL, object->p4_id)) {
     16698        psError(PS_ERR_UNKNOWN, false, "failed to add item p4_id");
     16699        psFree(md);
     16700        return false;
     16701    }
     16702    if (!psMetadataAdd(md, PS_LIST_TAIL, "skycell_id", PS_DATA_STRING, NULL, object->skycell_id)) {
     16703        psError(PS_ERR_UNKNOWN, false, "failed to add item skycell_id");
     16704        psFree(md);
     16705        return false;
     16706    }
     16707    if (!psMetadataAdd(md, PS_LIST_TAIL, "tess_id", PS_DATA_STRING, NULL, object->tess_id)) {
     16708        psError(PS_ERR_UNKNOWN, false, "failed to add item tess_id");
     16709        psFree(md);
     16710        return false;
     16711    }
     16712
     16713
     16714    return md;
     16715}
     16716
     16717p6InputScfileRow *p6InputScfileObjectFromMetadata(psMetadata *md)
     16718{
     16719
     16720bool status = false;
     16721    psS32 p6_id = psMetadataLookupS32(&status, md, "p6_id");
     16722    if (!status) {
     16723        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item p6_id");
     16724        return false;
     16725    }
     16726    psS32 p4_id = psMetadataLookupS32(&status, md, "p4_id");
     16727    if (!status) {
     16728        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item p4_id");
     16729        return false;
     16730    }
     16731    char* skycell_id = psMetadataLookupPtr(&status, md, "skycell_id");
     16732    if (!status) {
     16733        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item skycell_id");
     16734        return false;
     16735    }
     16736    char* tess_id = psMetadataLookupPtr(&status, md, "tess_id");
     16737    if (!status) {
     16738        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item tess_id");
     16739        return false;
     16740    }
     16741
     16742    return p6InputScfileRowAlloc(p6_id, p4_id, skycell_id, tess_id);
     16743}
     16744psArray *p6InputScfileSelectRowObjects(psDB *dbh, const psMetadata *where, unsigned long long limit)
     16745{
     16746    psArray         *rowSet;
     16747    psArray         *returnSet;
     16748    psU64           i;
     16749
     16750    rowSet = psDBSelectRows(dbh, P6INPUTSCFILE_TABLE_NAME, where, limit);
     16751    if (!rowSet) {
     16752        return NULL;
     16753    }
     16754
     16755    // convert psMetadata rows to row objects
     16756
     16757    returnSet = psArrayAllocEmpty(rowSet->n);
     16758
     16759    for (i = 0; i < rowSet->n; i++) {
     16760        p6InputScfileRow *object = p6InputScfileObjectFromMetadata(rowSet->data[i]);
     16761        psArrayAdd(returnSet, 0, object);
     16762        psFree(object);
     16763    }
     16764
     16765    psFree(rowSet);
     16766
     16767    return returnSet;
     16768}
     16769bool p6InputScfileDeleteObject(psDB *dbh, const p6InputScfileRow *object)
     16770{
     16771    psMetadata *where = p6InputScfileMetadataFromObject(object);
     16772    long long count = psDBDeleteRows(dbh, P6INPUTSCFILE_TABLE_NAME, where, 0);
     16773    psFree(where);
     16774    if (count < 0) {
     16775        psError(PS_ERR_UNKNOWN, true, "failed to delete row from p6InputScfile");
     16776        return false;
     16777    }
     16778    if (count > 1) {
     16779        // XXX should this be a psAbort() instead?  It is possible that
     16780        // having an object match multiple rows was by design.
     16781        psError(PS_ERR_UNKNOWN, true, "p6InputScfileRow object matched more then one row.  Check your database schema");
     16782        return false;
     16783    }
     16784
     16785    return true;
     16786}
     16787long long p6InputScfileDeleteRowObjects(psDB *dbh, const psArray *objects, unsigned long long limit)
     16788{
     16789    long long       deleted = 0;
     16790
     16791    for (long long i = 0; i < objects->n; i++) {
     16792        p6InputScfileRow *object = objects->data[i];
     16793        psMetadata *where = p6InputScfileMetadataFromObject(object);
     16794        long long count = psDBDeleteRows(dbh, P6INPUTSCFILE_TABLE_NAME, where, limit);
     16795        psFree(where);
     16796        if (count < 0) {
     16797            psError(PS_ERR_UNKNOWN, true, "failed to delete row from p6InputScfile");
     16798            return count;
     16799        }
     16800
     16801        deleted += count;
     16802    }
     16803
     16804    return deleted;
     16805}
     16806bool p6InputScfilePrintObjects(FILE *stream, psArray *objects, bool mdcf)
     16807{
     16808    PS_ASSERT_PTR_NON_NULL(objects, false);
     16809
     16810    psMetadata *output = psMetadataAlloc();
     16811    for (long i = 0; i < psArrayLength(objects); i++) {
     16812        psMetadata *md = p6InputScfileMetadataFromObject(objects->data[i]);
     16813        if (!psMetadataAddMetadata(
     16814            output,
     16815            PS_LIST_TAIL,
     16816            P6INPUTSCFILE_TABLE_NAME,
     16817            PS_META_DUPLICATE_OK,
     16818            NULL,
     16819            md
     16820        )) {
     16821            psError(PS_ERR_UNKNOWN, false, "failed to add metadata");
     16822            psFree(md);
     16823            psFree(output);
     16824            return false;
     16825        }
     16826        psFree(md);
     16827    }
     16828
     16829    if (!ippdbPrintMetadataRaw(stream, output, mdcf)) {
     16830        psError(PS_ERR_UNKNOWN, false, "failed to print metadata");
     16831        psFree(output);
     16832    }
     16833    psFree(output);
     16834
     16835    return true;
     16836}
     16837bool p6InputScfilePrintObject(FILE *stream, p6InputScfileRow *object, bool mdcf)
     16838{
     16839    PS_ASSERT_PTR_NON_NULL(object, false);
     16840
     16841    psMetadata *md = p6InputScfileMetadataFromObject(object);
     16842
     16843    if (!ippdbPrintMetadataRaw(stream, md, mdcf)) {
     16844        psError(PS_ERR_UNKNOWN, false, "failed to print metadata");
     16845        psFree(md);
     16846    }
     16847
     16848    psFree(md);
     16849
     16850    return true;
     16851}
     16852static void p6SumScfileRowFree(p6SumScfileRow *object);
     16853
     16854p6SumScfileRow *p6SumScfileRowAlloc(psS32 p6_id, const char *skycell_id, const char *tess_id, const char *uri, psF64 bg, psF64 bg_mean_stdev)
     16855{
     16856    p6SumScfileRow  *_object;
     16857
     16858    _object = psAlloc(sizeof(p6SumScfileRow));
     16859    psMemSetDeallocator(_object, (psFreeFunc)p6SumScfileRowFree);
     16860
     16861    _object->p6_id = p6_id;
     16862    _object->skycell_id = psStringCopy(skycell_id);
     16863    _object->tess_id = psStringCopy(tess_id);
     16864    _object->uri = psStringCopy(uri);
     16865    _object->bg = bg;
     16866    _object->bg_mean_stdev = bg_mean_stdev;
     16867
     16868    return _object;
     16869}
     16870
     16871static void p6SumScfileRowFree(p6SumScfileRow *object)
     16872{
     16873    psFree(object->skycell_id);
     16874    psFree(object->tess_id);
     16875    psFree(object->uri);
     16876}
     16877
     16878bool p6SumScfileCreateTable(psDB *dbh)
     16879{
     16880    psMetadata *md = psMetadataAlloc();
     16881    if (!psMetadataAdd(md, PS_LIST_TAIL, "p6_id", PS_DATA_S32, "Primary Key", 0)) {
     16882        psError(PS_ERR_UNKNOWN, false, "failed to add item p6_id");
     16883        psFree(md);
     16884        return false;
     16885    }
     16886    if (!psMetadataAdd(md, PS_LIST_TAIL, "skycell_id", PS_DATA_STRING, "Primary Key", "64")) {
     16887        psError(PS_ERR_UNKNOWN, false, "failed to add item skycell_id");
     16888        psFree(md);
     16889        return false;
     16890    }
     16891    if (!psMetadataAdd(md, PS_LIST_TAIL, "tess_id", PS_DATA_STRING, "Primary Key", "64")) {
     16892        psError(PS_ERR_UNKNOWN, false, "failed to add item tess_id");
     16893        psFree(md);
     16894        return false;
     16895    }
     16896    if (!psMetadataAdd(md, PS_LIST_TAIL, "uri", PS_DATA_STRING, NULL, "255")) {
     16897        psError(PS_ERR_UNKNOWN, false, "failed to add item uri");
     16898        psFree(md);
     16899        return false;
     16900    }
     16901    if (!psMetadataAdd(md, PS_LIST_TAIL, "bg", PS_DATA_F64, NULL, 0.0)) {
     16902        psError(PS_ERR_UNKNOWN, false, "failed to add item bg");
     16903        psFree(md);
     16904        return false;
     16905    }
     16906    if (!psMetadataAdd(md, PS_LIST_TAIL, "bg_mean_stdev", PS_DATA_F64, NULL, 0.0)) {
     16907        psError(PS_ERR_UNKNOWN, false, "failed to add item bg_mean_stdev");
     16908        psFree(md);
     16909        return false;
     16910    }
     16911
     16912    bool status = psDBCreateTable(dbh, P6SUMSCFILE_TABLE_NAME, md);
     16913
     16914    psFree(md);
     16915
     16916    return status;
     16917}
     16918
     16919bool p6SumScfileDropTable(psDB *dbh)
     16920{
     16921    return psDBDropTable(dbh, P6SUMSCFILE_TABLE_NAME);
     16922}
     16923
     16924bool p6SumScfileInsert(psDB * dbh, psS32 p6_id, const char *skycell_id, const char *tess_id, const char *uri, psF64 bg, psF64 bg_mean_stdev)
     16925{
     16926    psMetadata *md = psMetadataAlloc();
     16927    if (!psMetadataAdd(md, PS_LIST_TAIL, "p6_id", PS_DATA_S32, NULL, p6_id)) {
     16928        psError(PS_ERR_UNKNOWN, false, "failed to add item p6_id");
     16929        psFree(md);
     16930        return false;
     16931    }
     16932    if (!psMetadataAdd(md, PS_LIST_TAIL, "skycell_id", PS_DATA_STRING, NULL, skycell_id)) {
     16933        psError(PS_ERR_UNKNOWN, false, "failed to add item skycell_id");
     16934        psFree(md);
     16935        return false;
     16936    }
     16937    if (!psMetadataAdd(md, PS_LIST_TAIL, "tess_id", PS_DATA_STRING, NULL, tess_id)) {
     16938        psError(PS_ERR_UNKNOWN, false, "failed to add item tess_id");
     16939        psFree(md);
     16940        return false;
     16941    }
     16942    if (!psMetadataAdd(md, PS_LIST_TAIL, "uri", PS_DATA_STRING, NULL, uri)) {
     16943        psError(PS_ERR_UNKNOWN, false, "failed to add item uri");
     16944        psFree(md);
     16945        return false;
     16946    }
     16947    if (!psMetadataAdd(md, PS_LIST_TAIL, "bg", PS_DATA_F64, NULL, bg)) {
     16948        psError(PS_ERR_UNKNOWN, false, "failed to add item bg");
     16949        psFree(md);
     16950        return false;
     16951    }
     16952    if (!psMetadataAdd(md, PS_LIST_TAIL, "bg_mean_stdev", PS_DATA_F64, NULL, bg_mean_stdev)) {
     16953        psError(PS_ERR_UNKNOWN, false, "failed to add item bg_mean_stdev");
     16954        psFree(md);
     16955        return false;
     16956    }
     16957
     16958    bool status = psDBInsertOneRow(dbh, P6SUMSCFILE_TABLE_NAME, md);
     16959    psFree(md);
     16960
     16961    return status;
     16962}
     16963
     16964long long p6SumScfileDelete(psDB *dbh, const psMetadata *where, unsigned long long limit)
     16965{
     16966    long long       deleted = 0;
     16967
     16968    long long count = psDBDeleteRows(dbh, P6SUMSCFILE_TABLE_NAME, where, limit);
     16969    if (count < 0) {
     16970        psError(PS_ERR_UNKNOWN, true, "failed to delete row from p6SumScfile");
     16971        return count;
     16972
     16973        deleted += count;
     16974    }
     16975
     16976    return deleted;
     16977}
     16978bool p6SumScfileInsertObject(psDB *dbh, p6SumScfileRow *object)
     16979{
     16980    return p6SumScfileInsert(dbh, object->p6_id, object->skycell_id, object->tess_id, object->uri, object->bg, object->bg_mean_stdev);
     16981}
     16982
     16983bool p6SumScfileInsertObjects(psDB *dbh, psArray *objects)
     16984{
     16985    for (long i = 0; i < psArrayLength(objects); i++) {
     16986        if (!p6SumScfileInsertObject(dbh, objects->data[i])) {
     16987            return false;
     16988        }
     16989    }
     16990
     16991    return true;
     16992}
     16993
     16994bool p6SumScfileInsertFits(psDB *dbh, const psFits *fits)
     16995{
     16996    psArray         *rowSet;
     16997
     16998    // move to (the first?) extension named  P6SUMSCFILE_TABLE_NAME
     16999    if (!psFitsMoveExtName(fits, P6SUMSCFILE_TABLE_NAME)) {
     17000        psError(PS_ERR_UNKNOWN, true, "failed to find FITS extension %s", P6SUMSCFILE_TABLE_NAME);
     17001        return false;
     17002    }
     17003
     17004    // check HDU type
     17005    if (psFitsGetExtType(fits) != PS_FITS_TYPE_BINARY_TABLE)  {
     17006        psError(PS_ERR_UNKNOWN, true, "FITS HDU type is not PS_FITS_TYPE_BINARY_TABLE");
     17007        return false;
     17008    }
     17009
     17010    // read fits table
     17011    rowSet = psFitsReadTable(fits);
     17012    if (!rowSet) {
     17013        psError(PS_ERR_UNKNOWN, true, "FITS read error or FITS table is empty");
     17014        psFree(rowSet);
     17015        return false;
     17016    }
     17017
     17018    if (!psDBInsertRows(dbh, P6SUMSCFILE_TABLE_NAME, rowSet)) {
     17019        psError(PS_ERR_UNKNOWN, false, "databse insert failed");
     17020        psFree(rowSet);
     17021        return false;
     17022    }
     17023
     17024    psFree(rowSet);
     17025
     17026    return true;
     17027}
     17028
     17029bool p6SumScfileSelectRowsFits(psDB *dbh, psFits *fits, const psMetadata *where, unsigned long long limit)
     17030{
     17031    psArray         *rowSet;
     17032
     17033    rowSet = psDBSelectRows(dbh, P6SUMSCFILE_TABLE_NAME, where, limit);
     17034    if (!rowSet) {
     17035        return false;
     17036    }
     17037
     17038    // output to fits
     17039    if (!psFitsWriteTable(fits, NULL, rowSet, P6SUMSCFILE_TABLE_NAME)) {
     17040        psError(PS_ERR_UNKNOWN, false, "FITS table write failed");
     17041        psFree(rowSet);
     17042        return false;
     17043    }
     17044
     17045    psFree(rowSet);
     17046
     17047    return true;
     17048}
     17049
     17050psMetadata *p6SumScfileMetadataFromObject(const p6SumScfileRow *object)
     17051{
     17052    psMetadata *md = psMetadataAlloc();
     17053    if (!psMetadataAdd(md, PS_LIST_TAIL, "p6_id", PS_DATA_S32, NULL, object->p6_id)) {
     17054        psError(PS_ERR_UNKNOWN, false, "failed to add item p6_id");
     17055        psFree(md);
     17056        return false;
     17057    }
     17058    if (!psMetadataAdd(md, PS_LIST_TAIL, "skycell_id", PS_DATA_STRING, NULL, object->skycell_id)) {
     17059        psError(PS_ERR_UNKNOWN, false, "failed to add item skycell_id");
     17060        psFree(md);
     17061        return false;
     17062    }
     17063    if (!psMetadataAdd(md, PS_LIST_TAIL, "tess_id", PS_DATA_STRING, NULL, object->tess_id)) {
     17064        psError(PS_ERR_UNKNOWN, false, "failed to add item tess_id");
     17065        psFree(md);
     17066        return false;
     17067    }
     17068    if (!psMetadataAdd(md, PS_LIST_TAIL, "uri", PS_DATA_STRING, NULL, object->uri)) {
     17069        psError(PS_ERR_UNKNOWN, false, "failed to add item uri");
     17070        psFree(md);
     17071        return false;
     17072    }
     17073    if (!psMetadataAdd(md, PS_LIST_TAIL, "bg", PS_DATA_F64, NULL, object->bg)) {
     17074        psError(PS_ERR_UNKNOWN, false, "failed to add item bg");
     17075        psFree(md);
     17076        return false;
     17077    }
     17078    if (!psMetadataAdd(md, PS_LIST_TAIL, "bg_mean_stdev", PS_DATA_F64, NULL, object->bg_mean_stdev)) {
     17079        psError(PS_ERR_UNKNOWN, false, "failed to add item bg_mean_stdev");
     17080        psFree(md);
     17081        return false;
     17082    }
     17083
     17084
     17085    return md;
     17086}
     17087
     17088p6SumScfileRow *p6SumScfileObjectFromMetadata(psMetadata *md)
     17089{
     17090
     17091bool status = false;
     17092    psS32 p6_id = psMetadataLookupS32(&status, md, "p6_id");
     17093    if (!status) {
     17094        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item p6_id");
     17095        return false;
     17096    }
     17097    char* skycell_id = psMetadataLookupPtr(&status, md, "skycell_id");
     17098    if (!status) {
     17099        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item skycell_id");
     17100        return false;
     17101    }
     17102    char* tess_id = psMetadataLookupPtr(&status, md, "tess_id");
     17103    if (!status) {
     17104        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item tess_id");
     17105        return false;
     17106    }
     17107    char* uri = psMetadataLookupPtr(&status, md, "uri");
     17108    if (!status) {
     17109        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item uri");
     17110        return false;
     17111    }
     17112    psF64 bg = psMetadataLookupF64(&status, md, "bg");
     17113    if (!status) {
     17114        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item bg");
     17115        return false;
     17116    }
     17117    psF64 bg_mean_stdev = psMetadataLookupF64(&status, md, "bg_mean_stdev");
     17118    if (!status) {
     17119        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item bg_mean_stdev");
     17120        return false;
     17121    }
     17122
     17123    return p6SumScfileRowAlloc(p6_id, skycell_id, tess_id, uri, bg, bg_mean_stdev);
     17124}
     17125psArray *p6SumScfileSelectRowObjects(psDB *dbh, const psMetadata *where, unsigned long long limit)
     17126{
     17127    psArray         *rowSet;
     17128    psArray         *returnSet;
     17129    psU64           i;
     17130
     17131    rowSet = psDBSelectRows(dbh, P6SUMSCFILE_TABLE_NAME, where, limit);
     17132    if (!rowSet) {
     17133        return NULL;
     17134    }
     17135
     17136    // convert psMetadata rows to row objects
     17137
     17138    returnSet = psArrayAllocEmpty(rowSet->n);
     17139
     17140    for (i = 0; i < rowSet->n; i++) {
     17141        p6SumScfileRow *object = p6SumScfileObjectFromMetadata(rowSet->data[i]);
     17142        psArrayAdd(returnSet, 0, object);
     17143        psFree(object);
     17144    }
     17145
     17146    psFree(rowSet);
     17147
     17148    return returnSet;
     17149}
     17150bool p6SumScfileDeleteObject(psDB *dbh, const p6SumScfileRow *object)
     17151{
     17152    psMetadata *where = p6SumScfileMetadataFromObject(object);
     17153    long long count = psDBDeleteRows(dbh, P6SUMSCFILE_TABLE_NAME, where, 0);
     17154    psFree(where);
     17155    if (count < 0) {
     17156        psError(PS_ERR_UNKNOWN, true, "failed to delete row from p6SumScfile");
     17157        return false;
     17158    }
     17159    if (count > 1) {
     17160        // XXX should this be a psAbort() instead?  It is possible that
     17161        // having an object match multiple rows was by design.
     17162        psError(PS_ERR_UNKNOWN, true, "p6SumScfileRow object matched more then one row.  Check your database schema");
     17163        return false;
     17164    }
     17165
     17166    return true;
     17167}
     17168long long p6SumScfileDeleteRowObjects(psDB *dbh, const psArray *objects, unsigned long long limit)
     17169{
     17170    long long       deleted = 0;
     17171
     17172    for (long long i = 0; i < objects->n; i++) {
     17173        p6SumScfileRow *object = objects->data[i];
     17174        psMetadata *where = p6SumScfileMetadataFromObject(object);
     17175        long long count = psDBDeleteRows(dbh, P6SUMSCFILE_TABLE_NAME, where, limit);
     17176        psFree(where);
     17177        if (count < 0) {
     17178            psError(PS_ERR_UNKNOWN, true, "failed to delete row from p6SumScfile");
     17179            return count;
     17180        }
     17181
     17182        deleted += count;
     17183    }
     17184
     17185    return deleted;
     17186}
     17187bool p6SumScfilePrintObjects(FILE *stream, psArray *objects, bool mdcf)
     17188{
     17189    PS_ASSERT_PTR_NON_NULL(objects, false);
     17190
     17191    psMetadata *output = psMetadataAlloc();
     17192    for (long i = 0; i < psArrayLength(objects); i++) {
     17193        psMetadata *md = p6SumScfileMetadataFromObject(objects->data[i]);
     17194        if (!psMetadataAddMetadata(
     17195            output,
     17196            PS_LIST_TAIL,
     17197            P6SUMSCFILE_TABLE_NAME,
     17198            PS_META_DUPLICATE_OK,
     17199            NULL,
     17200            md
     17201        )) {
     17202            psError(PS_ERR_UNKNOWN, false, "failed to add metadata");
     17203            psFree(md);
     17204            psFree(output);
     17205            return false;
     17206        }
     17207        psFree(md);
     17208    }
     17209
     17210    if (!ippdbPrintMetadataRaw(stream, output, mdcf)) {
     17211        psError(PS_ERR_UNKNOWN, false, "failed to print metadata");
     17212        psFree(output);
     17213    }
     17214    psFree(output);
     17215
     17216    return true;
     17217}
     17218bool p6SumScfilePrintObject(FILE *stream, p6SumScfileRow *object, bool mdcf)
     17219{
     17220    PS_ASSERT_PTR_NON_NULL(object, false);
     17221
     17222    psMetadata *md = p6SumScfileMetadataFromObject(object);
     17223
     17224    if (!ippdbPrintMetadataRaw(stream, md, mdcf)) {
     17225        psError(PS_ERR_UNKNOWN, false, "failed to print metadata");
     17226        psFree(md);
     17227    }
     17228
     17229    psFree(md);
     17230
     17231    return true;
     17232}
  • trunk/ippdb/src/ippdb.h

    r11820 r11867  
    85108510    bool            mdcf                ///< format as mdconfig or simple
    85118511);
     8512/** p6InputScfileRow data structure
     8513 *
     8514 * Structure for representing a single row of p6InputScfile table data.
     8515 */
     8516
     8517typedef struct {
     8518    psS32           p6_id;
     8519    psS32           p4_id;
     8520    char            *skycell_id;
     8521    char            *tess_id;
     8522} p6InputScfileRow;
     8523
     8524/** Creates a new p6InputScfileRow object
     8525 *
     8526 *  @return A new p6InputScfileRow object or NULL on failure.
     8527 */
     8528
     8529p6InputScfileRow *p6InputScfileRowAlloc(
     8530    psS32           p6_id,
     8531    psS32           p4_id,
     8532    const char      *skycell_id,
     8533    const char      *tess_id
     8534);
     8535
     8536/** Creates a new p6InputScfile table
     8537 *
     8538 * @return true on success
     8539 */
     8540
     8541bool p6InputScfileCreateTable(
     8542    psDB            *dbh                ///< Database handle
     8543);
     8544
     8545/** Deletes a p6InputScfile table
     8546 *
     8547 * @return true on success
     8548 */
     8549
     8550bool p6InputScfileDropTable(
     8551    psDB            *dbh                ///< Database handle
     8552);
     8553
     8554/** Insert a single row into a table
     8555 *
     8556 * This function constructs and inserts a single row based on it's parameters.
     8557 *
     8558 * @return true on success
     8559 */
     8560
     8561bool p6InputScfileInsert(
     8562    psDB            *dbh,               ///< Database handle
     8563    psS32           p6_id,
     8564    psS32           p4_id,
     8565    const char      *skycell_id,
     8566    const char      *tess_id
     8567);
     8568
     8569/** Deletes up to limit rows from the database and returns the number of rows actually deleted.
     8570 *
     8571 * @return A The number of rows removed or a negative value on error
     8572 */
     8573
     8574long long p6InputScfileDelete(
     8575    psDB            *dbh,               ///< Database handle
     8576    const psMetadata *where,            ///< Row match criteria
     8577    unsigned long long limit            ///< Maximum number of elements to delete
     8578);
     8579
     8580/** Insert a single p6InputScfileRow object into a table
     8581 *
     8582 * This function constructs and inserts a single row based on it's parameters.
     8583 *
     8584 * @return true on success
     8585 */
     8586
     8587bool p6InputScfileInsertObject(
     8588    psDB            *dbh,               ///< Database handle
     8589    p6InputScfileRow *object             ///< p6InputScfileRow object
     8590);
     8591
     8592/** Insert an array of p6InputScfileRow object into a table
     8593 *
     8594 * This function constructs and inserts multiple rows based on it's parameters.
     8595 *
     8596 * @return true on success
     8597 */
     8598
     8599bool p6InputScfileInsertObjects(
     8600    psDB            *dbh,               ///< Database handle
     8601    psArray         *objects            ///< array of p6InputScfileRow objects
     8602);
     8603
     8604/** Insert data from a binary FITS table p6InputScfileRow into the database
     8605 *
     8606 * This function expects a psFits object with a FITS table as the first
     8607 * extension.  The table must have at least one row of data in it, that is of
     8608 * the appropriate format (number of columns and their type).  All other
     8609 * extensions are ignored.
     8610 *
     8611 * @return true on success
     8612 */
     8613
     8614bool p6InputScfileInsertFits(
     8615    psDB            *dbh,               ///< Database handle
     8616    const psFits    *fits               ///< psFits object
     8617);
     8618
     8619/** Selects up to limit from the database and returns them in a binary FITS table
     8620 *
     8621 * This function assumes an empty psFits object and will create a FITS table
     8622 * as the first extension.
     8623 *
     8624 *  See psDBSelectRows() for documentation on the format of where.
     8625 *
     8626 * @return true on success
     8627 */
     8628
     8629bool p6InputScfileSelectRowsFits(
     8630    psDB            *dbh,               ///< Database handle
     8631    psFits          *fits,              ///< psFits object
     8632    const psMetadata *where,            ///< Row match criteria
     8633    unsigned long long limit            ///< Maximum number of elements to return
     8634);
     8635
     8636/** Convert a p6InputScfileRow into an equivalent psMetadata
     8637 *
     8638 * @return A psMetadata pointer or NULL on error
     8639 */
     8640
     8641psMetadata *p6InputScfileMetadataFromObject(
     8642    const p6InputScfileRow *object             ///< fooRow to convert into a psMetadata
     8643);
     8644
     8645/** Convert a psMetadata into an equivalent fooRow
     8646 *
     8647 * @return A p6InputScfileRow pointer or NULL on error
     8648 */
     8649
     8650p6InputScfileRow *p6InputScfileObjectFromMetadata(
     8651    psMetadata      *md                 ///< psMetadata to convert into a fooRow
     8652);
     8653/** Selects up to limit rows from the database and returns as p6InputScfileRow objects in a psArray
     8654 *
     8655 *  See psDBSelectRows() for documentation on the format of where.
     8656 *
     8657 * @return A psArray pointer or NULL on error
     8658 */
     8659
     8660psArray *p6InputScfileSelectRowObjects(
     8661    psDB            *dbh,               ///< Database handle
     8662    const psMetadata *where,            ///< Row match criteria
     8663    unsigned long long limit            ///< Maximum number of elements to return
     8664);
     8665/** Deletes a row from the database coresponding to an p6InputScfile
     8666 *
     8667 *  Note that a 'where' search psMetadata is constructed from each object and
     8668 *  used to find rows to delete.
     8669 *
     8670 * @return A The number of rows removed or a negative value on error
     8671 */
     8672
     8673bool p6InputScfileDeleteObject(
     8674    psDB            *dbh,               ///< Database handle
     8675    const p6InputScfileRow *object    ///< Object to delete
     8676);
     8677/** Deletes up to limit rows from the database and returns the number of rows actually deleted.
     8678 *
     8679 *  Note that a 'where' search psMetadata is constructed from each object and
     8680 *  used to find rows to delete.
     8681 *
     8682 * @return A The number of rows removed or a negative value on error
     8683 */
     8684
     8685long long p6InputScfileDeleteRowObjects(
     8686    psDB            *dbh,               ///< Database handle
     8687    const psArray   *objects,           ///< Array of objects to delete
     8688    unsigned long long limit            ///< Maximum number of elements to delete
     8689);
     8690/** Formats and prints an array of p6InputScfileRow objects
     8691 *
     8692 * When mdcf is set the formated output is in psMetadataConfig
     8693 * format, otherwise it is in a simple tabular format.
     8694 *
     8695 * @return true on success
     8696 */
     8697
     8698bool p6InputScfilePrintObjects(
     8699    FILE            *stream,            ///< a stream
     8700    psArray         *objects,           ///< An array of p6InputScfileRow objects
     8701    bool            mdcf                ///< format as mdconfig or simple
     8702);
     8703/** Formats and prints an p6InputScfileRow object
     8704 *
     8705 * When mdcf is set the formated output is in psMetadataConfig
     8706 * format, otherwise it is in a simple tabular format.
     8707 *
     8708 * @return true on success
     8709 */
     8710
     8711bool p6InputScfilePrintObject(
     8712    FILE            *stream,            ///< a stream
     8713    p6InputScfileRow *object,    ///< an p6InputScfileRow object
     8714    bool            mdcf                ///< format as mdconfig or simple
     8715);
     8716/** p6SumScfileRow data structure
     8717 *
     8718 * Structure for representing a single row of p6SumScfile table data.
     8719 */
     8720
     8721typedef struct {
     8722    psS32           p6_id;
     8723    char            *skycell_id;
     8724    char            *tess_id;
     8725    char            *uri;
     8726    psF64           bg;
     8727    psF64           bg_mean_stdev;
     8728} p6SumScfileRow;
     8729
     8730/** Creates a new p6SumScfileRow object
     8731 *
     8732 *  @return A new p6SumScfileRow object or NULL on failure.
     8733 */
     8734
     8735p6SumScfileRow *p6SumScfileRowAlloc(
     8736    psS32           p6_id,
     8737    const char      *skycell_id,
     8738    const char      *tess_id,
     8739    const char      *uri,
     8740    psF64           bg,
     8741    psF64           bg_mean_stdev
     8742);
     8743
     8744/** Creates a new p6SumScfile table
     8745 *
     8746 * @return true on success
     8747 */
     8748
     8749bool p6SumScfileCreateTable(
     8750    psDB            *dbh                ///< Database handle
     8751);
     8752
     8753/** Deletes a p6SumScfile table
     8754 *
     8755 * @return true on success
     8756 */
     8757
     8758bool p6SumScfileDropTable(
     8759    psDB            *dbh                ///< Database handle
     8760);
     8761
     8762/** Insert a single row into a table
     8763 *
     8764 * This function constructs and inserts a single row based on it's parameters.
     8765 *
     8766 * @return true on success
     8767 */
     8768
     8769bool p6SumScfileInsert(
     8770    psDB            *dbh,               ///< Database handle
     8771    psS32           p6_id,
     8772    const char      *skycell_id,
     8773    const char      *tess_id,
     8774    const char      *uri,
     8775    psF64           bg,
     8776    psF64           bg_mean_stdev
     8777);
     8778
     8779/** Deletes up to limit rows from the database and returns the number of rows actually deleted.
     8780 *
     8781 * @return A The number of rows removed or a negative value on error
     8782 */
     8783
     8784long long p6SumScfileDelete(
     8785    psDB            *dbh,               ///< Database handle
     8786    const psMetadata *where,            ///< Row match criteria
     8787    unsigned long long limit            ///< Maximum number of elements to delete
     8788);
     8789
     8790/** Insert a single p6SumScfileRow object into a table
     8791 *
     8792 * This function constructs and inserts a single row based on it's parameters.
     8793 *
     8794 * @return true on success
     8795 */
     8796
     8797bool p6SumScfileInsertObject(
     8798    psDB            *dbh,               ///< Database handle
     8799    p6SumScfileRow  *object             ///< p6SumScfileRow object
     8800);
     8801
     8802/** Insert an array of p6SumScfileRow object into a table
     8803 *
     8804 * This function constructs and inserts multiple rows based on it's parameters.
     8805 *
     8806 * @return true on success
     8807 */
     8808
     8809bool p6SumScfileInsertObjects(
     8810    psDB            *dbh,               ///< Database handle
     8811    psArray         *objects            ///< array of p6SumScfileRow objects
     8812);
     8813
     8814/** Insert data from a binary FITS table p6SumScfileRow into the database
     8815 *
     8816 * This function expects a psFits object with a FITS table as the first
     8817 * extension.  The table must have at least one row of data in it, that is of
     8818 * the appropriate format (number of columns and their type).  All other
     8819 * extensions are ignored.
     8820 *
     8821 * @return true on success
     8822 */
     8823
     8824bool p6SumScfileInsertFits(
     8825    psDB            *dbh,               ///< Database handle
     8826    const psFits    *fits               ///< psFits object
     8827);
     8828
     8829/** Selects up to limit from the database and returns them in a binary FITS table
     8830 *
     8831 * This function assumes an empty psFits object and will create a FITS table
     8832 * as the first extension.
     8833 *
     8834 *  See psDBSelectRows() for documentation on the format of where.
     8835 *
     8836 * @return true on success
     8837 */
     8838
     8839bool p6SumScfileSelectRowsFits(
     8840    psDB            *dbh,               ///< Database handle
     8841    psFits          *fits,              ///< psFits object
     8842    const psMetadata *where,            ///< Row match criteria
     8843    unsigned long long limit            ///< Maximum number of elements to return
     8844);
     8845
     8846/** Convert a p6SumScfileRow into an equivalent psMetadata
     8847 *
     8848 * @return A psMetadata pointer or NULL on error
     8849 */
     8850
     8851psMetadata *p6SumScfileMetadataFromObject(
     8852    const p6SumScfileRow *object             ///< fooRow to convert into a psMetadata
     8853);
     8854
     8855/** Convert a psMetadata into an equivalent fooRow
     8856 *
     8857 * @return A p6SumScfileRow pointer or NULL on error
     8858 */
     8859
     8860p6SumScfileRow *p6SumScfileObjectFromMetadata(
     8861    psMetadata      *md                 ///< psMetadata to convert into a fooRow
     8862);
     8863/** Selects up to limit rows from the database and returns as p6SumScfileRow objects in a psArray
     8864 *
     8865 *  See psDBSelectRows() for documentation on the format of where.
     8866 *
     8867 * @return A psArray pointer or NULL on error
     8868 */
     8869
     8870psArray *p6SumScfileSelectRowObjects(
     8871    psDB            *dbh,               ///< Database handle
     8872    const psMetadata *where,            ///< Row match criteria
     8873    unsigned long long limit            ///< Maximum number of elements to return
     8874);
     8875/** Deletes a row from the database coresponding to an p6SumScfile
     8876 *
     8877 *  Note that a 'where' search psMetadata is constructed from each object and
     8878 *  used to find rows to delete.
     8879 *
     8880 * @return A The number of rows removed or a negative value on error
     8881 */
     8882
     8883bool p6SumScfileDeleteObject(
     8884    psDB            *dbh,               ///< Database handle
     8885    const p6SumScfileRow *object    ///< Object to delete
     8886);
     8887/** Deletes up to limit rows from the database and returns the number of rows actually deleted.
     8888 *
     8889 *  Note that a 'where' search psMetadata is constructed from each object and
     8890 *  used to find rows to delete.
     8891 *
     8892 * @return A The number of rows removed or a negative value on error
     8893 */
     8894
     8895long long p6SumScfileDeleteRowObjects(
     8896    psDB            *dbh,               ///< Database handle
     8897    const psArray   *objects,           ///< Array of objects to delete
     8898    unsigned long long limit            ///< Maximum number of elements to delete
     8899);
     8900/** Formats and prints an array of p6SumScfileRow objects
     8901 *
     8902 * When mdcf is set the formated output is in psMetadataConfig
     8903 * format, otherwise it is in a simple tabular format.
     8904 *
     8905 * @return true on success
     8906 */
     8907
     8908bool p6SumScfilePrintObjects(
     8909    FILE            *stream,            ///< a stream
     8910    psArray         *objects,           ///< An array of p6SumScfileRow objects
     8911    bool            mdcf                ///< format as mdconfig or simple
     8912);
     8913/** Formats and prints an p6SumScfileRow object
     8914 *
     8915 * When mdcf is set the formated output is in psMetadataConfig
     8916 * format, otherwise it is in a simple tabular format.
     8917 *
     8918 * @return true on success
     8919 */
     8920
     8921bool p6SumScfilePrintObject(
     8922    FILE            *stream,            ///< a stream
     8923    p6SumScfileRow *object,    ///< an p6SumScfileRow object
     8924    bool            mdcf                ///< format as mdconfig or simple
     8925);
    85128926
    85138927/// @}
     
    85178931#endif
    85188932
    8519 #endif // P6RUN_DB_H
     8933#endif // P6SUMSCFILE_DB_H
Note: See TracChangeset for help on using the changeset viewer.