Mercurial > hg > orthanc-databases
annotate SQLite/Plugins/SQLiteIndex.cpp @ 524:48aba35fe64e large-queries
merged BEGIN and SET TRANSACTION statements
author | Alain Mazy <am@orthanc.team> |
---|---|
date | Tue, 09 Jul 2024 16:30:52 +0200 |
parents | 54d518dcd74a |
children | 451125122692 |
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 | |
507
54d518dcd74a
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
459
diff
changeset
|
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium |
54d518dcd74a
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
459
diff
changeset
|
6 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium |
459
ecd0b719cff5
update year to 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
403
diff
changeset
|
7 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
0 | 8 * |
9 * This program is free software: you can redistribute it and/or | |
10 * modify it under the terms of the GNU Affero General Public License | |
11 * as published by the Free Software Foundation, either version 3 of | |
12 * the License, or (at your option) any later version. | |
13 * | |
14 * This program is distributed in the hope that it will be useful, but | |
15 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 * Affero General Public License for more details. | |
18 * | |
19 * You should have received a copy of the GNU Affero General Public License | |
20 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 **/ | |
22 | |
23 | |
24 #include "SQLiteIndex.h" | |
25 | |
85
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
26 #include "../../Framework/Common/Integer64Value.h" |
0 | 27 #include "../../Framework/Plugins/GlobalProperties.h" |
28 #include "../../Framework/SQLite/SQLiteDatabase.h" | |
29 #include "../../Framework/SQLite/SQLiteTransaction.h" | |
30 | |
31 #include <EmbeddedResources.h> // Auto-generated file | |
32 | |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
33 #include <Compatibility.h> // For std::unique_ptr<> |
152 | 34 #include <Logging.h> |
35 #include <OrthancException.h> | |
0 | 36 |
37 namespace OrthancDatabases | |
38 { | |
255
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
39 IDatabaseFactory* SQLiteIndex::CreateDatabaseFactory() |
0 | 40 { |
255
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
41 class Factory : public IDatabaseFactory |
226
a4918d57435c
DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
225
diff
changeset
|
42 { |
255
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
43 private: |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
44 std::string path_; |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
45 bool fast_; |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
46 |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
47 public: |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
48 Factory(const std::string& path, |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
49 bool fast) : |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
50 path_(path), |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
51 fast_(fast) |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
52 { |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
53 } |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
54 |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
55 virtual IDatabase* Open() ORTHANC_OVERRIDE |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
56 { |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
57 std::unique_ptr<SQLiteDatabase> db(new SQLiteDatabase); |
226
a4918d57435c
DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
225
diff
changeset
|
58 |
255
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
59 if (path_.empty()) |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
60 { |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
61 db->OpenInMemory(); |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
62 } |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
63 else |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
64 { |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
65 db->Open(path_); |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
66 } |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
67 |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
68 db->Execute("PRAGMA ENCODING=\"UTF-8\";"); |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
69 |
255
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
70 if (fast_) |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
71 { |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
72 // Performance tuning of SQLite with PRAGMAs |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
73 // http://www.sqlite.org/pragma.html |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
74 db->Execute("PRAGMA SYNCHRONOUS=NORMAL;"); |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
75 db->Execute("PRAGMA JOURNAL_MODE=WAL;"); |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
76 db->Execute("PRAGMA LOCKING_MODE=EXCLUSIVE;"); |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
77 db->Execute("PRAGMA WAL_AUTOCHECKPOINT=1000;"); |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
78 //db->Execute("PRAGMA TEMP_STORE=memory"); |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
79 } |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
80 |
255
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
81 return db.release(); |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
82 } |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
83 }; |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
84 |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
85 return new Factory(path_, fast_); |
226
a4918d57435c
DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
225
diff
changeset
|
86 } |
a4918d57435c
DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
225
diff
changeset
|
87 |
a4918d57435c
DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
225
diff
changeset
|
88 |
403
91124cc8a8c7
database plugins are informed about the identifier tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
399
diff
changeset
|
89 void SQLiteIndex::ConfigureDatabase(DatabaseManager& manager, |
91124cc8a8c7
database plugins are informed about the identifier tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
399
diff
changeset
|
90 bool hasIdentifierTags, |
91124cc8a8c7
database plugins are informed about the identifier tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
399
diff
changeset
|
91 const std::list<IdentifierTag>& identifierTags) |
226
a4918d57435c
DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
225
diff
changeset
|
92 { |
0 | 93 uint32_t expectedVersion = 6; |
199 | 94 |
95 if (GetContext()) // "GetContext()" can possibly be NULL in the unit tests | |
0 | 96 { |
199 | 97 expectedVersion = OrthancPluginGetExpectedDatabaseVersion(GetContext()); |
0 | 98 } |
99 | |
100 // Check the expected version of the database | |
101 if (expectedVersion != 6) | |
102 { | |
103 LOG(ERROR) << "This database plugin is incompatible with your version of Orthanc " | |
104 << "expecting the DB schema version " << expectedVersion | |
105 << ", but this plugin is only compatible with version 6"; | |
106 throw Orthanc::OrthancException(Orthanc::ErrorCode_Plugin); | |
107 } | |
108 | |
226
a4918d57435c
DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
225
diff
changeset
|
109 { |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
110 DatabaseManager::Transaction t(manager, TransactionType_ReadWrite); |
0 | 111 |
241
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
112 if (!t.GetDatabaseTransaction().DoesTableExist("Resources")) |
0 | 113 { |
114 std::string query; | |
115 | |
116 Orthanc::EmbeddedResources::GetFileResource | |
117 (query, Orthanc::EmbeddedResources::SQLITE_PREPARE_INDEX); | |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
118 |
241
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
119 t.GetDatabaseTransaction().ExecuteMultiLines(query); |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
120 |
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
121 SetGlobalIntegerProperty(manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabaseSchemaVersion, expectedVersion); |
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
122 SetGlobalIntegerProperty(manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabasePatchLevel, 1); |
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
123 } |
0 | 124 |
125 t.Commit(); | |
126 } | |
127 | |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
128 { |
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
129 DatabaseManager::Transaction t(manager, TransactionType_ReadWrite); |
0 | 130 |
241
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
131 if (!t.GetDatabaseTransaction().DoesTableExist("Resources")) |
0 | 132 { |
133 LOG(ERROR) << "Corrupted SQLite database"; | |
134 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
135 } | |
136 | |
137 int version = 0; | |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
138 if (!LookupGlobalIntegerProperty(version, manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabaseSchemaVersion) || |
0 | 139 version != 6) |
140 { | |
141 LOG(ERROR) << "SQLite plugin is incompatible with database schema version: " << version; | |
142 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); | |
143 } | |
144 | |
145 int revision; | |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
146 if (!LookupGlobalIntegerProperty(revision, manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabasePatchLevel)) |
0 | 147 { |
148 revision = 1; | |
237
35598014f140
refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
226
diff
changeset
|
149 SetGlobalIntegerProperty(manager, MISSING_SERVER_IDENTIFIER, Orthanc::GlobalProperty_DatabasePatchLevel, revision); |
0 | 150 } |
151 | |
152 if (revision != 1) | |
153 { | |
154 LOG(ERROR) << "SQLite plugin is incompatible with database schema revision: " << revision; | |
155 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); | |
156 } | |
157 | |
158 t.Commit(); | |
159 } | |
238
f033cc039264
new table: "ServerProperties"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
160 |
f033cc039264
new table: "ServerProperties"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
161 { |
f033cc039264
new table: "ServerProperties"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
162 DatabaseManager::Transaction t(manager, TransactionType_ReadWrite); |
f033cc039264
new table: "ServerProperties"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
163 |
241
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
164 if (!t.GetDatabaseTransaction().DoesTableExist("ServerProperties")) |
238
f033cc039264
new table: "ServerProperties"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
165 { |
241
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
166 t.GetDatabaseTransaction().ExecuteMultiLines("CREATE TABLE ServerProperties(server TEXT, " |
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
167 "property INTEGER, value TEXT, PRIMARY KEY(server, property))"); |
238
f033cc039264
new table: "ServerProperties"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
168 } |
f033cc039264
new table: "ServerProperties"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
169 |
f033cc039264
new table: "ServerProperties"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
170 t.Commit(); |
f033cc039264
new table: "ServerProperties"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
171 } |
399
19bd3ee1f0b3
support for labels in sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
389
diff
changeset
|
172 |
19bd3ee1f0b3
support for labels in sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
389
diff
changeset
|
173 { |
19bd3ee1f0b3
support for labels in sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
389
diff
changeset
|
174 DatabaseManager::Transaction t(manager, TransactionType_ReadWrite); |
19bd3ee1f0b3
support for labels in sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
389
diff
changeset
|
175 |
19bd3ee1f0b3
support for labels in sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
389
diff
changeset
|
176 if (!t.GetDatabaseTransaction().DoesTableExist("Labels")) |
19bd3ee1f0b3
support for labels in sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
389
diff
changeset
|
177 { |
19bd3ee1f0b3
support for labels in sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
389
diff
changeset
|
178 t.GetDatabaseTransaction().ExecuteMultiLines( |
19bd3ee1f0b3
support for labels in sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
389
diff
changeset
|
179 "CREATE TABLE Labels(" |
19bd3ee1f0b3
support for labels in sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
389
diff
changeset
|
180 " id INTEGER REFERENCES Resources(internalId) ON DELETE CASCADE," |
19bd3ee1f0b3
support for labels in sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
389
diff
changeset
|
181 " label TEXT NOT NULL," |
19bd3ee1f0b3
support for labels in sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
389
diff
changeset
|
182 " PRIMARY KEY(id, label));" |
19bd3ee1f0b3
support for labels in sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
389
diff
changeset
|
183 "CREATE INDEX LabelsIndex1 ON Labels(id);" |
19bd3ee1f0b3
support for labels in sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
389
diff
changeset
|
184 "CREATE INDEX LabelsIndex2 ON Labels(label);"); |
19bd3ee1f0b3
support for labels in sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
389
diff
changeset
|
185 } |
19bd3ee1f0b3
support for labels in sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
389
diff
changeset
|
186 |
19bd3ee1f0b3
support for labels in sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
389
diff
changeset
|
187 t.Commit(); |
19bd3ee1f0b3
support for labels in sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
389
diff
changeset
|
188 } |
0 | 189 } |
190 | |
191 | |
201
42990b2dd51b
create IDatabaseBackendOutput only if needed
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
192 SQLiteIndex::SQLiteIndex(OrthancPluginContext* context, |
42990b2dd51b
create IDatabaseBackendOutput only if needed
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
193 const std::string& path) : |
225
94c9908e6aca
removed DatabaseManager member out of class IndexBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
194 IndexBackend(context), |
0 | 195 path_(path), |
196 fast_(true) | |
197 { | |
198 if (path.empty()) | |
199 { | |
200 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
201 } | |
202 } | |
203 | |
204 | |
201
42990b2dd51b
create IDatabaseBackendOutput only if needed
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
205 SQLiteIndex::SQLiteIndex(OrthancPluginContext* context) : |
225
94c9908e6aca
removed DatabaseManager member out of class IndexBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
206 IndexBackend(context), |
0 | 207 fast_(true) |
208 { | |
209 } | |
210 | |
211 | |
225
94c9908e6aca
removed DatabaseManager member out of class IndexBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
212 int64_t SQLiteIndex::CreateResource(DatabaseManager& manager, |
94c9908e6aca
removed DatabaseManager member out of class IndexBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
213 const char* publicId, |
0 | 214 OrthancPluginResourceType type) |
215 { | |
216 DatabaseManager::CachedStatement statement( | |
225
94c9908e6aca
removed DatabaseManager member out of class IndexBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
217 STATEMENT_FROM_HERE, manager, |
0 | 218 "INSERT INTO Resources VALUES(NULL, ${type}, ${id}, NULL)"); |
219 | |
220 statement.SetParameterType("id", ValueType_Utf8String); | |
221 statement.SetParameterType("type", ValueType_Integer64); | |
222 | |
223 Dictionary args; | |
224 args.SetUtf8Value("id", publicId); | |
225 args.SetIntegerValue("type", static_cast<int>(type)); | |
226 | |
227 statement.Execute(args); | |
228 | |
229 return dynamic_cast<SQLiteDatabase&>(statement.GetDatabase()).GetLastInsertRowId(); | |
230 } | |
85
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
231 |
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
232 |
225
94c9908e6aca
removed DatabaseManager member out of class IndexBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
233 int64_t SQLiteIndex::GetLastChangeIndex(DatabaseManager& manager) |
85
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
234 { |
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
235 DatabaseManager::CachedStatement statement( |
225
94c9908e6aca
removed DatabaseManager member out of class IndexBackend
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
221
diff
changeset
|
236 STATEMENT_FROM_HERE, manager, |
85
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
237 "SELECT seq FROM sqlite_sequence WHERE name='Changes'"); |
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
238 |
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
239 statement.SetReadOnly(true); |
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
240 statement.Execute(); |
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
241 |
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
242 if (statement.IsDone()) |
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
243 { |
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
244 // No change has been recorded so far in the database |
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
245 return 0; |
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
246 } |
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
247 else |
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
248 { |
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
249 const IValue& value = statement.GetResultField(0); |
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
250 |
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
251 switch (value.GetType()) |
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
252 { |
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
253 case ValueType_Integer64: |
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
254 return dynamic_cast<const Integer64Value&>(value).GetValue(); |
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
255 |
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
256 default: |
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
257 //LOG(ERROR) << value.Format(); |
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
258 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); |
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
259 } |
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
260 } |
1012fe77241c
new extension implemented for PostgreSQL and SQLite: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
261 } |
0 | 262 } |