Mercurial > hg > orthanc-databases
annotate MySQL/Plugins/MySQLIndex.cpp @ 241:a063bbf10a3e
simplification of class DatabaseManager::Transaction
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 13 Apr 2021 12:07:30 +0200 |
parents | f033cc039264 |
children | 33fa478c119a |
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 | |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
157
diff
changeset
|
5 * Copyright (C) 2017-2021 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" | |
137
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
27 #include "MySQLDefinitions.h" |
0 | 28 |
29 #include <EmbeddedResources.h> // Auto-generated file | |
30 | |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
31 #include <Compatibility.h> // For std::unique_ptr<> |
152 | 32 #include <Logging.h> |
33 #include <OrthancException.h> | |
0 | 34 |
35 #include <ctype.h> | |
36 | |
37 namespace OrthancDatabases | |
38 { | |
226
a4918d57435c
DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
225
diff
changeset
|
39 MySQLIndex::MySQLIndex(OrthancPluginContext* context, |
a4918d57435c
DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
225
diff
changeset
|
40 const MySQLParameters& parameters) : |
a4918d57435c
DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
225
diff
changeset
|
41 IndexBackend(context), |
a4918d57435c
DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
225
diff
changeset
|
42 parameters_(parameters), |
a4918d57435c
DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
225
diff
changeset
|
43 clearAll_(false) |
a4918d57435c
DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
225
diff
changeset
|
44 { |
a4918d57435c
DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
225
diff
changeset
|
45 } |
a4918d57435c
DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
225
diff
changeset
|
46 |
a4918d57435c
DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
225
diff
changeset
|
47 |
a4918d57435c
DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
225
diff
changeset
|
48 IDatabase* MySQLIndex::OpenDatabaseConnection() |
a4918d57435c
DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
225
diff
changeset
|
49 { |
a4918d57435c
DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
225
diff
changeset
|
50 return MySQLDatabase::OpenDatabaseConnection(parameters_); |
a4918d57435c
DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
225
diff
changeset
|
51 } |
a4918d57435c
DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
225
diff
changeset
|
52 |
a4918d57435c
DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
225
diff
changeset
|
53 |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
54 static void ThrowCannotCreateTrigger() |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
55 { |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
56 LOG(ERROR) << "The MySQL user is not allowed to create triggers => 2 possible solutions:"; |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
57 LOG(ERROR) << " 1- Give the SUPER privilege to the MySQL database user, or"; |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
58 LOG(ERROR) << " 2- Run \"set global log_bin_trust_function_creators=1;\" as MySQL root user."; |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
59 LOG(ERROR) << "Once you are done, drop and recreate the MySQL database"; |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
60 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database, |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
61 "Need to fix the MySQL permissions for \"CREATE TRIGGER\""); |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
62 } |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
63 |
226
a4918d57435c
DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
225
diff
changeset
|
64 |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
65 void MySQLIndex::ConfigureDatabase(DatabaseManager& manager) |
0 | 66 { |
67 uint32_t expectedVersion = 6; | |
199 | 68 |
69 if (GetContext()) // "GetContext()" can possibly be NULL in the unit tests | |
0 | 70 { |
199 | 71 expectedVersion = OrthancPluginGetExpectedDatabaseVersion(GetContext()); |
0 | 72 } |
73 | |
74 // Check the expected version of the database | |
75 if (expectedVersion != 6) | |
76 { | |
77 LOG(ERROR) << "This database plugin is incompatible with your version of Orthanc " | |
78 << "expecting the DB schema version " << expectedVersion | |
79 << ", but this plugin is only compatible with version 6"; | |
80 throw Orthanc::OrthancException(Orthanc::ErrorCode_Plugin); | |
81 } | |
82 | |
60
412e30336847
allowing dollars and underscores in MySQL database identifiers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
83 if (!MySQLDatabase::IsValidDatabaseIdentifier(parameters_.GetDatabase())) |
24
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
84 { |
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
85 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); |
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
86 } |
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
87 |
0 | 88 if (clearAll_) |
89 { | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
90 MySQLDatabase::ClearDatabase(parameters_); |
0 | 91 } |
92 | |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
93 MySQLDatabase& db = dynamic_cast<MySQLDatabase&>(manager.GetDatabase()); |
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
94 |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
95 { |
226
a4918d57435c
DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
225
diff
changeset
|
96 MySQLDatabase::TransientAdvisoryLock lock(db, MYSQL_LOCK_DATABASE_SETUP); |
0 | 97 |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
98 /** |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
99 * In a first transaction, we create the tables. Such a |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
100 * transaction cannot be rollback: "The CREATE TABLE statement |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
101 * in InnoDB is processed as a single transaction. This means |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
102 * that a ROLLBACK from the user does not undo CREATE TABLE |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
103 * statements the user made during that transaction." |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
104 * https://dev.mysql.com/doc/refman/8.0/en/implicit-commit.html |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
105 * |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
106 * As a consequence, we delay the initial population of the |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
107 * tables in a sequence of transactions below. This solves the |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
108 * error message "MySQL plugin is incompatible with database |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
109 * schema version: 0" that was reported in the forum: |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
110 * https://groups.google.com/d/msg/orthanc-users/OCFFkm1qm0k/Mbroy8VWAQAJ |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
111 **/ |
0 | 112 { |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
113 DatabaseManager::Transaction t(manager, TransactionType_ReadWrite); |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
114 |
241
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
115 t.GetDatabaseTransaction().ExecuteMultiLines("ALTER DATABASE " + parameters_.GetDatabase() + |
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
116 " CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"); |
0 | 117 |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
118 // This is the first table to be created |
241
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
119 if (!t.GetDatabaseTransaction().DoesTableExist("GlobalProperties")) |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
120 { |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
121 std::string query; |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
122 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
123 Orthanc::EmbeddedResources::GetFileResource |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
124 (query, Orthanc::EmbeddedResources::MYSQL_PREPARE_INDEX); |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
125 |
241
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
126 // Need to escape arobases: Don't use "t.GetDatabaseTransaction().ExecuteMultiLines()" here |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
127 db.ExecuteMultiLines(query, true); |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
128 } |
0 | 129 |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
130 t.Commit(); |
0 | 131 } |
132 | |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
133 /** |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
134 * This is the sequence of transactions that initially populate |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
135 * the database. WARNING - As table creation cannot be rollback, |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
136 * don't forget to add "IF NOT EXISTS" if some table must be |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
137 * created below this point (in order to recover from failed |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
138 * transaction). |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
139 **/ |
0 | 140 |
141 int version = 0; | |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
142 |
0 | 143 { |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
144 DatabaseManager::Transaction t(manager, TransactionType_ReadWrite); |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
145 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
146 // This is the last table to be created |
241
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
147 if (!t.GetDatabaseTransaction().DoesTableExist("PatientRecyclingOrder")) |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
148 { |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
149 LOG(ERROR) << "Corrupted MySQL database"; |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
150 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
151 } |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
152 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
153 // This is the last item to be created |
241
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
154 if (!t.GetDatabaseTransaction().DoesTriggerExist("PatientAdded")) |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
155 { |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
156 ThrowCannotCreateTrigger(); |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
157 } |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
158 |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
159 if (!LookupGlobalIntegerProperty(version, manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabaseSchemaVersion)) |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
160 { |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
161 SetGlobalIntegerProperty(manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabaseSchemaVersion, expectedVersion); |
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
162 SetGlobalIntegerProperty(manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabasePatchLevel, 1); |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
163 version = expectedVersion; |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
164 } |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
165 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
166 if (version != 6) |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
167 { |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
168 LOG(ERROR) << "MySQL plugin is incompatible with database schema version: " << version; |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
169 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
170 } |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
171 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
172 t.Commit(); |
0 | 173 } |
174 | |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
175 int revision = 0; |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
176 |
0 | 177 { |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
178 DatabaseManager::Transaction t(manager, TransactionType_ReadWrite); |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
179 |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
180 if (!LookupGlobalIntegerProperty(revision, manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabasePatchLevel)) |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
181 { |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
182 revision = 1; |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
183 SetGlobalIntegerProperty(manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabasePatchLevel, revision); |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
184 } |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
185 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
186 t.Commit(); |
0 | 187 } |
188 | |
84
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
189 if (revision == 1) |
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
190 { |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
191 DatabaseManager::Transaction t(manager, TransactionType_ReadWrite); |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
192 |
84
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
193 // The serialization of jobs as a global property can lead to |
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
194 // very long values => switch to the LONGTEXT type that can |
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
195 // store up to 4GB: |
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
196 // https://stackoverflow.com/a/13932834/881731 |
241
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
197 t.GetDatabaseTransaction().ExecuteMultiLines("ALTER TABLE GlobalProperties MODIFY value LONGTEXT"); |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
198 |
84
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
199 revision = 2; |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
200 SetGlobalIntegerProperty(manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabasePatchLevel, revision); |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
201 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
202 t.Commit(); |
84
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
203 } |
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
204 |
87
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
205 if (revision == 2) |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
206 { |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
207 DatabaseManager::Transaction t(manager, TransactionType_ReadWrite); |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
208 |
110
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
209 // Install the "GetLastChangeIndex" extension |
87
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
210 std::string query; |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
211 |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
212 Orthanc::EmbeddedResources::GetFileResource |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
213 (query, Orthanc::EmbeddedResources::MYSQL_GET_LAST_CHANGE_INDEX); |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
214 |
241
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
215 // Need to escape arobases: Don't use "t.GetDatabaseTransaction().ExecuteMultiLines()" here |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
216 db.ExecuteMultiLines(query, true); |
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
217 |
241
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
218 if (!t.GetDatabaseTransaction().DoesTriggerExist("ChangeAdded")) |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
219 { |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
220 ThrowCannotCreateTrigger(); |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
221 } |
87
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
222 |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
223 revision = 3; |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
224 SetGlobalIntegerProperty(manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabasePatchLevel, revision); |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
225 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
226 t.Commit(); |
87
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
227 } |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
228 |
109
3f31e3fa5114
MySQL: metadata can store larger values
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
229 if (revision == 3) |
3f31e3fa5114
MySQL: metadata can store larger values
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
230 { |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
231 DatabaseManager::Transaction t(manager, TransactionType_ReadWrite); |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
232 |
109
3f31e3fa5114
MySQL: metadata can store larger values
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
233 // Reconfiguration of "Metadata" from TEXT type (up to 64KB) |
3f31e3fa5114
MySQL: metadata can store larger values
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
234 // to the LONGTEXT type (up to 4GB). This might be important |
3f31e3fa5114
MySQL: metadata can store larger values
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
235 // for applications such as the Osimis Web viewer that stores |
3f31e3fa5114
MySQL: metadata can store larger values
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
236 // large amount of metadata. |
3f31e3fa5114
MySQL: metadata can store larger values
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
237 // http://book.orthanc-server.com/faq/features.html#central-registry-of-metadata-and-attachments |
241
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
238 t.GetDatabaseTransaction().ExecuteMultiLines("ALTER TABLE Metadata MODIFY value LONGTEXT"); |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
239 |
109
3f31e3fa5114
MySQL: metadata can store larger values
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
240 revision = 4; |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
241 SetGlobalIntegerProperty(manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabasePatchLevel, revision); |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
242 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
243 t.Commit(); |
109
3f31e3fa5114
MySQL: metadata can store larger values
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
244 } |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
245 |
110
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
246 if (revision == 4) |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
247 { |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
248 DatabaseManager::Transaction t(manager, TransactionType_ReadWrite); |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
249 |
110
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
250 // Install the "CreateInstance" extension |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
251 std::string query; |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
252 |
110
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
253 Orthanc::EmbeddedResources::GetFileResource |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
254 (query, Orthanc::EmbeddedResources::MYSQL_CREATE_INSTANCE); |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
255 |
241
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
256 // Need to escape arobases: Don't use "t.GetDatabaseTransaction().ExecuteMultiLines()" here |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
257 db.ExecuteMultiLines(query, true); |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
258 |
110
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
259 revision = 5; |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
260 SetGlobalIntegerProperty(manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabasePatchLevel, revision); |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
261 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
262 t.Commit(); |
110
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
263 } |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
264 |
110
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
265 if (revision != 5) |
0 | 266 { |
53 | 267 LOG(ERROR) << "MySQL plugin is incompatible with database schema revision: " << revision; |
0 | 268 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); |
269 } | |
238
f033cc039264
new table: "ServerProperties"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
270 |
f033cc039264
new table: "ServerProperties"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
271 |
f033cc039264
new table: "ServerProperties"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
272 { |
f033cc039264
new table: "ServerProperties"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
273 // New in release 4.0 to deal with multiple writers |
f033cc039264
new table: "ServerProperties"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
274 DatabaseManager::Transaction t(manager, TransactionType_ReadWrite); |
f033cc039264
new table: "ServerProperties"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
275 |
241
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
276 if (!t.GetDatabaseTransaction().DoesTableExist("ServerProperties")) |
238
f033cc039264
new table: "ServerProperties"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
277 { |
241
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
278 t.GetDatabaseTransaction().ExecuteMultiLines("CREATE TABLE ServerProperties(server VARCHAR(64) NOT NULL, " |
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
279 "property INTEGER, value TEXT, PRIMARY KEY(server, property))"); |
238
f033cc039264
new table: "ServerProperties"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
280 } |
f033cc039264
new table: "ServerProperties"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
281 |
f033cc039264
new table: "ServerProperties"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
282 t.Commit(); |
f033cc039264
new table: "ServerProperties"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
283 } |
0 | 284 } |
137
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
285 |
238
f033cc039264
new table: "ServerProperties"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
286 |
137
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
287 /** |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
288 * WARNING: This lock must be acquired after |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
289 * "MYSQL_LOCK_DATABASE_SETUP" is released. Indeed, in MySQL < |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
290 * 5.7, it is impossible to acquire more than one lock at a time, |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
291 * as calling "SELECT GET_LOCK()" releases all the |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
292 * previously-acquired locks. |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
293 * https://dev.mysql.com/doc/refman/5.7/en/locking-functions.html |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
294 **/ |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
295 if (parameters_.HasLock()) |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
296 { |
226
a4918d57435c
DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
225
diff
changeset
|
297 db.AdvisoryLock(MYSQL_LOCK_INDEX); |
137
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
298 } |
0 | 299 } |
300 | |
301 | |
225
94c9908e6aca
removed DatabaseManager member out of class IndexBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
302 int64_t MySQLIndex::CreateResource(DatabaseManager& manager, |
94c9908e6aca
removed DatabaseManager member out of class IndexBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
303 const char* publicId, |
0 | 304 OrthancPluginResourceType type) |
305 { | |
306 { | |
307 DatabaseManager::CachedStatement statement( | |
225
94c9908e6aca
removed DatabaseManager member out of class IndexBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
308 STATEMENT_FROM_HERE, manager, |
0 | 309 "INSERT INTO Resources VALUES(${}, ${type}, ${id}, NULL)"); |
310 | |
311 statement.SetParameterType("id", ValueType_Utf8String); | |
312 statement.SetParameterType("type", ValueType_Integer64); | |
313 | |
314 Dictionary args; | |
315 args.SetUtf8Value("id", publicId); | |
316 args.SetIntegerValue("type", static_cast<int>(type)); | |
317 | |
318 statement.Execute(args); | |
319 } | |
320 | |
321 { | |
322 DatabaseManager::CachedStatement statement( | |
225
94c9908e6aca
removed DatabaseManager member out of class IndexBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
323 STATEMENT_FROM_HERE, manager, |
0 | 324 "SELECT LAST_INSERT_ID()"); |
325 | |
326 statement.Execute(); | |
327 | |
328 return ReadInteger64(statement, 0); | |
329 } | |
330 } | |
331 | |
332 | |
203
2089d4071408
moving classes out of OrthancPlugins namespace, to OrthancDatabases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
201
diff
changeset
|
333 void MySQLIndex::DeleteResource(IDatabaseBackendOutput& output, |
225
94c9908e6aca
removed DatabaseManager member out of class IndexBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
334 DatabaseManager& manager, |
201
42990b2dd51b
create IDatabaseBackendOutput only if needed
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
335 int64_t id) |
0 | 336 { |
225
94c9908e6aca
removed DatabaseManager member out of class IndexBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
337 ClearDeletedFiles(manager); |
0 | 338 |
339 // Recursive exploration of resources to be deleted, from the "id" | |
340 // resource to the top of the tree of resources | |
341 | |
342 bool done = false; | |
343 | |
344 while (!done) | |
345 { | |
346 int64_t parentId; | |
347 | |
348 { | |
349 DatabaseManager::CachedStatement lookupSiblings( | |
225
94c9908e6aca
removed DatabaseManager member out of class IndexBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
350 STATEMENT_FROM_HERE, manager, |
0 | 351 "SELECT parentId FROM Resources " |
352 "WHERE parentId = (SELECT parentId FROM Resources WHERE internalId=${id});"); | |
353 | |
354 lookupSiblings.SetParameterType("id", ValueType_Integer64); | |
355 | |
356 Dictionary args; | |
357 args.SetIntegerValue("id", id); | |
358 | |
359 lookupSiblings.Execute(args); | |
360 | |
361 if (lookupSiblings.IsDone()) | |
362 { | |
363 // "id" is a root node | |
364 done = true; | |
365 } | |
366 else | |
367 { | |
368 parentId = ReadInteger64(lookupSiblings, 0); | |
369 lookupSiblings.Next(); | |
370 | |
371 if (lookupSiblings.IsDone()) | |
372 { | |
373 // "id" has no sibling node, recursively remove | |
374 done = false; | |
375 id = parentId; | |
376 } | |
377 else | |
378 { | |
379 // "id" has at least one sibling node: the parent node is the remaining ancestor | |
380 done = true; | |
381 | |
382 DatabaseManager::CachedStatement parent( | |
225
94c9908e6aca
removed DatabaseManager member out of class IndexBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
383 STATEMENT_FROM_HERE, manager, |
0 | 384 "SELECT publicId, resourceType FROM Resources WHERE internalId=${id};"); |
385 | |
386 parent.SetParameterType("id", ValueType_Integer64); | |
387 | |
388 Dictionary args; | |
389 args.SetIntegerValue("id", parentId); | |
390 | |
391 parent.Execute(args); | |
392 | |
201
42990b2dd51b
create IDatabaseBackendOutput only if needed
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
393 output.SignalRemainingAncestor( |
0 | 394 ReadString(parent, 0), |
395 static_cast<OrthancPluginResourceType>(ReadInteger32(parent, 1))); | |
396 } | |
397 } | |
398 } | |
399 } | |
400 | |
401 { | |
402 DatabaseManager::CachedStatement deleteHierarchy( | |
225
94c9908e6aca
removed DatabaseManager member out of class IndexBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
403 STATEMENT_FROM_HERE, manager, |
0 | 404 "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);"); |
405 | |
406 deleteHierarchy.SetParameterType("id", ValueType_Integer64); | |
407 | |
408 Dictionary args; | |
409 args.SetIntegerValue("id", id); | |
410 | |
411 deleteHierarchy.Execute(args); | |
412 } | |
413 | |
225
94c9908e6aca
removed DatabaseManager member out of class IndexBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
414 SignalDeletedFiles(output, manager); |
0 | 415 } |
87
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
416 |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
417 |
225
94c9908e6aca
removed DatabaseManager member out of class IndexBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
418 int64_t MySQLIndex::GetLastChangeIndex(DatabaseManager& manager) |
87
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
419 { |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
420 DatabaseManager::CachedStatement statement( |
225
94c9908e6aca
removed DatabaseManager member out of class IndexBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
421 STATEMENT_FROM_HERE, manager, |
87
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
422 "SELECT value FROM GlobalIntegers WHERE property = 0"); |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
423 |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
424 statement.SetReadOnly(true); |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
425 statement.Execute(); |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
426 |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
427 return ReadInteger64(statement, 0); |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
428 } |
110
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
429 |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
430 |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
431 #if ORTHANC_PLUGINS_HAS_DATABASE_CONSTRAINT == 1 |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
432 void MySQLIndex::CreateInstance(OrthancPluginCreateInstanceResult& result, |
225
94c9908e6aca
removed DatabaseManager member out of class IndexBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
433 DatabaseManager& manager, |
110
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
434 const char* hashPatient, |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
435 const char* hashStudy, |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
436 const char* hashSeries, |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
437 const char* hashInstance) |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
438 { |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
439 { |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
440 DatabaseManager::CachedStatement statement( |
225
94c9908e6aca
removed DatabaseManager member out of class IndexBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
441 STATEMENT_FROM_HERE, manager, |
110
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
442 "CALL CreateInstance(${patient}, ${study}, ${series}, ${instance}, " |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
443 "@isNewPatient, @isNewStudy, @isNewSeries, @isNewInstance, " |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
444 "@patientKey, @studyKey, @seriesKey, @instanceKey)"); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
445 |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
446 statement.SetParameterType("patient", ValueType_Utf8String); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
447 statement.SetParameterType("study", ValueType_Utf8String); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
448 statement.SetParameterType("series", ValueType_Utf8String); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
449 statement.SetParameterType("instance", ValueType_Utf8String); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
450 |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
451 Dictionary args; |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
452 args.SetUtf8Value("patient", hashPatient); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
453 args.SetUtf8Value("study", hashStudy); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
454 args.SetUtf8Value("series", hashSeries); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
455 args.SetUtf8Value("instance", hashInstance); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
456 |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
457 statement.Execute(args); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
458 |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
459 if (!statement.IsDone()) |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
460 { |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
461 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
462 } |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
463 } |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
464 |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
465 { |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
466 DatabaseManager::CachedStatement statement( |
225
94c9908e6aca
removed DatabaseManager member out of class IndexBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
467 STATEMENT_FROM_HERE, manager, |
110
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
468 "SELECT @isNewPatient, @isNewStudy, @isNewSeries, @isNewInstance, " |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
469 "@patientKey, @studyKey, @seriesKey, @instanceKey"); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
470 |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
471 statement.Execute(); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
472 |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
473 for (size_t i = 0; i < 8; i++) |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
474 { |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
475 statement.SetResultFieldType(i, ValueType_Integer64); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
476 } |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
477 |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
478 result.isNewInstance = (ReadInteger64(statement, 3) == 1); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
479 result.instanceId = ReadInteger64(statement, 7); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
480 |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
481 if (result.isNewInstance) |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
482 { |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
483 result.isNewPatient = (ReadInteger64(statement, 0) == 1); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
484 result.isNewStudy = (ReadInteger64(statement, 1) == 1); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
485 result.isNewSeries = (ReadInteger64(statement, 2) == 1); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
486 result.patientId = ReadInteger64(statement, 4); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
487 result.studyId = ReadInteger64(statement, 5); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
488 result.seriesId = ReadInteger64(statement, 6); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
489 } |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
490 } |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
491 } |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
492 #endif |
0 | 493 } |