Mercurial > hg > orthanc
annotate Core/SQLite/StatementReference.cpp @ 2759:0cfab50c1381 Orthanc-0.7.5
close old branch
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 17 Jul 2018 09:41:32 +0200 |
parents | 2d0a347e8cfc |
children | a811bdf8b8eb |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
689 | 3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, |
0 | 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 "StatementReference.h" | |
38 | |
59 | 39 #include "../OrthancException.h" |
0 | 40 |
41 #include <cassert> | |
656 | 42 #include <glog/logging.h> |
0 | 43 #include "sqlite3.h" |
44 | |
59 | 45 namespace Orthanc |
0 | 46 { |
47 namespace SQLite | |
48 { | |
49 bool StatementReference::IsRoot() const | |
50 { | |
51 return root_ == NULL; | |
52 } | |
53 | |
54 StatementReference::StatementReference() | |
55 { | |
56 root_ = NULL; | |
57 refCount_ = 0; | |
58 statement_ = NULL; | |
59 assert(IsRoot()); | |
60 } | |
61 | |
62 StatementReference::StatementReference(sqlite3* database, | |
63 const char* sql) | |
64 { | |
65 if (database == NULL || sql == NULL) | |
66 { | |
59 | 67 throw OrthancException(ErrorCode_ParameterOutOfRange); |
0 | 68 } |
69 | |
70 root_ = NULL; | |
71 refCount_ = 0; | |
72 | |
73 int error = sqlite3_prepare_v2(database, sql, -1, &statement_, NULL); | |
74 if (error != SQLITE_OK) | |
75 { | |
59 | 76 throw OrthancException("SQLite: " + std::string(sqlite3_errmsg(database))); |
0 | 77 } |
78 | |
79 assert(IsRoot()); | |
80 } | |
81 | |
82 StatementReference::StatementReference(StatementReference& other) | |
83 { | |
84 refCount_ = 0; | |
85 | |
86 if (other.IsRoot()) | |
87 { | |
88 root_ = &other; | |
89 } | |
90 else | |
91 { | |
92 root_ = other.root_; | |
93 } | |
94 | |
95 root_->refCount_++; | |
96 statement_ = root_->statement_; | |
97 | |
98 assert(!IsRoot()); | |
99 } | |
100 | |
101 StatementReference::~StatementReference() | |
102 { | |
103 if (IsRoot()) | |
104 { | |
105 if (refCount_ != 0) | |
106 { | |
656 | 107 // There remain references to this object. We cannot throw |
108 // an exception because: | |
109 // http://www.parashift.com/c++-faq/dtors-shouldnt-throw.html | |
110 | |
111 LOG(ERROR) << "Bad value of the reference counter"; | |
0 | 112 } |
113 else if (statement_ != NULL) | |
114 { | |
115 sqlite3_finalize(statement_); | |
116 } | |
117 } | |
118 else | |
119 { | |
120 if (root_->refCount_ == 0) | |
121 { | |
657
5425bb6f1ea5
further cppcheck fixes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
656
diff
changeset
|
122 // There remain references to this object. We cannot throw |
5425bb6f1ea5
further cppcheck fixes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
656
diff
changeset
|
123 // an exception because: |
5425bb6f1ea5
further cppcheck fixes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
656
diff
changeset
|
124 // http://www.parashift.com/c++-faq/dtors-shouldnt-throw.html |
5425bb6f1ea5
further cppcheck fixes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
656
diff
changeset
|
125 |
5425bb6f1ea5
further cppcheck fixes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
656
diff
changeset
|
126 LOG(ERROR) << "Bad value of the reference counter"; |
0 | 127 } |
128 else | |
129 { | |
130 root_->refCount_--; | |
131 } | |
132 } | |
133 } | |
134 | |
135 uint32_t StatementReference::GetReferenceCount() const | |
136 { | |
137 return refCount_; | |
138 } | |
139 } | |
140 } |