comparison OrthancServer/DatabaseWrapper.cpp @ 606:ce5d2040c47b find-move-scp

integration mainline -> find-move-scp
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 17 Oct 2013 14:20:13 +0200
parents c2be0a0e049e b2357f1f026f
children dbecea588ef5
comparison
equal deleted inserted replaced
567:c2be0a0e049e 606:ce5d2040c47b
241 bool DatabaseWrapper::LookupParent(int64_t& parentId, 241 bool DatabaseWrapper::LookupParent(int64_t& parentId,
242 int64_t resourceId) 242 int64_t resourceId)
243 { 243 {
244 SQLite::Statement s(db_, SQLITE_FROM_HERE, 244 SQLite::Statement s(db_, SQLITE_FROM_HERE,
245 "SELECT parentId FROM Resources WHERE internalId=?"); 245 "SELECT parentId FROM Resources WHERE internalId=?");
246 s.BindInt(0, resourceId); 246 s.BindInt64(0, resourceId);
247 247
248 if (!s.Step()) 248 if (!s.Step())
249 { 249 {
250 throw OrthancException(ErrorCode_UnknownResource); 250 throw OrthancException(ErrorCode_UnknownResource);
251 } 251 }
263 263
264 std::string DatabaseWrapper::GetPublicId(int64_t resourceId) 264 std::string DatabaseWrapper::GetPublicId(int64_t resourceId)
265 { 265 {
266 SQLite::Statement s(db_, SQLITE_FROM_HERE, 266 SQLite::Statement s(db_, SQLITE_FROM_HERE,
267 "SELECT publicId FROM Resources WHERE internalId=?"); 267 "SELECT publicId FROM Resources WHERE internalId=?");
268 s.BindInt(0, resourceId); 268 s.BindInt64(0, resourceId);
269 269
270 if (!s.Step()) 270 if (!s.Step())
271 { 271 {
272 throw OrthancException(ErrorCode_UnknownResource); 272 throw OrthancException(ErrorCode_UnknownResource);
273 } 273 }
278 278
279 ResourceType DatabaseWrapper::GetResourceType(int64_t resourceId) 279 ResourceType DatabaseWrapper::GetResourceType(int64_t resourceId)
280 { 280 {
281 SQLite::Statement s(db_, SQLITE_FROM_HERE, 281 SQLite::Statement s(db_, SQLITE_FROM_HERE,
282 "SELECT resourceType FROM Resources WHERE internalId=?"); 282 "SELECT resourceType FROM Resources WHERE internalId=?");
283 s.BindInt(0, resourceId); 283 s.BindInt64(0, resourceId);
284 284
285 if (!s.Step()) 285 if (!s.Step())
286 { 286 {
287 throw OrthancException(ErrorCode_UnknownResource); 287 throw OrthancException(ErrorCode_UnknownResource);
288 } 288 }
293 293
294 void DatabaseWrapper::AttachChild(int64_t parent, 294 void DatabaseWrapper::AttachChild(int64_t parent,
295 int64_t child) 295 int64_t child)
296 { 296 {
297 SQLite::Statement s(db_, SQLITE_FROM_HERE, "UPDATE Resources SET parentId = ? WHERE internalId = ?"); 297 SQLite::Statement s(db_, SQLITE_FROM_HERE, "UPDATE Resources SET parentId = ? WHERE internalId = ?");
298 s.BindInt(0, parent); 298 s.BindInt64(0, parent);
299 s.BindInt(1, child); 299 s.BindInt64(1, child);
300 s.Run(); 300 s.Run();
301 } 301 }
302 302
303 void DatabaseWrapper::GetChildren(Json::Value& childrenPublicIds, 303 void DatabaseWrapper::GetChildren(Json::Value& childrenPublicIds,
304 int64_t id) 304 int64_t id)
305 { 305 {
306 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT publicId FROM Resources WHERE parentId=?"); 306 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT publicId FROM Resources WHERE parentId=?");
307 s.BindInt(0, id); 307 s.BindInt64(0, id);
308 308
309 childrenPublicIds = Json::arrayValue; 309 childrenPublicIds = Json::arrayValue;
310 while (s.Step()) 310 while (s.Step())
311 { 311 {
312 childrenPublicIds.append(s.ColumnString(0)); 312 childrenPublicIds.append(s.ColumnString(0));
317 void DatabaseWrapper::DeleteResource(int64_t id) 317 void DatabaseWrapper::DeleteResource(int64_t id)
318 { 318 {
319 signalRemainingAncestor_->Reset(); 319 signalRemainingAncestor_->Reset();
320 320
321 SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM Resources WHERE internalId=?"); 321 SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM Resources WHERE internalId=?");
322 s.BindInt(0, id); 322 s.BindInt64(0, id);
323 s.Run(); 323 s.Run();
324 324
325 if (signalRemainingAncestor_->HasRemainingAncestor()) 325 if (signalRemainingAncestor_->HasRemainingAncestor())
326 { 326 {
327 listener_.SignalRemainingAncestor(signalRemainingAncestor_->GetRemainingAncestorType(), 327 listener_.SignalRemainingAncestor(signalRemainingAncestor_->GetRemainingAncestorType(),
332 void DatabaseWrapper::SetMetadata(int64_t id, 332 void DatabaseWrapper::SetMetadata(int64_t id,
333 MetadataType type, 333 MetadataType type,
334 const std::string& value) 334 const std::string& value)
335 { 335 {
336 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT OR REPLACE INTO Metadata VALUES(?, ?, ?)"); 336 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT OR REPLACE INTO Metadata VALUES(?, ?, ?)");
337 s.BindInt(0, id); 337 s.BindInt64(0, id);
338 s.BindInt(1, type); 338 s.BindInt(1, type);
339 s.BindString(2, value); 339 s.BindString(2, value);
340 s.Run(); 340 s.Run();
341 } 341 }
342 342
343 void DatabaseWrapper::DeleteMetadata(int64_t id, 343 void DatabaseWrapper::DeleteMetadata(int64_t id,
344 MetadataType type) 344 MetadataType type)
345 { 345 {
346 SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM Metadata WHERE id=? and type=?"); 346 SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM Metadata WHERE id=? and type=?");
347 s.BindInt(0, id); 347 s.BindInt64(0, id);
348 s.BindInt(1, type); 348 s.BindInt(1, type);
349 s.Run(); 349 s.Run();
350 } 350 }
351 351
352 bool DatabaseWrapper::LookupMetadata(std::string& target, 352 bool DatabaseWrapper::LookupMetadata(std::string& target,
353 int64_t id, 353 int64_t id,
354 MetadataType type) 354 MetadataType type)
355 { 355 {
356 SQLite::Statement s(db_, SQLITE_FROM_HERE, 356 SQLite::Statement s(db_, SQLITE_FROM_HERE,
357 "SELECT value FROM Metadata WHERE id=? AND type=?"); 357 "SELECT value FROM Metadata WHERE id=? AND type=?");
358 s.BindInt(0, id); 358 s.BindInt64(0, id);
359 s.BindInt(1, type); 359 s.BindInt(1, type);
360 360
361 if (!s.Step()) 361 if (!s.Step())
362 { 362 {
363 return false; 363 return false;
373 int64_t id) 373 int64_t id)
374 { 374 {
375 target.clear(); 375 target.clear();
376 376
377 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT type FROM Metadata WHERE id=?"); 377 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT type FROM Metadata WHERE id=?");
378 s.BindInt(0, id); 378 s.BindInt64(0, id);
379 379
380 while (s.Step()) 380 while (s.Step())
381 { 381 {
382 target.push_back(static_cast<MetadataType>(s.ColumnInt(0))); 382 target.push_back(static_cast<MetadataType>(s.ColumnInt(0)));
383 } 383 }
427 427
428 void DatabaseWrapper::AddAttachment(int64_t id, 428 void DatabaseWrapper::AddAttachment(int64_t id,
429 const FileInfo& attachment) 429 const FileInfo& attachment)
430 { 430 {
431 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO AttachedFiles VALUES(?, ?, ?, ?, ?, ?)"); 431 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO AttachedFiles VALUES(?, ?, ?, ?, ?, ?)");
432 s.BindInt(0, id); 432 s.BindInt64(0, id);
433 s.BindInt(1, attachment.GetContentType()); 433 s.BindInt(1, attachment.GetContentType());
434 s.BindString(2, attachment.GetUuid()); 434 s.BindString(2, attachment.GetUuid());
435 s.BindInt(3, attachment.GetCompressedSize()); 435 s.BindInt64(3, attachment.GetCompressedSize());
436 s.BindInt(4, attachment.GetUncompressedSize()); 436 s.BindInt64(4, attachment.GetUncompressedSize());
437 s.BindInt(5, attachment.GetCompressionType()); 437 s.BindInt(5, attachment.GetCompressionType());
438 s.Run(); 438 s.Run();
439 } 439 }
440 440
441 void DatabaseWrapper::ListAvailableAttachments(std::list<FileContentType>& result, 441 void DatabaseWrapper::ListAvailableAttachments(std::list<FileContentType>& result,
443 { 443 {
444 result.clear(); 444 result.clear();
445 445
446 SQLite::Statement s(db_, SQLITE_FROM_HERE, 446 SQLite::Statement s(db_, SQLITE_FROM_HERE,
447 "SELECT fileType FROM AttachedFiles WHERE id=?"); 447 "SELECT fileType FROM AttachedFiles WHERE id=?");
448 s.BindInt(0, id); 448 s.BindInt64(0, id);
449 449
450 while (s.Step()) 450 while (s.Step())
451 { 451 {
452 result.push_back(static_cast<FileContentType>(s.ColumnInt(0))); 452 result.push_back(static_cast<FileContentType>(s.ColumnInt(0)));
453 } 453 }
457 int64_t id, 457 int64_t id,
458 FileContentType contentType) 458 FileContentType contentType)
459 { 459 {
460 SQLite::Statement s(db_, SQLITE_FROM_HERE, 460 SQLite::Statement s(db_, SQLITE_FROM_HERE,
461 "SELECT uuid, uncompressedSize, compressionType, compressedSize FROM AttachedFiles WHERE id=? AND fileType=?"); 461 "SELECT uuid, uncompressedSize, compressionType, compressedSize FROM AttachedFiles WHERE id=? AND fileType=?");
462 s.BindInt(0, id); 462 s.BindInt64(0, id);
463 s.BindInt(1, contentType); 463 s.BindInt(1, contentType);
464 464
465 if (!s.Step()) 465 if (!s.Step())
466 { 466 {
467 return false; 467 return false;
482 { 482 {
483 DicomArray flattened(tags); 483 DicomArray flattened(tags);
484 for (size_t i = 0; i < flattened.GetSize(); i++) 484 for (size_t i = 0; i < flattened.GetSize(); i++)
485 { 485 {
486 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO MainDicomTags VALUES(?, ?, ?, ?)"); 486 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO MainDicomTags VALUES(?, ?, ?, ?)");
487 s.BindInt(0, id); 487 s.BindInt64(0, id);
488 s.BindInt(1, flattened.GetElement(i).GetTag().GetGroup()); 488 s.BindInt(1, flattened.GetElement(i).GetTag().GetGroup());
489 s.BindInt(2, flattened.GetElement(i).GetTag().GetElement()); 489 s.BindInt(2, flattened.GetElement(i).GetTag().GetElement());
490 s.BindString(3, flattened.GetElement(i).GetValue().AsString()); 490 s.BindString(3, flattened.GetElement(i).GetValue().AsString());
491 s.Run(); 491 s.Run();
492 } 492 }
496 int64_t id) 496 int64_t id)
497 { 497 {
498 map.Clear(); 498 map.Clear();
499 499
500 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM MainDicomTags WHERE id=?"); 500 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM MainDicomTags WHERE id=?");
501 s.BindInt(0, id); 501 s.BindInt64(0, id);
502 while (s.Step()) 502 while (s.Step())
503 { 503 {
504 map.SetValue(s.ColumnInt(1), 504 map.SetValue(s.ColumnInt(1),
505 s.ColumnInt(2), 505 s.ColumnInt(2),
506 s.ColumnString(3)); 506 s.ColumnString(3));
511 bool DatabaseWrapper::GetParentPublicId(std::string& result, 511 bool DatabaseWrapper::GetParentPublicId(std::string& result,
512 int64_t id) 512 int64_t id)
513 { 513 {
514 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT a.publicId FROM Resources AS a, Resources AS b " 514 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT a.publicId FROM Resources AS a, Resources AS b "
515 "WHERE a.internalId = b.parentId AND b.internalId = ?"); 515 "WHERE a.internalId = b.parentId AND b.internalId = ?");
516 s.BindInt(0, id); 516 s.BindInt64(0, id);
517 517
518 if (s.Step()) 518 if (s.Step())
519 { 519 {
520 result = s.ColumnString(0); 520 result = s.ColumnString(0);
521 return true; 521 return true;
530 void DatabaseWrapper::GetChildrenPublicId(std::list<std::string>& result, 530 void DatabaseWrapper::GetChildrenPublicId(std::list<std::string>& result,
531 int64_t id) 531 int64_t id)
532 { 532 {
533 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT a.publicId FROM Resources AS a, Resources AS b " 533 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT a.publicId FROM Resources AS a, Resources AS b "
534 "WHERE a.parentId = b.internalId AND b.internalId = ?"); 534 "WHERE a.parentId = b.internalId AND b.internalId = ?");
535 s.BindInt(0, id); 535 s.BindInt64(0, id);
536 536
537 result.clear(); 537 result.clear();
538 538
539 while (s.Step()) 539 while (s.Step())
540 { 540 {
546 void DatabaseWrapper::GetChildrenInternalId(std::list<int64_t>& result, 546 void DatabaseWrapper::GetChildrenInternalId(std::list<int64_t>& result,
547 int64_t id) 547 int64_t id)
548 { 548 {
549 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT a.internalId FROM Resources AS a, Resources AS b " 549 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT a.internalId FROM Resources AS a, Resources AS b "
550 "WHERE a.parentId = b.internalId AND b.internalId = ?"); 550 "WHERE a.parentId = b.internalId AND b.internalId = ?");
551 s.BindInt(0, id); 551 s.BindInt64(0, id);
552 552
553 result.clear(); 553 result.clear();
554 554
555 while (s.Step()) 555 while (s.Step())
556 { 556 {
564 ResourceType resourceType, 564 ResourceType resourceType,
565 const boost::posix_time::ptime& date) 565 const boost::posix_time::ptime& date)
566 { 566 {
567 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Changes VALUES(NULL, ?, ?, ?, ?)"); 567 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Changes VALUES(NULL, ?, ?, ?, ?)");
568 s.BindInt(0, changeType); 568 s.BindInt(0, changeType);
569 s.BindInt(1, internalId); 569 s.BindInt64(1, internalId);
570 s.BindInt(2, resourceType); 570 s.BindInt(2, resourceType);
571 s.BindString(3, boost::posix_time::to_iso_string(date)); 571 s.BindString(3, boost::posix_time::to_iso_string(date));
572 s.Run(); 572 s.Run();
573 } 573 }
574 574
612 void DatabaseWrapper::GetChanges(Json::Value& target, 612 void DatabaseWrapper::GetChanges(Json::Value& target,
613 int64_t since, 613 int64_t since,
614 unsigned int maxResults) 614 unsigned int maxResults)
615 { 615 {
616 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM Changes WHERE seq>? ORDER BY seq LIMIT ?"); 616 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM Changes WHERE seq>? ORDER BY seq LIMIT ?");
617 s.BindInt(0, since); 617 s.BindInt64(0, since);
618 s.BindInt(1, maxResults + 1); 618 s.BindInt(1, maxResults + 1);
619 GetChangesInternal(target, s, since, maxResults); 619 GetChangesInternal(target, s, since, maxResults);
620 } 620 }
621 621
622 void DatabaseWrapper::GetLastChange(Json::Value& target) 622 void DatabaseWrapper::GetLastChange(Json::Value& target)
709 int64_t since, 709 int64_t since,
710 unsigned int maxResults) 710 unsigned int maxResults)
711 { 711 {
712 SQLite::Statement s(db_, SQLITE_FROM_HERE, 712 SQLite::Statement s(db_, SQLITE_FROM_HERE,
713 "SELECT * FROM ExportedResources WHERE seq>? ORDER BY seq LIMIT ?"); 713 "SELECT * FROM ExportedResources WHERE seq>? ORDER BY seq LIMIT ?");
714 s.BindInt(0, since); 714 s.BindInt64(0, since);
715 s.BindInt(1, maxResults + 1); 715 s.BindInt(1, maxResults + 1);
716 GetExportedResources(target, s, since, maxResults); 716 GetExportedResources(target, s, since, maxResults);
717 } 717 }
718 718
719 719
891 int64_t patientIdToAvoid) 891 int64_t patientIdToAvoid)
892 { 892 {
893 SQLite::Statement s(db_, SQLITE_FROM_HERE, 893 SQLite::Statement s(db_, SQLITE_FROM_HERE,
894 "SELECT patientId FROM PatientRecyclingOrder " 894 "SELECT patientId FROM PatientRecyclingOrder "
895 "WHERE patientId != ? ORDER BY seq ASC LIMIT 1"); 895 "WHERE patientId != ? ORDER BY seq ASC LIMIT 1");
896 s.BindInt(0, patientIdToAvoid); 896 s.BindInt64(0, patientIdToAvoid);
897 897
898 if (!s.Step()) 898 if (!s.Step())
899 { 899 {
900 // No patient remaining or all the patients are protected 900 // No patient remaining or all the patients are protected
901 return false; 901 return false;
909 909
910 bool DatabaseWrapper::IsProtectedPatient(int64_t internalId) 910 bool DatabaseWrapper::IsProtectedPatient(int64_t internalId)
911 { 911 {
912 SQLite::Statement s(db_, SQLITE_FROM_HERE, 912 SQLite::Statement s(db_, SQLITE_FROM_HERE,
913 "SELECT * FROM PatientRecyclingOrder WHERE patientId = ?"); 913 "SELECT * FROM PatientRecyclingOrder WHERE patientId = ?");
914 s.BindInt(0, internalId); 914 s.BindInt64(0, internalId);
915 return !s.Step(); 915 return !s.Step();
916 } 916 }
917 917
918 void DatabaseWrapper::SetProtectedPatient(int64_t internalId, 918 void DatabaseWrapper::SetProtectedPatient(int64_t internalId,
919 bool isProtected) 919 bool isProtected)
920 { 920 {
921 if (isProtected) 921 if (isProtected)
922 { 922 {
923 SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM PatientRecyclingOrder WHERE patientId=?"); 923 SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM PatientRecyclingOrder WHERE patientId=?");
924 s.BindInt(0, internalId); 924 s.BindInt64(0, internalId);
925 s.Run(); 925 s.Run();
926 } 926 }
927 else if (IsProtectedPatient(internalId)) 927 else if (IsProtectedPatient(internalId))
928 { 928 {
929 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO PatientRecyclingOrder VALUES(NULL, ?)"); 929 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO PatientRecyclingOrder VALUES(NULL, ?)");
930 s.BindInt(0, internalId); 930 s.BindInt64(0, internalId);
931 s.Run(); 931 s.Run();
932 } 932 }
933 else 933 else
934 { 934 {
935 // Nothing to do: The patient is already unprotected 935 // Nothing to do: The patient is already unprotected
973 973
974 bool DatabaseWrapper::IsExistingResource(int64_t internalId) 974 bool DatabaseWrapper::IsExistingResource(int64_t internalId)
975 { 975 {
976 SQLite::Statement s(db_, SQLITE_FROM_HERE, 976 SQLite::Statement s(db_, SQLITE_FROM_HERE,
977 "SELECT * FROM Resources WHERE internalId=?"); 977 "SELECT * FROM Resources WHERE internalId=?");
978 s.BindInt(0, internalId); 978 s.BindInt64(0, internalId);
979 return s.Step(); 979 return s.Step();
980 } 980 }
981 981
982 982
983 void DatabaseWrapper::LookupTagValue(std::list<int64_t>& result, 983 void DatabaseWrapper::LookupTagValue(std::list<int64_t>& result,