Mercurial > hg > orthanc
annotate Core/SQLite/StatementReference.cpp @ 2140:aa4b8895cd23
reorganization
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 09 Nov 2016 16:12:47 +0100 |
parents | c746e2d42ac8 |
children | fa5ad4368fe3 |
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 * |
1900 | 4 * Copyright (C) 2012-2016 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 "StatementReference.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 |
1226 | 45 #if ORTHANC_SQLITE_STANDALONE != 1 |
1486
f967bdf8534e
refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1398
diff
changeset
|
46 #include "../Logging.h" |
1226 | 47 #endif |
48 | |
1398 | 49 #include <string> |
0 | 50 #include <cassert> |
51 #include "sqlite3.h" | |
52 | |
59 | 53 namespace Orthanc |
0 | 54 { |
55 namespace SQLite | |
56 { | |
57 bool StatementReference::IsRoot() const | |
58 { | |
59 return root_ == NULL; | |
60 } | |
61 | |
62 StatementReference::StatementReference() | |
63 { | |
64 root_ = NULL; | |
65 refCount_ = 0; | |
66 statement_ = NULL; | |
67 assert(IsRoot()); | |
68 } | |
69 | |
70 StatementReference::StatementReference(sqlite3* database, | |
71 const char* sql) | |
72 { | |
73 if (database == NULL || sql == NULL) | |
74 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
75 throw OrthancSQLiteException(ErrorCode_ParameterOutOfRange); |
0 | 76 } |
77 | |
78 root_ = NULL; | |
79 refCount_ = 0; | |
80 | |
81 int error = sqlite3_prepare_v2(database, sql, -1, &statement_, NULL); | |
82 if (error != SQLITE_OK) | |
83 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
84 #if ORTHANC_SQLITE_STANDALONE != 1 |
1958
c746e2d42ac8
extended error codes for SQLite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
85 LOG(ERROR) << "SQLite: " << sqlite3_errmsg(database) |
c746e2d42ac8
extended error codes for SQLite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
86 << " (" << sqlite3_extended_errcode(database) << ")"; |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
87 #endif |
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
88 |
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
89 throw OrthancSQLiteException(ErrorCode_SQLitePrepareStatement); |
0 | 90 } |
91 | |
92 assert(IsRoot()); | |
93 } | |
94 | |
95 StatementReference::StatementReference(StatementReference& other) | |
96 { | |
97 refCount_ = 0; | |
98 | |
99 if (other.IsRoot()) | |
100 { | |
101 root_ = &other; | |
102 } | |
103 else | |
104 { | |
105 root_ = other.root_; | |
106 } | |
107 | |
108 root_->refCount_++; | |
109 statement_ = root_->statement_; | |
110 | |
111 assert(!IsRoot()); | |
112 } | |
113 | |
114 StatementReference::~StatementReference() | |
115 { | |
116 if (IsRoot()) | |
117 { | |
118 if (refCount_ != 0) | |
119 { | |
656 | 120 // There remain references to this object. We cannot throw |
121 // an exception because: | |
122 // http://www.parashift.com/c++-faq/dtors-shouldnt-throw.html | |
123 | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
124 #if ORTHANC_SQLITE_STANDALONE != 1 |
656 | 125 LOG(ERROR) << "Bad value of the reference counter"; |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
126 #endif |
0 | 127 } |
128 else if (statement_ != NULL) | |
129 { | |
130 sqlite3_finalize(statement_); | |
131 } | |
132 } | |
133 else | |
134 { | |
135 if (root_->refCount_ == 0) | |
136 { | |
657
5425bb6f1ea5
further cppcheck fixes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
656
diff
changeset
|
137 // There remain references to this object. We cannot throw |
5425bb6f1ea5
further cppcheck fixes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
656
diff
changeset
|
138 // an exception because: |
5425bb6f1ea5
further cppcheck fixes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
656
diff
changeset
|
139 // http://www.parashift.com/c++-faq/dtors-shouldnt-throw.html |
5425bb6f1ea5
further cppcheck fixes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
656
diff
changeset
|
140 |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
141 #if ORTHANC_SQLITE_STANDALONE != 1 |
657
5425bb6f1ea5
further cppcheck fixes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
656
diff
changeset
|
142 LOG(ERROR) << "Bad value of the reference counter"; |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
143 #endif |
0 | 144 } |
145 else | |
146 { | |
147 root_->refCount_--; | |
148 } | |
149 } | |
150 } | |
151 | |
152 uint32_t StatementReference::GetReferenceCount() const | |
153 { | |
154 return refCount_; | |
155 } | |
156 } | |
157 } |