IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Nov 20, 2006, 1:58:03 PM (20 years ago)
Author:
jhoblitt
Message:

VERSION 0.0.57

File:
1 edited

Legend:

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

    r10060 r10110  
    3131#include "ippdb.h"
    3232
     33#define EXPTAGCOUNTER_TABLE_NAME "expTagCounter"
    3334#define SUMMITEXP_TABLE_NAME "summitExp"
    3435#define SUMMITIMFILE_TABLE_NAME "summitImfile"
     
    235236}
    236237
     238static void expTagCounterRowFree(expTagCounterRow *object);
     239
     240expTagCounterRow *expTagCounterRowAlloc(psU64 counter)
     241{
     242    expTagCounterRow *_object;
     243
     244    _object = psAlloc(sizeof(expTagCounterRow));
     245    psMemSetDeallocator(_object, (psFreeFunc)expTagCounterRowFree);
     246
     247    _object->counter = counter;
     248
     249    return _object;
     250}
     251
     252static void expTagCounterRowFree(expTagCounterRow *object)
     253{
     254}
     255
     256bool expTagCounterCreateTable(psDB *dbh)
     257{
     258    psMetadata *md = psMetadataAlloc();
     259    if (!psMetadataAdd(md, PS_LIST_TAIL, "counter", PS_DATA_U64, NULL, 0)) {
     260        psError(PS_ERR_UNKNOWN, false, "failed to add item counter");
     261        psFree(md);
     262        return false;
     263    }
     264
     265    bool status = psDBCreateTable(dbh, EXPTAGCOUNTER_TABLE_NAME, md);
     266
     267    psFree(md);
     268
     269    return status;
     270}
     271
     272bool expTagCounterDropTable(psDB *dbh)
     273{
     274    return psDBDropTable(dbh, EXPTAGCOUNTER_TABLE_NAME);
     275}
     276
     277bool expTagCounterInsert(psDB * dbh, psU64 counter)
     278{
     279    psMetadata *md = psMetadataAlloc();
     280    if (!psMetadataAdd(md, PS_LIST_TAIL, "counter", PS_DATA_U64, NULL, counter)) {
     281        psError(PS_ERR_UNKNOWN, false, "failed to add item counter");
     282        psFree(md);
     283        return false;
     284    }
     285
     286    bool status = psDBInsertOneRow(dbh, EXPTAGCOUNTER_TABLE_NAME, md);
     287    psFree(md);
     288
     289    return status;
     290}
     291
     292long long expTagCounterDelete(psDB *dbh, const psMetadata *where, unsigned long long limit)
     293{
     294    long long       deleted = 0;
     295
     296    long long count = psDBDeleteRows(dbh, EXPTAGCOUNTER_TABLE_NAME, where, limit);
     297    if (count < 0) {
     298        psError(PS_ERR_UNKNOWN, true, "failed to delete row from expTagCounter");
     299        return count;
     300
     301        deleted += count;
     302    }
     303
     304    return deleted;
     305}
     306bool expTagCounterInsertObject(psDB *dbh, expTagCounterRow *object)
     307{
     308    return expTagCounterInsert(dbh, object->counter);
     309}
     310
     311bool expTagCounterInsertObjects(psDB *dbh, psArray *objects)
     312{
     313    for (long i = 0; i < psArrayLength(objects); i++) {
     314        if (!expTagCounterInsertObject(dbh, objects->data[i])) {
     315            return false;
     316        }
     317    }
     318
     319    return true;
     320}
     321
     322bool expTagCounterInsertFits(psDB *dbh, const psFits *fits)
     323{
     324    psArray         *rowSet;
     325
     326    // move to (the first?) extension named  EXPTAGCOUNTER_TABLE_NAME
     327    if (!psFitsMoveExtName(fits, EXPTAGCOUNTER_TABLE_NAME)) {
     328        psError(PS_ERR_UNKNOWN, true, "failed to find FITS extension %s", EXPTAGCOUNTER_TABLE_NAME);
     329        return false;
     330    }
     331
     332    // check HDU type
     333    if (psFitsGetExtType(fits) != PS_FITS_TYPE_BINARY_TABLE)  {
     334        psError(PS_ERR_UNKNOWN, true, "FITS HDU type is not PS_FITS_TYPE_BINARY_TABLE");
     335        return false;
     336    }
     337
     338    // read fits table
     339    rowSet = psFitsReadTable(fits);
     340    if (!rowSet) {
     341        psError(PS_ERR_UNKNOWN, true, "FITS read error or FITS table is empty");
     342        psFree(rowSet);
     343        return false;
     344    }
     345
     346    if (!psDBInsertRows(dbh, EXPTAGCOUNTER_TABLE_NAME, rowSet)) {
     347        psError(PS_ERR_UNKNOWN, false, "databse insert failed");
     348        psFree(rowSet);
     349        return false;
     350    }
     351
     352    psFree(rowSet);
     353
     354    return true;
     355}
     356
     357bool expTagCounterSelectRowsFits(psDB *dbh, psFits *fits, const psMetadata *where, unsigned long long limit)
     358{
     359    psArray         *rowSet;
     360
     361    rowSet = psDBSelectRows(dbh, EXPTAGCOUNTER_TABLE_NAME, where, limit);
     362    if (!rowSet) {
     363        return false;
     364    }
     365
     366    // output to fits
     367    if (!psFitsWriteTable(fits, NULL, rowSet, EXPTAGCOUNTER_TABLE_NAME)) {
     368        psError(PS_ERR_UNKNOWN, false, "FITS table write failed");
     369        psFree(rowSet);
     370        return false;
     371    }
     372
     373    psFree(rowSet);
     374
     375    return true;
     376}
     377
     378psMetadata *expTagCounterMetadataFromObject(const expTagCounterRow *object)
     379{
     380    psMetadata *md = psMetadataAlloc();
     381    if (!psMetadataAdd(md, PS_LIST_TAIL, "counter", PS_DATA_U64, NULL, object->counter)) {
     382        psError(PS_ERR_UNKNOWN, false, "failed to add item counter");
     383        psFree(md);
     384        return false;
     385    }
     386
     387
     388    return md;
     389}
     390
     391expTagCounterRow *expTagCounterObjectFromMetadata(psMetadata *md)
     392{
     393
     394bool status = false;
     395    psU64 counter = psMetadataLookupU64(&status, md, "counter");
     396    if (!status) {
     397        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item counter");
     398        return false;
     399    }
     400
     401    return expTagCounterRowAlloc(counter);
     402}
     403psArray *expTagCounterSelectRowObjects(psDB *dbh, const psMetadata *where, unsigned long long limit)
     404{
     405    psArray         *rowSet;
     406    psArray         *returnSet;
     407    psU64           i;
     408
     409    rowSet = psDBSelectRows(dbh, EXPTAGCOUNTER_TABLE_NAME, where, limit);
     410    if (!rowSet) {
     411        return NULL;
     412    }
     413
     414    // convert psMetadata rows to row objects
     415
     416    returnSet = psArrayAllocEmpty(rowSet->n);
     417
     418    for (i = 0; i < rowSet->n; i++) {
     419        expTagCounterRow *object = expTagCounterObjectFromMetadata(rowSet->data[i]);
     420        psArrayAdd(returnSet, 0, object);
     421        psFree(object);
     422    }
     423
     424    psFree(rowSet);
     425
     426    return returnSet;
     427}
     428bool expTagCounterDeleteObject(psDB *dbh, const expTagCounterRow *object)
     429{
     430    psMetadata *where = expTagCounterMetadataFromObject(object);
     431    long long count = psDBDeleteRows(dbh, EXPTAGCOUNTER_TABLE_NAME, where, 0);
     432    psFree(where)
     433    if (count < 0) {
     434        psError(PS_ERR_UNKNOWN, true, "failed to delete row from expTagCounter");
     435        return false;
     436    }
     437    if (count > 1) {
     438        // XXX should this be a psAbort() instead?  It is possible that
     439        // having an object match multiple rows was by design.
     440        psError(PS_ERR_UNKNOWN, true, "expTagCounterRow object matched more then one row.  Check your database schema");
     441        return false;
     442    }
     443
     444    return true;
     445}
     446long long expTagCounterDeleteRowObjects(psDB *dbh, const psArray *objects, unsigned long long limit)
     447{
     448    long long       deleted = 0;
     449
     450    for (long long i = 0; i < objects->n; i++) {
     451        expTagCounterRow *object = objects->data[i];
     452        psMetadata *where = expTagCounterMetadataFromObject(object);
     453        long long count = psDBDeleteRows(dbh, EXPTAGCOUNTER_TABLE_NAME, where, limit);
     454        psFree(where)
     455        if (count < 0) {
     456            psError(PS_ERR_UNKNOWN, true, "failed to delete row from expTagCounter");
     457            return count;
     458        }
     459
     460        deleted += count;
     461    }
     462
     463    return deleted;
     464}
     465bool expTagCounterPrintObjects(FILE *stream, psArray *objects, bool mdcf)
     466{
     467    PS_ASSERT_PTR_NON_NULL(objects, false);
     468
     469    psMetadata *output = psMetadataAlloc();
     470    for (long i = 0; i < psArrayLength(objects); i++) {
     471        psMetadata *md = expTagCounterMetadataFromObject(objects->data[i]);
     472        if (!psMetadataAddMetadata(
     473            output,
     474            PS_LIST_TAIL,
     475            EXPTAGCOUNTER_TABLE_NAME,
     476            PS_META_DUPLICATE_OK,
     477            NULL,
     478            md
     479        )) {
     480            psError(PS_ERR_UNKNOWN, false, "failed to add metadata");
     481            psFree(md);
     482            psFree(output);
     483            return false;
     484        }
     485        psFree(md);
     486    }
     487
     488    if (!ippdbPrintMetadataRaw(stream, output, mdcf)) {
     489        psError(PS_ERR_UNKNOWN, false, "failed to print metadata");
     490        psFree(output);
     491    }
     492    psFree(output);
     493
     494    return true;
     495}
    237496static void summitExpRowFree(summitExpRow *object);
    238497
     
    24082667static void newExpRowFree(newExpRow *object);
    24092668
    2410 newExpRow *newExpRowAlloc(psU64 exp_tag, const char *exp_id, const char *camera, const char *telescope, psTime* dateobs, const char *exp_type, psS32 imfiles)
     2669newExpRow *newExpRowAlloc(const char *exp_tag, const char *exp_id, const char *camera, const char *telescope, psTime* dateobs, const char *exp_type, psS32 imfiles)
    24112670{
    24122671    newExpRow       *_object;
     
    24152674    psMemSetDeallocator(_object, (psFreeFunc)newExpRowFree);
    24162675
    2417     _object->exp_tag = exp_tag;
     2676    _object->exp_tag = psStringCopy(exp_tag);
    24182677    _object->exp_id = psStringCopy(exp_id);
    24192678    _object->camera = psStringCopy(camera);
     
    24282687static void newExpRowFree(newExpRow *object)
    24292688{
     2689    psFree(object->exp_tag);
    24302690    psFree(object->exp_id);
    24312691    psFree(object->camera);
     
    24382698{
    24392699    psMetadata *md = psMetadataAlloc();
    2440     if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_U64, "Primary Key AUTO_INCREMENT", 0)) {
     2700    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_STRING, "Primary Key", "64")) {
    24412701        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag");
    24422702        psFree(md);
     
    24892749}
    24902750
    2491 bool newExpInsert(psDB * dbh, psU64 exp_tag, const char *exp_id, const char *camera, const char *telescope, psTime* dateobs, const char *exp_type, psS32 imfiles)
     2751bool newExpInsert(psDB * dbh, const char *exp_tag, const char *exp_id, const char *camera, const char *telescope, psTime* dateobs, const char *exp_type, psS32 imfiles)
    24922752{
    24932753    psMetadata *md = psMetadataAlloc();
    2494     if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_U64, NULL, exp_tag)) {
     2754    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_STRING, NULL, exp_tag)) {
    24952755        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag");
    24962756        psFree(md);
     
    26232883{
    26242884    psMetadata *md = psMetadataAlloc();
    2625     if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_U64, NULL, object->exp_tag)) {
     2885    if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_tag", PS_DATA_STRING, NULL, object->exp_tag)) {
    26262886        psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag");
    26272887        psFree(md);
     
    26672927
    26682928bool status = false;
    2669     psU64 exp_tag = psMetadataLookupU64(&status, md, "exp_tag");
     2929    char* exp_tag = psMetadataLookupPtr(&status, md, "exp_tag");
    26702930    if (!status) {
    26712931        psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item exp_tag");
Note: See TracChangeset for help on using the changeset viewer.