Changeset 15576 for trunk/ippdb/src
- Timestamp:
- Nov 9, 2007, 5:14:16 PM (19 years ago)
- Location:
- trunk/ippdb/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/ippdb/src/ippdb.c
r15569 r15576 81 81 #define CALDB_TABLE_NAME "calDB" 82 82 #define CALRUN_TABLE_NAME "calRun" 83 #define FLATCORRRUN_TABLE_NAME "flatcorrRun" 84 #define FLATCORREXP_TABLE_NAME "flatcorrExp" 83 85 #define MAX_STRING_LENGTH 1024 84 86 … … 22700 22702 return true; 22701 22703 } 22704 static void flatcorrRunRowFree(flatcorrRunRow *object); 22705 22706 flatcorrRunRow *flatcorrRunRowAlloc(psS64 corr_id, const char *dvodb, const char *state, const char *workdir, const char *label) 22707 { 22708 flatcorrRunRow *_object; 22709 22710 _object = psAlloc(sizeof(flatcorrRunRow)); 22711 psMemSetDeallocator(_object, (psFreeFunc)flatcorrRunRowFree); 22712 22713 _object->corr_id = corr_id; 22714 _object->dvodb = psStringCopy(dvodb); 22715 _object->state = psStringCopy(state); 22716 _object->workdir = psStringCopy(workdir); 22717 _object->label = psStringCopy(label); 22718 22719 return _object; 22720 } 22721 22722 static void flatcorrRunRowFree(flatcorrRunRow *object) 22723 { 22724 psFree(object->dvodb); 22725 psFree(object->state); 22726 psFree(object->workdir); 22727 psFree(object->label); 22728 } 22729 22730 bool flatcorrRunCreateTable(psDB *dbh) 22731 { 22732 psMetadata *md = psMetadataAlloc(); 22733 if (!psMetadataAdd(md, PS_LIST_TAIL, "corr_id", PS_DATA_S64, "Primary Key AUTO_INCREMENT", 0)) { 22734 psError(PS_ERR_UNKNOWN, false, "failed to add item corr_id"); 22735 psFree(md); 22736 return false; 22737 } 22738 if (!psMetadataAdd(md, PS_LIST_TAIL, "dvodb", PS_DATA_STRING, NULL, "64")) { 22739 psError(PS_ERR_UNKNOWN, false, "failed to add item dvodb"); 22740 psFree(md); 22741 return false; 22742 } 22743 if (!psMetadataAdd(md, PS_LIST_TAIL, "state", PS_DATA_STRING, NULL, "64")) { 22744 psError(PS_ERR_UNKNOWN, false, "failed to add item state"); 22745 psFree(md); 22746 return false; 22747 } 22748 if (!psMetadataAdd(md, PS_LIST_TAIL, "workdir", PS_DATA_STRING, NULL, "255")) { 22749 psError(PS_ERR_UNKNOWN, false, "failed to add item workdir"); 22750 psFree(md); 22751 return false; 22752 } 22753 if (!psMetadataAdd(md, PS_LIST_TAIL, "label", PS_DATA_STRING, NULL, "64")) { 22754 psError(PS_ERR_UNKNOWN, false, "failed to add item label"); 22755 psFree(md); 22756 return false; 22757 } 22758 22759 bool status = psDBCreateTable(dbh, FLATCORRRUN_TABLE_NAME, md); 22760 22761 psFree(md); 22762 22763 return status; 22764 } 22765 22766 bool flatcorrRunDropTable(psDB *dbh) 22767 { 22768 return psDBDropTable(dbh, FLATCORRRUN_TABLE_NAME); 22769 } 22770 22771 bool flatcorrRunInsert(psDB * dbh, psS64 corr_id, const char *dvodb, const char *state, const char *workdir, const char *label) 22772 { 22773 psMetadata *md = psMetadataAlloc(); 22774 if (!psMetadataAdd(md, PS_LIST_TAIL, "corr_id", PS_DATA_S64, NULL, corr_id)) { 22775 psError(PS_ERR_UNKNOWN, false, "failed to add item corr_id"); 22776 psFree(md); 22777 return false; 22778 } 22779 if (!psMetadataAdd(md, PS_LIST_TAIL, "dvodb", PS_DATA_STRING, NULL, dvodb)) { 22780 psError(PS_ERR_UNKNOWN, false, "failed to add item dvodb"); 22781 psFree(md); 22782 return false; 22783 } 22784 if (!psMetadataAdd(md, PS_LIST_TAIL, "state", PS_DATA_STRING, NULL, state)) { 22785 psError(PS_ERR_UNKNOWN, false, "failed to add item state"); 22786 psFree(md); 22787 return false; 22788 } 22789 if (!psMetadataAdd(md, PS_LIST_TAIL, "workdir", PS_DATA_STRING, NULL, workdir)) { 22790 psError(PS_ERR_UNKNOWN, false, "failed to add item workdir"); 22791 psFree(md); 22792 return false; 22793 } 22794 if (!psMetadataAdd(md, PS_LIST_TAIL, "label", PS_DATA_STRING, NULL, label)) { 22795 psError(PS_ERR_UNKNOWN, false, "failed to add item label"); 22796 psFree(md); 22797 return false; 22798 } 22799 22800 bool status = psDBInsertOneRow(dbh, FLATCORRRUN_TABLE_NAME, md); 22801 psFree(md); 22802 22803 return status; 22804 } 22805 22806 long long flatcorrRunDelete(psDB *dbh, const psMetadata *where, unsigned long long limit) 22807 { 22808 long long deleted = 0; 22809 22810 long long count = psDBDeleteRows(dbh, FLATCORRRUN_TABLE_NAME, where, limit); 22811 if (count < 0) { 22812 psError(PS_ERR_UNKNOWN, true, "failed to delete row from flatcorrRun"); 22813 return count; 22814 22815 deleted += count; 22816 } 22817 22818 return deleted; 22819 } 22820 bool flatcorrRunInsertObject(psDB *dbh, flatcorrRunRow *object) 22821 { 22822 return flatcorrRunInsert(dbh, object->corr_id, object->dvodb, object->state, object->workdir, object->label); 22823 } 22824 22825 bool flatcorrRunInsertObjects(psDB *dbh, psArray *objects) 22826 { 22827 for (long i = 0; i < psArrayLength(objects); i++) { 22828 if (!flatcorrRunInsertObject(dbh, objects->data[i])) { 22829 return false; 22830 } 22831 } 22832 22833 return true; 22834 } 22835 22836 bool flatcorrRunInsertFits(psDB *dbh, const psFits *fits) 22837 { 22838 psArray *rowSet; 22839 22840 // move to (the first?) extension named FLATCORRRUN_TABLE_NAME 22841 if (!psFitsMoveExtName(fits, FLATCORRRUN_TABLE_NAME)) { 22842 psError(PS_ERR_UNKNOWN, true, "failed to find FITS extension %s", FLATCORRRUN_TABLE_NAME); 22843 return false; 22844 } 22845 22846 // check HDU type 22847 if (psFitsGetExtType(fits) != PS_FITS_TYPE_BINARY_TABLE) { 22848 psError(PS_ERR_UNKNOWN, true, "FITS HDU type is not PS_FITS_TYPE_BINARY_TABLE"); 22849 return false; 22850 } 22851 22852 // read fits table 22853 rowSet = psFitsReadTable(fits); 22854 if (!rowSet) { 22855 psError(PS_ERR_UNKNOWN, true, "FITS read error or FITS table is empty"); 22856 psFree(rowSet); 22857 return false; 22858 } 22859 22860 if (!psDBInsertRows(dbh, FLATCORRRUN_TABLE_NAME, rowSet)) { 22861 psError(PS_ERR_UNKNOWN, false, "databse insert failed"); 22862 psFree(rowSet); 22863 return false; 22864 } 22865 22866 psFree(rowSet); 22867 22868 return true; 22869 } 22870 22871 bool flatcorrRunSelectRowsFits(psDB *dbh, psFits *fits, const psMetadata *where, unsigned long long limit) 22872 { 22873 psArray *rowSet; 22874 22875 rowSet = psDBSelectRows(dbh, FLATCORRRUN_TABLE_NAME, where, limit); 22876 if (!rowSet) { 22877 return false; 22878 } 22879 22880 // output to fits 22881 if (!psFitsWriteTable(fits, NULL, rowSet, FLATCORRRUN_TABLE_NAME)) { 22882 psError(PS_ERR_UNKNOWN, false, "FITS table write failed"); 22883 psFree(rowSet); 22884 return false; 22885 } 22886 22887 psFree(rowSet); 22888 22889 return true; 22890 } 22891 22892 psMetadata *flatcorrRunMetadataFromObject(const flatcorrRunRow *object) 22893 { 22894 psMetadata *md = psMetadataAlloc(); 22895 if (!psMetadataAdd(md, PS_LIST_TAIL, "corr_id", PS_DATA_S64, NULL, object->corr_id)) { 22896 psError(PS_ERR_UNKNOWN, false, "failed to add item corr_id"); 22897 psFree(md); 22898 return false; 22899 } 22900 if (!psMetadataAdd(md, PS_LIST_TAIL, "dvodb", PS_DATA_STRING, NULL, object->dvodb)) { 22901 psError(PS_ERR_UNKNOWN, false, "failed to add item dvodb"); 22902 psFree(md); 22903 return false; 22904 } 22905 if (!psMetadataAdd(md, PS_LIST_TAIL, "state", PS_DATA_STRING, NULL, object->state)) { 22906 psError(PS_ERR_UNKNOWN, false, "failed to add item state"); 22907 psFree(md); 22908 return false; 22909 } 22910 if (!psMetadataAdd(md, PS_LIST_TAIL, "workdir", PS_DATA_STRING, NULL, object->workdir)) { 22911 psError(PS_ERR_UNKNOWN, false, "failed to add item workdir"); 22912 psFree(md); 22913 return false; 22914 } 22915 if (!psMetadataAdd(md, PS_LIST_TAIL, "label", PS_DATA_STRING, NULL, object->label)) { 22916 psError(PS_ERR_UNKNOWN, false, "failed to add item label"); 22917 psFree(md); 22918 return false; 22919 } 22920 22921 22922 return md; 22923 } 22924 22925 flatcorrRunRow *flatcorrRunObjectFromMetadata(psMetadata *md) 22926 { 22927 22928 bool status = false; 22929 psS64 corr_id = psMetadataLookupS64(&status, md, "corr_id"); 22930 if (!status) { 22931 psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item corr_id"); 22932 return false; 22933 } 22934 char* dvodb = psMetadataLookupPtr(&status, md, "dvodb"); 22935 if (!status) { 22936 psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item dvodb"); 22937 return false; 22938 } 22939 char* state = psMetadataLookupPtr(&status, md, "state"); 22940 if (!status) { 22941 psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item state"); 22942 return false; 22943 } 22944 char* workdir = psMetadataLookupPtr(&status, md, "workdir"); 22945 if (!status) { 22946 psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item workdir"); 22947 return false; 22948 } 22949 char* label = psMetadataLookupPtr(&status, md, "label"); 22950 if (!status) { 22951 psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item label"); 22952 return false; 22953 } 22954 22955 return flatcorrRunRowAlloc(corr_id, dvodb, state, workdir, label); 22956 } 22957 psArray *flatcorrRunSelectRowObjects(psDB *dbh, const psMetadata *where, unsigned long long limit) 22958 { 22959 psArray *rowSet; 22960 psArray *returnSet; 22961 psU64 i; 22962 22963 rowSet = psDBSelectRows(dbh, FLATCORRRUN_TABLE_NAME, where, limit); 22964 if (!rowSet) { 22965 return NULL; 22966 } 22967 22968 // convert psMetadata rows to row objects 22969 22970 returnSet = psArrayAllocEmpty(rowSet->n); 22971 22972 for (i = 0; i < rowSet->n; i++) { 22973 flatcorrRunRow *object = flatcorrRunObjectFromMetadata(rowSet->data[i]); 22974 psArrayAdd(returnSet, 0, object); 22975 psFree(object); 22976 } 22977 22978 psFree(rowSet); 22979 22980 return returnSet; 22981 } 22982 bool flatcorrRunDeleteObject(psDB *dbh, const flatcorrRunRow *object) 22983 { 22984 psMetadata *where = flatcorrRunMetadataFromObject(object); 22985 long long count = psDBDeleteRows(dbh, FLATCORRRUN_TABLE_NAME, where, 0); 22986 psFree(where); 22987 if (count < 0) { 22988 psError(PS_ERR_UNKNOWN, true, "failed to delete row from flatcorrRun"); 22989 return false; 22990 } 22991 if (count > 1) { 22992 // XXX should this be a psAbort() instead? It is possible that 22993 // having an object match multiple rows was by design. 22994 psError(PS_ERR_UNKNOWN, true, "flatcorrRunRow object matched more then one row. Check your database schema"); 22995 return false; 22996 } 22997 22998 return true; 22999 } 23000 long long flatcorrRunDeleteRowObjects(psDB *dbh, const psArray *objects, unsigned long long limit) 23001 { 23002 long long deleted = 0; 23003 23004 for (long long i = 0; i < objects->n; i++) { 23005 flatcorrRunRow *object = objects->data[i]; 23006 psMetadata *where = flatcorrRunMetadataFromObject(object); 23007 long long count = psDBDeleteRows(dbh, FLATCORRRUN_TABLE_NAME, where, limit); 23008 psFree(where); 23009 if (count < 0) { 23010 psError(PS_ERR_UNKNOWN, true, "failed to delete row from flatcorrRun"); 23011 return count; 23012 } 23013 23014 deleted += count; 23015 } 23016 23017 return deleted; 23018 } 23019 bool flatcorrRunPrintObjects(FILE *stream, psArray *objects, bool mdcf) 23020 { 23021 PS_ASSERT_PTR_NON_NULL(objects, false); 23022 23023 psMetadata *output = psMetadataAlloc(); 23024 for (long i = 0; i < psArrayLength(objects); i++) { 23025 psMetadata *md = flatcorrRunMetadataFromObject(objects->data[i]); 23026 if (!psMetadataAddMetadata( 23027 output, 23028 PS_LIST_TAIL, 23029 FLATCORRRUN_TABLE_NAME, 23030 PS_META_DUPLICATE_OK, 23031 NULL, 23032 md 23033 )) { 23034 psError(PS_ERR_UNKNOWN, false, "failed to add metadata"); 23035 psFree(md); 23036 psFree(output); 23037 return false; 23038 } 23039 psFree(md); 23040 } 23041 23042 if (!ippdbPrintMetadataRaw(stream, output, mdcf)) { 23043 psError(PS_ERR_UNKNOWN, false, "failed to print metadata"); 23044 psFree(output); 23045 } 23046 psFree(output); 23047 23048 return true; 23049 } 23050 bool flatcorrRunPrintObject(FILE *stream, flatcorrRunRow *object, bool mdcf) 23051 { 23052 PS_ASSERT_PTR_NON_NULL(object, false); 23053 23054 psMetadata *md = flatcorrRunMetadataFromObject(object); 23055 23056 if (!ippdbPrintMetadataRaw(stream, md, mdcf)) { 23057 psError(PS_ERR_UNKNOWN, false, "failed to print metadata"); 23058 psFree(md); 23059 } 23060 23061 psFree(md); 23062 23063 return true; 23064 } 23065 static void flatcorrExpRowFree(flatcorrExpRow *object); 23066 23067 flatcorrExpRow *flatcorrExpRowAlloc(psS64 corr_id, psS64 chip_id, const char *state) 23068 { 23069 flatcorrExpRow *_object; 23070 23071 _object = psAlloc(sizeof(flatcorrExpRow)); 23072 psMemSetDeallocator(_object, (psFreeFunc)flatcorrExpRowFree); 23073 23074 _object->corr_id = corr_id; 23075 _object->chip_id = chip_id; 23076 _object->state = psStringCopy(state); 23077 23078 return _object; 23079 } 23080 23081 static void flatcorrExpRowFree(flatcorrExpRow *object) 23082 { 23083 psFree(object->state); 23084 } 23085 23086 bool flatcorrExpCreateTable(psDB *dbh) 23087 { 23088 psMetadata *md = psMetadataAlloc(); 23089 if (!psMetadataAdd(md, PS_LIST_TAIL, "corr_id", PS_DATA_S64, "Primary Key", 0)) { 23090 psError(PS_ERR_UNKNOWN, false, "failed to add item corr_id"); 23091 psFree(md); 23092 return false; 23093 } 23094 if (!psMetadataAdd(md, PS_LIST_TAIL, "chip_id", PS_DATA_S64, "Primary Key", 64)) { 23095 psError(PS_ERR_UNKNOWN, false, "failed to add item chip_id"); 23096 psFree(md); 23097 return false; 23098 } 23099 if (!psMetadataAdd(md, PS_LIST_TAIL, "state", PS_DATA_STRING, "fkey(corr_id) ref flatcorrRun(corr_id)", "64")) { 23100 psError(PS_ERR_UNKNOWN, false, "failed to add item state"); 23101 psFree(md); 23102 return false; 23103 } 23104 23105 bool status = psDBCreateTable(dbh, FLATCORREXP_TABLE_NAME, md); 23106 23107 psFree(md); 23108 23109 return status; 23110 } 23111 23112 bool flatcorrExpDropTable(psDB *dbh) 23113 { 23114 return psDBDropTable(dbh, FLATCORREXP_TABLE_NAME); 23115 } 23116 23117 bool flatcorrExpInsert(psDB * dbh, psS64 corr_id, psS64 chip_id, const char *state) 23118 { 23119 psMetadata *md = psMetadataAlloc(); 23120 if (!psMetadataAdd(md, PS_LIST_TAIL, "corr_id", PS_DATA_S64, NULL, corr_id)) { 23121 psError(PS_ERR_UNKNOWN, false, "failed to add item corr_id"); 23122 psFree(md); 23123 return false; 23124 } 23125 if (!psMetadataAdd(md, PS_LIST_TAIL, "chip_id", PS_DATA_S64, NULL, chip_id)) { 23126 psError(PS_ERR_UNKNOWN, false, "failed to add item chip_id"); 23127 psFree(md); 23128 return false; 23129 } 23130 if (!psMetadataAdd(md, PS_LIST_TAIL, "state", PS_DATA_STRING, NULL, state)) { 23131 psError(PS_ERR_UNKNOWN, false, "failed to add item state"); 23132 psFree(md); 23133 return false; 23134 } 23135 23136 bool status = psDBInsertOneRow(dbh, FLATCORREXP_TABLE_NAME, md); 23137 psFree(md); 23138 23139 return status; 23140 } 23141 23142 long long flatcorrExpDelete(psDB *dbh, const psMetadata *where, unsigned long long limit) 23143 { 23144 long long deleted = 0; 23145 23146 long long count = psDBDeleteRows(dbh, FLATCORREXP_TABLE_NAME, where, limit); 23147 if (count < 0) { 23148 psError(PS_ERR_UNKNOWN, true, "failed to delete row from flatcorrExp"); 23149 return count; 23150 23151 deleted += count; 23152 } 23153 23154 return deleted; 23155 } 23156 bool flatcorrExpInsertObject(psDB *dbh, flatcorrExpRow *object) 23157 { 23158 return flatcorrExpInsert(dbh, object->corr_id, object->chip_id, object->state); 23159 } 23160 23161 bool flatcorrExpInsertObjects(psDB *dbh, psArray *objects) 23162 { 23163 for (long i = 0; i < psArrayLength(objects); i++) { 23164 if (!flatcorrExpInsertObject(dbh, objects->data[i])) { 23165 return false; 23166 } 23167 } 23168 23169 return true; 23170 } 23171 23172 bool flatcorrExpInsertFits(psDB *dbh, const psFits *fits) 23173 { 23174 psArray *rowSet; 23175 23176 // move to (the first?) extension named FLATCORREXP_TABLE_NAME 23177 if (!psFitsMoveExtName(fits, FLATCORREXP_TABLE_NAME)) { 23178 psError(PS_ERR_UNKNOWN, true, "failed to find FITS extension %s", FLATCORREXP_TABLE_NAME); 23179 return false; 23180 } 23181 23182 // check HDU type 23183 if (psFitsGetExtType(fits) != PS_FITS_TYPE_BINARY_TABLE) { 23184 psError(PS_ERR_UNKNOWN, true, "FITS HDU type is not PS_FITS_TYPE_BINARY_TABLE"); 23185 return false; 23186 } 23187 23188 // read fits table 23189 rowSet = psFitsReadTable(fits); 23190 if (!rowSet) { 23191 psError(PS_ERR_UNKNOWN, true, "FITS read error or FITS table is empty"); 23192 psFree(rowSet); 23193 return false; 23194 } 23195 23196 if (!psDBInsertRows(dbh, FLATCORREXP_TABLE_NAME, rowSet)) { 23197 psError(PS_ERR_UNKNOWN, false, "databse insert failed"); 23198 psFree(rowSet); 23199 return false; 23200 } 23201 23202 psFree(rowSet); 23203 23204 return true; 23205 } 23206 23207 bool flatcorrExpSelectRowsFits(psDB *dbh, psFits *fits, const psMetadata *where, unsigned long long limit) 23208 { 23209 psArray *rowSet; 23210 23211 rowSet = psDBSelectRows(dbh, FLATCORREXP_TABLE_NAME, where, limit); 23212 if (!rowSet) { 23213 return false; 23214 } 23215 23216 // output to fits 23217 if (!psFitsWriteTable(fits, NULL, rowSet, FLATCORREXP_TABLE_NAME)) { 23218 psError(PS_ERR_UNKNOWN, false, "FITS table write failed"); 23219 psFree(rowSet); 23220 return false; 23221 } 23222 23223 psFree(rowSet); 23224 23225 return true; 23226 } 23227 23228 psMetadata *flatcorrExpMetadataFromObject(const flatcorrExpRow *object) 23229 { 23230 psMetadata *md = psMetadataAlloc(); 23231 if (!psMetadataAdd(md, PS_LIST_TAIL, "corr_id", PS_DATA_S64, NULL, object->corr_id)) { 23232 psError(PS_ERR_UNKNOWN, false, "failed to add item corr_id"); 23233 psFree(md); 23234 return false; 23235 } 23236 if (!psMetadataAdd(md, PS_LIST_TAIL, "chip_id", PS_DATA_S64, NULL, object->chip_id)) { 23237 psError(PS_ERR_UNKNOWN, false, "failed to add item chip_id"); 23238 psFree(md); 23239 return false; 23240 } 23241 if (!psMetadataAdd(md, PS_LIST_TAIL, "state", PS_DATA_STRING, NULL, object->state)) { 23242 psError(PS_ERR_UNKNOWN, false, "failed to add item state"); 23243 psFree(md); 23244 return false; 23245 } 23246 23247 23248 return md; 23249 } 23250 23251 flatcorrExpRow *flatcorrExpObjectFromMetadata(psMetadata *md) 23252 { 23253 23254 bool status = false; 23255 psS64 corr_id = psMetadataLookupS64(&status, md, "corr_id"); 23256 if (!status) { 23257 psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item corr_id"); 23258 return false; 23259 } 23260 psS64 chip_id = psMetadataLookupS64(&status, md, "chip_id"); 23261 if (!status) { 23262 psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item chip_id"); 23263 return false; 23264 } 23265 char* state = psMetadataLookupPtr(&status, md, "state"); 23266 if (!status) { 23267 psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item state"); 23268 return false; 23269 } 23270 23271 return flatcorrExpRowAlloc(corr_id, chip_id, state); 23272 } 23273 psArray *flatcorrExpSelectRowObjects(psDB *dbh, const psMetadata *where, unsigned long long limit) 23274 { 23275 psArray *rowSet; 23276 psArray *returnSet; 23277 psU64 i; 23278 23279 rowSet = psDBSelectRows(dbh, FLATCORREXP_TABLE_NAME, where, limit); 23280 if (!rowSet) { 23281 return NULL; 23282 } 23283 23284 // convert psMetadata rows to row objects 23285 23286 returnSet = psArrayAllocEmpty(rowSet->n); 23287 23288 for (i = 0; i < rowSet->n; i++) { 23289 flatcorrExpRow *object = flatcorrExpObjectFromMetadata(rowSet->data[i]); 23290 psArrayAdd(returnSet, 0, object); 23291 psFree(object); 23292 } 23293 23294 psFree(rowSet); 23295 23296 return returnSet; 23297 } 23298 bool flatcorrExpDeleteObject(psDB *dbh, const flatcorrExpRow *object) 23299 { 23300 psMetadata *where = flatcorrExpMetadataFromObject(object); 23301 long long count = psDBDeleteRows(dbh, FLATCORREXP_TABLE_NAME, where, 0); 23302 psFree(where); 23303 if (count < 0) { 23304 psError(PS_ERR_UNKNOWN, true, "failed to delete row from flatcorrExp"); 23305 return false; 23306 } 23307 if (count > 1) { 23308 // XXX should this be a psAbort() instead? It is possible that 23309 // having an object match multiple rows was by design. 23310 psError(PS_ERR_UNKNOWN, true, "flatcorrExpRow object matched more then one row. Check your database schema"); 23311 return false; 23312 } 23313 23314 return true; 23315 } 23316 long long flatcorrExpDeleteRowObjects(psDB *dbh, const psArray *objects, unsigned long long limit) 23317 { 23318 long long deleted = 0; 23319 23320 for (long long i = 0; i < objects->n; i++) { 23321 flatcorrExpRow *object = objects->data[i]; 23322 psMetadata *where = flatcorrExpMetadataFromObject(object); 23323 long long count = psDBDeleteRows(dbh, FLATCORREXP_TABLE_NAME, where, limit); 23324 psFree(where); 23325 if (count < 0) { 23326 psError(PS_ERR_UNKNOWN, true, "failed to delete row from flatcorrExp"); 23327 return count; 23328 } 23329 23330 deleted += count; 23331 } 23332 23333 return deleted; 23334 } 23335 bool flatcorrExpPrintObjects(FILE *stream, psArray *objects, bool mdcf) 23336 { 23337 PS_ASSERT_PTR_NON_NULL(objects, false); 23338 23339 psMetadata *output = psMetadataAlloc(); 23340 for (long i = 0; i < psArrayLength(objects); i++) { 23341 psMetadata *md = flatcorrExpMetadataFromObject(objects->data[i]); 23342 if (!psMetadataAddMetadata( 23343 output, 23344 PS_LIST_TAIL, 23345 FLATCORREXP_TABLE_NAME, 23346 PS_META_DUPLICATE_OK, 23347 NULL, 23348 md 23349 )) { 23350 psError(PS_ERR_UNKNOWN, false, "failed to add metadata"); 23351 psFree(md); 23352 psFree(output); 23353 return false; 23354 } 23355 psFree(md); 23356 } 23357 23358 if (!ippdbPrintMetadataRaw(stream, output, mdcf)) { 23359 psError(PS_ERR_UNKNOWN, false, "failed to print metadata"); 23360 psFree(output); 23361 } 23362 psFree(output); 23363 23364 return true; 23365 } 23366 bool flatcorrExpPrintObject(FILE *stream, flatcorrExpRow *object, bool mdcf) 23367 { 23368 PS_ASSERT_PTR_NON_NULL(object, false); 23369 23370 psMetadata *md = flatcorrExpMetadataFromObject(object); 23371 23372 if (!ippdbPrintMetadataRaw(stream, md, mdcf)) { 23373 psError(PS_ERR_UNKNOWN, false, "failed to print metadata"); 23374 psFree(md); 23375 } 23376 23377 psFree(md); 23378 23379 return true; 23380 } -
trunk/ippdb/src/ippdb.h
r15569 r15576 11105 11105 bool mdcf ///< format as mdconfig or simple 11106 11106 ); 11107 /** flatcorrRunRow data structure 11108 * 11109 * Structure for representing a single row of flatcorrRun table data. 11110 */ 11111 11112 typedef struct { 11113 psS64 corr_id; 11114 char *dvodb; 11115 char *state; 11116 char *workdir; 11117 char *label; 11118 } flatcorrRunRow; 11119 11120 /** Creates a new flatcorrRunRow object 11121 * 11122 * @return A new flatcorrRunRow object or NULL on failure. 11123 */ 11124 11125 flatcorrRunRow *flatcorrRunRowAlloc( 11126 psS64 corr_id, 11127 const char *dvodb, 11128 const char *state, 11129 const char *workdir, 11130 const char *label 11131 ); 11132 11133 /** Creates a new flatcorrRun table 11134 * 11135 * @return true on success 11136 */ 11137 11138 bool flatcorrRunCreateTable( 11139 psDB *dbh ///< Database handle 11140 ); 11141 11142 /** Deletes a flatcorrRun table 11143 * 11144 * @return true on success 11145 */ 11146 11147 bool flatcorrRunDropTable( 11148 psDB *dbh ///< Database handle 11149 ); 11150 11151 /** Insert a single row into a table 11152 * 11153 * This function constructs and inserts a single row based on it's parameters. 11154 * 11155 * @return true on success 11156 */ 11157 11158 bool flatcorrRunInsert( 11159 psDB *dbh, ///< Database handle 11160 psS64 corr_id, 11161 const char *dvodb, 11162 const char *state, 11163 const char *workdir, 11164 const char *label 11165 ); 11166 11167 /** Deletes up to limit rows from the database and returns the number of rows actually deleted. 11168 * 11169 * @return A The number of rows removed or a negative value on error 11170 */ 11171 11172 long long flatcorrRunDelete( 11173 psDB *dbh, ///< Database handle 11174 const psMetadata *where, ///< Row match criteria 11175 unsigned long long limit ///< Maximum number of elements to delete 11176 ); 11177 11178 /** Insert a single flatcorrRunRow object into a table 11179 * 11180 * This function constructs and inserts a single row based on it's parameters. 11181 * 11182 * @return true on success 11183 */ 11184 11185 bool flatcorrRunInsertObject( 11186 psDB *dbh, ///< Database handle 11187 flatcorrRunRow *object ///< flatcorrRunRow object 11188 ); 11189 11190 /** Insert an array of flatcorrRunRow object into a table 11191 * 11192 * This function constructs and inserts multiple rows based on it's parameters. 11193 * 11194 * @return true on success 11195 */ 11196 11197 bool flatcorrRunInsertObjects( 11198 psDB *dbh, ///< Database handle 11199 psArray *objects ///< array of flatcorrRunRow objects 11200 ); 11201 11202 /** Insert data from a binary FITS table flatcorrRunRow into the database 11203 * 11204 * This function expects a psFits object with a FITS table as the first 11205 * extension. The table must have at least one row of data in it, that is of 11206 * the appropriate format (number of columns and their type). All other 11207 * extensions are ignored. 11208 * 11209 * @return true on success 11210 */ 11211 11212 bool flatcorrRunInsertFits( 11213 psDB *dbh, ///< Database handle 11214 const psFits *fits ///< psFits object 11215 ); 11216 11217 /** Selects up to limit from the database and returns them in a binary FITS table 11218 * 11219 * This function assumes an empty psFits object and will create a FITS table 11220 * as the first extension. 11221 * 11222 * See psDBSelectRows() for documentation on the format of where. 11223 * 11224 * @return true on success 11225 */ 11226 11227 bool flatcorrRunSelectRowsFits( 11228 psDB *dbh, ///< Database handle 11229 psFits *fits, ///< psFits object 11230 const psMetadata *where, ///< Row match criteria 11231 unsigned long long limit ///< Maximum number of elements to return 11232 ); 11233 11234 /** Convert a flatcorrRunRow into an equivalent psMetadata 11235 * 11236 * @return A psMetadata pointer or NULL on error 11237 */ 11238 11239 psMetadata *flatcorrRunMetadataFromObject( 11240 const flatcorrRunRow *object ///< fooRow to convert into a psMetadata 11241 ); 11242 11243 /** Convert a psMetadata into an equivalent fooRow 11244 * 11245 * @return A flatcorrRunRow pointer or NULL on error 11246 */ 11247 11248 flatcorrRunRow *flatcorrRunObjectFromMetadata( 11249 psMetadata *md ///< psMetadata to convert into a fooRow 11250 ); 11251 /** Selects up to limit rows from the database and returns as flatcorrRunRow objects in a psArray 11252 * 11253 * See psDBSelectRows() for documentation on the format of where. 11254 * 11255 * @return A psArray pointer or NULL on error 11256 */ 11257 11258 psArray *flatcorrRunSelectRowObjects( 11259 psDB *dbh, ///< Database handle 11260 const psMetadata *where, ///< Row match criteria 11261 unsigned long long limit ///< Maximum number of elements to return 11262 ); 11263 /** Deletes a row from the database coresponding to an flatcorrRun 11264 * 11265 * Note that a 'where' search psMetadata is constructed from each object and 11266 * used to find rows to delete. 11267 * 11268 * @return A The number of rows removed or a negative value on error 11269 */ 11270 11271 bool flatcorrRunDeleteObject( 11272 psDB *dbh, ///< Database handle 11273 const flatcorrRunRow *object ///< Object to delete 11274 ); 11275 /** Deletes up to limit rows from the database and returns the number of rows actually deleted. 11276 * 11277 * Note that a 'where' search psMetadata is constructed from each object and 11278 * used to find rows to delete. 11279 * 11280 * @return A The number of rows removed or a negative value on error 11281 */ 11282 11283 long long flatcorrRunDeleteRowObjects( 11284 psDB *dbh, ///< Database handle 11285 const psArray *objects, ///< Array of objects to delete 11286 unsigned long long limit ///< Maximum number of elements to delete 11287 ); 11288 /** Formats and prints an array of flatcorrRunRow objects 11289 * 11290 * When mdcf is set the formated output is in psMetadataConfig 11291 * format, otherwise it is in a simple tabular format. 11292 * 11293 * @return true on success 11294 */ 11295 11296 bool flatcorrRunPrintObjects( 11297 FILE *stream, ///< a stream 11298 psArray *objects, ///< An array of flatcorrRunRow objects 11299 bool mdcf ///< format as mdconfig or simple 11300 ); 11301 /** Formats and prints an flatcorrRunRow object 11302 * 11303 * When mdcf is set the formated output is in psMetadataConfig 11304 * format, otherwise it is in a simple tabular format. 11305 * 11306 * @return true on success 11307 */ 11308 11309 bool flatcorrRunPrintObject( 11310 FILE *stream, ///< a stream 11311 flatcorrRunRow *object, ///< an flatcorrRunRow object 11312 bool mdcf ///< format as mdconfig or simple 11313 ); 11314 /** flatcorrExpRow data structure 11315 * 11316 * Structure for representing a single row of flatcorrExp table data. 11317 */ 11318 11319 typedef struct { 11320 psS64 corr_id; 11321 psS64 chip_id; 11322 char *state; 11323 } flatcorrExpRow; 11324 11325 /** Creates a new flatcorrExpRow object 11326 * 11327 * @return A new flatcorrExpRow object or NULL on failure. 11328 */ 11329 11330 flatcorrExpRow *flatcorrExpRowAlloc( 11331 psS64 corr_id, 11332 psS64 chip_id, 11333 const char *state 11334 ); 11335 11336 /** Creates a new flatcorrExp table 11337 * 11338 * @return true on success 11339 */ 11340 11341 bool flatcorrExpCreateTable( 11342 psDB *dbh ///< Database handle 11343 ); 11344 11345 /** Deletes a flatcorrExp table 11346 * 11347 * @return true on success 11348 */ 11349 11350 bool flatcorrExpDropTable( 11351 psDB *dbh ///< Database handle 11352 ); 11353 11354 /** Insert a single row into a table 11355 * 11356 * This function constructs and inserts a single row based on it's parameters. 11357 * 11358 * @return true on success 11359 */ 11360 11361 bool flatcorrExpInsert( 11362 psDB *dbh, ///< Database handle 11363 psS64 corr_id, 11364 psS64 chip_id, 11365 const char *state 11366 ); 11367 11368 /** Deletes up to limit rows from the database and returns the number of rows actually deleted. 11369 * 11370 * @return A The number of rows removed or a negative value on error 11371 */ 11372 11373 long long flatcorrExpDelete( 11374 psDB *dbh, ///< Database handle 11375 const psMetadata *where, ///< Row match criteria 11376 unsigned long long limit ///< Maximum number of elements to delete 11377 ); 11378 11379 /** Insert a single flatcorrExpRow object into a table 11380 * 11381 * This function constructs and inserts a single row based on it's parameters. 11382 * 11383 * @return true on success 11384 */ 11385 11386 bool flatcorrExpInsertObject( 11387 psDB *dbh, ///< Database handle 11388 flatcorrExpRow *object ///< flatcorrExpRow object 11389 ); 11390 11391 /** Insert an array of flatcorrExpRow object into a table 11392 * 11393 * This function constructs and inserts multiple rows based on it's parameters. 11394 * 11395 * @return true on success 11396 */ 11397 11398 bool flatcorrExpInsertObjects( 11399 psDB *dbh, ///< Database handle 11400 psArray *objects ///< array of flatcorrExpRow objects 11401 ); 11402 11403 /** Insert data from a binary FITS table flatcorrExpRow into the database 11404 * 11405 * This function expects a psFits object with a FITS table as the first 11406 * extension. The table must have at least one row of data in it, that is of 11407 * the appropriate format (number of columns and their type). All other 11408 * extensions are ignored. 11409 * 11410 * @return true on success 11411 */ 11412 11413 bool flatcorrExpInsertFits( 11414 psDB *dbh, ///< Database handle 11415 const psFits *fits ///< psFits object 11416 ); 11417 11418 /** Selects up to limit from the database and returns them in a binary FITS table 11419 * 11420 * This function assumes an empty psFits object and will create a FITS table 11421 * as the first extension. 11422 * 11423 * See psDBSelectRows() for documentation on the format of where. 11424 * 11425 * @return true on success 11426 */ 11427 11428 bool flatcorrExpSelectRowsFits( 11429 psDB *dbh, ///< Database handle 11430 psFits *fits, ///< psFits object 11431 const psMetadata *where, ///< Row match criteria 11432 unsigned long long limit ///< Maximum number of elements to return 11433 ); 11434 11435 /** Convert a flatcorrExpRow into an equivalent psMetadata 11436 * 11437 * @return A psMetadata pointer or NULL on error 11438 */ 11439 11440 psMetadata *flatcorrExpMetadataFromObject( 11441 const flatcorrExpRow *object ///< fooRow to convert into a psMetadata 11442 ); 11443 11444 /** Convert a psMetadata into an equivalent fooRow 11445 * 11446 * @return A flatcorrExpRow pointer or NULL on error 11447 */ 11448 11449 flatcorrExpRow *flatcorrExpObjectFromMetadata( 11450 psMetadata *md ///< psMetadata to convert into a fooRow 11451 ); 11452 /** Selects up to limit rows from the database and returns as flatcorrExpRow objects in a psArray 11453 * 11454 * See psDBSelectRows() for documentation on the format of where. 11455 * 11456 * @return A psArray pointer or NULL on error 11457 */ 11458 11459 psArray *flatcorrExpSelectRowObjects( 11460 psDB *dbh, ///< Database handle 11461 const psMetadata *where, ///< Row match criteria 11462 unsigned long long limit ///< Maximum number of elements to return 11463 ); 11464 /** Deletes a row from the database coresponding to an flatcorrExp 11465 * 11466 * Note that a 'where' search psMetadata is constructed from each object and 11467 * used to find rows to delete. 11468 * 11469 * @return A The number of rows removed or a negative value on error 11470 */ 11471 11472 bool flatcorrExpDeleteObject( 11473 psDB *dbh, ///< Database handle 11474 const flatcorrExpRow *object ///< Object to delete 11475 ); 11476 /** Deletes up to limit rows from the database and returns the number of rows actually deleted. 11477 * 11478 * Note that a 'where' search psMetadata is constructed from each object and 11479 * used to find rows to delete. 11480 * 11481 * @return A The number of rows removed or a negative value on error 11482 */ 11483 11484 long long flatcorrExpDeleteRowObjects( 11485 psDB *dbh, ///< Database handle 11486 const psArray *objects, ///< Array of objects to delete 11487 unsigned long long limit ///< Maximum number of elements to delete 11488 ); 11489 /** Formats and prints an array of flatcorrExpRow objects 11490 * 11491 * When mdcf is set the formated output is in psMetadataConfig 11492 * format, otherwise it is in a simple tabular format. 11493 * 11494 * @return true on success 11495 */ 11496 11497 bool flatcorrExpPrintObjects( 11498 FILE *stream, ///< a stream 11499 psArray *objects, ///< An array of flatcorrExpRow objects 11500 bool mdcf ///< format as mdconfig or simple 11501 ); 11502 /** Formats and prints an flatcorrExpRow object 11503 * 11504 * When mdcf is set the formated output is in psMetadataConfig 11505 * format, otherwise it is in a simple tabular format. 11506 * 11507 * @return true on success 11508 */ 11509 11510 bool flatcorrExpPrintObject( 11511 FILE *stream, ///< a stream 11512 flatcorrExpRow *object, ///< an flatcorrExpRow object 11513 bool mdcf ///< format as mdconfig or simple 11514 ); 11107 11515 11108 11516 /// @} … … 11112 11520 #endif 11113 11521 11114 #endif // CALRUN_DB_H11522 #endif // FLATCORREXP_DB_H
Note:
See TracChangeset
for help on using the changeset viewer.
