Mercurial > hg > orthanc
annotate Core/SQLite/Connection.cpp @ 1487:23083810d543
removal of unneeded static libraries
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 04 Aug 2015 10:45:58 +0200 |
parents | f967bdf8534e |
children | bd1889029cbb |
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 * |
1291
63d47b1fa239
upgrade to 2015 in SQLite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1225
diff
changeset
|
4 * Copyright (C) 2012-2015 Sebastien Jodogne <s.jodogne@gmail.com>, |
1220
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 #endif |
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
41 |
0 | 42 #include "Connection.h" |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
43 #include "OrthancSQLiteException.h" |
0 | 44 |
45 #include <memory> | |
46 #include <cassert> | |
47 #include <string.h> | |
48 | |
1225 | 49 #if ORTHANC_SQLITE_STANDALONE != 1 |
1486
f967bdf8534e
refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1474
diff
changeset
|
50 #include "../Logging.h" |
1225 | 51 #endif |
52 | |
1474
3b68924ffb24
fix path to sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1291
diff
changeset
|
53 #include "sqlite3.h" |
3b68924ffb24
fix path to sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1291
diff
changeset
|
54 |
1225 | 55 |
59 | 56 namespace Orthanc |
0 | 57 { |
58 namespace SQLite | |
59 { | |
60 Connection::Connection() : | |
61 db_(NULL), | |
62 transactionNesting_(0), | |
63 needsRollback_(false) | |
64 { | |
65 } | |
66 | |
67 | |
68 Connection::~Connection() | |
69 { | |
70 Close(); | |
71 } | |
72 | |
73 | |
74 void Connection::CheckIsOpen() const | |
75 { | |
76 if (!db_) | |
77 { | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
78 throw OrthancSQLiteException("SQLite: The database is not opened"); |
0 | 79 } |
80 } | |
81 | |
82 void Connection::Open(const std::string& path) | |
83 { | |
84 if (db_) | |
85 { | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
86 throw OrthancSQLiteException("SQLite: Connection is already open"); |
0 | 87 } |
88 | |
89 int err = sqlite3_open(path.c_str(), &db_); | |
90 if (err != SQLITE_OK) | |
91 { | |
92 Close(); | |
93 db_ = NULL; | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
94 throw OrthancSQLiteException("SQLite: Unable to open the database"); |
0 | 95 } |
96 | |
97 // Execute PRAGMAs at this point | |
98 // http://www.sqlite.org/pragma.html | |
99 Execute("PRAGMA FOREIGN_KEYS=ON;"); | |
181
2dece1526c06
simplifying db schema
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
100 Execute("PRAGMA RECURSIVE_TRIGGERS=ON;"); |
0 | 101 } |
102 | |
103 void Connection::OpenInMemory() | |
104 { | |
105 Open(":memory:"); | |
106 } | |
107 | |
108 void Connection::Close() | |
109 { | |
110 ClearCache(); | |
111 | |
112 if (db_) | |
113 { | |
114 sqlite3_close(db_); | |
115 db_ = NULL; | |
116 } | |
117 } | |
118 | |
119 void Connection::ClearCache() | |
120 { | |
121 for (CachedStatements::iterator | |
122 it = cachedStatements_.begin(); | |
656 | 123 it != cachedStatements_.end(); ++it) |
0 | 124 { |
125 delete it->second; | |
126 } | |
127 | |
128 cachedStatements_.clear(); | |
129 } | |
130 | |
131 | |
132 StatementReference& Connection::GetCachedStatement(const StatementId& id, | |
133 const char* sql) | |
134 { | |
135 CachedStatements::iterator i = cachedStatements_.find(id); | |
136 if (i != cachedStatements_.end()) | |
137 { | |
138 if (i->second->GetReferenceCount() >= 1) | |
139 { | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
140 throw OrthancSQLiteException("SQLite: This cached statement is already being referred to"); |
0 | 141 } |
142 | |
143 return *i->second; | |
144 } | |
145 else | |
146 { | |
147 StatementReference* statement = new StatementReference(db_, sql); | |
148 cachedStatements_[id] = statement; | |
149 return *statement; | |
150 } | |
151 } | |
152 | |
153 | |
154 bool Connection::Execute(const char* sql) | |
155 { | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
156 #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
|
157 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
|
158 #endif |
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
159 |
0 | 160 CheckIsOpen(); |
161 | |
162 int error = sqlite3_exec(db_, sql, NULL, NULL, NULL); | |
163 if (error == SQLITE_ERROR) | |
164 { | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
165 throw OrthancSQLiteException("SQLite Execute error: " + std::string(sqlite3_errmsg(db_))); |
0 | 166 } |
167 else | |
168 { | |
169 return error == SQLITE_OK; | |
170 } | |
171 } | |
172 | |
173 int Connection::ExecuteAndReturnErrorCode(const char* sql) | |
174 { | |
175 CheckIsOpen(); | |
176 return sqlite3_exec(db_, sql, NULL, NULL, NULL); | |
177 } | |
178 | |
179 // Info querying ------------------------------------------------------------- | |
180 | |
181 bool Connection::IsSQLValid(const char* sql) | |
182 { | |
183 sqlite3_stmt* stmt = NULL; | |
184 if (sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL) != SQLITE_OK) | |
185 return false; | |
186 | |
187 sqlite3_finalize(stmt); | |
188 return true; | |
189 } | |
190 | |
191 bool Connection::DoesTableOrIndexExist(const char* name, | |
192 const char* type) const | |
193 { | |
194 // Our SQL is non-mutating, so this cast is OK. | |
195 Statement statement(const_cast<Connection&>(*this), | |
196 "SELECT name FROM sqlite_master WHERE type=? AND name=?"); | |
197 statement.BindString(0, type); | |
198 statement.BindString(1, name); | |
199 return statement.Step(); // Table exists if any row was returned. | |
200 } | |
201 | |
202 bool Connection::DoesTableExist(const char* table_name) const | |
203 { | |
204 return DoesTableOrIndexExist(table_name, "table"); | |
205 } | |
206 | |
207 bool Connection::DoesIndexExist(const char* index_name) const | |
208 { | |
209 return DoesTableOrIndexExist(index_name, "index"); | |
210 } | |
211 | |
212 bool Connection::DoesColumnExist(const char* table_name, const char* column_name) const | |
213 { | |
214 std::string sql("PRAGMA TABLE_INFO("); | |
215 sql.append(table_name); | |
216 sql.append(")"); | |
217 | |
218 // Our SQL is non-mutating, so this cast is OK. | |
219 Statement statement(const_cast<Connection&>(*this), sql.c_str()); | |
220 | |
221 while (statement.Step()) { | |
222 if (!statement.ColumnString(1).compare(column_name)) | |
223 return true; | |
224 } | |
225 return false; | |
226 } | |
227 | |
228 int64_t Connection::GetLastInsertRowId() const | |
229 { | |
230 return sqlite3_last_insert_rowid(db_); | |
231 } | |
232 | |
233 int Connection::GetLastChangeCount() const | |
234 { | |
235 return sqlite3_changes(db_); | |
236 } | |
237 | |
238 int Connection::GetErrorCode() const | |
239 { | |
240 return sqlite3_errcode(db_); | |
241 } | |
242 | |
243 int Connection::GetLastErrno() const | |
244 { | |
245 int err = 0; | |
246 if (SQLITE_OK != sqlite3_file_control(db_, NULL, SQLITE_LAST_ERRNO, &err)) | |
247 return -2; | |
248 | |
249 return err; | |
250 } | |
251 | |
252 const char* Connection::GetErrorMessage() const | |
253 { | |
254 return sqlite3_errmsg(db_); | |
255 } | |
256 | |
257 | |
258 bool Connection::BeginTransaction() | |
259 { | |
260 if (needsRollback_) | |
261 { | |
262 assert(transactionNesting_ > 0); | |
263 | |
264 // When we're going to rollback, fail on this begin and don't actually | |
265 // mark us as entering the nested transaction. | |
266 return false; | |
267 } | |
268 | |
269 bool success = true; | |
270 if (!transactionNesting_) | |
271 { | |
272 needsRollback_ = false; | |
273 | |
274 Statement begin(*this, SQLITE_FROM_HERE, "BEGIN TRANSACTION"); | |
275 if (!begin.Run()) | |
276 return false; | |
277 } | |
278 transactionNesting_++; | |
279 return success; | |
280 } | |
281 | |
282 void Connection::RollbackTransaction() | |
283 { | |
284 if (!transactionNesting_) | |
285 { | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
286 throw OrthancSQLiteException("Rolling back a nonexistent transaction"); |
0 | 287 } |
288 | |
289 transactionNesting_--; | |
290 | |
291 if (transactionNesting_ > 0) | |
292 { | |
293 // Mark the outermost transaction as needing rollback. | |
294 needsRollback_ = true; | |
295 return; | |
296 } | |
297 | |
298 DoRollback(); | |
299 } | |
300 | |
301 bool Connection::CommitTransaction() | |
302 { | |
303 if (!transactionNesting_) | |
304 { | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
305 throw OrthancSQLiteException("Committing a nonexistent transaction"); |
0 | 306 } |
307 transactionNesting_--; | |
308 | |
309 if (transactionNesting_ > 0) | |
310 { | |
311 // Mark any nested transactions as failing after we've already got one. | |
312 return !needsRollback_; | |
313 } | |
314 | |
315 if (needsRollback_) | |
316 { | |
317 DoRollback(); | |
318 return false; | |
319 } | |
320 | |
321 Statement commit(*this, SQLITE_FROM_HERE, "COMMIT"); | |
322 return commit.Run(); | |
323 } | |
324 | |
325 void Connection::DoRollback() | |
326 { | |
327 Statement rollback(*this, SQLITE_FROM_HERE, "ROLLBACK"); | |
328 rollback.Run(); | |
329 needsRollback_ = false; | |
330 } | |
331 | |
332 | |
333 | |
334 | |
335 | |
336 | |
337 static void ScalarFunctionCaller(sqlite3_context* rawContext, | |
338 int argc, | |
339 sqlite3_value** argv) | |
340 { | |
341 FunctionContext context(rawContext, argc, argv); | |
342 | |
343 void* payload = sqlite3_user_data(rawContext); | |
344 assert(payload != NULL); | |
345 | |
656 | 346 IScalarFunction& func = *reinterpret_cast<IScalarFunction*>(payload); |
0 | 347 func.Compute(context); |
348 } | |
349 | |
350 | |
351 static void ScalarFunctionDestroyer(void* payload) | |
352 { | |
353 assert(payload != NULL); | |
656 | 354 delete reinterpret_cast<IScalarFunction*>(payload); |
0 | 355 } |
356 | |
357 | |
358 IScalarFunction* Connection::Register(IScalarFunction* func) | |
359 { | |
360 int err = sqlite3_create_function_v2(db_, | |
361 func->GetName(), | |
362 func->GetCardinality(), | |
363 SQLITE_UTF8, | |
364 func, | |
365 ScalarFunctionCaller, | |
366 NULL, | |
367 NULL, | |
368 ScalarFunctionDestroyer); | |
369 | |
370 if (err != SQLITE_OK) | |
371 { | |
372 delete func; | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
373 throw OrthancSQLiteException("SQLite: Unable to register a function"); |
0 | 374 } |
375 | |
376 return func; | |
377 } | |
378 | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
379 |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
380 void Connection::FlushToDisk() |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
381 { |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
382 #if ORTHANC_SQLITE_STANDALONE != 1 |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
383 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
|
384 #endif |
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
385 |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
386 int err = sqlite3_wal_checkpoint(db_, NULL); |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
387 |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
388 if (err != SQLITE_OK) |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
389 { |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
390 throw OrthancSQLiteException("SQLite: Unable to flush the database"); |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
391 } |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
392 } |
0 | 393 } |
394 } |