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