Changeset 10060 for trunk/ippdb/src/ippdb.c
- Timestamp:
- Nov 17, 2006, 2:19:43 PM (20 years ago)
- File:
-
- 1 edited
-
trunk/ippdb/src/ippdb.c (modified) (50 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ippdb/src/ippdb.c
r9970 r10060 32 32 33 33 #define SUMMITEXP_TABLE_NAME "summitExp" 34 #define SUMMITIMFILE_TABLE_NAME "summitImfile" 34 35 #define PZPENDINGEXP_TABLE_NAME "pzPendingExp" 35 36 #define PZPENDINGIMFILE_TABLE_NAME "pzPendingImfile" 37 #define PZDONEEXP_TABLE_NAME "pzDoneExp" 38 #define PZDONEIMFILE_TABLE_NAME "pzDoneImfile" 36 39 #define NEWEXP_TABLE_NAME "newExp" 37 40 #define NEWIMFILE_TABLE_NAME "newImfile" … … 234 237 static void summitExpRowFree(summitExpRow *object); 235 238 236 summitExpRow *summitExpRowAlloc(const char *exp_id, const char *camera, const char *telescope, psTime* time, const char *exp_type, const char *uri)239 summitExpRow *summitExpRowAlloc(const char *exp_id, const char *camera, const char *telescope, psTime* dateobs, const char *exp_type, const char *uri, psS32 imfiles) 237 240 { 238 241 summitExpRow *_object; … … 244 247 _object->camera = psStringCopy(camera); 245 248 _object->telescope = psStringCopy(telescope); 246 _object-> time = psTimeCopy(time);249 _object->dateobs = psTimeCopy(dateobs); 247 250 _object->exp_type = psStringCopy(exp_type); 248 251 _object->uri = psStringCopy(uri); 252 _object->imfiles = imfiles; 249 253 250 254 return _object; … … 256 260 psFree(object->camera); 257 261 psFree(object->telescope); 258 psFree(object-> time);262 psFree(object->dateobs); 259 263 psFree(object->exp_type); 260 264 psFree(object->uri); … … 279 283 return false; 280 284 } 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); 289 293 if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_type", PS_DATA_STRING, NULL, "64")) { 290 294 psError(PS_ERR_UNKNOWN, false, "failed to add item exp_type"); … … 297 301 return false; 298 302 } 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 } 299 308 300 309 bool status = psDBCreateTable(dbh, SUMMITEXP_TABLE_NAME, md); … … 310 319 } 311 320 312 bool summitExpInsert(psDB * dbh, const char *exp_id, const char *camera, const char *telescope, psTime* time, const char *exp_type, const char *uri)321 bool summitExpInsert(psDB * dbh, const char *exp_id, const char *camera, const char *telescope, psTime* dateobs, const char *exp_type, const char *uri, psS32 imfiles) 313 322 { 314 323 psMetadata *md = psMetadataAlloc(); … … 328 337 return false; 329 338 } 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"); 332 341 psFree(md); 333 342 return false; … … 340 349 if (!psMetadataAdd(md, PS_LIST_TAIL, "uri", PS_DATA_STRING, NULL, uri)) { 341 350 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"); 342 356 psFree(md); 343 357 return false; … … 366 380 bool summitExpInsertObject(psDB *dbh, summitExpRow *object) 367 381 { 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); 369 383 } 370 384 … … 454 468 return false; 455 469 } 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"); 458 472 psFree(md); 459 473 return false; … … 469 483 return false; 470 484 } 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 } 471 490 472 491 … … 493 512 return false; 494 513 } 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"); 498 517 return false; 499 518 } … … 508 527 return false; 509 528 } 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); 512 536 } 513 537 psArray *summitExpSelectRowObjects(psDB *dbh, const psMetadata *where, unsigned long long limit) … … 604 628 return true; 605 629 } 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);630 static void summitImfileRowFree(summitImfileRow *object); 631 632 summitImfileRow *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); 614 638 615 639 _object->exp_id = psStringCopy(exp_id); 616 640 _object->camera = psStringCopy(camera); 617 641 _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); 620 647 621 648 return _object; 622 649 } 623 650 624 static void pzPendingExpRowFree(pzPendingExpRow *object)651 static void summitImfileRowFree(summitImfileRow *object) 625 652 { 626 653 psFree(object->exp_id); 627 654 psFree(object->camera); 628 655 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 662 bool summitImfileCreateTable(psDB *dbh) 633 663 { 634 664 psMetadata *md = psMetadataAlloc(); … … 648 678 return false; 649 679 } 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); 662 707 663 708 psFree(md); … … 666 711 } 667 712 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)713 bool summitImfileDropTable(psDB *dbh) 714 { 715 return psDBDropTable(dbh, SUMMITIMFILE_TABLE_NAME); 716 } 717 718 bool 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) 674 719 { 675 720 psMetadata *md = psMetadataAlloc(); … … 689 734 return false; 690 735 } 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); 703 763 psFree(md); 704 764 … … 706 766 } 707 767 708 long long pzPendingExpDelete(psDB *dbh, const psMetadata *where, unsigned long long limit)768 long long summitImfileDelete(psDB *dbh, const psMetadata *where, unsigned long long limit) 709 769 { 710 770 long long deleted = 0; 711 771 712 long long count = psDBDeleteRows(dbh, PZPENDINGEXP_TABLE_NAME, where, limit);772 long long count = psDBDeleteRows(dbh, SUMMITIMFILE_TABLE_NAME, where, limit); 713 773 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"); 715 775 return count; 716 776 … … 720 780 return deleted; 721 781 } 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)782 bool 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 787 bool summitImfileInsertObjects(psDB *dbh, psArray *objects) 728 788 { 729 789 for (long i = 0; i < psArrayLength(objects); i++) { 730 if (! pzPendingExpInsertObject(dbh, objects->data[i])) {790 if (!summitImfileInsertObject(dbh, objects->data[i])) { 731 791 return false; 732 792 } … … 736 796 } 737 797 738 bool pzPendingExpInsertFits(psDB *dbh, const psFits *fits)798 bool summitImfileInsertFits(psDB *dbh, const psFits *fits) 739 799 { 740 800 psArray *rowSet; 741 801 742 // move to (the first?) extension named PZPENDINGEXP_TABLE_NAME743 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); 745 805 return false; 746 806 } … … 760 820 } 761 821 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 833 bool 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 854 psMetadata *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 902 summitImfileRow *summitImfileObjectFromMetadata(psMetadata *md) 903 { 904 905 bool 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 } 949 psArray *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 } 974 bool 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 } 992 long 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 } 1011 bool 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 } 1042 static void pzPendingExpRowFree(pzPendingExpRow *object); 1043 1044 pzPendingExpRow *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 1058 static void pzPendingExpRowFree(pzPendingExpRow *object) 1059 { 1060 psFree(object->exp_id); 1061 psFree(object->camera); 1062 psFree(object->telescope); 1063 } 1064 1065 bool 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 1091 bool pzPendingExpDropTable(psDB *dbh) 1092 { 1093 return psDBDropTable(dbh, PZPENDINGEXP_TABLE_NAME); 1094 } 1095 1096 bool 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 1121 long 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 } 1135 bool pzPendingExpInsertObject(psDB *dbh, pzPendingExpRow *object) 1136 { 1137 return pzPendingExpInsert(dbh, object->exp_id, object->camera, object->telescope); 1138 } 1139 1140 bool 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 1151 bool 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 762 1175 if (!psDBInsertRows(dbh, PZPENDINGEXP_TABLE_NAME, rowSet)) { 763 1176 psError(PS_ERR_UNKNOWN, false, "databse insert failed"); … … 810 1223 return false; 811 1224 } 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 }822 1225 823 1226 … … 844 1247 return false; 845 1248 } 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); 858 1251 } 859 1252 psArray *pzPendingExpSelectRowObjects(psDB *dbh, const psMetadata *where, unsigned long long limit) … … 952 1345 static void pzPendingImfileRowFree(pzPendingImfileRow *object); 953 1346 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)1347 pzPendingImfileRow *pzPendingImfileRowAlloc(const char *exp_id, const char *camera, const char *telescope, const char *class, const char *class_id, const char *exp_tag) 955 1348 { 956 1349 pzPendingImfileRow *_object; … … 962 1355 _object->camera = psStringCopy(camera); 963 1356 _object->telescope = psStringCopy(telescope); 964 _object->bytes = bytes;965 _object->md5sum = psStringCopy(md5sum);966 1357 _object->class = psStringCopy(class); 967 1358 _object->class_id = psStringCopy(class_id); 968 _object-> uri = psStringCopy(uri);1359 _object->exp_tag = psStringCopy(exp_tag); 969 1360 970 1361 return _object; … … 976 1367 psFree(object->camera); 977 1368 psFree(object->telescope); 978 psFree(object->md5sum);979 1369 psFree(object->class); 980 1370 psFree(object->class_id); 981 psFree(object-> uri);1371 psFree(object->exp_tag); 982 1372 } 983 1373 … … 1000 1390 return false; 1001 1391 } 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")) { 1013 1393 psError(PS_ERR_UNKNOWN, false, "failed to add item class"); 1014 1394 psFree(md); … … 1020 1400 return false; 1021 1401 } 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"); 1024 1404 psFree(md); 1025 1405 return false; … … 1038 1418 } 1039 1419 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)1420 bool pzPendingImfileInsert(psDB * dbh, const char *exp_id, const char *camera, const char *telescope, const char *class, const char *class_id, const char *exp_tag) 1041 1421 { 1042 1422 psMetadata *md = psMetadataAlloc(); … … 1056 1436 return false; 1057 1437 } 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 }1068 1438 if (!psMetadataAdd(md, PS_LIST_TAIL, "class", PS_DATA_STRING, NULL, class)) { 1069 1439 psError(PS_ERR_UNKNOWN, false, "failed to add item class"); … … 1076 1446 return false; 1077 1447 } 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"); 1080 1450 psFree(md); 1081 1451 return false; … … 1104 1474 bool pzPendingImfileInsertObject(psDB *dbh, pzPendingImfileRow *object) 1105 1475 { 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); 1107 1477 } 1108 1478 … … 1192 1562 return false; 1193 1563 } 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 }1204 1564 if (!psMetadataAdd(md, PS_LIST_TAIL, "class", PS_DATA_STRING, NULL, object->class)) { 1205 1565 psError(PS_ERR_UNKNOWN, false, "failed to add item class"); … … 1212 1572 return false; 1213 1573 } 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"); 1216 1576 psFree(md); 1217 1577 return false; … … 1241 1601 return false; 1242 1602 } 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 }1253 1603 char* class = psMetadataLookupPtr(&status, md, "class"); 1254 1604 if (!status) { … … 1261 1611 return false; 1262 1612 } 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); 1270 1620 } 1271 1621 psArray *pzPendingImfileSelectRowObjects(psDB *dbh, const psMetadata *where, unsigned long long limit) … … 1362 1712 return true; 1363 1713 } 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); 1714 static void pzDoneExpRowFree(pzDoneExpRow *object); 1715 1716 pzDoneExpRow *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 1374 1723 _object->exp_id = psStringCopy(exp_id); 1375 1724 _object->camera = psStringCopy(camera); 1376 1725 _object->telescope = psStringCopy(telescope); 1377 _object->exp_type = psStringCopy(exp_type);1378 _object->imfiles = imfiles;1379 1726 1380 1727 return _object; 1381 1728 } 1382 1729 1383 static void newExpRowFree(newExpRow *object) 1384 { 1385 psFree(object->exp_tag); 1730 static void pzDoneExpRowFree(pzDoneExpRow *object) 1731 { 1386 1732 psFree(object->exp_id); 1387 1733 psFree(object->camera); 1388 1734 psFree(object->telescope); 1389 psFree(object->exp_type); 1390 } 1391 1392 bool newExpCreateTable(psDB *dbh) 1735 } 1736 1737 bool pzDoneExpCreateTable(psDB *dbh) 1393 1738 { 1394 1739 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")) { 1401 1741 psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id"); 1402 1742 psFree(md); 1403 1743 return false; 1404 1744 } 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")) { 1406 1746 psError(PS_ERR_UNKNOWN, false, "failed to add item camera"); 1407 1747 psFree(md); 1408 1748 return false; 1409 1749 } 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")) { 1411 1751 psError(PS_ERR_UNKNOWN, false, "failed to add item telescope"); 1412 1752 psFree(md); 1413 1753 return false; 1414 1754 } 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); 1427 1757 1428 1758 psFree(md); … … 1431 1761 } 1432 1762 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)1763 bool pzDoneExpDropTable(psDB *dbh) 1764 { 1765 return psDBDropTable(dbh, PZDONEEXP_TABLE_NAME); 1766 } 1767 1768 bool pzDoneExpInsert(psDB * dbh, const char *exp_id, const char *camera, const char *telescope) 1439 1769 { 1440 1770 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 }1446 1771 if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_id", PS_DATA_STRING, NULL, exp_id)) { 1447 1772 psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id"); … … 1459 1784 return false; 1460 1785 } 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); 1473 1788 psFree(md); 1474 1789 … … 1476 1791 } 1477 1792 1478 long long newExpDelete(psDB *dbh, const psMetadata *where, unsigned long long limit)1793 long long pzDoneExpDelete(psDB *dbh, const psMetadata *where, unsigned long long limit) 1479 1794 { 1480 1795 long long deleted = 0; 1481 1796 1482 long long count = psDBDeleteRows(dbh, NEWEXP_TABLE_NAME, where, limit);1797 long long count = psDBDeleteRows(dbh, PZDONEEXP_TABLE_NAME, where, limit); 1483 1798 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"); 1485 1800 return count; 1486 1801 … … 1490 1805 return deleted; 1491 1806 } 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)1807 bool pzDoneExpInsertObject(psDB *dbh, pzDoneExpRow *object) 1808 { 1809 return pzDoneExpInsert(dbh, object->exp_id, object->camera, object->telescope); 1810 } 1811 1812 bool pzDoneExpInsertObjects(psDB *dbh, psArray *objects) 1498 1813 { 1499 1814 for (long i = 0; i < psArrayLength(objects); i++) { 1500 if (! newExpInsertObject(dbh, objects->data[i])) {1815 if (!pzDoneExpInsertObject(dbh, objects->data[i])) { 1501 1816 return false; 1502 1817 } … … 1506 1821 } 1507 1822 1508 bool newExpInsertFits(psDB *dbh, const psFits *fits)1823 bool pzDoneExpInsertFits(psDB *dbh, const psFits *fits) 1509 1824 { 1510 1825 psArray *rowSet; 1511 1826 1512 // move to (the first?) extension named NEWEXP_TABLE_NAME1513 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); 1515 1830 return false; 1516 1831 } … … 1530 1845 } 1531 1846 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 1858 bool 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 1879 psMetadata *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 1902 pzDoneExpRow *pzDoneExpObjectFromMetadata(psMetadata *md) 1903 { 1904 1905 bool 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 } 1924 psArray *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 } 1949 bool 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 } 1967 long 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 } 1986 bool 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 } 2017 static void pzDoneImfileRowFree(pzDoneImfileRow *object); 2018 2019 pzDoneImfileRow *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 2037 static 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 2048 bool 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 2094 bool pzDoneImfileDropTable(psDB *dbh) 2095 { 2096 return psDBDropTable(dbh, PZDONEIMFILE_TABLE_NAME); 2097 } 2098 2099 bool 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 2144 long 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 } 2158 bool 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 2163 bool 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 2174 bool 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 2209 bool 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 2230 psMetadata *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 2273 pzDoneImfileRow *pzDoneImfileObjectFromMetadata(psMetadata *md) 2274 { 2275 2276 bool 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 } 2315 psArray *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 } 2340 bool 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 } 2358 long 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 } 2377 bool 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 } 2408 static void newExpRowFree(newExpRow *object); 2409 2410 newExpRow *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 2428 static 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 2437 bool 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 2486 bool newExpDropTable(psDB *dbh) 2487 { 2488 return psDBDropTable(dbh, NEWEXP_TABLE_NAME); 2489 } 2490 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) 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 2536 long 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 } 2550 bool 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 2555 bool 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 2566 bool 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 1532 2590 if (!psDBInsertRows(dbh, NEWEXP_TABLE_NAME, rowSet)) { 1533 2591 psError(PS_ERR_UNKNOWN, false, "databse insert failed"); … … 1565 2623 { 1566 2624 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)) { 1568 2626 psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag"); 1569 2627 psFree(md); … … 1585 2643 return false; 1586 2644 } 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 } 1587 2650 if (!psMetadataAdd(md, PS_LIST_TAIL, "exp_type", PS_DATA_STRING, NULL, object->exp_type)) { 1588 2651 psError(PS_ERR_UNKNOWN, false, "failed to add item exp_type"); … … 1604 2667 1605 2668 bool status = false; 1606 char* exp_tag = psMetadataLookupPtr(&status, md, "exp_tag");2669 psU64 exp_tag = psMetadataLookupU64(&status, md, "exp_tag"); 1607 2670 if (!status) { 1608 2671 psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item exp_tag"); … … 1624 2687 return false; 1625 2688 } 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 } 1626 2694 char* exp_type = psMetadataLookupPtr(&status, md, "exp_type"); 1627 2695 if (!status) { … … 1635 2703 } 1636 2704 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); 1638 2706 } 1639 2707 psArray *newExpSelectRowObjects(psDB *dbh, const psMetadata *where, unsigned long long limit) … … 1763 2831 return false; 1764 2832 } 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")) { 1766 2834 psError(PS_ERR_UNKNOWN, false, "failed to add item class"); 1767 2835 psFree(md);
Note:
See TracChangeset
for help on using the changeset viewer.
