Mercurial > hg > orthanc-webviewer
comparison Orthanc/Core/SQLite/Transaction.cpp @ 25:15acbf5e7545
refactoring
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 02 Jun 2015 11:16:30 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
24:ed9acb0f938e | 25:15acbf5e7545 |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * | |
4 * Copyright (C) 2012-2015 Sebastien Jodogne <s.jodogne@gmail.com>, | |
5 * Medical Physics Department, CHU of Liege, Belgium | |
6 * | |
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: | |
12 * | |
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. | |
35 **/ | |
36 | |
37 | |
38 #if ORTHANC_SQLITE_STANDALONE != 1 | |
39 #include "../PrecompiledHeaders.h" | |
40 #endif | |
41 | |
42 #include "Transaction.h" | |
43 #include "OrthancSQLiteException.h" | |
44 | |
45 namespace Orthanc | |
46 { | |
47 namespace SQLite | |
48 { | |
49 Transaction::Transaction(Connection& connection) : | |
50 connection_(connection), | |
51 isOpen_(false) | |
52 { | |
53 } | |
54 | |
55 Transaction::~Transaction() | |
56 { | |
57 if (isOpen_) | |
58 { | |
59 connection_.RollbackTransaction(); | |
60 } | |
61 } | |
62 | |
63 void Transaction::Begin() | |
64 { | |
65 if (isOpen_) | |
66 { | |
67 throw OrthancSQLiteException("SQLite: Beginning a transaction twice!"); | |
68 } | |
69 | |
70 isOpen_ = connection_.BeginTransaction(); | |
71 if (!isOpen_) | |
72 { | |
73 throw OrthancSQLiteException("SQLite: Unable to create a transaction"); | |
74 } | |
75 } | |
76 | |
77 void Transaction::Rollback() | |
78 { | |
79 if (!isOpen_) | |
80 { | |
81 throw OrthancSQLiteException("SQLite: Attempting to roll back a nonexistent transaction. " | |
82 "Did you remember to call Begin()?"); | |
83 } | |
84 | |
85 isOpen_ = false; | |
86 | |
87 connection_.RollbackTransaction(); | |
88 } | |
89 | |
90 void Transaction::Commit() | |
91 { | |
92 if (!isOpen_) | |
93 { | |
94 throw OrthancSQLiteException("SQLite: Attempting to roll back a nonexistent transaction. " | |
95 "Did you remember to call Begin()?"); | |
96 } | |
97 | |
98 isOpen_ = false; | |
99 | |
100 if (!connection_.CommitTransaction()) | |
101 { | |
102 throw OrthancSQLiteException("SQLite: Failure when committing the transaction"); | |
103 } | |
104 } | |
105 } | |
106 } |