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