Mercurial > hg > orthanc-databases
annotate MySQL/Plugins/MySQLIndex.cpp @ 83:ff2d56d37bfd
fix windows build
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 10 Jan 2019 14:51:51 +0100 |
parents | 714c5d2bee76 |
children | b96446b8718b |
rev | line source |
---|---|
0 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
67 | 5 * Copyright (C) 2017-2019 Osimis S.A., Belgium |
0 | 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 | |
60
412e30336847
allowing dollars and underscores in MySQL database identifiers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
59 if (!MySQLDatabase::IsValidDatabaseIdentifier(parameters_.GetDatabase())) |
24
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
60 { |
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
61 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); |
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
62 } |
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
63 |
0 | 64 if (clearAll_) |
65 { | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
66 MySQLDatabase::ClearDatabase(parameters_); |
0 | 67 } |
68 | |
69 std::auto_ptr<MySQLDatabase> db(new MySQLDatabase(parameters_)); | |
70 | |
71 db->Open(); | |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
72 |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
73 db->Execute("SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE", false); |
0 | 74 |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
75 if (parameters_.HasLock()) |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
76 { |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
77 db->AdvisoryLock(42 /* some arbitrary constant */); |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
78 } |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
79 |
0 | 80 { |
81 MySQLTransaction t(*db); | |
82 | |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
83 db->Execute("ALTER DATABASE " + parameters_.GetDatabase() + |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
84 " CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci", false); |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
85 |
0 | 86 if (!db->DoesTableExist(t, "Resources")) |
87 { | |
88 std::string query; | |
89 | |
90 Orthanc::EmbeddedResources::GetFileResource | |
91 (query, Orthanc::EmbeddedResources::MYSQL_PREPARE_INDEX); | |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
92 db->Execute(query, true); |
0 | 93 |
94 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_DatabaseSchemaVersion, expectedVersion); | |
95 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_DatabasePatchLevel, 1); | |
96 } | |
97 | |
98 if (!db->DoesTableExist(t, "Resources")) | |
99 { | |
100 LOG(ERROR) << "Corrupted MySQL database"; | |
101 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
102 } | |
103 | |
104 int version = 0; | |
105 if (!LookupGlobalIntegerProperty(version, *db, t, Orthanc::GlobalProperty_DatabaseSchemaVersion) || | |
106 version != 6) | |
107 { | |
53 | 108 LOG(ERROR) << "MySQL plugin is incompatible with database schema version: " << version; |
0 | 109 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); |
110 } | |
111 | |
112 int revision; | |
113 if (!LookupGlobalIntegerProperty(revision, *db, t, Orthanc::GlobalProperty_DatabasePatchLevel)) | |
114 { | |
115 revision = 1; | |
116 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_DatabasePatchLevel, revision); | |
117 } | |
118 | |
119 if (revision != 1) | |
120 { | |
53 | 121 LOG(ERROR) << "MySQL plugin is incompatible with database schema revision: " << revision; |
0 | 122 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); |
123 } | |
124 | |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
125 t.Commit(); |
0 | 126 } |
127 | |
128 return db.release(); | |
129 } | |
130 | |
131 | |
132 MySQLIndex::MySQLIndex(const MySQLParameters& parameters) : | |
133 IndexBackend(new Factory(*this)), | |
134 context_(NULL), | |
135 parameters_(parameters), | |
136 clearAll_(false) | |
137 { | |
138 } | |
139 | |
140 | |
141 int64_t MySQLIndex::CreateResource(const char* publicId, | |
142 OrthancPluginResourceType type) | |
143 { | |
144 { | |
145 DatabaseManager::CachedStatement statement( | |
146 STATEMENT_FROM_HERE, GetManager(), | |
147 "INSERT INTO Resources VALUES(${}, ${type}, ${id}, NULL)"); | |
148 | |
149 statement.SetParameterType("id", ValueType_Utf8String); | |
150 statement.SetParameterType("type", ValueType_Integer64); | |
151 | |
152 Dictionary args; | |
153 args.SetUtf8Value("id", publicId); | |
154 args.SetIntegerValue("type", static_cast<int>(type)); | |
155 | |
156 statement.Execute(args); | |
157 } | |
158 | |
159 { | |
160 DatabaseManager::CachedStatement statement( | |
161 STATEMENT_FROM_HERE, GetManager(), | |
162 "SELECT LAST_INSERT_ID()"); | |
163 | |
164 statement.Execute(); | |
165 | |
166 return ReadInteger64(statement, 0); | |
167 } | |
168 } | |
169 | |
170 | |
171 void MySQLIndex::DeleteResource(int64_t id) | |
172 { | |
173 ClearDeletedFiles(); | |
174 | |
175 // Recursive exploration of resources to be deleted, from the "id" | |
176 // resource to the top of the tree of resources | |
177 | |
178 bool done = false; | |
179 | |
180 while (!done) | |
181 { | |
182 int64_t parentId; | |
183 | |
184 { | |
185 DatabaseManager::CachedStatement lookupSiblings( | |
186 STATEMENT_FROM_HERE, GetManager(), | |
187 "SELECT parentId FROM Resources " | |
188 "WHERE parentId = (SELECT parentId FROM Resources WHERE internalId=${id});"); | |
189 | |
190 lookupSiblings.SetParameterType("id", ValueType_Integer64); | |
191 | |
192 Dictionary args; | |
193 args.SetIntegerValue("id", id); | |
194 | |
195 lookupSiblings.Execute(args); | |
196 | |
197 if (lookupSiblings.IsDone()) | |
198 { | |
199 // "id" is a root node | |
200 done = true; | |
201 } | |
202 else | |
203 { | |
204 parentId = ReadInteger64(lookupSiblings, 0); | |
205 lookupSiblings.Next(); | |
206 | |
207 if (lookupSiblings.IsDone()) | |
208 { | |
209 // "id" has no sibling node, recursively remove | |
210 done = false; | |
211 id = parentId; | |
212 } | |
213 else | |
214 { | |
215 // "id" has at least one sibling node: the parent node is the remaining ancestor | |
216 done = true; | |
217 | |
218 DatabaseManager::CachedStatement parent( | |
219 STATEMENT_FROM_HERE, GetManager(), | |
220 "SELECT publicId, resourceType FROM Resources WHERE internalId=${id};"); | |
221 | |
222 parent.SetParameterType("id", ValueType_Integer64); | |
223 | |
224 Dictionary args; | |
225 args.SetIntegerValue("id", parentId); | |
226 | |
227 parent.Execute(args); | |
228 | |
229 GetOutput().SignalRemainingAncestor( | |
230 ReadString(parent, 0), | |
231 static_cast<OrthancPluginResourceType>(ReadInteger32(parent, 1))); | |
232 } | |
233 } | |
234 } | |
235 } | |
236 | |
237 { | |
238 DatabaseManager::CachedStatement deleteHierarchy( | |
239 STATEMENT_FROM_HERE, GetManager(), | |
240 "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);"); | |
241 | |
242 deleteHierarchy.SetParameterType("id", ValueType_Integer64); | |
243 | |
244 Dictionary args; | |
245 args.SetIntegerValue("id", id); | |
246 | |
247 deleteHierarchy.Execute(args); | |
248 } | |
249 | |
250 SignalDeletedFiles(); | |
251 } | |
252 } |