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