Mercurial > hg > orthanc-databases
comparison Framework/Plugins/StorageBackend.cpp @ 16:9e419261f1c9
mysql storage area working
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 10 Jul 2018 10:10:35 +0200 |
parents | 9774802fd05f |
children | 714c5d2bee76 |
comparison
equal
deleted
inserted
replaced
15:dfc7002add9c | 16:9e419261f1c9 |
---|---|
23 | 23 |
24 #if HAS_ORTHANC_EXCEPTION != 1 | 24 #if HAS_ORTHANC_EXCEPTION != 1 |
25 # error HAS_ORTHANC_EXCEPTION must be set to 1 | 25 # error HAS_ORTHANC_EXCEPTION must be set to 1 |
26 #endif | 26 #endif |
27 | 27 |
28 #include "../../Framework/Common/BinaryStringValue.h" | |
29 #include "../../Framework/Common/FileValue.h" | |
30 | |
28 #include <Core/OrthancException.h> | 31 #include <Core/OrthancException.h> |
29 | 32 |
30 | 33 |
31 #define ORTHANC_PLUGINS_DATABASE_CATCH \ | 34 #define ORTHANC_PLUGINS_DATABASE_CATCH \ |
32 catch (::Orthanc::OrthancException& e) \ | 35 catch (::Orthanc::OrthancException& e) \ |
109 | 112 |
110 free(buffer); | 113 free(buffer); |
111 } | 114 } |
112 | 115 |
113 | 116 |
117 void StorageBackend::Create(DatabaseManager::Transaction& transaction, | |
118 const std::string& uuid, | |
119 const void* content, | |
120 size_t size, | |
121 OrthancPluginContentType type) | |
122 { | |
123 DatabaseManager::CachedStatement statement( | |
124 STATEMENT_FROM_HERE, GetManager(), | |
125 "INSERT INTO StorageArea VALUES (${uuid}, ${content}, ${type})"); | |
126 | |
127 statement.SetParameterType("uuid", ValueType_Utf8String); | |
128 statement.SetParameterType("content", ValueType_File); | |
129 statement.SetParameterType("type", ValueType_Integer64); | |
130 | |
131 Dictionary args; | |
132 args.SetUtf8Value("uuid", uuid); | |
133 args.SetFileValue("content", content, size); | |
134 args.SetIntegerValue("type", type); | |
135 | |
136 statement.Execute(args); | |
137 } | |
138 | |
139 | |
140 void StorageBackend::Read(void*& content, | |
141 size_t& size, | |
142 DatabaseManager::Transaction& transaction, | |
143 const std::string& uuid, | |
144 OrthancPluginContentType type) | |
145 { | |
146 DatabaseManager::CachedStatement statement( | |
147 STATEMENT_FROM_HERE, GetManager(), | |
148 "SELECT content FROM StorageArea WHERE uuid=${uuid} AND type=${type}"); | |
149 | |
150 statement.SetParameterType("uuid", ValueType_Utf8String); | |
151 statement.SetParameterType("type", ValueType_Integer64); | |
152 | |
153 Dictionary args; | |
154 args.SetUtf8Value("uuid", uuid); | |
155 args.SetIntegerValue("type", type); | |
156 | |
157 statement.Execute(args); | |
158 | |
159 if (statement.IsDone()) | |
160 { | |
161 throw Orthanc::OrthancException(Orthanc::ErrorCode_UnknownResource); | |
162 } | |
163 else if (statement.GetResultFieldsCount() != 1) | |
164 { | |
165 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); | |
166 } | |
167 else | |
168 { | |
169 const IValue& value = statement.GetResultField(0); | |
170 | |
171 switch (value.GetType()) | |
172 { | |
173 case ValueType_File: | |
174 ReadFromString(content, size, | |
175 dynamic_cast<const FileValue&>(value).GetContent()); | |
176 break; | |
177 | |
178 case ValueType_BinaryString: | |
179 ReadFromString(content, size, | |
180 dynamic_cast<const BinaryStringValue&>(value).GetContent()); | |
181 break; | |
182 | |
183 default: | |
184 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); | |
185 } | |
186 } | |
187 } | |
188 | |
189 | |
190 void StorageBackend::Remove(DatabaseManager::Transaction& transaction, | |
191 const std::string& uuid, | |
192 OrthancPluginContentType type) | |
193 { | |
194 DatabaseManager::CachedStatement statement( | |
195 STATEMENT_FROM_HERE, GetManager(), | |
196 "DELETE FROM StorageArea WHERE uuid=${uuid} AND type=${type}"); | |
197 | |
198 statement.SetParameterType("uuid", ValueType_Utf8String); | |
199 statement.SetParameterType("type", ValueType_Integer64); | |
200 | |
201 Dictionary args; | |
202 args.SetUtf8Value("uuid", uuid); | |
203 args.SetIntegerValue("type", type); | |
204 | |
205 statement.Execute(args); | |
206 } | |
207 | |
208 | |
209 | |
114 static OrthancPluginContext* context_ = NULL; | 210 static OrthancPluginContext* context_ = NULL; |
115 static std::auto_ptr<StorageBackend> backend_; | 211 static std::auto_ptr<StorageBackend> backend_; |
116 | 212 |
117 | 213 |
118 static OrthancPluginErrorCode StorageCreate(const char* uuid, | 214 static OrthancPluginErrorCode StorageCreate(const char* uuid, |
180 } | 276 } |
181 else | 277 else |
182 { | 278 { |
183 context_ = context; | 279 context_ = context; |
184 backend_.reset(backend); | 280 backend_.reset(backend); |
281 backend_->GetManager().Open(); | |
185 | 282 |
186 OrthancPluginRegisterStorageArea(context_, StorageCreate, StorageRead, StorageRemove); | 283 OrthancPluginRegisterStorageArea(context_, StorageCreate, StorageRead, StorageRemove); |
187 } | 284 } |
188 } | 285 } |
189 | 286 |