comparison Framework/Plugins/IndexBackend.cpp @ 263:29d2b76516f6

fix mysql and postgresql builds
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 20 Apr 2021 15:08:09 +0200
parents b0c65094b299
children cd73e34d5411
comparison
equal deleted inserted replaced
262:b0c65094b299 263:29d2b76516f6
56 56
57 return s; 57 return s;
58 } 58 }
59 59
60 60
61 static int64_t ReadInteger64(const DatabaseManager::StatementBase& statement,
62 size_t field)
63 {
64 if (statement.IsDone())
65 {
66 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
67 }
68
69 const IValue& value = statement.GetResultField(field);
70
71 switch (value.GetType())
72 {
73 case ValueType_Integer64:
74 return dynamic_cast<const Integer64Value&>(value).GetValue();
75
76 default:
77 //LOG(ERROR) << value.Format();
78 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
79 }
80 }
81
82
83 static int32_t ReadInteger32(const DatabaseManager::StatementBase& statement,
84 size_t field)
85 {
86 if (statement.IsDone())
87 {
88 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
89 }
90
91 int64_t value = ReadInteger64(statement, field);
92
93 if (value != static_cast<int64_t>(static_cast<int32_t>(value)))
94 {
95 LOG(ERROR) << "Integer overflow";
96 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
97 }
98 else
99 {
100 return static_cast<int32_t>(value);
101 }
102 }
103
104
105 static std::string ReadString(const DatabaseManager::StatementBase& statement,
106 size_t field)
107 {
108 const IValue& value = statement.GetResultField(field);
109
110 switch (value.GetType())
111 {
112 case ValueType_BinaryString:
113 return dynamic_cast<const BinaryStringValue&>(value).GetContent();
114
115 case ValueType_Utf8String:
116 return dynamic_cast<const Utf8StringValue&>(value).GetContent();
117
118 default:
119 //LOG(ERROR) << value.Format();
120 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
121 }
122 }
123
124
125 template <typename T> 61 template <typename T>
126 static void ReadListOfIntegers(std::list<T>& target, 62 static void ReadListOfIntegers(std::list<T>& target,
127 DatabaseManager::CachedStatement& statement, 63 DatabaseManager::CachedStatement& statement,
128 const Dictionary& args) 64 const Dictionary& args)
129 { 65 {
140 76
141 statement.SetResultFieldType(0, ValueType_Integer64); 77 statement.SetResultFieldType(0, ValueType_Integer64);
142 78
143 while (!statement.IsDone()) 79 while (!statement.IsDone())
144 { 80 {
145 target.push_back(static_cast<T>(ReadInteger64(statement, 0))); 81 target.push_back(static_cast<T>(statement.ReadInteger64(0)));
146 statement.Next(); 82 statement.Next();
147 } 83 }
148 } 84 }
149 } 85 }
150 86
164 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); 100 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
165 } 101 }
166 102
167 while (!statement.IsDone()) 103 while (!statement.IsDone())
168 { 104 {
169 target.push_back(ReadString(statement, 0)); 105 target.push_back(statement.ReadString(0));
170 statement.Next(); 106 statement.Next();
171 } 107 }
172 } 108 }
173 } 109 }
174 110
186 122
187 while (count < maxResults && 123 while (count < maxResults &&
188 !statement.IsDone()) 124 !statement.IsDone())
189 { 125 {
190 output.AnswerChange( 126 output.AnswerChange(
191 ReadInteger64(statement, 0), 127 statement.ReadInteger64(0),
192 ReadInteger32(statement, 1), 128 statement.ReadInteger32(1),
193 static_cast<OrthancPluginResourceType>(ReadInteger32(statement, 3)), 129 static_cast<OrthancPluginResourceType>(statement.ReadInteger32(3)),
194 GetPublicId(manager, ReadInteger64(statement, 2)), 130 GetPublicId(manager, statement.ReadInteger64(2)),
195 ReadString(statement, 4)); 131 statement.ReadString(4));
196 132
197 statement.Next(); 133 statement.Next();
198 count++; 134 count++;
199 } 135 }
200 136
214 uint32_t count = 0; 150 uint32_t count = 0;
215 151
216 while (count < maxResults && 152 while (count < maxResults &&
217 !statement.IsDone()) 153 !statement.IsDone())
218 { 154 {
219 int64_t seq = ReadInteger64(statement, 0); 155 int64_t seq = statement.ReadInteger64(0);
220 OrthancPluginResourceType resourceType = 156 OrthancPluginResourceType resourceType =
221 static_cast<OrthancPluginResourceType>(ReadInteger32(statement, 1)); 157 static_cast<OrthancPluginResourceType>(statement.ReadInteger32(1));
222 std::string publicId = ReadString(statement, 2); 158 std::string publicId = statement.ReadString(2);
223 159
224 output.AnswerExportedResource(seq, 160 output.AnswerExportedResource(seq,
225 resourceType, 161 resourceType,
226 publicId, 162 publicId,
227 ReadString(statement, 3), // modality 163 statement.ReadString(3), // modality
228 ReadString(statement, 8), // date 164 statement.ReadString(8), // date
229 ReadString(statement, 4), // patient ID 165 statement.ReadString(4), // patient ID
230 ReadString(statement, 5), // study instance UID 166 statement.ReadString(5), // study instance UID
231 ReadString(statement, 6), // series instance UID 167 statement.ReadString(6), // series instance UID
232 ReadString(statement, 7)); // sop instance UID 168 statement.ReadString(7)); // sop instance UID
233 169
234 statement.Next(); 170 statement.Next();
235 count++; 171 count++;
236 } 172 }
237 173
270 statement.SetReadOnly(true); 206 statement.SetReadOnly(true);
271 statement.Execute(); 207 statement.Execute();
272 208
273 while (!statement.IsDone()) 209 while (!statement.IsDone())
274 { 210 {
275 std::string a = ReadString(statement, 0); 211 std::string a = statement.ReadString(0);
276 std::string b = ReadString(statement, 5); 212 std::string b = statement.ReadString(5);
277 std::string c = ReadString(statement, 6); 213 std::string c = statement.ReadString(6);
278 214
279 output.SignalDeletedAttachment(a.c_str(), 215 output.SignalDeletedAttachment(a.c_str(),
280 ReadInteger32(statement, 1), 216 statement.ReadInteger32(1),
281 ReadInteger64(statement, 3), 217 statement.ReadInteger64(3),
282 b.c_str(), 218 b.c_str(),
283 ReadInteger32(statement, 4), 219 statement.ReadInteger32(4),
284 ReadInteger64(statement, 2), 220 statement.ReadInteger64(2),
285 c.c_str()); 221 c.c_str());
286 222
287 statement.Next(); 223 statement.Next();
288 } 224 }
289 } 225 }
300 statement.Execute(); 236 statement.Execute();
301 237
302 while (!statement.IsDone()) 238 while (!statement.IsDone())
303 { 239 {
304 output.SignalDeletedResource( 240 output.SignalDeletedResource(
305 ReadString(statement, 1), 241 statement.ReadString(1),
306 static_cast<OrthancPluginResourceType>(ReadInteger32(statement, 0))); 242 static_cast<OrthancPluginResourceType>(statement.ReadInteger32(0)));
307 243
308 statement.Next(); 244 statement.Next();
309 } 245 }
310 } 246 }
311 247
532 statement.Execute(); 468 statement.Execute();
533 469
534 if (!statement.IsDone()) 470 if (!statement.IsDone())
535 { 471 {
536 output.SignalRemainingAncestor( 472 output.SignalRemainingAncestor(
537 ReadString(statement, 1), 473 statement.ReadString(1),
538 static_cast<OrthancPluginResourceType>(ReadInteger32(statement, 0))); 474 static_cast<OrthancPluginResourceType>(statement.ReadInteger32(0)));
539 475
540 // There is at most 1 remaining ancestor 476 // There is at most 1 remaining ancestor
541 assert((statement.Next(), statement.IsDone())); 477 assert((statement.Next(), statement.IsDone()));
542 } 478 }
543 } 479 }
744 680
745 statement.Execute(args); 681 statement.Execute(args);
746 682
747 while (!statement.IsDone()) 683 while (!statement.IsDone())
748 { 684 {
749 output.AnswerDicomTag(static_cast<uint16_t>(ReadInteger64(statement, 1)), 685 output.AnswerDicomTag(static_cast<uint16_t>(statement.ReadInteger64(1)),
750 static_cast<uint16_t>(ReadInteger64(statement, 2)), 686 static_cast<uint16_t>(statement.ReadInteger64(2)),
751 ReadString(statement, 3)); 687 statement.ReadString(3));
752 statement.Next(); 688 statement.Next();
753 } 689 }
754 } 690 }
755 691
756 692
773 { 709 {
774 throw Orthanc::OrthancException(Orthanc::ErrorCode_UnknownResource); 710 throw Orthanc::OrthancException(Orthanc::ErrorCode_UnknownResource);
775 } 711 }
776 else 712 else
777 { 713 {
778 return ReadString(statement, 0); 714 return statement.ReadString(0);
779 } 715 }
780 } 716 }
781 717
782 718
783 uint64_t IndexBackend::GetResourcesCount(DatabaseManager& manager, 719 uint64_t IndexBackend::GetResourcesCount(DatabaseManager& manager,
815 Dictionary args; 751 Dictionary args;
816 args.SetIntegerValue("type", resourceType); 752 args.SetIntegerValue("type", resourceType);
817 753
818 statement->Execute(args); 754 statement->Execute(args);
819 755
820 return static_cast<uint64_t>(ReadInteger64(*statement, 0)); 756 return static_cast<uint64_t>(statement->ReadInteger64(0));
821 } 757 }
822 758
823 759
824 OrthancPluginResourceType IndexBackend::GetResourceType(DatabaseManager& manager, 760 OrthancPluginResourceType IndexBackend::GetResourceType(DatabaseManager& manager,
825 int64_t resourceId) 761 int64_t resourceId)
840 { 776 {
841 throw Orthanc::OrthancException(Orthanc::ErrorCode_UnknownResource); 777 throw Orthanc::OrthancException(Orthanc::ErrorCode_UnknownResource);
842 } 778 }
843 else 779 else
844 { 780 {
845 return static_cast<OrthancPluginResourceType>(ReadInteger32(statement, 0)); 781 return static_cast<OrthancPluginResourceType>(statement.ReadInteger32(0));
846 } 782 }
847 } 783 }
848 784
849 785
850 uint64_t IndexBackend::GetTotalCompressedSize(DatabaseManager& manager) 786 uint64_t IndexBackend::GetTotalCompressedSize(DatabaseManager& manager)
878 } 814 }
879 815
880 statement->SetReadOnly(true); 816 statement->SetReadOnly(true);
881 statement->Execute(); 817 statement->Execute();
882 818
883 return static_cast<uint64_t>(ReadInteger64(*statement, 0)); 819 return static_cast<uint64_t>(statement->ReadInteger64(0));
884 } 820 }
885 821
886 822
887 uint64_t IndexBackend::GetTotalUncompressedSize(DatabaseManager& manager) 823 uint64_t IndexBackend::GetTotalUncompressedSize(DatabaseManager& manager)
888 { 824 {
915 } 851 }
916 852
917 statement->SetReadOnly(true); 853 statement->SetReadOnly(true);
918 statement->Execute(); 854 statement->Execute();
919 855
920 return static_cast<uint64_t>(ReadInteger64(*statement, 0)); 856 return static_cast<uint64_t>(statement->ReadInteger64(0));
921 } 857 }
922 858
923 859
924 bool IndexBackend::IsExistingResource(DatabaseManager& manager, 860 bool IndexBackend::IsExistingResource(DatabaseManager& manager,
925 int64_t internalId) 861 int64_t internalId)
1070 { 1006 {
1071 return false; 1007 return false;
1072 } 1008 }
1073 else 1009 else
1074 { 1010 {
1075 output.AnswerAttachment(ReadString(statement, 0), 1011 output.AnswerAttachment(statement.ReadString(0),
1076 contentType, 1012 contentType,
1077 ReadInteger64(statement, 1), 1013 statement.ReadInteger64(1),
1078 ReadString(statement, 4), 1014 statement.ReadString(4),
1079 ReadInteger32(statement, 2), 1015 statement.ReadInteger32(2),
1080 ReadInteger64(statement, 3), 1016 statement.ReadInteger64(3),
1081 ReadString(statement, 5)); 1017 statement.ReadString(5));
1082 return true; 1018 return true;
1083 } 1019 }
1084 } 1020 }
1085 1021
1086 1022
1099 "SELECT uuid, uncompressedSize, compressionType, compressedSize, uncompressedHash, " 1035 "SELECT uuid, uncompressedSize, compressionType, compressedSize, uncompressedHash, "
1100 "compressedHash, revision FROM AttachedFiles WHERE id=${id} AND fileType=${type}"); 1036 "compressedHash, revision FROM AttachedFiles WHERE id=${id} AND fileType=${type}");
1101 1037
1102 if (ExecuteLookupAttachment(statement, output, id, contentType)) 1038 if (ExecuteLookupAttachment(statement, output, id, contentType))
1103 { 1039 {
1104 revision = ReadInteger64(statement, 6); 1040 revision = statement.ReadInteger64(6);
1105 return true; 1041 return true;
1106 } 1042 }
1107 else 1043 else
1108 { 1044 {
1109 return false; 1045 return false;
1268 statement->Execute(args); 1204 statement->Execute(args);
1269 1205
1270 target.clear(); 1206 target.clear();
1271 while (!statement->IsDone()) 1207 while (!statement->IsDone())
1272 { 1208 {
1273 target.push_back(ReadInteger64(*statement, 0)); 1209 target.push_back(statement->ReadInteger64(0));
1274 statement->Next(); 1210 statement->Next();
1275 } 1211 }
1276 } 1212 }
1277 1213
1278 1214
1307 statement.Execute(args); 1243 statement.Execute(args);
1308 1244
1309 target.clear(); 1245 target.clear();
1310 while (!statement.IsDone()) 1246 while (!statement.IsDone())
1311 { 1247 {
1312 target.push_back(ReadInteger64(statement, 0)); 1248 target.push_back(statement.ReadInteger64(0));
1313 statement.Next(); 1249 statement.Next();
1314 } 1250 }
1315 } 1251 }
1316 1252
1317 1253
1357 { 1293 {
1358 return false; 1294 return false;
1359 } 1295 }
1360 else 1296 else
1361 { 1297 {
1362 target = ReadString(*statement, 0); 1298 target = statement->ReadString(0);
1363 1299
1364 if (manager.GetDialect() == Dialect_SQLite) 1300 if (manager.GetDialect() == Dialect_SQLite)
1365 { 1301 {
1366 revision = ReadInteger64(*statement, 1); 1302 revision = statement->ReadInteger64(1);
1367 } 1303 }
1368 else 1304 else
1369 { 1305 {
1370 revision = 0; // TODO - REVISIONS 1306 revision = 0; // TODO - REVISIONS
1371 } 1307 }
1396 { 1332 {
1397 return false; 1333 return false;
1398 } 1334 }
1399 else 1335 else
1400 { 1336 {
1401 parentId = ReadInteger64(statement, 0); 1337 parentId = statement.ReadInteger64(0);
1402 return true; 1338 return true;
1403 } 1339 }
1404 } 1340 }
1405 1341
1406 1342
1425 { 1361 {
1426 return false; 1362 return false;
1427 } 1363 }
1428 else 1364 else
1429 { 1365 {
1430 id = ReadInteger64(statement, 0); 1366 id = statement.ReadInteger64(0);
1431 type = static_cast<OrthancPluginResourceType>(ReadInteger32(statement, 1)); 1367 type = static_cast<OrthancPluginResourceType>(statement.ReadInteger32(1));
1432 return true; 1368 return true;
1433 } 1369 }
1434 } 1370 }
1435 1371
1436 1372
1448 { 1384 {
1449 return false; 1385 return false;
1450 } 1386 }
1451 else 1387 else
1452 { 1388 {
1453 internalId = ReadInteger64(statement, 0); 1389 internalId = statement.ReadInteger64(0);
1454 return true; 1390 return true;
1455 } 1391 }
1456 } 1392 }
1457 1393
1458 1394
1477 { 1413 {
1478 return false; 1414 return false;
1479 } 1415 }
1480 else 1416 else
1481 { 1417 {
1482 internalId = ReadInteger64(statement, 0); 1418 internalId = statement.ReadInteger64(0);
1483 return true; 1419 return true;
1484 } 1420 }
1485 } 1421 }
1486 1422
1487 1423
1846 } 1782 }
1847 1783
1848 statement->SetReadOnly(true); 1784 statement->SetReadOnly(true);
1849 statement->Execute(); 1785 statement->Execute();
1850 1786
1851 return static_cast<uint64_t>(ReadInteger64(*statement, 0)); 1787 return static_cast<uint64_t>(statement->ReadInteger64(0));
1852 } 1788 }
1853 1789
1854 1790
1855 // For unit testing only! 1791 // For unit testing only!
1856 uint64_t IndexBackend::GetUnprotectedPatientsCount(DatabaseManager& manager) 1792 uint64_t IndexBackend::GetUnprotectedPatientsCount(DatabaseManager& manager)
1882 } 1818 }
1883 1819
1884 statement->SetReadOnly(true); 1820 statement->SetReadOnly(true);
1885 statement->Execute(); 1821 statement->Execute();
1886 1822
1887 return static_cast<uint64_t>(ReadInteger64(*statement, 0)); 1823 return static_cast<uint64_t>(statement->ReadInteger64(0));
1888 } 1824 }
1889 1825
1890 1826
1891 // For unit testing only! 1827 // For unit testing only!
1892 bool IndexBackend::GetParentPublicId(std::string& target, 1828 bool IndexBackend::GetParentPublicId(std::string& target,
1910 { 1846 {
1911 return false; 1847 return false;
1912 } 1848 }
1913 else 1849 else
1914 { 1850 {
1915 target = ReadString(statement, 0); 1851 target = statement.ReadString(0);
1916 return true; 1852 return true;
1917 } 1853 }
1918 } 1854 }
1919 1855
1920 1856
2063 1999
2064 while (!statement.IsDone()) 2000 while (!statement.IsDone())
2065 { 2001 {
2066 if (requestSomeInstance) 2002 if (requestSomeInstance)
2067 { 2003 {
2068 output.AnswerMatchingResource(ReadString(statement, 0), ReadString(statement, 1)); 2004 output.AnswerMatchingResource(statement.ReadString(0), statement.ReadString(1));
2069 } 2005 }
2070 else 2006 else
2071 { 2007 {
2072 output.AnswerMatchingResource(ReadString(statement, 0)); 2008 output.AnswerMatchingResource(statement.ReadString(0));
2073 } 2009 }
2074 2010
2075 statement.Next(); 2011 statement.Next();
2076 } 2012 }
2077 } 2013 }
2273 { 2209 {
2274 // The patient is protected, don't add it to the recycling order 2210 // The patient is protected, don't add it to the recycling order
2275 return; 2211 return;
2276 } 2212 }
2277 2213
2278 seq = ReadInteger64(statement, 0); 2214 seq = statement.ReadInteger64(0);
2279 2215
2280 statement.Next(); 2216 statement.Next();
2281 2217
2282 if (statement.IsDone()) 2218 if (statement.IsDone())
2283 { 2219 {
2354 2290
2355 statement.SetResultFieldType(0, ValueType_Integer64); 2291 statement.SetResultFieldType(0, ValueType_Integer64);
2356 statement.SetResultFieldType(1, ValueType_Integer64); 2292 statement.SetResultFieldType(1, ValueType_Integer64);
2357 statement.SetResultFieldType(2, ValueType_Utf8String); 2293 statement.SetResultFieldType(2, ValueType_Utf8String);
2358 2294
2359 id = ReadInteger64(statement, 0); 2295 id = statement.ReadInteger64(0);
2360 type = static_cast<OrthancPluginResourceType>(ReadInteger32(statement, 1)); 2296 type = static_cast<OrthancPluginResourceType>(statement.ReadInteger32(1));
2361 2297
2362 const IValue& value = statement.GetResultField(2); 2298 const IValue& value = statement.GetResultField(2);
2363 2299
2364 switch (value.GetType()) 2300 switch (value.GetType())
2365 { 2301 {
2414 statement.SetResultFieldType(0, ValueType_Integer64); 2350 statement.SetResultFieldType(0, ValueType_Integer64);
2415 statement.SetResultFieldType(1, ValueType_Utf8String); 2351 statement.SetResultFieldType(1, ValueType_Utf8String);
2416 2352
2417 while (!statement.IsDone()) 2353 while (!statement.IsDone())
2418 { 2354 {
2419 result[ReadInteger32(statement, 0)] = ReadString(statement, 1); 2355 result[statement.ReadInteger32(0)] = statement.ReadString(1);
2420 statement.Next(); 2356 statement.Next();
2421 } 2357 }
2422 } 2358 }
2423 } 2359 }
2424 # endif 2360 # endif