Mercurial > hg > orthanc
annotate Core/SQLite/Connection.cpp @ 1220:9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 10 Nov 2014 16:33:51 +0100 |
parents | a811bdf8b8eb |
children | 4c649dbeb61a |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
3 * |
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
4 * Copyright (C) 2012-2014 Sebastien Jodogne <s.jodogne@gmail.com>, |
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
5 * Medical Physics Department, CHU of Liege, Belgium |
0 | 6 * |
17 | 7 * Copyright (c) 2012 The Chromium Authors. All rights reserved. |
8 * | |
9 * Redistribution and use in source and binary forms, with or without | |
10 * modification, are permitted provided that the following conditions are | |
11 * met: | |
0 | 12 * |
17 | 13 * * Redistributions of source code must retain the above copyright |
14 * notice, this list of conditions and the following disclaimer. | |
15 * * Redistributions in binary form must reproduce the above | |
16 * copyright notice, this list of conditions and the following disclaimer | |
17 * in the documentation and/or other materials provided with the | |
18 * distribution. | |
19 * * Neither the name of Google Inc., the name of the CHU of Liege, | |
20 * nor the names of its contributors may be used to endorse or promote | |
21 * products derived from this software without specific prior written | |
22 * permission. | |
23 * | |
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
0 | 35 **/ |
36 | |
37 | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
38 #if ORTHANC_SQLITE_STANDALONE != 1 |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
39 #include "../PrecompiledHeaders.h" |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
40 #include <glog/logging.h> |
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
41 #endif |
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
42 |
0 | 43 #include "Connection.h" |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
44 #include "OrthancSQLiteException.h" |
0 | 45 |
46 #include <memory> | |
47 #include <cassert> | |
48 #include <sqlite3.h> | |
49 #include <string.h> | |
50 | |
59 | 51 namespace Orthanc |
0 | 52 { |
53 namespace SQLite | |
54 { | |
55 Connection::Connection() : | |
56 db_(NULL), | |
57 transactionNesting_(0), | |
58 needsRollback_(false) | |
59 { | |
60 } | |
61 | |
62 | |
63 Connection::~Connection() | |
64 { | |
65 Close(); | |
66 } | |
67 | |
68 | |
69 void Connection::CheckIsOpen() const | |
70 { | |
71 if (!db_) | |
72 { | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
73 throw OrthancSQLiteException("SQLite: The database is not opened"); |
0 | 74 } |
75 } | |
76 | |
77 void Connection::Open(const std::string& path) | |
78 { | |
79 if (db_) | |
80 { | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
81 throw OrthancSQLiteException("SQLite: Connection is already open"); |
0 | 82 } |
83 | |
84 int err = sqlite3_open(path.c_str(), &db_); | |
85 if (err != SQLITE_OK) | |
86 { | |
87 Close(); | |
88 db_ = NULL; | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
89 throw OrthancSQLiteException("SQLite: Unable to open the database"); |
0 | 90 } |
91 | |
92 // Execute PRAGMAs at this point | |
93 // http://www.sqlite.org/pragma.html | |
94 Execute("PRAGMA FOREIGN_KEYS=ON;"); | |
181
2dece1526c06
simplifying db schema
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
95 Execute("PRAGMA RECURSIVE_TRIGGERS=ON;"); |
0 | 96 } |
97 | |
98 void Connection::OpenInMemory() | |
99 { | |
100 Open(":memory:"); | |
101 } | |
102 | |
103 void Connection::Close() | |
104 { | |
105 ClearCache(); | |
106 | |
107 if (db_) | |
108 { | |
109 sqlite3_close(db_); | |
110 db_ = NULL; | |
111 } | |
112 } | |
113 | |
114 void Connection::ClearCache() | |
115 { | |
116 for (CachedStatements::iterator | |
117 it = cachedStatements_.begin(); | |
656 | 118 it != cachedStatements_.end(); ++it) |
0 | 119 { |
120 delete it->second; | |
121 } | |
122 | |
123 cachedStatements_.clear(); | |
124 } | |
125 | |
126 | |
127 StatementReference& Connection::GetCachedStatement(const StatementId& id, | |
128 const char* sql) | |
129 { | |
130 CachedStatements::iterator i = cachedStatements_.find(id); | |
131 if (i != cachedStatements_.end()) | |
132 { | |
133 if (i->second->GetReferenceCount() >= 1) | |
134 { | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
135 throw OrthancSQLiteException("SQLite: This cached statement is already being referred to"); |
0 | 136 } |
137 | |
138 return *i->second; | |
139 } | |
140 else | |
141 { | |
142 StatementReference* statement = new StatementReference(db_, sql); | |
143 cachedStatements_[id] = statement; | |
144 return *statement; | |
145 } | |
146 } | |
147 | |
148 | |
149 bool Connection::Execute(const char* sql) | |
150 { | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
151 #if ORTHANC_SQLITE_STANDALONE != 1 |
137
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
59
diff
changeset
|
152 VLOG(1) << "SQLite::Connection::Execute " << sql; |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
153 #endif |
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
154 |
0 | 155 CheckIsOpen(); |
156 | |
157 int error = sqlite3_exec(db_, sql, NULL, NULL, NULL); | |
158 if (error == SQLITE_ERROR) | |
159 { | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
160 throw OrthancSQLiteException("SQLite Execute error: " + std::string(sqlite3_errmsg(db_))); |
0 | 161 } |
162 else | |
163 { | |
164 return error == SQLITE_OK; | |
165 } | |
166 } | |
167 | |
168 int Connection::ExecuteAndReturnErrorCode(const char* sql) | |
169 { | |
170 CheckIsOpen(); | |
171 return sqlite3_exec(db_, sql, NULL, NULL, NULL); | |
172 } | |
173 | |
174 // Info querying ------------------------------------------------------------- | |
175 | |
176 bool Connection::IsSQLValid(const char* sql) | |
177 { | |
178 sqlite3_stmt* stmt = NULL; | |
179 if (sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL) != SQLITE_OK) | |
180 return false; | |
181 | |
182 sqlite3_finalize(stmt); | |
183 return true; | |
184 } | |
185 | |
186 bool Connection::DoesTableOrIndexExist(const char* name, | |
187 const char* type) const | |
188 { | |
189 // Our SQL is non-mutating, so this cast is OK. | |
190 Statement statement(const_cast<Connection&>(*this), | |
191 "SELECT name FROM sqlite_master WHERE type=? AND name=?"); | |
192 statement.BindString(0, type); | |
193 statement.BindString(1, name); | |
194 return statement.Step(); // Table exists if any row was returned. | |
195 } | |
196 | |
197 bool Connection::DoesTableExist(const char* table_name) const | |
198 { | |
199 return DoesTableOrIndexExist(table_name, "table"); | |
200 } | |
201 | |
202 bool Connection::DoesIndexExist(const char* index_name) const | |
203 { | |
204 return DoesTableOrIndexExist(index_name, "index"); | |
205 } | |
206 | |
207 bool Connection::DoesColumnExist(const char* table_name, const char* column_name) const | |
208 { | |
209 std::string sql("PRAGMA TABLE_INFO("); | |
210 sql.append(table_name); | |
211 sql.append(")"); | |
212 | |
213 // Our SQL is non-mutating, so this cast is OK. | |
214 Statement statement(const_cast<Connection&>(*this), sql.c_str()); | |
215 | |
216 while (statement.Step()) { | |
217 if (!statement.ColumnString(1).compare(column_name)) | |
218 return true; | |
219 } | |
220 return false; | |
221 } | |
222 | |
223 int64_t Connection::GetLastInsertRowId() const | |
224 { | |
225 return sqlite3_last_insert_rowid(db_); | |
226 } | |
227 | |
228 int Connection::GetLastChangeCount() const | |
229 { | |
230 return sqlite3_changes(db_); | |
231 } | |
232 | |
233 int Connection::GetErrorCode() const | |
234 { | |
235 return sqlite3_errcode(db_); | |
236 } | |
237 | |
238 int Connection::GetLastErrno() const | |
239 { | |
240 int err = 0; | |
241 if (SQLITE_OK != sqlite3_file_control(db_, NULL, SQLITE_LAST_ERRNO, &err)) | |
242 return -2; | |
243 | |
244 return err; | |
245 } | |
246 | |
247 const char* Connection::GetErrorMessage() const | |
248 { | |
249 return sqlite3_errmsg(db_); | |
250 } | |
251 | |
252 | |
253 bool Connection::BeginTransaction() | |
254 { | |
255 if (needsRollback_) | |
256 { | |
257 assert(transactionNesting_ > 0); | |
258 | |
259 // When we're going to rollback, fail on this begin and don't actually | |
260 // mark us as entering the nested transaction. | |
261 return false; | |
262 } | |
263 | |
264 bool success = true; | |
265 if (!transactionNesting_) | |
266 { | |
267 needsRollback_ = false; | |
268 | |
269 Statement begin(*this, SQLITE_FROM_HERE, "BEGIN TRANSACTION"); | |
270 if (!begin.Run()) | |
271 return false; | |
272 } | |
273 transactionNesting_++; | |
274 return success; | |
275 } | |
276 | |
277 void Connection::RollbackTransaction() | |
278 { | |
279 if (!transactionNesting_) | |
280 { | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
281 throw OrthancSQLiteException("Rolling back a nonexistent transaction"); |
0 | 282 } |
283 | |
284 transactionNesting_--; | |
285 | |
286 if (transactionNesting_ > 0) | |
287 { | |
288 // Mark the outermost transaction as needing rollback. | |
289 needsRollback_ = true; | |
290 return; | |
291 } | |
292 | |
293 DoRollback(); | |
294 } | |
295 | |
296 bool Connection::CommitTransaction() | |
297 { | |
298 if (!transactionNesting_) | |
299 { | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
300 throw OrthancSQLiteException("Committing a nonexistent transaction"); |
0 | 301 } |
302 transactionNesting_--; | |
303 | |
304 if (transactionNesting_ > 0) | |
305 { | |
306 // Mark any nested transactions as failing after we've already got one. | |
307 return !needsRollback_; | |
308 } | |
309 | |
310 if (needsRollback_) | |
311 { | |
312 DoRollback(); | |
313 return false; | |
314 } | |
315 | |
316 Statement commit(*this, SQLITE_FROM_HERE, "COMMIT"); | |
317 return commit.Run(); | |
318 } | |
319 | |
320 void Connection::DoRollback() | |
321 { | |
322 Statement rollback(*this, SQLITE_FROM_HERE, "ROLLBACK"); | |
323 rollback.Run(); | |
324 needsRollback_ = false; | |
325 } | |
326 | |
327 | |
328 | |
329 | |
330 | |
331 | |
332 static void ScalarFunctionCaller(sqlite3_context* rawContext, | |
333 int argc, | |
334 sqlite3_value** argv) | |
335 { | |
336 FunctionContext context(rawContext, argc, argv); | |
337 | |
338 void* payload = sqlite3_user_data(rawContext); | |
339 assert(payload != NULL); | |
340 | |
656 | 341 IScalarFunction& func = *reinterpret_cast<IScalarFunction*>(payload); |
0 | 342 func.Compute(context); |
343 } | |
344 | |
345 | |
346 static void ScalarFunctionDestroyer(void* payload) | |
347 { | |
348 assert(payload != NULL); | |
656 | 349 delete reinterpret_cast<IScalarFunction*>(payload); |
0 | 350 } |
351 | |
352 | |
353 IScalarFunction* Connection::Register(IScalarFunction* func) | |
354 { | |
355 int err = sqlite3_create_function_v2(db_, | |
356 func->GetName(), | |
357 func->GetCardinality(), | |
358 SQLITE_UTF8, | |
359 func, | |
360 ScalarFunctionCaller, | |
361 NULL, | |
362 NULL, | |
363 ScalarFunctionDestroyer); | |
364 | |
365 if (err != SQLITE_OK) | |
366 { | |
367 delete func; | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
368 throw OrthancSQLiteException("SQLite: Unable to register a function"); |
0 | 369 } |
370 | |
371 return func; | |
372 } | |
373 | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
374 |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
375 void Connection::FlushToDisk() |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
376 { |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
377 #if ORTHANC_SQLITE_STANDALONE != 1 |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
378 VLOG(1) << "SQLite::Connection::FlushToDisk"; |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
379 #endif |
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
380 |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
381 int err = sqlite3_wal_checkpoint(db_, NULL); |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
382 |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
383 if (err != SQLITE_OK) |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
384 { |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
385 throw OrthancSQLiteException("SQLite: Unable to flush the database"); |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
386 } |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
387 } |
0 | 388 } |
389 } |