Mercurial > hg > orthanc-databases
annotate MySQL/Plugins/MySQLIndex.cpp @ 144:740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 12 Mar 2020 12:10:52 +0100 |
parents | 4cd7e45b671e |
children | 063aa53b5917 |
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 | |
140
4cd7e45b671e
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
5 * Copyright (C) 2017-2020 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 | |
31 #include <Core/Logging.h> | |
32 #include <Core/OrthancException.h> | |
33 | |
34 #include <ctype.h> | |
35 | |
36 namespace OrthancDatabases | |
37 { | |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
38 static void ThrowCannotCreateTrigger() |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
39 { |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
40 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
|
41 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
|
42 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
|
43 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
|
44 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
|
45 "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
|
46 } |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
47 |
0 | 48 IDatabase* MySQLIndex::OpenInternal() |
49 { | |
50 uint32_t expectedVersion = 6; | |
51 if (context_) | |
52 { | |
53 expectedVersion = OrthancPluginGetExpectedDatabaseVersion(context_); | |
54 } | |
55 else | |
56 { | |
57 // This case only occurs during unit testing | |
58 expectedVersion = 6; | |
59 } | |
60 | |
61 // Check the expected version of the database | |
62 if (expectedVersion != 6) | |
63 { | |
64 LOG(ERROR) << "This database plugin is incompatible with your version of Orthanc " | |
65 << "expecting the DB schema version " << expectedVersion | |
66 << ", but this plugin is only compatible with version 6"; | |
67 throw Orthanc::OrthancException(Orthanc::ErrorCode_Plugin); | |
68 } | |
69 | |
60
412e30336847
allowing dollars and underscores in MySQL database identifiers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
70 if (!MySQLDatabase::IsValidDatabaseIdentifier(parameters_.GetDatabase())) |
24
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
71 { |
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
72 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); |
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
73 } |
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
74 |
0 | 75 if (clearAll_) |
76 { | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
77 MySQLDatabase::ClearDatabase(parameters_); |
0 | 78 } |
79 | |
80 std::auto_ptr<MySQLDatabase> db(new MySQLDatabase(parameters_)); | |
81 | |
82 db->Open(); | |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
83 db->Execute("SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE", false); |
0 | 84 |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
85 { |
137
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
86 MySQLDatabase::TransientAdvisoryLock lock(*db, MYSQL_LOCK_DATABASE_SETUP); |
0 | 87 |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
88 /** |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
89 * 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
|
90 * 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
|
91 * 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
|
92 * 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
|
93 * 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
|
94 * 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
|
95 * |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
96 * 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
|
97 * 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
|
98 * 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
|
99 * 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
|
100 * 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
|
101 **/ |
0 | 102 { |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
103 MySQLTransaction t(*db); |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
104 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
105 db->Execute("ALTER DATABASE " + parameters_.GetDatabase() + |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
106 " CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci", false); |
0 | 107 |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
108 // This is the first table to be created |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
109 if (!db->DoesTableExist(t, "GlobalProperties")) |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
110 { |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
111 std::string query; |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
112 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
113 Orthanc::EmbeddedResources::GetFileResource |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
114 (query, Orthanc::EmbeddedResources::MYSQL_PREPARE_INDEX); |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
115 db->Execute(query, true); |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
116 } |
0 | 117 |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
118 t.Commit(); |
0 | 119 } |
120 | |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
121 /** |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
122 * 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
|
123 * 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
|
124 * 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
|
125 * 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
|
126 * transaction). |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
127 **/ |
0 | 128 |
129 int version = 0; | |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
130 |
0 | 131 { |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
132 MySQLTransaction t(*db); |
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 last table to be created |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
135 if (!db->DoesTableExist(t, "PatientRecyclingOrder")) |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
136 { |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
137 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
|
138 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
|
139 } |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
140 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
141 // This is the last item to be created |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
142 if (!db->DoesTriggerExist(t, "PatientAdded")) |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
143 { |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
144 ThrowCannotCreateTrigger(); |
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 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
147 if (!LookupGlobalIntegerProperty(version, *db, t, Orthanc::GlobalProperty_DatabaseSchemaVersion)) |
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 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_DatabaseSchemaVersion, expectedVersion); |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
150 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_DatabasePatchLevel, 1); |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
151 version = expectedVersion; |
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 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
154 if (version != 6) |
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 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
|
157 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
|
158 } |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
159 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
160 t.Commit(); |
0 | 161 } |
162 | |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
163 int revision = 0; |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
164 |
0 | 165 { |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
166 MySQLTransaction t(*db); |
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 if (!LookupGlobalIntegerProperty(revision, *db, t, Orthanc::GlobalProperty_DatabasePatchLevel)) |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
169 { |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
170 revision = 1; |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
171 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_DatabasePatchLevel, revision); |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
172 } |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
173 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
174 t.Commit(); |
0 | 175 } |
176 | |
84
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
177 if (revision == 1) |
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
178 { |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
179 MySQLTransaction t(*db); |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
180 |
84
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
181 // 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
|
182 // 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
|
183 // store up to 4GB: |
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
184 // https://stackoverflow.com/a/13932834/881731 |
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
185 db->Execute("ALTER TABLE GlobalProperties MODIFY value LONGTEXT", false); |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
186 |
84
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
187 revision = 2; |
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
188 SetGlobalIntegerProperty(*db, t, 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
|
189 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
190 t.Commit(); |
84
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
191 } |
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
192 |
87
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
193 if (revision == 2) |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
194 { |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
195 MySQLTransaction t(*db); |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
196 |
110
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
197 // Install the "GetLastChangeIndex" extension |
87
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
198 std::string query; |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
199 |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
200 Orthanc::EmbeddedResources::GetFileResource |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
201 (query, Orthanc::EmbeddedResources::MYSQL_GET_LAST_CHANGE_INDEX); |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
202 db->Execute(query, true); |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
203 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
204 if (!db->DoesTriggerExist(t, "ChangeAdded")) |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
205 { |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
206 ThrowCannotCreateTrigger(); |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
207 } |
87
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
208 |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
209 revision = 3; |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
210 SetGlobalIntegerProperty(*db, t, 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
|
211 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
212 t.Commit(); |
87
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
213 } |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
214 |
109
3f31e3fa5114
MySQL: metadata can store larger values
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
215 if (revision == 3) |
3f31e3fa5114
MySQL: metadata can store larger values
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
216 { |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
217 MySQLTransaction t(*db); |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
218 |
109
3f31e3fa5114
MySQL: metadata can store larger values
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
219 // 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
|
220 // 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
|
221 // 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
|
222 // large amount of metadata. |
3f31e3fa5114
MySQL: metadata can store larger values
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
223 // http://book.orthanc-server.com/faq/features.html#central-registry-of-metadata-and-attachments |
3f31e3fa5114
MySQL: metadata can store larger values
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
224 db->Execute("ALTER TABLE Metadata MODIFY value LONGTEXT", false); |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
225 |
109
3f31e3fa5114
MySQL: metadata can store larger values
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
226 revision = 4; |
3f31e3fa5114
MySQL: metadata can store larger values
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
227 SetGlobalIntegerProperty(*db, t, 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
|
228 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
229 t.Commit(); |
109
3f31e3fa5114
MySQL: metadata can store larger values
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
230 } |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
231 |
110
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
232 if (revision == 4) |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
233 { |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
234 MySQLTransaction t(*db); |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
235 |
110
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
236 // Install the "CreateInstance" extension |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
237 std::string query; |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
238 |
110
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
239 Orthanc::EmbeddedResources::GetFileResource |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
240 (query, Orthanc::EmbeddedResources::MYSQL_CREATE_INSTANCE); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
241 db->Execute(query, true); |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
242 |
110
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
243 revision = 5; |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
244 SetGlobalIntegerProperty(*db, t, 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
|
245 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
246 t.Commit(); |
110
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
247 } |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
248 |
110
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
249 if (revision != 5) |
0 | 250 { |
53 | 251 LOG(ERROR) << "MySQL plugin is incompatible with database schema revision: " << revision; |
0 | 252 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); |
253 } | |
254 } | |
137
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
255 |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
256 /** |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
257 * 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
|
258 * "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
|
259 * 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
|
260 * 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
|
261 * previously-acquired locks. |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
262 * 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
|
263 **/ |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
264 if (parameters_.HasLock()) |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
265 { |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
266 db->AdvisoryLock(MYSQL_LOCK_INDEX); |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
267 } |
0 | 268 |
269 return db.release(); | |
270 } | |
271 | |
272 | |
273 MySQLIndex::MySQLIndex(const MySQLParameters& parameters) : | |
274 IndexBackend(new Factory(*this)), | |
275 context_(NULL), | |
276 parameters_(parameters), | |
277 clearAll_(false) | |
278 { | |
279 } | |
280 | |
281 | |
282 int64_t MySQLIndex::CreateResource(const char* publicId, | |
283 OrthancPluginResourceType type) | |
284 { | |
285 { | |
286 DatabaseManager::CachedStatement statement( | |
287 STATEMENT_FROM_HERE, GetManager(), | |
288 "INSERT INTO Resources VALUES(${}, ${type}, ${id}, NULL)"); | |
289 | |
290 statement.SetParameterType("id", ValueType_Utf8String); | |
291 statement.SetParameterType("type", ValueType_Integer64); | |
292 | |
293 Dictionary args; | |
294 args.SetUtf8Value("id", publicId); | |
295 args.SetIntegerValue("type", static_cast<int>(type)); | |
296 | |
297 statement.Execute(args); | |
298 } | |
299 | |
300 { | |
301 DatabaseManager::CachedStatement statement( | |
302 STATEMENT_FROM_HERE, GetManager(), | |
303 "SELECT LAST_INSERT_ID()"); | |
304 | |
305 statement.Execute(); | |
306 | |
307 return ReadInteger64(statement, 0); | |
308 } | |
309 } | |
310 | |
311 | |
312 void MySQLIndex::DeleteResource(int64_t id) | |
313 { | |
314 ClearDeletedFiles(); | |
315 | |
316 // Recursive exploration of resources to be deleted, from the "id" | |
317 // resource to the top of the tree of resources | |
318 | |
319 bool done = false; | |
320 | |
321 while (!done) | |
322 { | |
323 int64_t parentId; | |
324 | |
325 { | |
326 DatabaseManager::CachedStatement lookupSiblings( | |
327 STATEMENT_FROM_HERE, GetManager(), | |
328 "SELECT parentId FROM Resources " | |
329 "WHERE parentId = (SELECT parentId FROM Resources WHERE internalId=${id});"); | |
330 | |
331 lookupSiblings.SetParameterType("id", ValueType_Integer64); | |
332 | |
333 Dictionary args; | |
334 args.SetIntegerValue("id", id); | |
335 | |
336 lookupSiblings.Execute(args); | |
337 | |
338 if (lookupSiblings.IsDone()) | |
339 { | |
340 // "id" is a root node | |
341 done = true; | |
342 } | |
343 else | |
344 { | |
345 parentId = ReadInteger64(lookupSiblings, 0); | |
346 lookupSiblings.Next(); | |
347 | |
348 if (lookupSiblings.IsDone()) | |
349 { | |
350 // "id" has no sibling node, recursively remove | |
351 done = false; | |
352 id = parentId; | |
353 } | |
354 else | |
355 { | |
356 // "id" has at least one sibling node: the parent node is the remaining ancestor | |
357 done = true; | |
358 | |
359 DatabaseManager::CachedStatement parent( | |
360 STATEMENT_FROM_HERE, GetManager(), | |
361 "SELECT publicId, resourceType FROM Resources WHERE internalId=${id};"); | |
362 | |
363 parent.SetParameterType("id", ValueType_Integer64); | |
364 | |
365 Dictionary args; | |
366 args.SetIntegerValue("id", parentId); | |
367 | |
368 parent.Execute(args); | |
369 | |
370 GetOutput().SignalRemainingAncestor( | |
371 ReadString(parent, 0), | |
372 static_cast<OrthancPluginResourceType>(ReadInteger32(parent, 1))); | |
373 } | |
374 } | |
375 } | |
376 } | |
377 | |
378 { | |
379 DatabaseManager::CachedStatement deleteHierarchy( | |
380 STATEMENT_FROM_HERE, GetManager(), | |
381 "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);"); | |
382 | |
383 deleteHierarchy.SetParameterType("id", ValueType_Integer64); | |
384 | |
385 Dictionary args; | |
386 args.SetIntegerValue("id", id); | |
387 | |
388 deleteHierarchy.Execute(args); | |
389 } | |
390 | |
391 SignalDeletedFiles(); | |
392 } | |
87
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
393 |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
394 |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
395 int64_t MySQLIndex::GetLastChangeIndex() |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
396 { |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
397 DatabaseManager::CachedStatement statement( |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
398 STATEMENT_FROM_HERE, GetManager(), |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
399 "SELECT value FROM GlobalIntegers WHERE property = 0"); |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
400 |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
401 statement.SetReadOnly(true); |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
402 statement.Execute(); |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
403 |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
404 return ReadInteger64(statement, 0); |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
405 } |
110
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
406 |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
407 |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
408 #if ORTHANC_PLUGINS_HAS_DATABASE_CONSTRAINT == 1 |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
409 void MySQLIndex::CreateInstance(OrthancPluginCreateInstanceResult& result, |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
410 const char* hashPatient, |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
411 const char* hashStudy, |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
412 const char* hashSeries, |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
413 const char* hashInstance) |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
414 { |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
415 { |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
416 DatabaseManager::CachedStatement statement( |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
417 STATEMENT_FROM_HERE, GetManager(), |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
418 "CALL CreateInstance(${patient}, ${study}, ${series}, ${instance}, " |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
419 "@isNewPatient, @isNewStudy, @isNewSeries, @isNewInstance, " |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
420 "@patientKey, @studyKey, @seriesKey, @instanceKey)"); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
421 |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
422 statement.SetParameterType("patient", ValueType_Utf8String); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
423 statement.SetParameterType("study", ValueType_Utf8String); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
424 statement.SetParameterType("series", ValueType_Utf8String); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
425 statement.SetParameterType("instance", ValueType_Utf8String); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
426 |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
427 Dictionary args; |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
428 args.SetUtf8Value("patient", hashPatient); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
429 args.SetUtf8Value("study", hashStudy); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
430 args.SetUtf8Value("series", hashSeries); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
431 args.SetUtf8Value("instance", hashInstance); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
432 |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
433 statement.Execute(args); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
434 |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
435 if (!statement.IsDone()) |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
436 { |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
437 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); |
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 |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
441 { |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
442 DatabaseManager::CachedStatement statement( |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
443 STATEMENT_FROM_HERE, GetManager(), |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
444 "SELECT @isNewPatient, @isNewStudy, @isNewSeries, @isNewInstance, " |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
445 "@patientKey, @studyKey, @seriesKey, @instanceKey"); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
446 |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
447 statement.Execute(); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
448 |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
449 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
|
450 { |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
451 statement.SetResultFieldType(i, ValueType_Integer64); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
452 } |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
453 |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
454 result.isNewInstance = (ReadInteger64(statement, 3) == 1); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
455 result.instanceId = ReadInteger64(statement, 7); |
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 if (result.isNewInstance) |
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 result.isNewPatient = (ReadInteger64(statement, 0) == 1); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
460 result.isNewStudy = (ReadInteger64(statement, 1) == 1); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
461 result.isNewSeries = (ReadInteger64(statement, 2) == 1); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
462 result.patientId = ReadInteger64(statement, 4); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
463 result.studyId = ReadInteger64(statement, 5); |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
464 result.seriesId = ReadInteger64(statement, 6); |
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 } |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
467 } |
441a472bfd93
new extension implemented for MySQL: CreateInstance
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
468 #endif |
0 | 469 } |