comparison OrthancServer/Sources/Database/SQLiteDatabaseWrapper.cpp @ 4570:648defffc8cc db-changes

new enum: TransactionType
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 08 Mar 2021 16:04:56 +0100
parents 2a0f8031fb93
children 9224e107d613
comparison
equal deleted inserted replaced
4569:19ea4ecd6d9a 4570:648defffc8cc
594 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM Changes ORDER BY seq DESC LIMIT 1"); 594 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM Changes ORDER BY seq DESC LIMIT 1");
595 GetChangesInternal(target, done, s, 1); 595 GetChangesInternal(target, done, s, 1);
596 } 596 }
597 597
598 598
599 class SQLiteDatabaseWrapper::Transaction : public IDatabaseWrapper::ITransaction 599 class SQLiteDatabaseWrapper::ReadWriteTransaction : public IDatabaseWrapper::ITransaction
600 { 600 {
601 private: 601 private:
602 SQLiteDatabaseWrapper& that_; 602 SQLiteDatabaseWrapper& that_;
603 std::unique_ptr<SQLite::Transaction> transaction_; 603 std::unique_ptr<SQLite::Transaction> transaction_;
604 int64_t initialDiskSize_; 604 int64_t initialDiskSize_;
605 605
606 public: 606 public:
607 Transaction(SQLiteDatabaseWrapper& that) : 607 ReadWriteTransaction(SQLiteDatabaseWrapper& that) :
608 that_(that), 608 that_(that),
609 transaction_(new SQLite::Transaction(that_.db_)) 609 transaction_(new SQLite::Transaction(that_.db_))
610 { 610 {
611 #if defined(NDEBUG) 611 #if defined(NDEBUG)
612 // Release mode 612 // Release mode
635 initialDiskSize_ + fileSizeDelta == static_cast<int64_t>(that_.GetTotalCompressedSize())); 635 initialDiskSize_ + fileSizeDelta == static_cast<int64_t>(that_.GetTotalCompressedSize()));
636 } 636 }
637 }; 637 };
638 638
639 639
640 IDatabaseWrapper::ITransaction* SQLiteDatabaseWrapper::StartTransaction() 640 class SQLiteDatabaseWrapper::ReadOnlyTransaction : public IDatabaseWrapper::ITransaction
641 { 641 {
642 std::unique_ptr<Transaction> transaction(new Transaction(*this)); 642 public:
643 transaction->Begin(); 643 virtual void Rollback() ORTHANC_OVERRIDE
644 return transaction.release(); 644 {
645 }
646
647 virtual void Commit(int64_t fileSizeDelta /* only used in debug */) ORTHANC_OVERRIDE
648 {
649 if (fileSizeDelta != 0)
650 {
651 throw OrthancException(ErrorCode_InternalError);
652 }
653 }
654 };
655
656
657 IDatabaseWrapper::ITransaction* SQLiteDatabaseWrapper::StartTransaction(TransactionType type)
658 {
659 switch (type)
660 {
661 case TransactionType_ReadOnly:
662 return new ReadOnlyTransaction; // This is a no-op transaction in SQLite (thanks to mutex)
663
664 case TransactionType_ReadWrite:
665 {
666 std::unique_ptr<ReadWriteTransaction> transaction;
667 transaction.reset(new ReadWriteTransaction(*this));
668 transaction->Begin();
669 return transaction.release();
670 }
671
672 default:
673 throw OrthancException(ErrorCode_InternalError);
674 }
645 } 675 }
646 676
647 677
648 void SQLiteDatabaseWrapper::GetAllMetadata(std::map<MetadataType, std::string>& target, 678 void SQLiteDatabaseWrapper::GetAllMetadata(std::map<MetadataType, std::string>& target,
649 int64_t id) 679 int64_t id)