# HG changeset patch # User Sebastien Jodogne # Date 1547287733 -3600 # Node ID 0fa7181ac4e5fdcafba72ea07f8dfa0d3abe7c5b # Parent 2c108461d4099f1e37b8c75ac5f685e19bd0305c conrt diff -r 2c108461d409 -r 0fa7181ac4e5 OrthancServer/Database/IDatabaseWrapper.h --- a/OrthancServer/Database/IDatabaseWrapper.h Sat Jan 12 09:15:32 2019 +0100 +++ b/OrthancServer/Database/IDatabaseWrapper.h Sat Jan 12 11:08:53 2019 +0100 @@ -221,9 +221,12 @@ ResourceType queryLevel, size_t limit) = 0; - // Returns "true" iff. the instance already exists. If "false" is - // returned, the content of "result" is undefined, but - // "instanceId" must be properly set. + // Returns "true" iff. the instance is new and has been inserted + // into the database. If "false" is returned, the content of + // "result" is undefined, but "instanceId" must be properly + // set. This method must also tag the parent patient as the most + // recent in the patient recycling order if it is not protected + // (so as to fix issue #58). virtual bool CreateInstance(CreateInstanceResult& result, /* out */ int64_t& instanceId, /* out */ const std::string& patient, diff -r 2c108461d409 -r 0fa7181ac4e5 OrthancServer/Database/SQLiteDatabaseWrapper.cpp --- a/OrthancServer/Database/SQLiteDatabaseWrapper.cpp Sat Jan 12 09:15:32 2019 +0100 +++ b/OrthancServer/Database/SQLiteDatabaseWrapper.cpp Sat Jan 12 11:08:53 2019 +0100 @@ -329,6 +329,31 @@ } + int SQLiteDatabaseWrapper::GetGlobalIntegerProperty(GlobalProperty property, + unsigned int defaultValue) + { + std::string tmp; + + if (!LookupGlobalProperty(tmp, GlobalProperty_DatabasePatchLevel)) + { + return defaultValue; + } + else + { + try + { + return boost::lexical_cast(tmp); + } + catch (boost::bad_lexical_cast&) + { + throw OrthancException(ErrorCode_ParameterOutOfRange, + "Global property " + boost::lexical_cast(property) + + " should be an integer, but found: " + tmp); + } + } + } + + void SQLiteDatabaseWrapper::Open() { db_.Execute("PRAGMA ENCODING=\"UTF-8\";"); @@ -344,49 +369,61 @@ // Make "LIKE" case-sensitive in SQLite db_.Execute("PRAGMA case_sensitive_like = true;"); - if (!db_.DoesTableExist("GlobalProperties")) { - LOG(INFO) << "Creating the database"; - std::string query; - EmbeddedResources::GetFileResource(query, EmbeddedResources::PREPARE_DATABASE); - db_.Execute(query); - } - - // Check the version of the database - std::string tmp; - if (!LookupGlobalProperty(tmp, GlobalProperty_DatabaseSchemaVersion)) - { - tmp = "Unknown"; - } + SQLite::Transaction t(db_); + t.Begin(); - bool ok = false; - try - { - LOG(INFO) << "Version of the Orthanc database: " << tmp; - version_ = boost::lexical_cast(tmp); - ok = true; - } - catch (boost::bad_lexical_cast&) - { - } - - if (!ok) - { - throw OrthancException(ErrorCode_IncompatibleDatabaseVersion, - "Incompatible version of the Orthanc database: " + tmp); - } - - // New in Orthanc 1.5.1 - if (version_ == 6) - { - if (!LookupGlobalProperty(tmp, GlobalProperty_GetTotalSizeIsFast) || - tmp != "1") + if (!db_.DoesTableExist("GlobalProperties")) { - LOG(INFO) << "Installing the SQLite triggers to track the size of the attachments"; + LOG(INFO) << "Creating the database"; std::string query; - EmbeddedResources::GetFileResource(query, EmbeddedResources::INSTALL_TRACK_ATTACHMENTS_SIZE); + EmbeddedResources::GetFileResource(query, EmbeddedResources::PREPARE_DATABASE); db_.Execute(query); } + + // Check the version of the database + std::string tmp; + if (!LookupGlobalProperty(tmp, GlobalProperty_DatabaseSchemaVersion)) + { + tmp = "Unknown"; + } + + bool ok = false; + try + { + LOG(INFO) << "Version of the Orthanc database: " << tmp; + version_ = boost::lexical_cast(tmp); + ok = true; + } + catch (boost::bad_lexical_cast&) + { + } + + if (!ok) + { + throw OrthancException(ErrorCode_IncompatibleDatabaseVersion, + "Incompatible version of the Orthanc database: " + tmp); + } + + // New in Orthanc 1.5.1 + if (version_ == 6) + { + if (!LookupGlobalProperty(tmp, GlobalProperty_GetTotalSizeIsFast) || + tmp != "1") + { + LOG(INFO) << "Installing the SQLite triggers to track the size of the attachments"; + std::string query; + EmbeddedResources::GetFileResource(query, EmbeddedResources::INSTALL_TRACK_ATTACHMENTS_SIZE); + db_.Execute(query); + } + } + + /*if (GetGlobalIntegerProperty(GlobalProperty_DatabasePatchLevel, 0) <= 0) + { + SetGlobalProperty(GlobalProperty_DatabasePatchLevel, "1"); + }*/ + + t.Commit(); } signalRemainingAncestor_ = new Internals::SignalRemainingAncestor; diff -r 2c108461d409 -r 0fa7181ac4e5 OrthancServer/Database/SQLiteDatabaseWrapper.h --- a/OrthancServer/Database/SQLiteDatabaseWrapper.h Sat Jan 12 09:15:32 2019 +0100 +++ b/OrthancServer/Database/SQLiteDatabaseWrapper.h Sat Jan 12 11:08:53 2019 +0100 @@ -79,6 +79,9 @@ void ClearTable(const std::string& tableName); + int GetGlobalIntegerProperty(GlobalProperty property, + unsigned int defaultValue); + public: SQLiteDatabaseWrapper(const std::string& path); diff -r 2c108461d409 -r 0fa7181ac4e5 UnitTestsSources/ServerIndexTests.cpp --- a/UnitTestsSources/ServerIndexTests.cpp Sat Jan 12 09:15:32 2019 +0100 +++ b/UnitTestsSources/ServerIndexTests.cpp Sat Jan 12 11:08:53 2019 +0100 @@ -395,7 +395,7 @@ CheckTableRecordCount(0, "Resources"); CheckTableRecordCount(0, "AttachedFiles"); - CheckTableRecordCount(3, "GlobalProperties"); + CheckTableRecordCount(4, "GlobalProperties"); // 4 since 1.5.2 because of GlobalProperty_DatabasePatchLevel std::string tmp; ASSERT_TRUE(index_->LookupGlobalProperty(tmp, GlobalProperty_DatabaseSchemaVersion));