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