Changeset 7509 for trunk/ippTools/src/dettool.c
- Timestamp:
- Jun 9, 2006, 6:50:21 PM (20 years ago)
- File:
-
- 1 edited
-
trunk/ippTools/src/dettool.c (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ippTools/src/dettool.c
r7471 r7509 34 34 static detInputExpRow *rawDetrenTodetInputExpRow(rawDetrendExpRow *rawExp, psS32 det_id, psS32 iteration); 35 35 static psArray *searchRawImfiles(pxConfig *config, psMetadata *where); 36 static psS32 incrementIteration(pxConfig *config, const char *det_id); 36 37 37 38 # define MODECASE(caseName, func) \ … … 95 96 return false; 96 97 } 97 98 // start a transaction so we don't end up with orphaned det_ids99 if (!psDBTransaction(config->dbh)) {100 psError(PS_ERR_UNKNOWN, false, "database error");101 return false;102 }103 104 // grab the 'position' column's value as the new det_id105 // the first iteration is always 0106 detRunInsert(config->dbh, det_type);107 long det_id = psDBLastInsertID(config->dbh);108 98 109 99 // we have to support multipe exp_ids … … 128 118 psError(PS_ERR_UNKNOWN, true, 129 119 "at least one -exp_id is required"); 130 // rollback131 if (!psDBRollback(config->dbh)) {132 psError(PS_ERR_UNKNOWN, false, "database error");133 }134 120 psFree(where); 135 121 return false; 136 122 } 137 123 138 if (!psMetadataAddStr(where, PS_LIST_TAIL, "exp_id", 139 PS_META_DUPLICATE_OK, "==",exp_id)) {124 if (!psMetadataAddStr(where, PS_LIST_TAIL, "exp_id", 0, "==", 125 exp_id)) { 140 126 psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id"); 141 // rollback142 if (!psDBRollback(config->dbh)) {143 psError(PS_ERR_UNKNOWN, false, "database error");144 }145 127 psFree(iter); 146 128 psFree(where); … … 150 132 psFree(iter); 151 133 } else { 152 // rollback153 if (!psDBRollback(config->dbh)) {154 psError(PS_ERR_UNKNOWN, false, "database error");155 }156 134 psAbort(config->argv[0], 157 135 "-exp_id was not parsed correctly (this should not happen"); … … 168 146 if (!detrendExps) { 169 147 psError(PS_ERR_UNKNOWN, false, "no rawDetrendExp rows found"); 170 // rollback 171 if (!psDBRollback(config->dbh)) { 172 psError(PS_ERR_UNKNOWN, false, "database error"); 173 } 174 return false; 175 } 176 177 if (item->type == PS_DATA_METADATA_MULTI) { 178 // we should have one rawDetrendExp row be exp_id specified 179 if (psListLength(item->data.list) != psArrayLength(detrendExps)) { 180 // rollback 181 if (!psDBRollback(config->dbh)) { 182 psError(PS_ERR_UNKNOWN, false, "database error"); 183 } 184 psAbort(config->argv[0], 185 "an -exp_id matched more then one rawDetrendExp (this should not happen"); 186 187 } 188 } else { 189 // we should have matched only one exp_id 190 if (psArrayLength(detrendExps) != 1) { 191 // rollback 192 if (!psDBRollback(config->dbh)) { 193 psError(PS_ERR_UNKNOWN, false, "database error"); 194 } 195 psAbort(config->argv[0], 196 "-exp_id matched more then one rawDetrendExp (this should not happen"); 197 } 198 } 148 return false; 149 } 150 151 // we should have one rawDetrendExp row be exp_id specified 152 if (psListLength(item->data.list) != psArrayLength(detrendExps)) { 153 psAbort(config->argv[0], 154 "an -exp_id matched more then one rawDetrendExp (this should not happen"); 155 156 } 157 158 // start a transaction so we don't end up with orphaned det_ids 159 if (!psDBTransaction(config->dbh)) { 160 psError(PS_ERR_UNKNOWN, false, "database error"); 161 return false; 162 } 163 164 // grab the 'position' column's value as the new det_id 165 // the first iteration is always 0 166 detRunInsert(config->dbh, det_type, 0); 167 long det_id = psDBLastInsertID(config->dbh); 199 168 200 169 // create new detInputExp row(s) from the rawDetrendExp row(s) … … 1809 1778 return false; 1810 1779 } 1780 1811 1781 psList *exp_id_list = item->data.list; 1812 1782 psMetadata *where = psMetadataAlloc(); 1813 1783 // make sure that -exp_id was parsed correctly 1814 1784 // XXX this can be removed someday 1815 1785 if (item->type == PS_DATA_METADATA_MULTI) { 1816 psListIterator *iter = psListIteratorAlloc( exp_id_list, 0, false);1786 psListIterator *iter = psListIteratorAlloc(item->data.list, 0, false); 1817 1787 psMetadataItem *mItem = NULL; 1818 1788 while ((mItem = psListGetAndIncrement(iter))) { 1819 1789 psString exp_id = mItem->data.V; 1820 1790 // if exp_id is NULL then it means that -exp_id has not been 1821 // specified or something really bad happened1791 // specified 1822 1792 if (!exp_id) { 1823 1793 psError(PS_ERR_UNKNOWN, true, 1824 1794 "at least one -exp_id is required"); 1795 psFree(where); 1796 return false; 1797 } 1798 1799 if (!psMetadataAddStr(where, PS_LIST_TAIL, "exp_id", 1800 PS_META_DUPLICATE_OK, "==", exp_id)) { 1801 psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id"); 1802 psFree(iter); 1803 psFree(where); 1825 1804 return false; 1826 1805 } … … 1836 1815 1837 1816 // add the det_id & iteration == 0 to the where claus 1838 psMetadata *where = psMetadataAlloc();1839 1817 if (!psMetadataAddS32(where, PS_LIST_TAIL, "det_id", 0, "==", 1840 1818 (psS32)atol(det_id))) { … … 1853 1831 if (!detrendExps) { 1854 1832 psError(PS_ERR_UNKNOWN, false, "no rawDetrendExp rows found"); 1833 psFree(where); 1855 1834 return false; 1856 1835 } … … 1861 1840 psHashAdd(valid_exp_ids, 1862 1841 ((detInputExpRow *)detrendExps->data[i])->exp_id, 1863 ((detInputExpRow *)detrendExps->data[i])->exp_id1842 detrendExps->data[i] 1864 1843 ); 1865 1844 } 1866 1845 1846 // start a transaction so we don't end up with an incremented iteration 1847 // count but no detInputExps 1848 if (!psDBTransaction(config->dbh)) { 1849 psError(PS_ERR_UNKNOWN, false, "database error"); 1850 psFree(valid_exp_ids); 1851 return false; 1852 } 1853 1854 // up the detRuns iteration count 1855 psS32 newIteration = incrementIteration(config, det_id); 1856 if (!newIteration) { 1857 // rollback 1858 if (!psDBRollback(config->dbh)) { 1859 psError(PS_ERR_UNKNOWN, false, "database error"); 1860 } 1861 psFree(valid_exp_ids); 1862 return false; 1863 } 1864 1865 // check exp_ids and build up an array of new detInputExp rows at the same 1866 // time 1867 1867 psListIterator *iter = psListIteratorAlloc(exp_id_list, 0, false); 1868 1868 psMetadataItem *mItem = NULL; 1869 psArray *newInputExps = psArrayAlloc(psListLength(exp_id_list)); 1869 1870 while ((mItem = psListGetAndIncrement(iter))) { 1870 if (!psHashLookup(valid_exp_ids, (char *)mItem->data.V)) { 1871 detInputExpRow *inputExp = psHashLookup(valid_exp_ids, 1872 (char *)mItem->data.V); 1873 if (!inputExp) { 1874 // rollback 1875 if (!psDBRollback(config->dbh)) { 1876 psError(PS_ERR_UNKNOWN, false, "database error"); 1877 } 1871 1878 // invalid exp_id 1872 1879 psError(PS_ERR_UNKNOWN, false, "exp_id %s is invalid for det_id %s", 1873 1880 (char *)mItem->data.V, det_id); 1881 psFree(iter); 1874 1882 psFree(valid_exp_ids); 1875 psFree(iter);1876 1883 return false; 1877 1884 } 1878 } 1885 psArrayAdd(newInputExps, 0, detInputExpRowAlloc( 1886 (psS32)atol(det_id), 1887 newIteration, 1888 inputExp->exp_id, 1889 inputExp->camera, 1890 inputExp->telescope, 1891 inputExp->exp_type, 1892 inputExp->imfiles, 1893 inputExp->filter, 1894 inputExp->stats 1895 )); 1896 } 1897 psFree(iter); 1879 1898 psFree(valid_exp_ids); 1880 psFree(iter); 1881 1882 psFree(exp_id_list); 1899 1900 for (long i = 0; i < psArrayLength(newInputExps); i++) { 1901 if (!detInputExpInsertObject(config->dbh, newInputExps->data[i])) { 1902 psError(PS_ERR_UNKNOWN, false, "database error"); 1903 // rollback 1904 if (!psDBRollback(config->dbh)) { 1905 psError(PS_ERR_UNKNOWN, false, "database error"); 1906 } 1907 return false; 1908 } 1909 } 1910 1911 // point of no return for det_id creation 1912 if (!psDBCommit(config->dbh)) { 1913 psError(PS_ERR_UNKNOWN, false, "database error"); 1914 return false; 1915 } 1883 1916 1884 1917 return true; 1885 1918 } 1919 1920 static psS32 incrementIteration(pxConfig *config, const char *det_id) 1921 { 1922 // this function returns zero on error 1923 PS_ASSERT_PTR_NON_NULL(config, 0); 1924 PS_ASSERT_PTR_NON_NULL(det_id, 0); 1925 1926 char *query = "UPDATE detRun SET iteration = iteration + 1 WHERE position = '%s'"; 1927 if (!p_psDBRunQuery(config->dbh, query, det_id)) { 1928 psError(PS_ERR_UNKNOWN, false, 1929 "failed to increment iteration for det_id %s", det_id); 1930 return 0; 1931 } 1932 1933 psMetadata *where = psMetadataAlloc(); 1934 if (!psMetadataAddS32(where, PS_LIST_TAIL, "position", 0, "==", 1935 (psS32)atol(det_id))) { 1936 psError(PS_ERR_UNKNOWN, false, "failed to add item det_id"); 1937 psFree(where); 1938 return 0; 1939 } 1940 1941 psArray *detRuns = detRunSelectRowObjects(config->dbh, where, 0); 1942 psFree(where); 1943 if (!detRuns) { 1944 psError(PS_ERR_UNKNOWN, false, "no detRun rows found"); 1945 return 0; 1946 } 1947 1948 // sanity check the database 1949 if (psArrayLength(detRuns) != 1) { 1950 // this should no happen 1951 psAbort(config->argv[0], 1952 "database query return too many rows (this should not happen"); 1953 } 1954 1955 psS32 newIteration = ((detRunRow *)detRuns->data[0])->iteration; 1956 psFree(detRuns); 1957 1958 return newIteration; 1959 }
Note:
See TracChangeset
for help on using the changeset viewer.
