Mercurial > hg > orthanc
annotate OrthancFramework/Sources/SQLite/StatementReference.cpp @ 4831:7053502fbf97
added copyright UCLouvain
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 25 Nov 2021 19:01:11 +0100 |
parents | c847b0dfd255 |
children | 43e613a7756b |
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 * |
3063 | 4 * Copyright (C) 2012-2016 Sebastien Jodogne <s.jodogne@orthanc-labs.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 |
4829
c847b0dfd255
added missing copyright headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
6 * Copyright (C) 2017-2021 Osimis S.A., Belgium |
4831
7053502fbf97
added copyright UCLouvain
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4829
diff
changeset
|
7 * Copyright (C) 2021-2021 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
0 | 8 * |
17 | 9 * Copyright (c) 2012 The Chromium Authors. All rights reserved. |
10 * | |
11 * Redistribution and use in source and binary forms, with or without | |
12 * modification, are permitted provided that the following conditions are | |
13 * met: | |
0 | 14 * |
17 | 15 * * Redistributions of source code must retain the above copyright |
16 * notice, this list of conditions and the following disclaimer. | |
17 * * Redistributions in binary form must reproduce the above | |
18 * copyright notice, this list of conditions and the following disclaimer | |
19 * in the documentation and/or other materials provided with the | |
20 * distribution. | |
21 * * Neither the name of Google Inc., the name of the CHU of Liege, | |
22 * nor the names of its contributors may be used to endorse or promote | |
23 * products derived from this software without specific prior written | |
24 * permission. | |
25 * | |
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
0 | 37 **/ |
38 | |
39 | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
40 #if ORTHANC_SQLITE_STANDALONE != 1 |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
41 #include "../PrecompiledHeaders.h" |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
42 #endif |
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
43 |
0 | 44 #include "StatementReference.h" |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
45 #include "OrthancSQLiteException.h" |
0 | 46 |
1226 | 47 #if ORTHANC_SQLITE_STANDALONE != 1 |
1486
f967bdf8534e
refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1398
diff
changeset
|
48 #include "../Logging.h" |
1226 | 49 #endif |
50 | |
1398 | 51 #include <string> |
0 | 52 #include <cassert> |
53 #include "sqlite3.h" | |
54 | |
59 | 55 namespace Orthanc |
0 | 56 { |
57 namespace SQLite | |
58 { | |
59 bool StatementReference::IsRoot() const | |
60 { | |
61 return root_ == NULL; | |
62 } | |
63 | |
64 StatementReference::StatementReference() | |
65 { | |
66 root_ = NULL; | |
67 refCount_ = 0; | |
68 statement_ = NULL; | |
69 assert(IsRoot()); | |
70 } | |
71 | |
72 StatementReference::StatementReference(sqlite3* database, | |
73 const char* sql) | |
74 { | |
75 if (database == NULL || sql == NULL) | |
76 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
77 throw OrthancSQLiteException(ErrorCode_ParameterOutOfRange); |
0 | 78 } |
79 | |
80 root_ = NULL; | |
81 refCount_ = 0; | |
82 | |
83 int error = sqlite3_prepare_v2(database, sql, -1, &statement_, NULL); | |
84 if (error != SQLITE_OK) | |
85 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
86 #if ORTHANC_SQLITE_STANDALONE != 1 |
3699
efa815dd2c20
additional logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3063
diff
changeset
|
87 int extended = sqlite3_extended_errcode(database); |
efa815dd2c20
additional logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3063
diff
changeset
|
88 LOG(ERROR) << "SQLite: " << sqlite3_errmsg(database) << " (" << extended << ")"; |
efa815dd2c20
additional logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3063
diff
changeset
|
89 if (extended == SQLITE_IOERR_SHMSIZE /* 4874 */) |
efa815dd2c20
additional logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3063
diff
changeset
|
90 { |
efa815dd2c20
additional logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3063
diff
changeset
|
91 LOG(ERROR) << " This probably indicates that your filesystem is full"; |
efa815dd2c20
additional logging
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3063
diff
changeset
|
92 } |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
93 #endif |
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
94 |
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
95 throw OrthancSQLiteException(ErrorCode_SQLitePrepareStatement); |
0 | 96 } |
97 | |
98 assert(IsRoot()); | |
99 } | |
100 | |
101 StatementReference::StatementReference(StatementReference& other) | |
102 { | |
103 refCount_ = 0; | |
104 | |
105 if (other.IsRoot()) | |
106 { | |
107 root_ = &other; | |
108 } | |
109 else | |
110 { | |
111 root_ = other.root_; | |
112 } | |
113 | |
114 root_->refCount_++; | |
115 statement_ = root_->statement_; | |
116 | |
117 assert(!IsRoot()); | |
118 } | |
119 | |
120 StatementReference::~StatementReference() | |
121 { | |
122 if (IsRoot()) | |
123 { | |
124 if (refCount_ != 0) | |
125 { | |
656 | 126 // There remain references to this object. We cannot throw |
127 // an exception because: | |
128 // http://www.parashift.com/c++-faq/dtors-shouldnt-throw.html | |
129 | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
130 #if ORTHANC_SQLITE_STANDALONE != 1 |
656 | 131 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
|
132 #endif |
0 | 133 } |
134 else if (statement_ != NULL) | |
135 { | |
136 sqlite3_finalize(statement_); | |
137 } | |
138 } | |
139 else | |
140 { | |
141 if (root_->refCount_ == 0) | |
142 { | |
657
5425bb6f1ea5
further cppcheck fixes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
656
diff
changeset
|
143 // There remain references to this object. We cannot throw |
5425bb6f1ea5
further cppcheck fixes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
656
diff
changeset
|
144 // an exception because: |
5425bb6f1ea5
further cppcheck fixes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
656
diff
changeset
|
145 // http://www.parashift.com/c++-faq/dtors-shouldnt-throw.html |
5425bb6f1ea5
further cppcheck fixes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
656
diff
changeset
|
146 |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
147 #if ORTHANC_SQLITE_STANDALONE != 1 |
657
5425bb6f1ea5
further cppcheck fixes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
656
diff
changeset
|
148 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
|
149 #endif |
0 | 150 } |
151 else | |
152 { | |
153 root_->refCount_--; | |
154 } | |
155 } | |
156 } | |
157 | |
158 uint32_t StatementReference::GetReferenceCount() const | |
159 { | |
160 return refCount_; | |
161 } | |
4304 | 162 |
163 sqlite3_stmt *StatementReference::GetWrappedObject() const | |
164 { | |
165 assert(statement_ != NULL); | |
166 return statement_; | |
167 } | |
0 | 168 } |
169 } |