Mercurial > hg > orthanc-databases
comparison MySQL/Plugins/MySQLIndex.cpp @ 0:7cea966b6829
initial commit
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 04 Jul 2018 08:16:29 +0200 |
parents | |
children | 9e419261f1c9 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:7cea966b6829 |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "MySQLIndex.h" | |
23 | |
24 #include "../../Framework/Plugins/GlobalProperties.h" | |
25 #include "../../Framework/MySQL/MySQLDatabase.h" | |
26 #include "../../Framework/MySQL/MySQLTransaction.h" | |
27 | |
28 #include <EmbeddedResources.h> // Auto-generated file | |
29 | |
30 #include <Core/Logging.h> | |
31 #include <Core/OrthancException.h> | |
32 | |
33 #include <ctype.h> | |
34 | |
35 namespace OrthancDatabases | |
36 { | |
37 IDatabase* MySQLIndex::OpenInternal() | |
38 { | |
39 uint32_t expectedVersion = 6; | |
40 if (context_) | |
41 { | |
42 expectedVersion = OrthancPluginGetExpectedDatabaseVersion(context_); | |
43 } | |
44 else | |
45 { | |
46 // This case only occurs during unit testing | |
47 expectedVersion = 6; | |
48 } | |
49 | |
50 // Check the expected version of the database | |
51 if (expectedVersion != 6) | |
52 { | |
53 LOG(ERROR) << "This database plugin is incompatible with your version of Orthanc " | |
54 << "expecting the DB schema version " << expectedVersion | |
55 << ", but this plugin is only compatible with version 6"; | |
56 throw Orthanc::OrthancException(Orthanc::ErrorCode_Plugin); | |
57 } | |
58 | |
59 if (parameters_.GetDatabase().empty()) | |
60 { | |
61 LOG(ERROR) << "Empty database name"; | |
62 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
63 } | |
64 | |
65 for (size_t i = 0; i < parameters_.GetDatabase().length(); i++) | |
66 { | |
67 if (!isalnum(parameters_.GetDatabase() [i])) | |
68 { | |
69 LOG(ERROR) << "Only alphanumeric characters are allowed in a " | |
70 << "MySQL database name: \"" << parameters_.GetDatabase() << "\""; | |
71 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
72 } | |
73 } | |
74 | |
75 if (clearAll_) | |
76 { | |
77 MySQLParameters p = parameters_; | |
78 const std::string database = p.GetDatabase(); | |
79 p.SetDatabase(""); | |
80 | |
81 MySQLDatabase db(p); | |
82 db.Open(); | |
83 | |
84 MySQLTransaction t(db); | |
85 db.Execute("DROP DATABASE IF EXISTS " + database); | |
86 db.Execute("CREATE DATABASE " + database); | |
87 t.Commit(); | |
88 } | |
89 | |
90 std::auto_ptr<MySQLDatabase> db(new MySQLDatabase(parameters_)); | |
91 | |
92 db->Open(); | |
93 db->Execute("ALTER DATABASE " + parameters_.GetDatabase() + | |
94 " CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"); | |
95 db->Execute("SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE"); | |
96 | |
97 { | |
98 MySQLTransaction t(*db); | |
99 | |
100 if (!db->DoesTableExist(t, "Resources")) | |
101 { | |
102 std::string query; | |
103 | |
104 Orthanc::EmbeddedResources::GetFileResource | |
105 (query, Orthanc::EmbeddedResources::MYSQL_PREPARE_INDEX); | |
106 db->Execute(query); | |
107 | |
108 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_DatabaseSchemaVersion, expectedVersion); | |
109 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_DatabasePatchLevel, 1); | |
110 } | |
111 | |
112 t.Commit(); | |
113 } | |
114 | |
115 { | |
116 MySQLTransaction t(*db); | |
117 | |
118 if (!db->DoesTableExist(t, "Resources")) | |
119 { | |
120 LOG(ERROR) << "Corrupted MySQL database"; | |
121 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
122 } | |
123 | |
124 int version = 0; | |
125 if (!LookupGlobalIntegerProperty(version, *db, t, Orthanc::GlobalProperty_DatabaseSchemaVersion) || | |
126 version != 6) | |
127 { | |
128 LOG(ERROR) << "PostgreSQL plugin is incompatible with database schema version: " << version; | |
129 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); | |
130 } | |
131 | |
132 int revision; | |
133 if (!LookupGlobalIntegerProperty(revision, *db, t, Orthanc::GlobalProperty_DatabasePatchLevel)) | |
134 { | |
135 revision = 1; | |
136 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_DatabasePatchLevel, revision); | |
137 } | |
138 | |
139 if (revision != 1) | |
140 { | |
141 LOG(ERROR) << "PostgreSQL plugin is incompatible with database schema revision: " << revision; | |
142 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); | |
143 } | |
144 | |
145 t.Rollback(); | |
146 } | |
147 | |
148 return db.release(); | |
149 } | |
150 | |
151 | |
152 MySQLIndex::MySQLIndex(const MySQLParameters& parameters) : | |
153 IndexBackend(new Factory(*this)), | |
154 context_(NULL), | |
155 parameters_(parameters), | |
156 clearAll_(false) | |
157 { | |
158 } | |
159 | |
160 | |
161 int64_t MySQLIndex::CreateResource(const char* publicId, | |
162 OrthancPluginResourceType type) | |
163 { | |
164 { | |
165 DatabaseManager::CachedStatement statement( | |
166 STATEMENT_FROM_HERE, GetManager(), | |
167 "INSERT INTO Resources VALUES(${}, ${type}, ${id}, NULL)"); | |
168 | |
169 statement.SetParameterType("id", ValueType_Utf8String); | |
170 statement.SetParameterType("type", ValueType_Integer64); | |
171 | |
172 Dictionary args; | |
173 args.SetUtf8Value("id", publicId); | |
174 args.SetIntegerValue("type", static_cast<int>(type)); | |
175 | |
176 statement.Execute(args); | |
177 } | |
178 | |
179 { | |
180 DatabaseManager::CachedStatement statement( | |
181 STATEMENT_FROM_HERE, GetManager(), | |
182 "SELECT LAST_INSERT_ID()"); | |
183 | |
184 statement.Execute(); | |
185 | |
186 return ReadInteger64(statement, 0); | |
187 } | |
188 } | |
189 | |
190 | |
191 void MySQLIndex::DeleteResource(int64_t id) | |
192 { | |
193 ClearDeletedFiles(); | |
194 | |
195 // Recursive exploration of resources to be deleted, from the "id" | |
196 // resource to the top of the tree of resources | |
197 | |
198 bool done = false; | |
199 | |
200 while (!done) | |
201 { | |
202 int64_t parentId; | |
203 | |
204 { | |
205 DatabaseManager::CachedStatement lookupSiblings( | |
206 STATEMENT_FROM_HERE, GetManager(), | |
207 "SELECT parentId FROM Resources " | |
208 "WHERE parentId = (SELECT parentId FROM Resources WHERE internalId=${id});"); | |
209 | |
210 lookupSiblings.SetParameterType("id", ValueType_Integer64); | |
211 | |
212 Dictionary args; | |
213 args.SetIntegerValue("id", id); | |
214 | |
215 lookupSiblings.Execute(args); | |
216 | |
217 if (lookupSiblings.IsDone()) | |
218 { | |
219 // "id" is a root node | |
220 done = true; | |
221 } | |
222 else | |
223 { | |
224 parentId = ReadInteger64(lookupSiblings, 0); | |
225 lookupSiblings.Next(); | |
226 | |
227 if (lookupSiblings.IsDone()) | |
228 { | |
229 // "id" has no sibling node, recursively remove | |
230 done = false; | |
231 id = parentId; | |
232 } | |
233 else | |
234 { | |
235 // "id" has at least one sibling node: the parent node is the remaining ancestor | |
236 done = true; | |
237 | |
238 DatabaseManager::CachedStatement parent( | |
239 STATEMENT_FROM_HERE, GetManager(), | |
240 "SELECT publicId, resourceType FROM Resources WHERE internalId=${id};"); | |
241 | |
242 parent.SetParameterType("id", ValueType_Integer64); | |
243 | |
244 Dictionary args; | |
245 args.SetIntegerValue("id", parentId); | |
246 | |
247 parent.Execute(args); | |
248 | |
249 GetOutput().SignalRemainingAncestor( | |
250 ReadString(parent, 0), | |
251 static_cast<OrthancPluginResourceType>(ReadInteger32(parent, 1))); | |
252 } | |
253 } | |
254 } | |
255 } | |
256 | |
257 { | |
258 DatabaseManager::CachedStatement deleteHierarchy( | |
259 STATEMENT_FROM_HERE, GetManager(), | |
260 "DELETE FROM Resources WHERE internalId IN (SELECT * FROM (SELECT internalId FROM Resources WHERE internalId=${id} OR parentId=${id} OR parentId IN (SELECT internalId FROM Resources WHERE parentId=${id}) OR parentId IN (SELECT internalId FROM Resources WHERE parentId IN (SELECT internalId FROM Resources WHERE parentId=${id}))) as t);"); | |
261 | |
262 deleteHierarchy.SetParameterType("id", ValueType_Integer64); | |
263 | |
264 Dictionary args; | |
265 args.SetIntegerValue("id", id); | |
266 | |
267 deleteHierarchy.Execute(args); | |
268 } | |
269 | |
270 SignalDeletedFiles(); | |
271 } | |
272 } |