Changeset 10110 for trunk/ippdb/src/ippdb.c
- Timestamp:
- Nov 20, 2006, 1:58:03 PM (20 years ago)
- File:
-
- 1 edited
-
trunk/ippdb/src/ippdb.c (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ippdb/src/ippdb.c
r10060 r10110 31 31 #include "ippdb.h" 32 32 33 #define EXPTAGCOUNTER_TABLE_NAME "expTagCounter" 33 34 #define SUMMITEXP_TABLE_NAME "summitExp" 34 35 #define SUMMITIMFILE_TABLE_NAME "summitImfile" … … 235 236 } 236 237 238 static void expTagCounterRowFree(expTagCounterRow *object); 239 240 expTagCounterRow *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 252 static void expTagCounterRowFree(expTagCounterRow *object) 253 { 254 } 255 256 bool 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 272 bool expTagCounterDropTable(psDB *dbh) 273 { 274 return psDBDropTable(dbh, EXPTAGCOUNTER_TABLE_NAME); 275 } 276 277 bool 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 292 long 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 } 306 bool expTagCounterInsertObject(psDB *dbh, expTagCounterRow *object) 307 { 308 return expTagCounterInsert(dbh, object->counter); 309 } 310 311 bool 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 322 bool 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 357 bool 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 378 psMetadata *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 391 expTagCounterRow *expTagCounterObjectFromMetadata(psMetadata *md) 392 { 393 394 bool 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 } 403 psArray *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 } 428 bool 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 } 446 long 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 } 465 bool 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 } 237 496 static void summitExpRowFree(summitExpRow *object); 238 497 … … 2408 2667 static void newExpRowFree(newExpRow *object); 2409 2668 2410 newExpRow *newExpRowAlloc( psU64exp_tag, const char *exp_id, const char *camera, const char *telescope, psTime* dateobs, const char *exp_type, psS32 imfiles)2669 newExpRow *newExpRowAlloc(const char *exp_tag, const char *exp_id, const char *camera, const char *telescope, psTime* dateobs, const char *exp_type, psS32 imfiles) 2411 2670 { 2412 2671 newExpRow *_object; … … 2415 2674 psMemSetDeallocator(_object, (psFreeFunc)newExpRowFree); 2416 2675 2417 _object->exp_tag = exp_tag;2676 _object->exp_tag = psStringCopy(exp_tag); 2418 2677 _object->exp_id = psStringCopy(exp_id); 2419 2678 _object->camera = psStringCopy(camera); … … 2428 2687 static void newExpRowFree(newExpRow *object) 2429 2688 { 2689 psFree(object->exp_tag); 2430 2690 psFree(object->exp_id); 2431 2691 psFree(object->camera); … … 2438 2698 { 2439 2699 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")) { 2441 2701 psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag"); 2442 2702 psFree(md); … … 2489 2749 } 2490 2750 2491 bool newExpInsert(psDB * dbh, psU64exp_tag, const char *exp_id, const char *camera, const char *telescope, psTime* dateobs, const char *exp_type, psS32 imfiles)2751 bool 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) 2492 2752 { 2493 2753 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)) { 2495 2755 psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag"); 2496 2756 psFree(md); … … 2623 2883 { 2624 2884 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)) { 2626 2886 psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag"); 2627 2887 psFree(md); … … 2667 2927 2668 2928 bool status = false; 2669 psU64 exp_tag = psMetadataLookupU64(&status, md, "exp_tag");2929 char* exp_tag = psMetadataLookupPtr(&status, md, "exp_tag"); 2670 2930 if (!status) { 2671 2931 psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item exp_tag");
Note:
See TracChangeset
for help on using the changeset viewer.
