comparison SQLite/Plugins/SQLiteIndex.cpp @ 0:7cea966b6829

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 04 Jul 2018 08:16:29 +0200
parents
children 714c5d2bee76
comparison
equal deleted inserted replaced
-1:000000000000 0:7cea966b6829
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include "SQLiteIndex.h"
23
24 #include "../../Framework/Plugins/GlobalProperties.h"
25 #include "../../Framework/SQLite/SQLiteDatabase.h"
26 #include "../../Framework/SQLite/SQLiteTransaction.h"
27
28 #include <EmbeddedResources.h> // Auto-generated file
29
30 #include <Core/Logging.h>
31 #include <Core/OrthancException.h>
32
33 namespace OrthancDatabases
34 {
35 IDatabase* SQLiteIndex::OpenInternal()
36 {
37 uint32_t expectedVersion = 6;
38 if (context_)
39 {
40 expectedVersion = OrthancPluginGetExpectedDatabaseVersion(context_);
41 }
42 else
43 {
44 // This case only occurs during unit testing
45 expectedVersion = 6;
46 }
47
48 // Check the expected version of the database
49 if (expectedVersion != 6)
50 {
51 LOG(ERROR) << "This database plugin is incompatible with your version of Orthanc "
52 << "expecting the DB schema version " << expectedVersion
53 << ", but this plugin is only compatible with version 6";
54 throw Orthanc::OrthancException(Orthanc::ErrorCode_Plugin);
55 }
56
57
58 std::auto_ptr<SQLiteDatabase> db(new SQLiteDatabase);
59
60 if (path_.empty())
61 {
62 db->OpenInMemory();
63 }
64 else
65 {
66 db->Open(path_);
67 }
68
69 {
70 SQLiteTransaction t(*db);
71
72 if (!db->DoesTableExist("Resources"))
73 {
74 std::string query;
75
76 Orthanc::EmbeddedResources::GetFileResource
77 (query, Orthanc::EmbeddedResources::SQLITE_PREPARE_INDEX);
78 db->Execute(query);
79
80 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_DatabaseSchemaVersion, expectedVersion);
81 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_DatabasePatchLevel, 1);
82 }
83
84 t.Commit();
85 }
86
87 db->Execute("PRAGMA ENCODING=\"UTF-8\";");
88
89 if (fast_)
90 {
91 // Performance tuning of SQLite with PRAGMAs
92 // http://www.sqlite.org/pragma.html
93 db->Execute("PRAGMA SYNCHRONOUS=NORMAL;");
94 db->Execute("PRAGMA JOURNAL_MODE=WAL;");
95 db->Execute("PRAGMA LOCKING_MODE=EXCLUSIVE;");
96 db->Execute("PRAGMA WAL_AUTOCHECKPOINT=1000;");
97 //db->Execute("PRAGMA TEMP_STORE=memory");
98 }
99
100 {
101 SQLiteTransaction t(*db);
102
103 if (!db->DoesTableExist("Resources"))
104 {
105 LOG(ERROR) << "Corrupted SQLite database";
106 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
107 }
108
109 int version = 0;
110 if (!LookupGlobalIntegerProperty(version, *db, t, Orthanc::GlobalProperty_DatabaseSchemaVersion) ||
111 version != 6)
112 {
113 LOG(ERROR) << "SQLite plugin is incompatible with database schema version: " << version;
114 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
115 }
116
117 int revision;
118 if (!LookupGlobalIntegerProperty(revision, *db, t, Orthanc::GlobalProperty_DatabasePatchLevel))
119 {
120 revision = 1;
121 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_DatabasePatchLevel, revision);
122 }
123
124 if (revision != 1)
125 {
126 LOG(ERROR) << "SQLite plugin is incompatible with database schema revision: " << revision;
127 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
128 }
129
130 t.Commit();
131 }
132
133 return db.release();
134 }
135
136
137 SQLiteIndex::SQLiteIndex(const std::string& path) :
138 IndexBackend(new Factory(*this)),
139 context_(NULL),
140 path_(path),
141 fast_(true)
142 {
143 if (path.empty())
144 {
145 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
146 }
147 }
148
149
150 SQLiteIndex::SQLiteIndex() :
151 IndexBackend(new Factory(*this)),
152 context_(NULL),
153 fast_(true)
154 {
155 }
156
157
158 int64_t SQLiteIndex::CreateResource(const char* publicId,
159 OrthancPluginResourceType type)
160 {
161 DatabaseManager::CachedStatement statement(
162 STATEMENT_FROM_HERE, GetManager(),
163 "INSERT INTO Resources VALUES(NULL, ${type}, ${id}, NULL)");
164
165 statement.SetParameterType("id", ValueType_Utf8String);
166 statement.SetParameterType("type", ValueType_Integer64);
167
168 Dictionary args;
169 args.SetUtf8Value("id", publicId);
170 args.SetIntegerValue("type", static_cast<int>(type));
171
172 statement.Execute(args);
173
174 return dynamic_cast<SQLiteDatabase&>(statement.GetDatabase()).GetLastInsertRowId();
175 }
176 }