Mercurial > hg > orthanc
comparison OrthancServer/DatabaseWrapper.cpp @ 2773:bb63068844ae
moving DatabaseWrapperBase into graveyard
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 17 Jul 2018 11:25:54 +0200 |
parents | 878b59270859 |
children | bbfd95a0c429 |
comparison
equal
deleted
inserted
replaced
2772:f3df536e7366 | 2773:bb63068844ae |
---|---|
42 #include <stdio.h> | 42 #include <stdio.h> |
43 #include <boost/lexical_cast.hpp> | 43 #include <boost/lexical_cast.hpp> |
44 | 44 |
45 namespace Orthanc | 45 namespace Orthanc |
46 { | 46 { |
47 | |
48 namespace Internals | 47 namespace Internals |
49 { | 48 { |
50 class SignalFileDeleted : public SQLite::IScalarFunction | 49 class SignalFileDeleted : public SQLite::IScalarFunction |
51 { | 50 { |
52 private: | 51 private: |
185 } | 184 } |
186 }; | 185 }; |
187 } | 186 } |
188 | 187 |
189 | 188 |
189 void DatabaseWrapper::GetChangesInternal(std::list<ServerIndexChange>& target, | |
190 bool& done, | |
191 SQLite::Statement& s, | |
192 uint32_t maxResults) | |
193 { | |
194 target.clear(); | |
195 | |
196 while (target.size() < maxResults && s.Step()) | |
197 { | |
198 int64_t seq = s.ColumnInt64(0); | |
199 ChangeType changeType = static_cast<ChangeType>(s.ColumnInt(1)); | |
200 ResourceType resourceType = static_cast<ResourceType>(s.ColumnInt(3)); | |
201 const std::string& date = s.ColumnString(4); | |
202 | |
203 int64_t internalId = s.ColumnInt64(2); | |
204 std::string publicId = GetPublicId(internalId); | |
205 | |
206 target.push_back(ServerIndexChange(seq, changeType, resourceType, publicId, date)); | |
207 } | |
208 | |
209 done = !(target.size() == maxResults && s.Step()); | |
210 } | |
211 | |
212 | |
213 void DatabaseWrapper::GetExportedResourcesInternal(std::list<ExportedResource>& target, | |
214 bool& done, | |
215 SQLite::Statement& s, | |
216 uint32_t maxResults) | |
217 { | |
218 target.clear(); | |
219 | |
220 while (target.size() < maxResults && s.Step()) | |
221 { | |
222 int64_t seq = s.ColumnInt64(0); | |
223 ResourceType resourceType = static_cast<ResourceType>(s.ColumnInt(1)); | |
224 std::string publicId = s.ColumnString(2); | |
225 | |
226 ExportedResource resource(seq, | |
227 resourceType, | |
228 publicId, | |
229 s.ColumnString(3), // modality | |
230 s.ColumnString(8), // date | |
231 s.ColumnString(4), // patient ID | |
232 s.ColumnString(5), // study instance UID | |
233 s.ColumnString(6), // series instance UID | |
234 s.ColumnString(7)); // sop instance UID | |
235 | |
236 target.push_back(resource); | |
237 } | |
238 | |
239 done = !(target.size() == maxResults && s.Step()); | |
240 } | |
241 | |
190 | 242 |
191 void DatabaseWrapper::GetChildren(std::list<std::string>& childrenPublicIds, | 243 void DatabaseWrapper::GetChildren(std::list<std::string>& childrenPublicIds, |
192 int64_t id) | 244 int64_t id) |
193 { | 245 { |
194 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT publicId FROM Resources WHERE parentId=?"); | 246 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT publicId FROM Resources WHERE parentId=?"); |
256 } | 308 } |
257 | 309 |
258 | 310 |
259 DatabaseWrapper::DatabaseWrapper(const std::string& path) : | 311 DatabaseWrapper::DatabaseWrapper(const std::string& path) : |
260 listener_(NULL), | 312 listener_(NULL), |
261 base_(db_), | |
262 signalRemainingAncestor_(NULL), | 313 signalRemainingAncestor_(NULL), |
263 version_(0) | 314 version_(0) |
264 { | 315 { |
265 db_.Open(path); | 316 db_.Open(path); |
266 } | 317 } |
267 | 318 |
319 | |
268 DatabaseWrapper::DatabaseWrapper() : | 320 DatabaseWrapper::DatabaseWrapper() : |
269 listener_(NULL), | 321 listener_(NULL), |
270 base_(db_), | |
271 signalRemainingAncestor_(NULL), | 322 signalRemainingAncestor_(NULL), |
272 version_(0) | 323 version_(0) |
273 { | 324 { |
274 db_.OpenInMemory(); | 325 db_.OpenInMemory(); |
275 } | 326 } |
327 | |
276 | 328 |
277 void DatabaseWrapper::Open() | 329 void DatabaseWrapper::Open() |
278 { | 330 { |
279 db_.Execute("PRAGMA ENCODING=\"UTF-8\";"); | 331 db_.Execute("PRAGMA ENCODING=\"UTF-8\";"); |
280 | 332 |
400 | 452 |
401 | 453 |
402 bool DatabaseWrapper::LookupParent(int64_t& parentId, | 454 bool DatabaseWrapper::LookupParent(int64_t& parentId, |
403 int64_t resourceId) | 455 int64_t resourceId) |
404 { | 456 { |
405 bool found; | 457 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
406 ErrorCode error = base_.LookupParent(found, parentId, resourceId); | 458 "SELECT parentId FROM Resources WHERE internalId=?"); |
407 | 459 s.BindInt64(0, resourceId); |
408 if (error != ErrorCode_Success) | 460 |
409 { | 461 if (!s.Step()) |
410 throw OrthancException(error); | 462 { |
463 throw OrthancException(ErrorCode_UnknownResource); | |
464 } | |
465 | |
466 if (s.ColumnIsNull(0)) | |
467 { | |
468 return false; | |
411 } | 469 } |
412 else | 470 else |
413 { | 471 { |
414 return found; | 472 parentId = s.ColumnInt(0); |
473 return true; | |
415 } | 474 } |
416 } | 475 } |
417 | 476 |
418 | 477 |
419 ResourceType DatabaseWrapper::GetResourceType(int64_t resourceId) | 478 ResourceType DatabaseWrapper::GetResourceType(int64_t resourceId) |
420 { | 479 { |
421 ResourceType result; | 480 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
422 ErrorCode code = base_.GetResourceType(result, resourceId); | 481 "SELECT resourceType FROM Resources WHERE internalId=?"); |
423 | 482 s.BindInt64(0, resourceId); |
424 if (code == ErrorCode_Success) | 483 |
425 { | 484 if (s.Step()) |
426 return result; | 485 { |
486 return static_cast<ResourceType>(s.ColumnInt(0)); | |
427 } | 487 } |
428 else | 488 else |
429 { | 489 { |
430 throw OrthancException(code); | 490 throw OrthancException(ErrorCode_UnknownResource); |
431 } | 491 } |
432 } | 492 } |
433 | 493 |
434 | 494 |
435 std::string DatabaseWrapper::GetPublicId(int64_t resourceId) | 495 std::string DatabaseWrapper::GetPublicId(int64_t resourceId) |
436 { | 496 { |
437 std::string id; | 497 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
438 | 498 "SELECT publicId FROM Resources WHERE internalId=?"); |
439 if (base_.GetPublicId(id, resourceId)) | 499 s.BindInt64(0, resourceId); |
440 { | 500 |
441 return id; | 501 if (s.Step()) |
502 { | |
503 return s.ColumnString(0); | |
442 } | 504 } |
443 else | 505 else |
444 { | 506 { |
445 throw OrthancException(ErrorCode_UnknownResource); | 507 throw OrthancException(ErrorCode_UnknownResource); |
446 } | 508 } |
450 void DatabaseWrapper::GetChanges(std::list<ServerIndexChange>& target /*out*/, | 512 void DatabaseWrapper::GetChanges(std::list<ServerIndexChange>& target /*out*/, |
451 bool& done /*out*/, | 513 bool& done /*out*/, |
452 int64_t since, | 514 int64_t since, |
453 uint32_t maxResults) | 515 uint32_t maxResults) |
454 { | 516 { |
455 ErrorCode code = base_.GetChanges(target, done, since, maxResults); | 517 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM Changes WHERE seq>? ORDER BY seq LIMIT ?"); |
456 | 518 s.BindInt64(0, since); |
457 if (code != ErrorCode_Success) | 519 s.BindInt(1, maxResults + 1); |
458 { | 520 GetChangesInternal(target, done, s, maxResults); |
459 throw OrthancException(code); | |
460 } | |
461 } | 521 } |
462 | 522 |
463 | 523 |
464 void DatabaseWrapper::GetLastChange(std::list<ServerIndexChange>& target /*out*/) | 524 void DatabaseWrapper::GetLastChange(std::list<ServerIndexChange>& target /*out*/) |
465 { | 525 { |
466 ErrorCode code = base_.GetLastChange(target); | 526 bool done; // Ignored |
467 | 527 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM Changes ORDER BY seq DESC LIMIT 1"); |
468 if (code != ErrorCode_Success) | 528 GetChangesInternal(target, done, s, 1); |
469 { | |
470 throw OrthancException(code); | |
471 } | |
472 } | 529 } |
473 | 530 |
474 | 531 |
475 void DatabaseWrapper::GetAllMetadata(std::map<MetadataType, std::string>& target, | 532 void DatabaseWrapper::GetAllMetadata(std::map<MetadataType, std::string>& target, |
476 int64_t id) | 533 int64_t id) |
485 MetadataType key = static_cast<MetadataType>(s.ColumnInt(0)); | 542 MetadataType key = static_cast<MetadataType>(s.ColumnInt(0)); |
486 target[key] = s.ColumnString(1); | 543 target[key] = s.ColumnString(1); |
487 } | 544 } |
488 } | 545 } |
489 | 546 |
547 | |
548 void DatabaseWrapper::SetGlobalProperty(GlobalProperty property, | |
549 const std::string& value) | |
550 { | |
551 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT OR REPLACE INTO GlobalProperties VALUES(?, ?)"); | |
552 s.BindInt(0, property); | |
553 s.BindString(1, value); | |
554 s.Run(); | |
555 } | |
556 | |
557 | |
558 bool DatabaseWrapper::LookupGlobalProperty(std::string& target, | |
559 GlobalProperty property) | |
560 { | |
561 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
562 "SELECT value FROM GlobalProperties WHERE property=?"); | |
563 s.BindInt(0, property); | |
564 | |
565 if (!s.Step()) | |
566 { | |
567 return false; | |
568 } | |
569 else | |
570 { | |
571 target = s.ColumnString(0); | |
572 return true; | |
573 } | |
574 } | |
575 | |
576 | |
577 int64_t DatabaseWrapper::CreateResource(const std::string& publicId, | |
578 ResourceType type) | |
579 { | |
580 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Resources VALUES(NULL, ?, ?, NULL)"); | |
581 s.BindInt(0, type); | |
582 s.BindString(1, publicId); | |
583 s.Run(); | |
584 return db_.GetLastInsertRowId(); | |
585 } | |
586 | |
587 | |
588 bool DatabaseWrapper::LookupResource(int64_t& id, | |
589 ResourceType& type, | |
590 const std::string& publicId) | |
591 { | |
592 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
593 "SELECT internalId, resourceType FROM Resources WHERE publicId=?"); | |
594 s.BindString(0, publicId); | |
595 | |
596 if (!s.Step()) | |
597 { | |
598 return false; | |
599 } | |
600 else | |
601 { | |
602 id = s.ColumnInt(0); | |
603 type = static_cast<ResourceType>(s.ColumnInt(1)); | |
604 | |
605 // Check whether there is a single resource with this public id | |
606 assert(!s.Step()); | |
607 | |
608 return true; | |
609 } | |
610 } | |
611 | |
612 | |
613 void DatabaseWrapper::AttachChild(int64_t parent, | |
614 int64_t child) | |
615 { | |
616 SQLite::Statement s(db_, SQLITE_FROM_HERE, "UPDATE Resources SET parentId = ? WHERE internalId = ?"); | |
617 s.BindInt64(0, parent); | |
618 s.BindInt64(1, child); | |
619 s.Run(); | |
620 } | |
621 | |
622 | |
623 void DatabaseWrapper::SetMetadata(int64_t id, | |
624 MetadataType type, | |
625 const std::string& value) | |
626 { | |
627 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT OR REPLACE INTO Metadata VALUES(?, ?, ?)"); | |
628 s.BindInt64(0, id); | |
629 s.BindInt(1, type); | |
630 s.BindString(2, value); | |
631 s.Run(); | |
632 } | |
633 | |
634 | |
635 void DatabaseWrapper::DeleteMetadata(int64_t id, | |
636 MetadataType type) | |
637 { | |
638 SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM Metadata WHERE id=? and type=?"); | |
639 s.BindInt64(0, id); | |
640 s.BindInt(1, type); | |
641 s.Run(); | |
642 } | |
643 | |
644 | |
645 bool DatabaseWrapper::LookupMetadata(std::string& target, | |
646 int64_t id, | |
647 MetadataType type) | |
648 { | |
649 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
650 "SELECT value FROM Metadata WHERE id=? AND type=?"); | |
651 s.BindInt64(0, id); | |
652 s.BindInt(1, type); | |
653 | |
654 if (!s.Step()) | |
655 { | |
656 return false; | |
657 } | |
658 else | |
659 { | |
660 target = s.ColumnString(0); | |
661 return true; | |
662 } | |
663 } | |
664 | |
665 | |
666 void DatabaseWrapper::ListAvailableMetadata(std::list<MetadataType>& target, | |
667 int64_t id) | |
668 { | |
669 target.clear(); | |
670 | |
671 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT type FROM Metadata WHERE id=?"); | |
672 s.BindInt64(0, id); | |
673 | |
674 while (s.Step()) | |
675 { | |
676 target.push_back(static_cast<MetadataType>(s.ColumnInt(0))); | |
677 } | |
678 } | |
679 | |
680 | |
681 void DatabaseWrapper::AddAttachment(int64_t id, | |
682 const FileInfo& attachment) | |
683 { | |
684 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO AttachedFiles VALUES(?, ?, ?, ?, ?, ?, ?, ?)"); | |
685 s.BindInt64(0, id); | |
686 s.BindInt(1, attachment.GetContentType()); | |
687 s.BindString(2, attachment.GetUuid()); | |
688 s.BindInt64(3, attachment.GetCompressedSize()); | |
689 s.BindInt64(4, attachment.GetUncompressedSize()); | |
690 s.BindInt(5, attachment.GetCompressionType()); | |
691 s.BindString(6, attachment.GetUncompressedMD5()); | |
692 s.BindString(7, attachment.GetCompressedMD5()); | |
693 s.Run(); | |
694 } | |
695 | |
696 | |
697 void DatabaseWrapper::DeleteAttachment(int64_t id, | |
698 FileContentType attachment) | |
699 { | |
700 SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM AttachedFiles WHERE id=? AND fileType=?"); | |
701 s.BindInt64(0, id); | |
702 s.BindInt(1, attachment); | |
703 s.Run(); | |
704 } | |
705 | |
706 | |
707 void DatabaseWrapper::ListAvailableAttachments(std::list<FileContentType>& target, | |
708 int64_t id) | |
709 { | |
710 target.clear(); | |
711 | |
712 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
713 "SELECT fileType FROM AttachedFiles WHERE id=?"); | |
714 s.BindInt64(0, id); | |
715 | |
716 while (s.Step()) | |
717 { | |
718 target.push_back(static_cast<FileContentType>(s.ColumnInt(0))); | |
719 } | |
720 } | |
721 | |
722 bool DatabaseWrapper::LookupAttachment(FileInfo& attachment, | |
723 int64_t id, | |
724 FileContentType contentType) | |
725 { | |
726 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
727 "SELECT uuid, uncompressedSize, compressionType, compressedSize, " | |
728 "uncompressedMD5, compressedMD5 FROM AttachedFiles WHERE id=? AND fileType=?"); | |
729 s.BindInt64(0, id); | |
730 s.BindInt(1, contentType); | |
731 | |
732 if (!s.Step()) | |
733 { | |
734 return false; | |
735 } | |
736 else | |
737 { | |
738 attachment = FileInfo(s.ColumnString(0), | |
739 contentType, | |
740 s.ColumnInt64(1), | |
741 s.ColumnString(4), | |
742 static_cast<CompressionType>(s.ColumnInt(2)), | |
743 s.ColumnInt64(3), | |
744 s.ColumnString(5)); | |
745 return true; | |
746 } | |
747 } | |
748 | |
749 | |
750 void DatabaseWrapper::ClearMainDicomTags(int64_t id) | |
751 { | |
752 { | |
753 SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM DicomIdentifiers WHERE id=?"); | |
754 s.BindInt64(0, id); | |
755 s.Run(); | |
756 } | |
757 | |
758 { | |
759 SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM MainDicomTags WHERE id=?"); | |
760 s.BindInt64(0, id); | |
761 s.Run(); | |
762 } | |
763 } | |
764 | |
765 | |
766 void DatabaseWrapper::SetMainDicomTag(int64_t id, | |
767 const DicomTag& tag, | |
768 const std::string& value) | |
769 { | |
770 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO MainDicomTags VALUES(?, ?, ?, ?)"); | |
771 s.BindInt64(0, id); | |
772 s.BindInt(1, tag.GetGroup()); | |
773 s.BindInt(2, tag.GetElement()); | |
774 s.BindString(3, value); | |
775 s.Run(); | |
776 } | |
777 | |
778 | |
779 void DatabaseWrapper::SetIdentifierTag(int64_t id, | |
780 const DicomTag& tag, | |
781 const std::string& value) | |
782 { | |
783 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO DicomIdentifiers VALUES(?, ?, ?, ?)"); | |
784 s.BindInt64(0, id); | |
785 s.BindInt(1, tag.GetGroup()); | |
786 s.BindInt(2, tag.GetElement()); | |
787 s.BindString(3, value); | |
788 s.Run(); | |
789 } | |
790 | |
791 | |
792 void DatabaseWrapper::GetMainDicomTags(DicomMap& map, | |
793 int64_t id) | |
794 { | |
795 map.Clear(); | |
796 | |
797 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM MainDicomTags WHERE id=?"); | |
798 s.BindInt64(0, id); | |
799 while (s.Step()) | |
800 { | |
801 map.SetValue(s.ColumnInt(1), | |
802 s.ColumnInt(2), | |
803 s.ColumnString(3), false); | |
804 } | |
805 } | |
806 | |
807 | |
808 void DatabaseWrapper::GetChildrenPublicId(std::list<std::string>& target, | |
809 int64_t id) | |
810 { | |
811 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT a.publicId FROM Resources AS a, Resources AS b " | |
812 "WHERE a.parentId = b.internalId AND b.internalId = ?"); | |
813 s.BindInt64(0, id); | |
814 | |
815 target.clear(); | |
816 | |
817 while (s.Step()) | |
818 { | |
819 target.push_back(s.ColumnString(0)); | |
820 } | |
821 } | |
822 | |
823 | |
824 void DatabaseWrapper::GetChildrenInternalId(std::list<int64_t>& target, | |
825 int64_t id) | |
826 { | |
827 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT a.internalId FROM Resources AS a, Resources AS b " | |
828 "WHERE a.parentId = b.internalId AND b.internalId = ?"); | |
829 s.BindInt64(0, id); | |
830 | |
831 target.clear(); | |
832 | |
833 while (s.Step()) | |
834 { | |
835 target.push_back(s.ColumnInt64(0)); | |
836 } | |
837 } | |
838 | |
839 | |
840 void DatabaseWrapper::LogChange(int64_t internalId, | |
841 const ServerIndexChange& change) | |
842 { | |
843 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Changes VALUES(NULL, ?, ?, ?, ?)"); | |
844 s.BindInt(0, change.GetChangeType()); | |
845 s.BindInt64(1, internalId); | |
846 s.BindInt(2, change.GetResourceType()); | |
847 s.BindString(3, change.GetDate()); | |
848 s.Run(); | |
849 } | |
850 | |
851 | |
852 void DatabaseWrapper::LogExportedResource(const ExportedResource& resource) | |
853 { | |
854 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
855 "INSERT INTO ExportedResources VALUES(NULL, ?, ?, ?, ?, ?, ?, ?, ?)"); | |
856 | |
857 s.BindInt(0, resource.GetResourceType()); | |
858 s.BindString(1, resource.GetPublicId()); | |
859 s.BindString(2, resource.GetModality()); | |
860 s.BindString(3, resource.GetPatientId()); | |
861 s.BindString(4, resource.GetStudyInstanceUid()); | |
862 s.BindString(5, resource.GetSeriesInstanceUid()); | |
863 s.BindString(6, resource.GetSopInstanceUid()); | |
864 s.BindString(7, resource.GetDate()); | |
865 s.Run(); | |
866 } | |
867 | |
868 | |
869 void DatabaseWrapper::GetExportedResources(std::list<ExportedResource>& target, | |
870 bool& done, | |
871 int64_t since, | |
872 uint32_t maxResults) | |
873 { | |
874 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
875 "SELECT * FROM ExportedResources WHERE seq>? ORDER BY seq LIMIT ?"); | |
876 s.BindInt64(0, since); | |
877 s.BindInt(1, maxResults + 1); | |
878 GetExportedResourcesInternal(target, done, s, maxResults); | |
879 } | |
880 | |
881 | |
882 void DatabaseWrapper::GetLastExportedResource(std::list<ExportedResource>& target) | |
883 { | |
884 bool done; // Ignored | |
885 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
886 "SELECT * FROM ExportedResources ORDER BY seq DESC LIMIT 1"); | |
887 GetExportedResourcesInternal(target, done, s, 1); | |
888 } | |
889 | |
890 | |
891 uint64_t DatabaseWrapper::GetTotalCompressedSize() | |
892 { | |
893 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT SUM(compressedSize) FROM AttachedFiles"); | |
894 s.Run(); | |
895 return static_cast<uint64_t>(s.ColumnInt64(0)); | |
896 } | |
897 | |
898 | |
899 uint64_t DatabaseWrapper::GetTotalUncompressedSize() | |
900 { | |
901 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT SUM(uncompressedSize) FROM AttachedFiles"); | |
902 s.Run(); | |
903 return static_cast<uint64_t>(s.ColumnInt64(0)); | |
904 } | |
905 | |
906 | |
907 uint64_t DatabaseWrapper::GetResourceCount(ResourceType resourceType) | |
908 { | |
909 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
910 "SELECT COUNT(*) FROM Resources WHERE resourceType=?"); | |
911 s.BindInt(0, resourceType); | |
912 | |
913 if (!s.Step()) | |
914 { | |
915 return 0; | |
916 } | |
917 else | |
918 { | |
919 int64_t c = s.ColumnInt(0); | |
920 assert(!s.Step()); | |
921 return c; | |
922 } | |
923 } | |
924 | |
925 | |
926 void DatabaseWrapper::GetAllInternalIds(std::list<int64_t>& target, | |
927 ResourceType resourceType) | |
928 { | |
929 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT internalId FROM Resources WHERE resourceType=?"); | |
930 s.BindInt(0, resourceType); | |
931 | |
932 target.clear(); | |
933 while (s.Step()) | |
934 { | |
935 target.push_back(s.ColumnInt64(0)); | |
936 } | |
937 } | |
938 | |
939 | |
940 void DatabaseWrapper::GetAllPublicIds(std::list<std::string>& target, | |
941 ResourceType resourceType) | |
942 { | |
943 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT publicId FROM Resources WHERE resourceType=?"); | |
944 s.BindInt(0, resourceType); | |
945 | |
946 target.clear(); | |
947 while (s.Step()) | |
948 { | |
949 target.push_back(s.ColumnString(0)); | |
950 } | |
951 } | |
952 | |
953 | |
954 void DatabaseWrapper::GetAllPublicIds(std::list<std::string>& target, | |
955 ResourceType resourceType, | |
956 size_t since, | |
957 size_t limit) | |
958 { | |
959 if (limit == 0) | |
960 { | |
961 target.clear(); | |
962 return; | |
963 } | |
964 | |
965 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
966 "SELECT publicId FROM Resources WHERE " | |
967 "resourceType=? LIMIT ? OFFSET ?"); | |
968 s.BindInt(0, resourceType); | |
969 s.BindInt64(1, limit); | |
970 s.BindInt64(2, since); | |
971 | |
972 target.clear(); | |
973 while (s.Step()) | |
974 { | |
975 target.push_back(s.ColumnString(0)); | |
976 } | |
977 } | |
978 | |
979 | |
980 bool DatabaseWrapper::SelectPatientToRecycle(int64_t& internalId) | |
981 { | |
982 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
983 "SELECT patientId FROM PatientRecyclingOrder ORDER BY seq ASC LIMIT 1"); | |
984 | |
985 if (!s.Step()) | |
986 { | |
987 // No patient remaining or all the patients are protected | |
988 return false; | |
989 } | |
990 else | |
991 { | |
992 internalId = s.ColumnInt(0); | |
993 return true; | |
994 } | |
995 } | |
996 | |
997 | |
998 bool DatabaseWrapper::SelectPatientToRecycle(int64_t& internalId, | |
999 int64_t patientIdToAvoid) | |
1000 { | |
1001 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
1002 "SELECT patientId FROM PatientRecyclingOrder " | |
1003 "WHERE patientId != ? ORDER BY seq ASC LIMIT 1"); | |
1004 s.BindInt64(0, patientIdToAvoid); | |
1005 | |
1006 if (!s.Step()) | |
1007 { | |
1008 // No patient remaining or all the patients are protected | |
1009 return false; | |
1010 } | |
1011 else | |
1012 { | |
1013 internalId = s.ColumnInt(0); | |
1014 return true; | |
1015 } | |
1016 } | |
1017 | |
1018 | |
1019 bool DatabaseWrapper::IsProtectedPatient(int64_t internalId) | |
1020 { | |
1021 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
1022 "SELECT * FROM PatientRecyclingOrder WHERE patientId = ?"); | |
1023 s.BindInt64(0, internalId); | |
1024 return !s.Step(); | |
1025 } | |
1026 | |
1027 | |
1028 void DatabaseWrapper::SetProtectedPatient(int64_t internalId, | |
1029 bool isProtected) | |
1030 { | |
1031 if (isProtected) | |
1032 { | |
1033 SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM PatientRecyclingOrder WHERE patientId=?"); | |
1034 s.BindInt64(0, internalId); | |
1035 s.Run(); | |
1036 } | |
1037 else if (IsProtectedPatient(internalId)) | |
1038 { | |
1039 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO PatientRecyclingOrder VALUES(NULL, ?)"); | |
1040 s.BindInt64(0, internalId); | |
1041 s.Run(); | |
1042 } | |
1043 else | |
1044 { | |
1045 // Nothing to do: The patient is already unprotected | |
1046 } | |
1047 } | |
1048 | |
1049 | |
1050 bool DatabaseWrapper::IsExistingResource(int64_t internalId) | |
1051 { | |
1052 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
1053 "SELECT * FROM Resources WHERE internalId=?"); | |
1054 s.BindInt64(0, internalId); | |
1055 return s.Step(); | |
1056 } | |
1057 | |
1058 | |
1059 void DatabaseWrapper::LookupIdentifier(std::list<int64_t>& target, | |
1060 ResourceType level, | |
1061 const DicomTag& tag, | |
1062 IdentifierConstraintType type, | |
1063 const std::string& value) | |
1064 { | |
1065 static const char* COMMON = ("SELECT d.id FROM DicomIdentifiers AS d, Resources AS r WHERE " | |
1066 "d.id = r.internalId AND r.resourceType=? AND " | |
1067 "d.tagGroup=? AND d.tagElement=? AND "); | |
1068 | |
1069 std::auto_ptr<SQLite::Statement> s; | |
1070 | |
1071 switch (type) | |
1072 { | |
1073 case IdentifierConstraintType_GreaterOrEqual: | |
1074 s.reset(new SQLite::Statement(db_, std::string(COMMON) + "d.value>=?")); | |
1075 break; | |
1076 | |
1077 case IdentifierConstraintType_SmallerOrEqual: | |
1078 s.reset(new SQLite::Statement(db_, std::string(COMMON) + "d.value<=?")); | |
1079 break; | |
1080 | |
1081 case IdentifierConstraintType_Wildcard: | |
1082 s.reset(new SQLite::Statement(db_, std::string(COMMON) + "d.value GLOB ?")); | |
1083 break; | |
1084 | |
1085 case IdentifierConstraintType_Equal: | |
1086 default: | |
1087 s.reset(new SQLite::Statement(db_, std::string(COMMON) + "d.value=?")); | |
1088 break; | |
1089 } | |
1090 | |
1091 assert(s.get() != NULL); | |
1092 | |
1093 s->BindInt(0, level); | |
1094 s->BindInt(1, tag.GetGroup()); | |
1095 s->BindInt(2, tag.GetElement()); | |
1096 s->BindString(3, value); | |
1097 | |
1098 target.clear(); | |
1099 | |
1100 while (s->Step()) | |
1101 { | |
1102 target.push_back(s->ColumnInt64(0)); | |
1103 } | |
1104 } | |
1105 | |
1106 | |
1107 void DatabaseWrapper::LookupIdentifierRange(std::list<int64_t>& target, | |
1108 ResourceType level, | |
1109 const DicomTag& tag, | |
1110 const std::string& start, | |
1111 const std::string& end) | |
1112 { | |
1113 SQLite::Statement statement(db_, SQLITE_FROM_HERE, | |
1114 "SELECT d.id FROM DicomIdentifiers AS d, Resources AS r WHERE " | |
1115 "d.id = r.internalId AND r.resourceType=? AND " | |
1116 "d.tagGroup=? AND d.tagElement=? AND d.value>=? AND d.value<=?"); | |
1117 | |
1118 statement.BindInt(0, level); | |
1119 statement.BindInt(1, tag.GetGroup()); | |
1120 statement.BindInt(2, tag.GetElement()); | |
1121 statement.BindString(3, start); | |
1122 statement.BindString(4, end); | |
1123 | |
1124 target.clear(); | |
1125 | |
1126 while (statement.Step()) | |
1127 { | |
1128 target.push_back(statement.ColumnInt64(0)); | |
1129 } | |
1130 } | |
490 } | 1131 } |