comparison 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
comparison
equal deleted inserted replaced
1671:2f2e2ec17bc4 1672:4c5a85c3ff43
30 **/ 30 **/
31 31
32 32
33 #include "Database.h" 33 #include "Database.h"
34 34
35 #include "../../../Core/DicomFormat/DicomArray.h"
36
35 #include <EmbeddedResources.h> 37 #include <EmbeddedResources.h>
36 38 #include <boost/lexical_cast.hpp>
37 #include "../../../Core/DicomFormat/DicomArray.h" 39
38 40
39 namespace Internals 41 namespace Internals
40 { 42 {
41 class SignalFileDeleted : public Orthanc::SQLite::IScalarFunction 43 class SignalFileDeleted : public Orthanc::SQLite::IScalarFunction
42 { 44 {
104 } 106 }
105 107
106 virtual void Compute(Orthanc::SQLite::FunctionContext& context) 108 virtual void Compute(Orthanc::SQLite::FunctionContext& context)
107 { 109 {
108 output_.SignalDeletedResource(context.GetStringValue(0), 110 output_.SignalDeletedResource(context.GetStringValue(0),
109 static_cast<OrthancPluginResourceType>(context.GetIntValue(1))); 111 Orthanc::Plugins::Convert(static_cast<Orthanc::ResourceType>(context.GetIntValue(1))));
110 } 112 }
111 }; 113 };
112 } 114 }
113 115
114 116
115 class Database::SignalRemainingAncestor : public Orthanc::SQLite::IScalarFunction 117 class Database::SignalRemainingAncestor : public Orthanc::SQLite::IScalarFunction
116 { 118 {
117 private: 119 private:
118 bool hasRemainingAncestor_; 120 bool hasRemainingAncestor_;
119 std::string remainingPublicId_; 121 std::string remainingPublicId_;
120 Orthanc::ResourceType remainingType_; 122 OrthancPluginResourceType remainingType_;
121 123
122 public: 124 public:
123 SignalRemainingAncestor() : 125 SignalRemainingAncestor() :
124 hasRemainingAncestor_(false) 126 hasRemainingAncestor_(false)
125 { 127 {
145 if (!hasRemainingAncestor_ || 147 if (!hasRemainingAncestor_ ||
146 remainingType_ >= context.GetIntValue(1)) 148 remainingType_ >= context.GetIntValue(1))
147 { 149 {
148 hasRemainingAncestor_ = true; 150 hasRemainingAncestor_ = true;
149 remainingPublicId_ = context.GetStringValue(0); 151 remainingPublicId_ = context.GetStringValue(0);
150 remainingType_ = static_cast<Orthanc::ResourceType>(context.GetIntValue(1)); 152 remainingType_ = Orthanc::Plugins::Convert(static_cast<Orthanc::ResourceType>(context.GetIntValue(1)));
151 } 153 }
152 } 154 }
153 155
154 bool HasRemainingAncestor() const 156 bool HasRemainingAncestor() const
155 { 157 {
160 { 162 {
161 assert(hasRemainingAncestor_); 163 assert(hasRemainingAncestor_);
162 return remainingPublicId_; 164 return remainingPublicId_;
163 } 165 }
164 166
165 Orthanc::ResourceType GetRemainingAncestorType() const 167 OrthancPluginResourceType GetRemainingAncestorType() const
166 { 168 {
167 assert(hasRemainingAncestor_); 169 assert(hasRemainingAncestor_);
168 return remainingType_; 170 return remainingType_;
169 } 171 }
170 }; 172 };
173 175
174 Database::Database(const std::string& path) : 176 Database::Database(const std::string& path) :
175 path_(path), 177 path_(path),
176 base_(db_) 178 base_(db_)
177 { 179 {
180 }
181
182
183 void Database::Open()
184 {
178 db_.Open(path_); 185 db_.Open(path_);
179 Open(); 186
180 } 187 // http://www.sqlite.org/pragma.html
181 188 db_.Execute("PRAGMA SYNCHRONOUS=NORMAL;");
182 189 db_.Execute("PRAGMA JOURNAL_MODE=WAL;");
183 void Database::Open() 190 db_.Execute("PRAGMA LOCKING_MODE=EXCLUSIVE;");
184 { 191 db_.Execute("PRAGMA WAL_AUTOCHECKPOINT=1000;");
192 //db_.Execute("PRAGMA TEMP_STORE=memory");
193
185 if (!db_.DoesTableExist("GlobalProperties")) 194 if (!db_.DoesTableExist("GlobalProperties"))
186 { 195 {
187 std::string query; 196 std::string query;
188 Orthanc::EmbeddedResources::GetFileResource(query, Orthanc::EmbeddedResources::PREPARE_DATABASE); 197 Orthanc::EmbeddedResources::GetFileResource(query, Orthanc::EmbeddedResources::PREPARE_DATABASE);
189 db_.Execute(query); 198 db_.Execute(query);
216 } 225 }
217 226
218 227
219 void Database::DeleteResource(int64_t id) 228 void Database::DeleteResource(int64_t id)
220 { 229 {
221 // TODO 230 signalRemainingAncestor_->Reset();
231
232 Orthanc::SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM Resources WHERE internalId=?");
233 s.BindInt64(0, id);
234 s.Run();
235
236 if (signalRemainingAncestor_->HasRemainingAncestor())
237 {
238 GetOutput().SignalRemainingAncestor(signalRemainingAncestor_->GetRemainingAncestorId(),
239 signalRemainingAncestor_->GetRemainingAncestorType());
240 }
222 } 241 }
223 242
224 243
225 static void Answer(OrthancPlugins::DatabaseBackendOutput& output, 244 static void Answer(OrthancPlugins::DatabaseBackendOutput& output,
226 const Orthanc::ServerIndexChange& change) 245 const Orthanc::ServerIndexChange& change)
227 { 246 {
228 output.AnswerChange(change.GetSeq(), 247 output.AnswerChange(change.GetSeq(),
229 static_cast<int32_t>(change.GetChangeType()), 248 change.GetChangeType(),
230 Orthanc::Plugins::Convert(change.GetResourceType()), 249 Orthanc::Plugins::Convert(change.GetResourceType()),
231 change.GetPublicId(), 250 change.GetPublicId(),
232 change.GetDate()); 251 change.GetDate());
233 } 252 }
234 253
498 } 517 }
499 518
500 519
501 uint32_t Database::GetDatabaseVersion() 520 uint32_t Database::GetDatabaseVersion()
502 { 521 {
503 return 6; 522 std::string version;
523
524 if (!LookupGlobalProperty(version, Orthanc::GlobalProperty_DatabaseSchemaVersion))
525 {
526 throw OrthancPlugins::DatabaseException(OrthancPluginErrorCode_InternalError);
527 }
528
529 try
530 {
531 return boost::lexical_cast<uint32_t>(version);
532 }
533 catch (boost::bad_lexical_cast&)
534 {
535 throw OrthancPlugins::DatabaseException(OrthancPluginErrorCode_InternalError);
536 }
504 } 537 }
505 538
506 539
507 void Database::UpgradeDatabase(uint32_t targetVersion, 540 void Database::UpgradeDatabase(uint32_t targetVersion,
508 OrthancPluginStorageArea* storageArea) 541 OrthancPluginStorageArea* storageArea)