diff Plugins/Samples/DatabasePlugin/Database.cpp @ 1672:4c5a85c3ff43 db-changes

sample database plugin now working
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 02 Oct 2015 12:20:49 +0200
parents 2f2e2ec17bc4
children 0bbcfd9695e5
line wrap: on
line diff
--- a/Plugins/Samples/DatabasePlugin/Database.cpp	Thu Oct 01 17:44:43 2015 +0200
+++ b/Plugins/Samples/DatabasePlugin/Database.cpp	Fri Oct 02 12:20:49 2015 +0200
@@ -32,9 +32,11 @@
 
 #include "Database.h"
 
-#include <EmbeddedResources.h>
+#include "../../../Core/DicomFormat/DicomArray.h"
 
-#include "../../../Core/DicomFormat/DicomArray.h"
+#include <EmbeddedResources.h>
+#include <boost/lexical_cast.hpp>
+
 
 namespace Internals
 {
@@ -106,7 +108,7 @@
     virtual void Compute(Orthanc::SQLite::FunctionContext& context)
     {
       output_.SignalDeletedResource(context.GetStringValue(0),
-                                    static_cast<OrthancPluginResourceType>(context.GetIntValue(1)));
+                                    Orthanc::Plugins::Convert(static_cast<Orthanc::ResourceType>(context.GetIntValue(1))));
     }
   };
 }
@@ -117,7 +119,7 @@
 private:
   bool hasRemainingAncestor_;
   std::string remainingPublicId_;
-  Orthanc::ResourceType remainingType_;
+  OrthancPluginResourceType remainingType_;
 
 public:
   SignalRemainingAncestor() : 
@@ -147,7 +149,7 @@
     {
       hasRemainingAncestor_ = true;
       remainingPublicId_ = context.GetStringValue(0);
-      remainingType_ = static_cast<Orthanc::ResourceType>(context.GetIntValue(1));
+      remainingType_ = Orthanc::Plugins::Convert(static_cast<Orthanc::ResourceType>(context.GetIntValue(1)));
     }
   }
 
@@ -162,7 +164,7 @@
     return remainingPublicId_;
   }
 
-  Orthanc::ResourceType GetRemainingAncestorType() const
+  OrthancPluginResourceType GetRemainingAncestorType() const
   {
     assert(hasRemainingAncestor_);
     return remainingType_;
@@ -175,13 +177,20 @@
   path_(path),
   base_(db_)
 {
-  db_.Open(path_);
-  Open();
 }
 
 
 void Database::Open()
 {
+  db_.Open(path_);
+
+  // http://www.sqlite.org/pragma.html
+  db_.Execute("PRAGMA SYNCHRONOUS=NORMAL;");
+  db_.Execute("PRAGMA JOURNAL_MODE=WAL;");
+  db_.Execute("PRAGMA LOCKING_MODE=EXCLUSIVE;");
+  db_.Execute("PRAGMA WAL_AUTOCHECKPOINT=1000;");
+  //db_.Execute("PRAGMA TEMP_STORE=memory");
+
   if (!db_.DoesTableExist("GlobalProperties"))
   {
     std::string query;
@@ -218,7 +227,17 @@
 
 void Database::DeleteResource(int64_t id)
 {
-  // TODO
+  signalRemainingAncestor_->Reset();
+
+  Orthanc::SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM Resources WHERE internalId=?");
+  s.BindInt64(0, id);
+  s.Run();
+
+  if (signalRemainingAncestor_->HasRemainingAncestor())
+  {
+    GetOutput().SignalRemainingAncestor(signalRemainingAncestor_->GetRemainingAncestorId(),
+                                        signalRemainingAncestor_->GetRemainingAncestorType());
+  }
 }
 
 
@@ -226,7 +245,7 @@
                    const Orthanc::ServerIndexChange& change)
 {
   output.AnswerChange(change.GetSeq(), 
-                      static_cast<int32_t>(change.GetChangeType()),
+                      change.GetChangeType(),
                       Orthanc::Plugins::Convert(change.GetResourceType()),
                       change.GetPublicId(),
                       change.GetDate());
@@ -500,7 +519,21 @@
 
 uint32_t Database::GetDatabaseVersion()
 {
-  return 6;
+  std::string version;
+
+  if (!LookupGlobalProperty(version, Orthanc::GlobalProperty_DatabaseSchemaVersion))
+  {
+    throw OrthancPlugins::DatabaseException(OrthancPluginErrorCode_InternalError);
+  }
+
+  try
+  {
+    return boost::lexical_cast<uint32_t>(version);
+  }
+  catch (boost::bad_lexical_cast&)
+  {
+    throw OrthancPlugins::DatabaseException(OrthancPluginErrorCode_InternalError);
+  }
 }