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