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